@@ -3,6 +3,7 @@ import {WorkspaceProject} from '@angular-devkit/core/src/workspace';
33import { Tree } from '@angular-devkit/schematics' ;
44import { SchematicTestRunner } from '@angular-devkit/schematics/testing' ;
55import {
6+ addModuleImportToRootModule ,
67 createTestApp ,
78 getProjectFromWorkspace ,
89 getProjectStyleFile ,
@@ -37,9 +38,9 @@ describe('ng-add schematic', () => {
3738 it ( 'should update package.json' , ( ) => {
3839 // By default, the Angular workspace schematic sets up "@angular/animations". In order
3940 // to verify that we would set up the dependency properly if someone doesn't have the
40- // animations installed already, we remove the animations dependency explicitly.
41+ // animations installed already, we remove the animations dependency explicitly.
4142 removePackageJsonDependency ( appTree , '@angular/animations' ) ;
42-
43+
4344 const tree = runner . runSchematic ( 'ng-add' , { } , appTree ) ;
4445 const packageJson = JSON . parse ( getFileContent ( tree , '/package.json' ) ) ;
4546 const dependencies = packageJson . dependencies ;
@@ -155,27 +156,55 @@ describe('ng-add schematic', () => {
155156 } ) ;
156157 } ) ;
157158
158- describe ( 'animations disabled' , ( ) => {
159+ describe ( 'animations enabled' , ( ) => {
160+ it ( 'should add the BrowserAnimationsModule to the project module' , ( ) => {
161+ const tree = runner . runSchematic ( 'ng-add-setup-project' , { } , appTree ) ;
162+ const fileContent = getFileContent ( tree , '/projects/material/src/app/app.module.ts' ) ;
159163
160- it ( 'should not add @angular/animations to package.json' , ( ) => {
161- // By default, the Angular workspace schematic sets up "@angular/animations". In order
162- // to verify that we don't add the animations if the Animations are not installed, we need
163- // to remove the dependency for this unit test tree.
164- removePackageJsonDependency ( appTree , '@angular/animations' ) ;
164+ expect ( fileContent ) . toContain ( 'BrowserAnimationsModule' ,
165+ 'Expected the project app module to import the "BrowserAnimationsModule".' ) ;
166+ } ) ;
165167
166- const tree = runner . runSchematic ( 'ng-add' , { animations : false } , appTree ) ;
167- const packageJson = JSON . parse ( getFileContent ( tree , '/package.json' ) ) ;
168+ it ( 'should not add BrowserAnimationsModule if NoopAnimationsModule is set up' , ( ) => {
169+ const workspace = getWorkspace ( appTree ) ;
170+ const project = getProjectFromWorkspace ( workspace ) ;
171+
172+ // Simulate the case where a developer uses `ng-add` on an Angular CLI project which already
173+ // explicitly uses the `NoopAnimationsModule`. It would be wrong to forcibly enable browser
174+ // animations without knowing what other components would be affected. In this case, we
175+ // just print a warning message.
176+ addModuleImportToRootModule ( appTree , 'NoopAnimationsModule' ,
177+ '@angular/platform-browser/animations' , project ) ;
178+
179+ spyOn ( console , 'warn' ) ;
180+ runner . runSchematic ( 'ng-add-setup-project' , { } , appTree ) ;
168181
169- expect ( packageJson . dependencies [ '@angular/animations' ] )
170- . toBeUndefined ( `Expected '@angular/animations' to be not added to the package.json` ) ;
182+ expect ( console . warn ) . toHaveBeenCalledWith (
183+ jasmine . stringMatching ( / C o u l d n o t s e t u p " B r o w s e r A n i m a t i o n s M o d u l e " / ) ) ;
171184 } ) ;
185+ } ) ;
172186
173- it ( 'should not add the BrowserAnimationsModule to the project module' , ( ) => {
174- const tree = runner . runSchematic ( 'ng-add' , { gestures : false } , appTree ) ;
187+ describe ( 'animations disabled' , ( ) => {
188+ it ( 'should add the NoopAnimationsModule to the project module' , ( ) => {
189+ const tree = runner . runSchematic ( 'ng-add-setup-project' , { animations : false } , appTree ) ;
175190 const fileContent = getFileContent ( tree , '/projects/material/src/app/app.module.ts' ) ;
176191
177- expect ( fileContent ) . not . toContain ( 'BrowserAnimationsModule' ,
178- 'Expected the project app module to not import the "BrowserAnimationsModule".' ) ;
192+ expect ( fileContent ) . toContain ( 'NoopAnimationsModule' ,
193+ 'Expected the project app module to import the "NoopAnimationsModule".' ) ;
194+ } ) ;
195+
196+ it ( 'should not add NoopAnimationsModule if BrowserAnimationsModule is set up' , ( ) => {
197+ const workspace = getWorkspace ( appTree ) ;
198+ const project = getProjectFromWorkspace ( workspace ) ;
199+
200+ // Simulate the case where a developer uses `ng-add` on an Angular CLI project which already
201+ // explicitly uses the `BrowserAnimationsModule`. It would be wrong to forcibly change
202+ // to noop animations.
203+ const fileContent = addModuleImportToRootModule ( appTree , 'BrowserAnimationsModule' ,
204+ '@angular/platform-browser/animations' , project ) ;
205+
206+ expect ( fileContent ) . not . toContain ( 'NoopAnimationsModule' ,
207+ 'Expected the project app module to not import the "NoopAnimationsModule".' ) ;
179208 } ) ;
180209 } ) ;
181210} ) ;
0 commit comments