@@ -222,4 +222,97 @@ describe('Acceptance: ng generate component', function () {
222222 expect ( existsSync ( testPath ) ) . to . equal ( false ) ;
223223 } ) ;
224224 } ) ;
225+
226+ it ( 'should error out when given an incorrect module path' , ( ) => {
227+ return Promise . resolve ( )
228+ . then ( ( ) => ng ( [ 'generate' , 'component' , 'baz' , '--module' , 'foo' ] ) )
229+ . catch ( ( error ) => {
230+ expect ( error ) . to . equal ( 'Specified module does not exist' ) ;
231+ } )
232+ } ) ;
233+
234+ describe ( 'should import and add to declaration list' , ( ) => {
235+ it ( 'when given a root level module with module.ts suffix' , ( ) => {
236+ const appRoot = path . join ( root , 'tmp/foo' ) ;
237+ const modulePath = path . join ( appRoot , 'src/app/app.module.ts' ) ;
238+
239+ return Promise . resolve ( )
240+ . then ( ( ) => ng ( [ 'generate' , 'component' , 'baz' , '--module' , 'app.module.ts' ] ) )
241+ . then ( ( ) => readFile ( modulePath , 'utf-8' ) )
242+ . then ( content => {
243+ expect ( content ) . matches ( / i m p o r t .* B a z C o m p o n e n t .* f r o m ' .\/ b a z \/ b a z .c o m p o n e n t ' ; / ) ;
244+ expect ( content ) . matches ( / d e c l a r a t i o n s : \s + \[ \r ? \n \s + A p p C o m p o n e n t , \r ? \n \s + B a z C o m p o n e n t \r ? \n \s + \] / m) ;
245+ } ) ;
246+ } ) ;
247+
248+ it ( 'when given a root level module with missing module.ts suffix' , ( ) => {
249+ const appRoot = path . join ( root , 'tmp/foo' ) ;
250+ const modulePath = path . join ( appRoot , 'src/app/app.module.ts' ) ;
251+
252+ return Promise . resolve ( )
253+ . then ( ( ) => ng ( [ 'generate' , 'component' , 'baz' , '--module' , 'app' ] ) )
254+ . then ( ( ) => readFile ( modulePath , 'utf-8' ) )
255+ . then ( content => {
256+ expect ( content ) . matches ( / i m p o r t .* B a z C o m p o n e n t .* f r o m ' .\/ b a z \/ b a z .c o m p o n e n t ' ; / ) ;
257+ expect ( content ) . matches ( / d e c l a r a t i o n s : \s + \[ \r ? \n \s + A p p C o m p o n e n t , \r ? \n \s + B a z C o m p o n e n t \r ? \n \s + \] / m) ;
258+ } ) ;
259+ } ) ;
260+
261+ it ( 'when given a submodule with module.ts suffix' , ( ) => {
262+ const appRoot = path . join ( root , 'tmp/foo' ) ;
263+ const modulePath = path . join ( appRoot , 'src/app/foo/foo.module.ts' ) ;
264+
265+ return Promise . resolve ( )
266+ . then ( ( ) => ng ( [ 'generate' , 'module' , 'foo' ] ) )
267+ . then ( ( ) => ng ( [ 'generate' , 'component' , 'baz' , '--module' , path . join ( 'foo' , 'foo.module.ts' ) ] ) )
268+ . then ( ( ) => readFile ( modulePath , 'utf-8' ) )
269+ . then ( content => {
270+ expect ( content ) . matches ( / i m p o r t .* B a z C o m p o n e n t .* f r o m ' .\/ ..\/ b a z \/ b a z .c o m p o n e n t ' ; / ) ;
271+ expect ( content ) . matches ( / d e c l a r a t i o n s : \s + \[ B a z C o m p o n e n t ] / m) ;
272+ } ) ;
273+ } ) ;
274+
275+ it ( 'when given a submodule with missing module.ts suffix' , ( ) => {
276+ const appRoot = path . join ( root , 'tmp/foo' ) ;
277+ const modulePath = path . join ( appRoot , 'src/app/foo/foo.module.ts' ) ;
278+
279+ return Promise . resolve ( )
280+ . then ( ( ) => ng ( [ 'generate' , 'module' , 'foo' ] ) )
281+ . then ( ( ) => ng ( [ 'generate' , 'component' , 'baz' , '--module' , path . join ( 'foo' , 'foo' ) ] ) )
282+ . then ( ( ) => readFile ( modulePath , 'utf-8' ) )
283+ . then ( content => {
284+ expect ( content ) . matches ( / i m p o r t .* B a z C o m p o n e n t .* f r o m ' .\/ ..\/ b a z \/ b a z .c o m p o n e n t ' ; / ) ;
285+ expect ( content ) . matches ( / d e c l a r a t i o n s : \s + \[ B a z C o m p o n e n t ] / m) ;
286+ } ) ;
287+ } ) ;
288+
289+ it ( 'when given a submodule folder' , ( ) => {
290+ const appRoot = path . join ( root , 'tmp/foo' ) ;
291+ const modulePath = path . join ( appRoot , 'src/app/foo/foo.module.ts' ) ;
292+
293+ return Promise . resolve ( )
294+ . then ( ( ) => ng ( [ 'generate' , 'module' , 'foo' ] ) )
295+ . then ( ( ) => ng ( [ 'generate' , 'component' , 'baz' , '--module' , 'foo' ] ) )
296+ . then ( ( ) => readFile ( modulePath , 'utf-8' ) )
297+ . then ( content => {
298+ expect ( content ) . matches ( / i m p o r t .* B a z C o m p o n e n t .* f r o m ' .\/ ..\/ b a z \/ b a z .c o m p o n e n t ' ; / ) ;
299+ expect ( content ) . matches ( / d e c l a r a t i o n s : \s + \[ B a z C o m p o n e n t ] / m) ;
300+ } ) ;
301+ } ) ;
302+
303+ it ( 'when given deep submodule folder with missing module.ts suffix' , ( ) => {
304+ const appRoot = path . join ( root , 'tmp/foo' ) ;
305+ const modulePath = path . join ( appRoot , 'src/app/foo/bar/bar.module.ts' ) ;
306+
307+ return Promise . resolve ( )
308+ . then ( ( ) => ng ( [ 'generate' , 'module' , 'foo' ] ) )
309+ . then ( ( ) => ng ( [ 'generate' , 'module' , path . join ( 'foo' , 'bar' ) ] ) )
310+ . then ( ( ) => ng ( [ 'generate' , 'component' , 'baz' , '--module' , path . join ( 'foo' , 'bar' ) ] ) )
311+ . then ( ( ) => readFile ( modulePath , 'utf-8' ) )
312+ . then ( content => {
313+ expect ( content ) . matches ( / i m p o r t .* B a z C o m p o n e n t .* f r o m ' .\/ ..\/ ..\/ b a z \/ b a z .c o m p o n e n t ' ; / ) ;
314+ expect ( content ) . matches ( / d e c l a r a t i o n s : \s + \[ B a z C o m p o n e n t ] / m) ;
315+ } ) ;
316+ } ) ;
317+ } ) ;
225318} ) ;
0 commit comments