@@ -10,7 +10,7 @@ import { Schema as ApplicationOptions } from '../application/schema';
1010import { Schema as WorkspaceOptions } from '../workspace/schema' ;
1111import { Schema as ServiceWorkerOptions } from './schema' ;
1212
13-
13+ // tslint:disable-next-line:no-big-function
1414describe ( 'Service Worker Schematic' , ( ) => {
1515 const schematicRunner = new SchematicTestRunner (
1616 '@schematics/angular' ,
@@ -97,6 +97,64 @@ describe('Service Worker Schematic', () => {
9797 expect ( pkgText ) . toContain ( expectedText ) ;
9898 } ) ;
9999
100+ it ( 'should add the SW import to the NgModule imports with aliased environment' , async ( ) => {
101+ const moduleContent = `
102+ import { BrowserModule } from '@angular/platform-browser';
103+ import { NgModule } from '@angular/core';
104+
105+ import { AppComponent } from './app.component';
106+ import { environment as env } from '../environments/environment';
107+
108+ @NgModule({
109+ declarations: [
110+ AppComponent
111+ ],
112+ imports: [
113+ BrowserModule
114+ ],
115+ bootstrap: [AppComponent]
116+ })
117+ export class AppModule {}
118+ ` ;
119+
120+ appTree . overwrite ( '/projects/bar/src/app/app.module.ts' , moduleContent ) ;
121+
122+ const tree = await schematicRunner . runSchematicAsync ( 'service-worker' , defaultOptions , appTree )
123+ . toPromise ( ) ;
124+ const pkgText = tree . readContent ( '/projects/bar/src/app/app.module.ts' ) ;
125+ const expectedText = 'ServiceWorkerModule.register(\'ngsw-worker.js\', { enabled: env.production })' ;
126+ expect ( pkgText ) . toContain ( expectedText ) ;
127+ } ) ;
128+
129+ it ( 'should add the SW import to the NgModule imports with existing environment' , async ( ) => {
130+ const moduleContent = `
131+ import { BrowserModule } from '@angular/platform-browser';
132+ import { NgModule } from '@angular/core';
133+
134+ import { AppComponent } from './app.component';
135+ import { environment } from '../environments/environment';
136+
137+ @NgModule({
138+ declarations: [
139+ AppComponent
140+ ],
141+ imports: [
142+ BrowserModule
143+ ],
144+ bootstrap: [AppComponent]
145+ })
146+ export class AppModule {}
147+ ` ;
148+
149+ appTree . overwrite ( '/projects/bar/src/app/app.module.ts' , moduleContent ) ;
150+
151+ const tree = await schematicRunner . runSchematicAsync ( 'service-worker' , defaultOptions , appTree )
152+ . toPromise ( ) ;
153+ const pkgText = tree . readContent ( '/projects/bar/src/app/app.module.ts' ) ;
154+ const expectedText = 'ServiceWorkerModule.register(\'ngsw-worker.js\', { enabled: environment.production })' ;
155+ expect ( pkgText ) . toContain ( expectedText ) ;
156+ } ) ;
157+
100158 it ( 'should put the ngsw-config.json file in the project root' , async ( ) => {
101159 const tree = await schematicRunner . runSchematicAsync ( 'service-worker' , defaultOptions , appTree )
102160 . toPromise ( ) ;
@@ -188,5 +246,4 @@ describe('Service Worker Schematic', () => {
188246 expect ( projects . foo . architect . build . configurations . production . ngswConfigPath )
189247 . toBe ( 'ngsw-config.json' ) ;
190248 } ) ;
191-
192249} ) ;
0 commit comments