@@ -2,19 +2,22 @@ import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/te
22import { getFileContent } from '@schematics/angular/utility/test' ;
33import * as path from 'path' ;
44
5- import { createTestApp , createCommonWebpackConfig } from '../../utils/testing' ;
5+ import { createTestApp , createCommonWebpackConfigForAngular6 , createCommonWebpackConfigForAngular7orGreater } from '../../utils/testing' ;
66import { NgAddOptions } from '.' ;
77
8- describe ( 'ng-add' , ( ) => {
9- const TARGET_CONFIG_PATH = 'node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/common.js' ;
8+ const TARGET_COMMON_CONFIG_PATH = 'node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/common.js' ;
9+ const TARGET_TYPESCRIPT_CONFIG_PATH = 'node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/typescript.js' ;
10+
11+ describe ( 'ng-add-angular-6' , ( ) => {
12+
1013 let runner : SchematicTestRunner ;
1114 let appTree : UnitTestTree ;
1215
1316 const defaultOptions : NgAddOptions = { } ;
1417
1518 beforeEach ( ( ) => {
1619 appTree = createTestApp ( ) ;
17- createCommonWebpackConfig ( appTree ) ;
20+ createCommonWebpackConfigForAngular6 ( appTree ) ;
1821 runner = new SchematicTestRunner ( 'schematics' , path . join ( __dirname , '../collection.json' ) ) ;
1922 } ) ;
2023
@@ -29,16 +32,16 @@ describe('ng-add', () => {
2932 } ) ;
3033
3134 it ( 'should add 2 new pug rules' , ( ) => {
32- const oldConfig = eval ( getFileContent ( appTree , TARGET_CONFIG_PATH ) ) ;
35+ const oldConfig = eval ( getFileContent ( appTree , TARGET_COMMON_CONFIG_PATH ) ) ;
3336 const tree = runner . runSchematic ( 'ng-add' , defaultOptions , appTree ) ;
34- const config = eval ( getFileContent ( tree , TARGET_CONFIG_PATH ) ) ;
37+ const config = eval ( getFileContent ( tree , TARGET_COMMON_CONFIG_PATH ) ) ;
3538
3639 expect ( config . rules . length - oldConfig . rules . length ) . toBe ( 2 ) ;
3740 } ) ;
3841
3942 it ( 'should add the apply and pug loaders in webpack config' , ( ) => {
4043 const tree = runner . runSchematic ( 'ng-add' , defaultOptions , appTree ) ;
41- const config = eval ( getFileContent ( tree , TARGET_CONFIG_PATH ) ) ;
44+ const config = eval ( getFileContent ( tree , TARGET_COMMON_CONFIG_PATH ) ) ;
4245 const pugRule = config . rules [ 0 ] ;
4346
4447 expect ( pugRule . test . toString ( ) ) . toBe ( / \. ( p u g | j a d e ) $ / . toString ( ) ) ;
@@ -51,13 +54,20 @@ describe('ng-add', () => {
5154
5255 it ( 'should add the pug loader only for include/partial files in webpack config' , ( ) => {
5356 const tree = runner . runSchematic ( 'ng-add' , defaultOptions , appTree ) ;
54- const config = eval ( getFileContent ( tree , TARGET_CONFIG_PATH ) ) ;
57+ const config = eval ( getFileContent ( tree , TARGET_COMMON_CONFIG_PATH ) ) ;
5558 const partialPugRule = config . rules [ 1 ] ;
5659
5760 expect ( partialPugRule . test . toString ( ) ) . toBe ( / \. ( i n c l u d e | p a r t i a l ) \. ( p u g | j a d e ) $ / . toString ( ) ) ;
5861 expect ( partialPugRule . loader ) . toBe ( 'pug-loader' ) ;
5962 } ) ;
6063
64+ it ( 'should not modify the typescript.js file in webpack config' , ( ) => {
65+ const tree = runner . runSchematic ( 'ng-add' , defaultOptions , appTree ) ;
66+ const newTypescriptConfig = getFileContent ( tree , TARGET_TYPESCRIPT_CONFIG_PATH ) ;
67+
68+ expect ( newTypescriptConfig ) . not . toContain ( 'directTemplateLoading' ) ;
69+ } ) ;
70+
6171 it ( 'should add script file to root' , ( ) => {
6272 const tree = runner . runSchematic ( 'ng-add' , defaultOptions , appTree ) ;
6373 expect ( tree . read ( '/ng-add-pug-loader.js' ) ) . toBeDefined ( ) ;
@@ -73,3 +83,23 @@ describe('ng-add', () => {
7383 expect ( ( npmInstallTask . options as any ) . command ) . toBe ( 'install' ) ;
7484 } ) ;
7585} ) ;
86+
87+
88+ describe ( 'ng-add-angular-7-or-greater-specific' , ( ) => {
89+ let runner : SchematicTestRunner ;
90+ let appTree : UnitTestTree ;
91+
92+ const defaultOptions : NgAddOptions = { } ;
93+
94+ beforeEach ( ( ) => {
95+ appTree = createTestApp ( ) ;
96+ createCommonWebpackConfigForAngular7orGreater ( appTree ) ;
97+ runner = new SchematicTestRunner ( 'schematics' , path . join ( __dirname , '../collection.json' ) ) ;
98+ } ) ;
99+
100+ it ( 'the typescript.js file in webpack config should have directTemplateLoading: false' , ( ) => {
101+ const tree = runner . runSchematic ( 'ng-add' , defaultOptions , appTree ) ;
102+ const newTypescriptConfig = getFileContent ( tree , TARGET_TYPESCRIPT_CONFIG_PATH ) ;
103+ expect ( newTypescriptConfig ) . toContain ( 'directTemplateLoading: false' ) ;
104+ } ) ;
105+ } ) ;
0 commit comments