test(service-worker): minor test fixes and refactorings (#22883)

PR Close #22883
This commit is contained in:
George Kalpakas 2018-03-20 00:29:00 +02:00 committed by Alex Rickabaugh
parent 776bb8206f
commit d0f575bc54

View File

@ -170,7 +170,11 @@ const manifestUpdateHash = sha1(JSON.stringify(manifestUpdate));
let driver: Driver; let driver: Driver;
beforeEach(() => { beforeEach(() => {
server.clearRequests(); server.reset();
serverUpdate.reset();
server404.reset();
brokenServer.reset();
scope = new SwTestHarnessBuilder().withServerState(server).build(); scope = new SwTestHarnessBuilder().withServerState(server).build();
driver = new Driver(scope, scope, new CacheDatabase(scope, scope)); driver = new Driver(scope, scope, new CacheDatabase(scope, scope));
}); });
@ -275,39 +279,6 @@ const manifestUpdateHash = sha1(JSON.stringify(manifestUpdate));
serverUpdate.assertNoOtherRequests(); serverUpdate.assertNoOtherRequests();
}); });
async_it('updates to new content when requested', async() => {
expect(await makeRequest(scope, '/foo.txt')).toEqual('this is foo');
await driver.initialized;
const client = scope.clients.getMock('default') !;
expect(client.messages).toEqual([]);
scope.updateServerState(serverUpdate);
expect(await driver.checkForUpdate()).toEqual(true);
serverUpdate.assertSawRequestFor('ngsw.json');
serverUpdate.assertSawRequestFor('/foo.txt');
serverUpdate.assertSawRequestFor('/redirected.txt');
serverUpdate.assertNoOtherRequests();
expect(client.messages).toEqual([{
type: 'UPDATE_AVAILABLE',
current: {hash: manifestHash, appData: {version: 'original'}},
available: {hash: manifestUpdateHash, appData: {version: 'update'}},
}]);
// Default client is still on the old version of the app.
expect(await makeRequest(scope, '/foo.txt')).toEqual('this is foo');
// Sending a new client id should result in the updated version being returned.
expect(await makeRequest(scope, '/foo.txt', 'new')).toEqual('this is foo v2');
// Of course, the old version should still work.
expect(await makeRequest(scope, '/foo.txt')).toEqual('this is foo');
expect(await makeRequest(scope, '/bar.txt')).toEqual('this is bar');
serverUpdate.assertNoOtherRequests();
});
async_it('updates a specific client to new content on request', async() => { async_it('updates a specific client to new content on request', async() => {
expect(await makeRequest(scope, '/foo.txt')).toEqual('this is foo'); expect(await makeRequest(scope, '/foo.txt')).toEqual('this is foo');
await driver.initialized; await driver.initialized;
@ -403,8 +374,8 @@ const manifestUpdateHash = sha1(JSON.stringify(manifestUpdate));
serverUpdate.clearRequests(); serverUpdate.clearRequests();
scope = new SwTestHarnessBuilder() scope = new SwTestHarnessBuilder()
.withServerState(serverUpdate)
.withCacheState(scope.caches.dehydrate()) .withCacheState(scope.caches.dehydrate())
.withServerState(serverUpdate)
.build(); .build();
driver = new Driver(scope, scope, new CacheDatabase(scope, scope)); driver = new Driver(scope, scope, new CacheDatabase(scope, scope));
@ -478,6 +449,10 @@ const manifestUpdateHash = sha1(JSON.stringify(manifestUpdate));
await driver.initialized; await driver.initialized;
serverUpdate.assertNoOtherRequests(); serverUpdate.assertNoOtherRequests();
let keys = await scope.caches.keys();
let hasOriginalCaches = keys.some(name => name.startsWith(`ngsw:${manifestHash}:`));
expect(hasOriginalCaches).toEqual(true);
scope.clients.remove('default'); scope.clients.remove('default');
scope.advance(12000); scope.advance(12000);
@ -487,10 +462,9 @@ const manifestUpdateHash = sha1(JSON.stringify(manifestUpdate));
driver = new Driver(scope, scope, new CacheDatabase(scope, scope)); driver = new Driver(scope, scope, new CacheDatabase(scope, scope));
expect(await makeRequest(scope, '/foo.txt')).toEqual('this is foo v2'); expect(await makeRequest(scope, '/foo.txt')).toEqual('this is foo v2');
const oldManifestHash = sha1(JSON.stringify(manifest)); keys = await scope.caches.keys();
const keys = await scope.caches.keys(); hasOriginalCaches = keys.some(name => name.startsWith(`ngsw:${manifestHash}:`));
const hasOldCaches = keys.some(name => name.startsWith(oldManifestHash + ':')); expect(hasOriginalCaches).toEqual(false);
expect(hasOldCaches).toEqual(false);
}); });
async_it('shows notifications for push notifications', async() => { async_it('shows notifications for push notifications', async() => {
@ -639,6 +613,7 @@ const manifestUpdateHash = sha1(JSON.stringify(manifestUpdate));
serverUpdate.assertNoOtherRequests(); serverUpdate.assertNoOtherRequests();
}); });
}); });
describe('routing', () => { describe('routing', () => {
async_beforeEach(async() => { async_beforeEach(async() => {
expect(await makeRequest(scope, '/foo.txt')).toEqual('this is foo'); expect(await makeRequest(scope, '/foo.txt')).toEqual('this is foo');
@ -680,6 +655,7 @@ const manifestUpdateHash = sha1(JSON.stringify(manifestUpdate));
headers: { headers: {
'Accept': 'text/plain, text/html, text/css', 'Accept': 'text/plain, text/html, text/css',
}, },
mode: 'navigate',
})).toBeNull(); })).toBeNull();
server.assertSawRequestFor('/baz.html'); server.assertSawRequestFor('/baz.html');
}); });
@ -726,11 +702,11 @@ const manifestUpdateHash = sha1(JSON.stringify(manifestUpdate));
})(); })();
async function makeRequest( async function makeRequest(
scope: SwTestHarness, url: string, clientId?: string, init?: Object): Promise<string|null> { scope: SwTestHarness, url: string, clientId = 'default', init?: Object): Promise<string|null> {
const [resPromise, done] = scope.handleFetch(new MockRequest(url, init), clientId || 'default'); const [resPromise, done] = scope.handleFetch(new MockRequest(url, init), clientId);
await done; await done;
const res = await resPromise; const res = await resPromise;
scope.clients.add(clientId || 'default'); scope.clients.add(clientId);
if (res !== undefined && res.ok) { if (res !== undefined && res.ok) {
return res.text(); return res.text();
} }