@@ -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 ,
@@ -27,7 +28,19 @@ describe('ng-add schematic', () => {
2728 `Expected "${ filePath } " to be added to the project styles in the workspace.` ) ;
2829 }
2930
31+ /** Removes the specified dependency from the /package.json in the given tree. */
32+ function removePackageJsonDependency ( tree : Tree , dependencyName : string ) {
33+ const packageContent = JSON . parse ( getFileContent ( tree , '/package.json' ) ) ;
34+ delete packageContent . dependencies [ dependencyName ] ;
35+ tree . overwrite ( '/package.json' , JSON . stringify ( packageContent , null , 2 ) ) ;
36+ }
37+
3038 it ( 'should update package.json' , ( ) => {
39+ // By default, the Angular workspace schematic sets up "@angular/animations". In order
40+ // to verify that we would set up the dependency properly if someone doesn't have the
41+ // animations installed already, we remove the animations dependency explicitly.
42+ removePackageJsonDependency ( appTree , '@angular/animations' ) ;
43+
3144 const tree = runner . runSchematic ( 'ng-add' , { } , appTree ) ;
3245 const packageJson = JSON . parse ( getFileContent ( tree , '/package.json' ) ) ;
3346 const dependencies = packageJson . dependencies ;
@@ -142,4 +155,56 @@ describe('ng-add schematic', () => {
142155 'Expected the project main file to not contain a HammerJS import.' ) ;
143156 } ) ;
144157 } ) ;
158+
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' ) ;
163+
164+ expect ( fileContent ) . toContain ( 'BrowserAnimationsModule' ,
165+ 'Expected the project app module to import the "BrowserAnimationsModule".' ) ;
166+ } ) ;
167+
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 ) ;
181+
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 " / ) ) ;
184+ } ) ;
185+ } ) ;
186+
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 ) ;
190+ const fileContent = getFileContent ( tree , '/projects/material/src/app/app.module.ts' ) ;
191+
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".' ) ;
208+ } ) ;
209+ } ) ;
145210} ) ;
0 commit comments