@@ -10,7 +10,7 @@ import { Schema as ApplicationOptions } from '../application/schema';
10
10
import { Schema as WorkspaceOptions } from '../workspace/schema' ;
11
11
import { Schema as ServiceWorkerOptions } from './schema' ;
12
12
13
-
13
+ // tslint:disable-next-line:no-big-function
14
14
describe ( 'Service Worker Schematic' , ( ) => {
15
15
const schematicRunner = new SchematicTestRunner (
16
16
'@schematics/angular' ,
@@ -97,6 +97,64 @@ describe('Service Worker Schematic', () => {
97
97
expect ( pkgText ) . toContain ( expectedText ) ;
98
98
} ) ;
99
99
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
+
100
158
it ( 'should put the ngsw-config.json file in the project root' , async ( ) => {
101
159
const tree = await schematicRunner . runSchematicAsync ( 'service-worker' , defaultOptions , appTree )
102
160
. toPromise ( ) ;
@@ -188,5 +246,4 @@ describe('Service Worker Schematic', () => {
188
246
expect ( projects . foo . architect . build . configurations . production . ngswConfigPath )
189
247
. toBe ( 'ngsw-config.json' ) ;
190
248
} ) ;
191
-
192
249
} ) ;
0 commit comments