/
service-worker_spec.ts
232 lines (205 loc) · 7.34 KB
/
service-worker_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
/**
* @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.io/license
*/
import { Architect } from '@angular-devkit/architect';
import { normalize, virtualFs } from '@angular-devkit/core';
import { debounceTime, take, tap } from 'rxjs/operators';
import { createArchitect, host } from '../../../testing/test-utils';
describe('Browser Builder service worker', () => {
const manifest = {
index: '/index.html',
assetGroups: [
{
name: 'app',
installMode: 'prefetch',
resources: {
files: ['/favicon.ico', '/index.html', '/*.bundle.css', '/*.bundle.js', '/*.chunk.js'],
},
},
{
name: 'assets',
installMode: 'lazy',
updateMode: 'prefetch',
resources: {
files: ['/assets/**', '/*.(eot|svg|cur|jpg|png|webp|gif|otf|ttf|woff|woff2|ani)'],
},
},
],
};
const target = { project: 'app', target: 'build' };
let architect: Architect;
beforeEach(async () => {
await host.initialize().toPromise();
architect = (await createArchitect(host.root())).architect;
});
afterEach(async () => host.restore().toPromise());
it('errors if no ngsw-config.json is present', async () => {
const overrides = { serviceWorker: true };
const run = await architect.scheduleTarget(target, overrides);
await expectAsync(run.result).toBeResolvedTo(jasmine.objectContaining({ success: false }));
await run.stop();
});
it('works with service worker', async () => {
host.writeMultipleFiles({
'src/ngsw-config.json': JSON.stringify(manifest),
'src/assets/folder-asset.txt': 'folder-asset.txt',
'src/styles.css': `body { background: url(./spectrum.png); }`,
});
const overrides = { serviceWorker: true };
const run = await architect.scheduleTarget(target, overrides);
await expectAsync(run.result).toBeResolvedTo(jasmine.objectContaining({ success: true }));
expect(host.scopedSync().exists(normalize('dist/ngsw.json'))).toBeTrue();
const ngswJson = JSON.parse(
virtualFs.fileBufferToString(host.scopedSync().read(normalize('dist/ngsw.json'))),
);
// Verify index and assets are there.
expect(ngswJson).toEqual(
jasmine.objectContaining({
configVersion: 1,
index: '/index.html',
navigationUrls: [
{ positive: true, regex: '^\\/.*$' },
{ positive: false, regex: '^\\/(?:.+\\/)?[^/]*\\.[^/]*$' },
{ positive: false, regex: '^\\/(?:.+\\/)?[^/]*__[^/]*$' },
{ positive: false, regex: '^\\/(?:.+\\/)?[^/]*__[^/]*\\/.*$' },
],
assetGroups: [
{
name: 'app',
installMode: 'prefetch',
updateMode: 'prefetch',
urls: ['/favicon.ico', '/index.html'],
cacheQueryOptions: {
ignoreVary: true,
},
patterns: [],
},
{
name: 'assets',
installMode: 'lazy',
updateMode: 'prefetch',
urls: ['/assets/folder-asset.txt', '/spectrum.png'],
cacheQueryOptions: {
ignoreVary: true,
},
patterns: [],
},
],
dataGroups: [],
hashTable: {
'/favicon.ico': '84161b857f5c547e3699ddfbffc6d8d737542e01',
'/assets/folder-asset.txt': '617f202968a6a81050aa617c2e28e1dca11ce8d4',
'/index.html': '8964a35a8b850942f8d18ba919f248762ff3154d',
'/spectrum.png': '8d048ece46c0f3af4b598a95fd8e4709b631c3c0',
},
}),
);
await run.stop();
});
it('works in watch mode', async () => {
host.writeMultipleFiles({
'src/ngsw-config.json': JSON.stringify(manifest),
'src/assets/folder-asset.txt': 'folder-asset.txt',
'src/styles.css': `body { background: url(./spectrum.png); }`,
});
const overrides = { serviceWorker: true, watch: true };
const run = await architect.scheduleTarget(target, overrides);
let buildNumber = 0;
await run.output
.pipe(
debounceTime(3000),
tap((buildEvent) => {
expect(buildEvent.success).toBeTrue();
const ngswJsonPath = normalize('dist/ngsw.json');
expect(host.scopedSync().exists(ngswJsonPath)).toBeTrue();
const ngswJson = JSON.parse(
virtualFs.fileBufferToString(host.scopedSync().read(ngswJsonPath)),
);
const hashTableEntries = Object.keys(ngswJson.hashTable);
buildNumber += 1;
switch (buildNumber) {
case 1:
expect(hashTableEntries).toEqual([
'/assets/folder-asset.txt',
'/favicon.ico',
'/index.html',
'/spectrum.png',
]);
host.copyFile('src/assets/folder-asset.txt', 'src/assets/folder-new-asset.txt');
break;
case 2:
expect(hashTableEntries).toEqual([
'/assets/folder-asset.txt',
'/assets/folder-new-asset.txt',
'/favicon.ico',
'/index.html',
'/spectrum.png',
]);
break;
}
}),
take(2),
)
.toPromise();
await run.stop();
});
it('works with service worker and baseHref', async () => {
host.writeMultipleFiles({
'src/ngsw-config.json': JSON.stringify(manifest),
'src/assets/folder-asset.txt': 'folder-asset.txt',
});
const overrides = { serviceWorker: true, baseHref: '/foo/bar' };
const run = await architect.scheduleTarget(target, overrides);
await expectAsync(run.result).toBeResolvedTo(jasmine.objectContaining({ success: true }));
expect(host.scopedSync().exists(normalize('dist/ngsw.json'))).toBeTrue();
const ngswJson = JSON.parse(
virtualFs.fileBufferToString(host.scopedSync().read(normalize('dist/ngsw.json'))),
);
// Verify index and assets include the base href.
expect(ngswJson).toEqual(
jasmine.objectContaining({
configVersion: 1,
index: '/foo/bar/index.html',
navigationUrls: [
{ positive: true, regex: '^\\/.*$' },
{ positive: false, regex: '^\\/(?:.+\\/)?[^/]*\\.[^/]*$' },
{ positive: false, regex: '^\\/(?:.+\\/)?[^/]*__[^/]*$' },
{ positive: false, regex: '^\\/(?:.+\\/)?[^/]*__[^/]*\\/.*$' },
],
assetGroups: [
{
name: 'app',
installMode: 'prefetch',
updateMode: 'prefetch',
urls: ['/foo/bar/favicon.ico', '/foo/bar/index.html'],
patterns: [],
cacheQueryOptions: {
ignoreVary: true,
},
},
{
name: 'assets',
installMode: 'lazy',
updateMode: 'prefetch',
urls: ['/foo/bar/assets/folder-asset.txt'],
patterns: [],
cacheQueryOptions: {
ignoreVary: true,
},
},
],
dataGroups: [],
hashTable: {
'/foo/bar/favicon.ico': '84161b857f5c547e3699ddfbffc6d8d737542e01',
'/foo/bar/assets/folder-asset.txt': '617f202968a6a81050aa617c2e28e1dca11ce8d4',
'/foo/bar/index.html': '5c99755c1e7cfd1c8aba34ad1155afc72a288fec',
},
}),
);
await run.stop();
});
});