Skip to content

Commit 342bcdb

Browse files
committed
test: add test for service worker in browser builder
1 parent c4efd82 commit 342bcdb

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { normalize, virtualFs } from '@angular-devkit/core';
10+
import { tap } from 'rxjs/operators';
11+
import { browserTargetSpec, host, runTargetSpec } from '../utils';
12+
13+
14+
describe('Browser Builder', () => {
15+
const manifest = {
16+
index: '/index.html',
17+
assetGroups: [
18+
{
19+
name: 'app',
20+
installMode: 'prefetch',
21+
resources: {
22+
files: [ '/favicon.ico', '/index.html' ],
23+
versionedFiles: [
24+
'/*.bundle.css',
25+
'/*.bundle.js',
26+
'/*.chunk.js',
27+
],
28+
},
29+
},
30+
{
31+
name: 'assets',
32+
installMode: 'lazy',
33+
updateMode: 'prefetch',
34+
resources: {
35+
files: [ '/assets/**' ],
36+
},
37+
},
38+
],
39+
};
40+
41+
beforeEach(done => host.initialize().subscribe(undefined, done.fail, done));
42+
afterEach(done => host.restore().subscribe(undefined, done.fail, done));
43+
44+
it('errors if no ngsw-config.json is present', (done) => {
45+
const overrides = { serviceWorker: true };
46+
47+
runTargetSpec(host, browserTargetSpec, overrides)
48+
.subscribe(event => {
49+
expect(event.success).toBe(false);
50+
}, done, done.fail);
51+
}, 30000);
52+
53+
it('works with service worker', (done) => {
54+
host.writeMultipleFiles({
55+
'src/ngsw-config.json': JSON.stringify(manifest),
56+
});
57+
58+
const overrides = { serviceWorker: true };
59+
runTargetSpec(host, browserTargetSpec, overrides).pipe(
60+
tap(buildEvent => {
61+
expect(buildEvent.success).toBe(true);
62+
expect(host.scopedSync().exists(normalize('dist/ngsw.json')));
63+
}),
64+
).subscribe(undefined, done.fail, done);
65+
}, 30000);
66+
67+
it('works with service worker and baseHref', (done) => {
68+
host.writeMultipleFiles({
69+
'src/ngsw-config.json': JSON.stringify(manifest),
70+
});
71+
72+
const overrides = { serviceWorker: true, baseHref: '/foo/bar' };
73+
runTargetSpec(host, browserTargetSpec, overrides).pipe(
74+
tap(buildEvent => {
75+
expect(buildEvent.success).toBe(true);
76+
expect(host.scopedSync().exists(normalize('dist/ngsw.json')));
77+
expect(virtualFs.fileBufferToString(
78+
host.scopedSync().read(normalize('dist/ngsw.json')),
79+
)).toMatch(/"\/foo\/bar\/index.html"/);
80+
}),
81+
).subscribe(undefined, done.fail, done);
82+
}, 30000);
83+
});

0 commit comments

Comments
 (0)