forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_spec.ts
240 lines (208 loc) · 8.64 KB
/
index_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { tags } from '@angular-devkit/core';
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { Schema as ApplicationOptions } from '../application/schema';
import { Builders } from '../utility/workspace-models';
import { Schema as WorkspaceOptions } from '../workspace/schema';
import { Schema as ServiceWorkerOptions } from './schema';
describe('Service Worker Schematic', () => {
const schematicRunner = new SchematicTestRunner(
'@schematics/angular',
require.resolve('../collection.json'),
);
const defaultOptions: ServiceWorkerOptions = {
project: 'bar',
target: 'build',
};
let appTree: UnitTestTree;
const workspaceOptions: WorkspaceOptions = {
name: 'workspace',
newProjectRoot: 'projects',
version: '6.0.0',
};
const appOptions: ApplicationOptions = {
name: 'bar',
inlineStyle: false,
inlineTemplate: false,
routing: false,
skipTests: false,
skipPackageJson: false,
};
beforeEach(async () => {
appTree = await schematicRunner.runSchematic('workspace', workspaceOptions);
appTree = await schematicRunner.runSchematic('application', appOptions, appTree);
});
it('should add `serviceWorker` option to build target', async () => {
const tree = await schematicRunner.runSchematic('service-worker', defaultOptions, appTree);
const configText = tree.readContent('/angular.json');
const buildConfig = JSON.parse(configText).projects.bar.architect.build;
expect(buildConfig.configurations.production.serviceWorker).toBe(
'projects/bar/ngsw-config.json',
);
});
it('should add the necessary dependency', async () => {
const tree = await schematicRunner.runSchematic('service-worker', defaultOptions, appTree);
const pkgText = tree.readContent('/package.json');
const pkg = JSON.parse(pkgText);
const version = pkg.dependencies['@angular/core'];
expect(pkg.dependencies['@angular/service-worker']).toEqual(version);
});
it('should put the ngsw-config.json file in the project root', async () => {
const tree = await schematicRunner.runSchematic('service-worker', defaultOptions, appTree);
const path = '/projects/bar/ngsw-config.json';
expect(tree.exists(path)).toEqual(true);
const { projects } = JSON.parse(tree.readContent('/angular.json'));
expect(projects.bar.architect.build.configurations.production.serviceWorker).toBe(
'projects/bar/ngsw-config.json',
);
});
it('should add $schema in ngsw-config.json with correct relative path', async () => {
const pathToNgswConfigSchema = 'node_modules/@angular/service-worker/config/schema.json';
const name = 'foo';
const rootAppOptions: ApplicationOptions = {
...appOptions,
name,
projectRoot: '',
};
const rootSWOptions: ServiceWorkerOptions = {
...defaultOptions,
project: name,
};
const rootAppTree = await schematicRunner.runSchematic('application', rootAppOptions, appTree);
const treeInRoot = await schematicRunner.runSchematic(
'service-worker',
rootSWOptions,
rootAppTree,
);
const pkgTextInRoot = treeInRoot.readContent('/ngsw-config.json');
const configInRoot = JSON.parse(pkgTextInRoot);
expect(configInRoot.$schema).toBe(`./${pathToNgswConfigSchema}`);
const treeNotInRoot = await schematicRunner.runSchematic(
'service-worker',
defaultOptions,
appTree,
);
const pkgTextNotInRoot = treeNotInRoot.readContent('/projects/bar/ngsw-config.json');
const configNotInRoot = JSON.parse(pkgTextNotInRoot);
expect(configNotInRoot.$schema).toBe(`../../${pathToNgswConfigSchema}`);
});
it('should generate ngsw-config.json in root when the application is at root level', async () => {
const name = 'foo';
const rootAppOptions: ApplicationOptions = {
...appOptions,
name,
projectRoot: '',
};
const rootSWOptions: ServiceWorkerOptions = {
...defaultOptions,
project: name,
};
let tree = await schematicRunner.runSchematic('application', rootAppOptions, appTree);
tree = await schematicRunner.runSchematic('service-worker', rootSWOptions, tree);
expect(tree.exists('/ngsw-config.json')).toBe(true);
});
it(`should add the 'provideServiceWorker' to providers`, async () => {
const tree = await schematicRunner.runSchematic('service-worker', defaultOptions, appTree);
const content = tree.readContent('/projects/bar/src/app/app.config.ts');
expect(tags.oneLine`${content}`).toContain(tags.oneLine`
provideServiceWorker('ngsw-worker.js', {
enabled: !isDevMode(),
registrationStrategy: 'registerWhenStable:30000'
})
`);
});
it(`should import 'isDevMode' from '@angular/core'`, async () => {
const tree = await schematicRunner.runSchematic('service-worker', defaultOptions, appTree);
const content = tree.readContent('/projects/bar/src/app/app.config.ts');
expect(content).toContain(
`import { ApplicationConfig, provideZoneChangeDetection, isDevMode } from '@angular/core';`,
);
});
it(`should import 'provideServiceWorker' from '@angular/service-worker'`, async () => {
const tree = await schematicRunner.runSchematic('service-worker', defaultOptions, appTree);
const content = tree.readContent('/projects/bar/src/app/app.config.ts');
expect(content).toContain(`import { provideServiceWorker } from '@angular/service-worker';`);
});
describe('standalone=false', () => {
const name = 'buz';
const nonStandaloneAppOptions: ApplicationOptions = {
...appOptions,
name,
standalone: false,
};
const nonStandaloneSWOptions: ServiceWorkerOptions = {
...defaultOptions,
project: name,
};
beforeEach(async () => {
appTree = await schematicRunner.runSchematic('application', nonStandaloneAppOptions, appTree);
});
it('should import ServiceWorkerModule', async () => {
const tree = await schematicRunner.runSchematic(
'service-worker',
nonStandaloneSWOptions,
appTree,
);
const pkgText = tree.readContent('/projects/buz/src/app/app-module.ts');
expect(pkgText).toMatch(/import \{ ServiceWorkerModule \} from '@angular\/service-worker'/);
});
it('should add the SW import to the NgModule imports', async () => {
const tree = await schematicRunner.runSchematic(
'service-worker',
nonStandaloneSWOptions,
appTree,
);
const pkgText = tree.readContent('/projects/buz/src/app/app-module.ts');
expect(pkgText).toMatch(
new RegExp(
"(\\s+)ServiceWorkerModule\\.register\\('ngsw-worker\\.js', \\{\\n" +
'\\1 enabled: !isDevMode\\(\\),\\n' +
'\\1 // Register the ServiceWorker as soon as the application is stable\\n' +
'\\1 // or after 30 seconds \\(whichever comes first\\)\\.\\n' +
"\\1 registrationStrategy: 'registerWhenStable:30000'\\n" +
'\\1}\\)',
),
);
});
});
describe('Legacy browser builder', () => {
function convertBuilderToLegacyBrowser(): void {
const config = JSON.parse(appTree.readContent('/angular.json'));
const build = config.projects.bar.architect.build;
build.builder = Builders.Browser;
build.options = {
...build.options,
main: build.options.browser,
browser: undefined,
};
build.configurations.development = {
...build.configurations.development,
vendorChunk: true,
namedChunks: true,
buildOptimizer: false,
};
appTree.overwrite('/angular.json', JSON.stringify(config, undefined, 2));
}
beforeEach(() => {
convertBuilderToLegacyBrowser();
});
it('should add `serviceWorker` option to build target', async () => {
const tree = await schematicRunner.runSchematic('service-worker', defaultOptions, appTree);
const configText = tree.readContent('/angular.json');
const buildConfig = JSON.parse(configText).projects.bar.architect.build;
expect(buildConfig.options.serviceWorker).toBeTrue();
});
it('should add `ngswConfigPath` option to build target', async () => {
const tree = await schematicRunner.runSchematic('service-worker', defaultOptions, appTree);
const configText = tree.readContent('/angular.json');
const buildConfig = JSON.parse(configText).projects.bar.architect.build;
expect(buildConfig.options.ngswConfigPath).toBe('projects/bar/ngsw-config.json');
});
});
});