Skip to content
This repository was archived by the owner on Oct 12, 2021. It is now read-only.

Commit 4c05cee

Browse files
committed
feat(testing): End-to-end testing configuration for the Service Worker, including a basic sanity test which verifies that the worker installs correctly.
1 parent 465a4b7 commit 4c05cee

File tree

16 files changed

+509
-101
lines changed

16 files changed

+509
-101
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// Place your settings in this file to overwrite default and user settings.
22
{
3-
"typescript.tsdk": "node_modules/typescript/lib"
3+
"typescript.tsdk": "node_modules/typescript/lib",
4+
"editor.insertSpaces": true,
5+
"editor.tabSize": 2
46
}

service-worker/worker/gulpfile.ts

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var commonCompilerConfig = JSON.parse(fs.readFileSync('./tsconfig.cjs.json')).co
1818
commonCompilerConfig.typescript = require('typescript');
1919
systemCompilerConfig.typescript = require('typescript');
2020

21-
gulp.task('default', ['build']);
21+
gulp.task('default', ['prepublish']);
2222

2323
gulp.task('clean', (done) => {
2424
rimraf('./dist', done);
@@ -69,6 +69,76 @@ gulp.task('build:generator', () => gulp
6969
.pipe(ts(commonCompilerConfig))
7070
.pipe(gulp.dest('dist/src/generator')));
7171

72+
gulp.task('build:test_harness', done => runSequence(
73+
'clean',
74+
[
75+
'!build:test_harness:client',
76+
'!build:test_harness:tests'
77+
],
78+
done));
79+
80+
gulp.task('!build:test_harness:client', done => runSequence(
81+
[
82+
'!bundle',
83+
'!build:test_harness:client:compile',
84+
'copy:test_harness:client:local',
85+
'copy:test_harness:client:modules'
86+
],
87+
'copy:test_harness:client:worker',
88+
done
89+
));
90+
91+
gulp.task('!build:test_harness:client:compile', () => gulp
92+
.src([
93+
'typings/globals/**/*.d.ts',
94+
'src/testing/harness/client/**/*.ts'
95+
])
96+
.pipe(ts(systemCompilerConfig))
97+
.pipe(gulp.dest('dist/harness/client')));
98+
99+
gulp.task('copy:test_harness:client:local', () => gulp
100+
.src([
101+
'src/testing/harness/client/index.html'
102+
], {base: 'src/testing/harness/client'})
103+
.pipe(gulp.dest('dist/harness/client')));
104+
105+
gulp.task('copy:test_harness:client:modules', () => gulp
106+
.src([
107+
'node_modules/@angular/**/*.js',
108+
'node_modules/systemjs/dist/system.js',
109+
'node_modules/reflect-metadata/Reflect.js',
110+
'node_modules/zone.js/dist/zone.js',
111+
'node_modules/rxjs/**/*.js'
112+
], {base: '.'})
113+
.pipe(gulp.dest('dist/harness/client')))
114+
115+
gulp.task('copy:test_harness:client:worker', () => gulp
116+
.src([
117+
'dist/worker.js'
118+
])
119+
.pipe(gulp.dest('dist/harness/client')));
120+
121+
gulp.task('!build:test_harness:tests', [
122+
'!copy:test_harness:tests:protractor',
123+
'!build:test_harness:tests:compile'
124+
]);
125+
126+
gulp.task('!build:test_harness:tests:compile', () => gulp
127+
.src([
128+
'typings/globals/**/*.d.ts',
129+
'src/typings/*.d.ts',
130+
'src/e2e/**/*.ts',
131+
'src/testing/harness/server/**/*.ts',
132+
], {base: '.'})
133+
.pipe(ts(commonCompilerConfig))
134+
.pipe(gulp.dest('dist/e2e')));
135+
136+
gulp.task('!copy:test_harness:tests:protractor', () => gulp
137+
.src([
138+
'src/e2e/**/protractor.config.js'
139+
], {base: '.'})
140+
.pipe(gulp.dest('dist/e2e')));
141+
72142
gulp.task('copy:generator', ['build:generator'], () => gulp
73143
.src([
74144
'dist/src/generator/**/*.js'

service-worker/worker/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
},
2121
"homepage": "https://github.com/angular/progressive#readme",
2222
"devDependencies": {
23+
"@angular/common": "^2.0.0-rc.1",
24+
"@angular/compiler": "^2.0.0-rc.1",
25+
"@angular/platform-browser": "^2.0.0-rc.1",
26+
"@angular/platform-browser-dynamic": "^2.0.0-rc.1",
2327
"es6-shim": "^0.33.13",
28+
"express": "^4.13.4",
2429
"gulp": "^3.9.0",
2530
"gulp-clean": "^0.3.1",
2631
"gulp-concat": "^2.6.0",
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
declare var browser;
2+
declare var element;
3+
declare var by;
4+
5+
export class HarnessPageObject {
6+
7+
sendKeysSlow(el, keys) {
8+
keys.split('').forEach(char => el.sendKeys(char));
9+
}
10+
11+
selectAction(action: string) {
12+
this.sendKeysSlow(element(by.css('#actionInput')).clear(), action);
13+
element(by.css('#actionExec'))
14+
.click();
15+
}
16+
17+
setTextOn(id: string, text: string) {
18+
this.sendKeysSlow(element(by.css(`#${id}`)).clear(), text);
19+
}
20+
21+
clickButton(id: string) {
22+
element(by.css(`#${id}`))
23+
.click();
24+
}
25+
26+
get result(): Promise<string> {
27+
return element(by.css('#result')).getText();
28+
}
29+
30+
request(url: string): Promise<string> {
31+
this.selectAction('MAKE_REQUEST');
32+
this.setTextOn('requestUrl', url);
33+
this.clickButton('requestAction');
34+
return this.result;
35+
}
36+
37+
installServiceWorker(url: string): void {
38+
this.selectAction('SW_INSTALL');
39+
this.setTextOn('workerUrl', url);
40+
this.clickButton('installAction');
41+
}
42+
43+
hasActiveWorker(): Promise<boolean> {
44+
this.selectAction('SW_CHECK');
45+
return this
46+
.result
47+
.then(JSON.parse)
48+
.then(res => res.some(worker => worker.active));
49+
}
50+
51+
hasServiceWorker(): Promise<boolean> {
52+
this.selectAction('SW_CHECK');
53+
browser.waitForAngular();
54+
return this
55+
.result
56+
.then(value => {
57+
return value;
58+
})
59+
.then(value => value !== '[]');
60+
}
61+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
exports.config = {
2+
baseUrl: 'http://localhost:8080/dev/src/',
3+
specs: ['*.e2e.js'],
4+
directConnect: true,
5+
exclude: [],
6+
multiCapabilities: [{
7+
browserName: 'chrome'
8+
}],
9+
allScriptsTimeout: 110000,
10+
getPageTimeout: 100000,
11+
framework: 'jasmine2',
12+
jasmineNodeOpts: {
13+
isVerbose: false,
14+
showColors: true,
15+
includeStackTrace: false,
16+
defaultTimeoutInterval: 400000
17+
},
18+
19+
/**
20+
* ng2 related configuration
21+
*
22+
* useAllAngular2AppRoots: tells Protractor to wait for any angular2 apps on the page instead of just the one matching
23+
* `rootEl`
24+
*
25+
*/
26+
useAllAngular2AppRoots: true
27+
};
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import {create, Server} from '../../testing/harness/server/server';
2+
import {HarnessPageObject} from '../page-object';
3+
declare var browser;
4+
declare var element;
5+
declare var by;
6+
7+
8+
let server: Server;
9+
let po: HarnessPageObject;
10+
11+
const SIMPLE_MANIFEST = {
12+
group: {
13+
default: {
14+
version: 'test',
15+
url: {
16+
'/hello.txt': {}
17+
}
18+
}
19+
}
20+
};
21+
22+
beforeAll(done => {
23+
create(8080, 'dist/harness/client').then(s => {
24+
server = s;
25+
done();
26+
});
27+
});
28+
29+
beforeEach(() => {
30+
server.clearResponses();
31+
po = new HarnessPageObject();
32+
});
33+
34+
function expectNoServiceWorker(): Promise<void> {
35+
return po
36+
.hasServiceWorker()
37+
.then(workerPresent => {
38+
expect(workerPresent).toBeFalsy();
39+
console.log('expectation', workerPresent);
40+
});
41+
}
42+
43+
describe('world sanity', () => {
44+
it('starts without a service worker', done => {
45+
browser.get('/index.html');
46+
po
47+
.hasActiveWorker()
48+
.then(hasWorker => {
49+
expect(hasWorker).toBeFalsy();
50+
})
51+
.then(done);
52+
});
53+
it('able to mock a request', done => {
54+
server.addResponse('/hello.txt', 'Hello world!');
55+
po
56+
.request('/hello.txt')
57+
.then(result => {
58+
expect(result).toBe('Hello world!');
59+
})
60+
.then(done);
61+
});
62+
it('can install a service worker', done => {
63+
server.addResponse('/ngsw-manifest.json.js', '/* mocked */');
64+
server.addResponse('/ngsw-manifest.json', JSON.stringify(SIMPLE_MANIFEST));
65+
server.addResponse('/hello.txt', 'Hello world!');
66+
po.installServiceWorker('/worker.js');
67+
po
68+
.hasActiveWorker()
69+
.then(hasWorker => {
70+
expect(hasWorker).toBeTruthy();
71+
})
72+
.then(done);
73+
});
74+
it('worker is not yet controlling the page', done => {
75+
server.addResponse('/hello.txt', 'Still from the server');
76+
po
77+
.request('/hello.txt')
78+
.then(response => {
79+
expect(response).toBe('Still from the server');
80+
})
81+
.then(done);
82+
});
83+
it('after reload, worker serves cached /hello.txt', done => {
84+
browser.get('/index.html');
85+
server.addResponse('/hello.txt', 'Goodbye world?');
86+
po
87+
.request('/hello.txt')
88+
.then(result => {
89+
expect(result).toBe('Hello world!');
90+
})
91+
.then(done);
92+
});
93+
});
94+
95+
afterAll(done => {
96+
server.shutdown();
97+
done();
98+
});

service-worker/worker/src/manual_typings/jshashes.d.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

service-worker/worker/src/manual_typings/service-worker.d.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

service-worker/worker/src/test/mock_adapter.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)