1
+ import * as fs from 'fs' ;
1
2
import { join } from 'path' ;
3
+
2
4
import * as util from './util' ;
3
5
4
6
import * as Constants from '../util/constants' ;
@@ -558,21 +560,41 @@ export function removeDecorators(fileName: string, source: string): string {
558
560
} ) ;
559
561
560
562
describe ( 'getNgModuleDataFromPage' , ( ) => {
561
- it ( 'should throw when NgModule is not in cache' , ( ) => {
563
+ it ( 'should throw when NgModule is not in cache and create default ngModule flag is off ' , ( ) => {
562
564
const prefix = join ( 'Users' , 'noone' , 'myApp' , 'src' ) ;
563
565
const appNgModulePath = join ( prefix , 'app' , 'app.module.ts' ) ;
564
566
const pagePath = join ( prefix , 'pages' , 'page-one' , 'page-one.ts' ) ;
567
+ const knownClassName = 'PageOne' ;
565
568
const fileCache = new FileCache ( ) ;
566
569
spyOn ( helpers , helpers . getStringPropertyValue . name ) . and . returnValue ( '.module.ts' ) ;
570
+ spyOn ( helpers , helpers . getBooleanPropertyValue . name ) . and . returnValue ( false ) ;
571
+
567
572
const knownErrorMsg = 'Should never happen' ;
568
573
try {
569
- util . getNgModuleDataFromPage ( appNgModulePath , pagePath , fileCache , false ) ;
574
+ util . getNgModuleDataFromPage ( appNgModulePath , pagePath , knownClassName , fileCache , false ) ;
570
575
throw new Error ( knownErrorMsg ) ;
571
576
} catch ( ex ) {
572
577
expect ( ex . message ) . not . toEqual ( knownErrorMsg ) ;
578
+ expect ( helpers . getBooleanPropertyValue ) . toHaveBeenCalledWith ( Constants . ENV_CREATE_DEFAULT_NG_MODULE_WHEN_MISSING ) ;
573
579
}
574
580
} ) ;
575
581
582
+ it ( 'should create a default ngModule and write it to disk when create default ngModule flag is on' , ( ) => {
583
+ const prefix = join ( 'Users' , 'noone' , 'myApp' , 'src' ) ;
584
+ const appNgModulePath = join ( prefix , 'app' , 'app.module.ts' ) ;
585
+ const pagePath = join ( prefix , 'pages' , 'page-one' , 'page-one.ts' ) ;
586
+ const knownClassName = 'PageOne' ;
587
+ const fileCache = new FileCache ( ) ;
588
+ spyOn ( helpers , helpers . getStringPropertyValue . name ) . and . returnValue ( '.module.ts' ) ;
589
+ spyOn ( helpers , helpers . getBooleanPropertyValue . name ) . and . returnValue ( true ) ;
590
+ spyOn ( fs , 'writeFileSync' ) ;
591
+ const result = util . getNgModuleDataFromPage ( appNgModulePath , pagePath , knownClassName , fileCache , false ) ;
592
+ expect ( result . absolutePath ) . toEqual ( 'Users/noone/myApp/src/pages/page-one/page-one.module.ts' ) ;
593
+ expect ( result . userlandModulePath ) . toEqual ( '../pages/page-one/page-one.module' ) ;
594
+ expect ( result . className ) . toEqual ( 'PageOneModule' ) ;
595
+ expect ( fs . writeFileSync ) . toHaveBeenCalled ( ) ;
596
+ } ) ;
597
+
576
598
it ( 'should return non-aot adjusted paths when not in AoT' , ( ) => {
577
599
const pageNgModuleContent = `
578
600
import { NgModule } from '@angular/core';
@@ -594,15 +616,18 @@ export class HomePageModule {}
594
616
const appNgModulePath = join ( prefix , 'app' , 'app.module.ts' ) ;
595
617
const pageNgModulePath = join ( prefix , 'pages' , 'page-one' , 'page-one.module.ts' ) ;
596
618
const pagePath = join ( prefix , 'pages' , 'page-one' , 'page-one.ts' ) ;
619
+ const knownClassName = 'PageOne' ;
597
620
const fileCache = new FileCache ( ) ;
598
621
fileCache . set ( pageNgModulePath , { path : pageNgModulePath , content : pageNgModuleContent } ) ;
599
622
spyOn ( helpers , helpers . getStringPropertyValue . name ) . and . returnValue ( '.module.ts' ) ;
623
+ spyOn ( helpers , helpers . getBooleanPropertyValue . name ) ;
600
624
601
- const result = util . getNgModuleDataFromPage ( appNgModulePath , pagePath , fileCache , false ) ;
625
+ const result = util . getNgModuleDataFromPage ( appNgModulePath , pagePath , knownClassName , fileCache , false ) ;
602
626
603
627
expect ( result . absolutePath ) . toEqual ( pageNgModulePath ) ;
604
628
expect ( result . userlandModulePath ) . toEqual ( '../pages/page-one/page-one.module' ) ;
605
629
expect ( result . className ) . toEqual ( 'HomePageModule' ) ;
630
+ expect ( helpers . getBooleanPropertyValue ) . not . toHaveBeenCalled ( ) ;
606
631
} ) ;
607
632
608
633
it ( 'should return adjusted paths to account for AoT' , ( ) => {
@@ -626,14 +651,17 @@ export class HomePageModule {}
626
651
const appNgModulePath = join ( prefix , 'app' , 'app.module.ts' ) ;
627
652
const pageNgModulePath = join ( prefix , 'pages' , 'page-one' , 'page-one.module.ts' ) ;
628
653
const pagePath = join ( prefix , 'pages' , 'page-one' , 'page-one.ts' ) ;
654
+ const knownClassName = 'PageOne' ;
629
655
const fileCache = new FileCache ( ) ;
630
656
fileCache . set ( pageNgModulePath , { path : pageNgModulePath , content : pageNgModuleContent } ) ;
631
657
spyOn ( helpers , helpers . getStringPropertyValue . name ) . and . returnValue ( '.module.ts' ) ;
658
+ spyOn ( helpers , helpers . getBooleanPropertyValue . name ) ;
632
659
633
- const result = util . getNgModuleDataFromPage ( appNgModulePath , pagePath , fileCache , true ) ;
660
+ const result = util . getNgModuleDataFromPage ( appNgModulePath , pagePath , knownClassName , fileCache , true ) ;
634
661
expect ( result . absolutePath ) . toEqual ( helpers . changeExtension ( pageNgModulePath , '.ngfactory.ts' ) ) ;
635
662
expect ( result . userlandModulePath ) . toEqual ( '../pages/page-one/page-one.module.ngfactory' ) ;
636
663
expect ( result . className ) . toEqual ( 'HomePageModuleNgFactory' ) ;
664
+ expect ( helpers . getBooleanPropertyValue ) . not . toHaveBeenCalled ( ) ;
637
665
} ) ;
638
666
} ) ;
639
667
@@ -2320,4 +2348,30 @@ export const AppModuleNgFactory:import0.NgModuleFactory<import1.AppModule> = new
2320
2348
expect ( result . indexOf ( expectedDeepLinkString ) ) . toBeGreaterThanOrEqual ( 0 ) ;
2321
2349
} ) ;
2322
2350
} ) ;
2351
+
2352
+ describe ( 'generateDefaultDeepLinkNgModuleContent' , ( ) => {
2353
+ it ( 'should generate a default NgModule for a DeepLinked component' , ( ) => {
2354
+ const knownFileContent = `
2355
+ import { NgModule } from '@angular/core';
2356
+ import { DeepLinkModule } from 'ionic-angular';
2357
+ import { PageOne } from './page-one';
2358
+
2359
+
2360
+ @NgModule({
2361
+ declarations: [
2362
+ PageOne,
2363
+ ],
2364
+ imports: [
2365
+ DeepLinkModule.forChild(PageOne)
2366
+ ]
2367
+ })
2368
+ export class PageOneModule {}
2369
+
2370
+ ` ;
2371
+ const knownFilePath = '/someFakePath/myApp/src/pages/page-one/page-one.ts' ;
2372
+ const knownClassName = 'PageOne' ;
2373
+ const fileContent = util . generateDefaultDeepLinkNgModuleContent ( knownFilePath , knownClassName ) ;
2374
+ expect ( fileContent ) . toEqual ( knownFileContent ) ;
2375
+ } ) ;
2376
+ } ) ;
2323
2377
} ) ;
0 commit comments