test(service-worker): improve adding clients in `SwTestHarness` (#23625)
This commits changes how clients are added in `SwTestHarness`, so that the behavior in tests closer mimics what would happen in an actual ServiceWorker. It also removes auto-adding clients when calling `clients.get()`, which could hide bugs related to non-existing clients. PR Close #23625
This commit is contained in:
parent
08e7efc69e
commit
d1abf4e897
|
@ -483,7 +483,6 @@ const manifestUpdateHash = sha1(JSON.stringify(manifestUpdate));
|
||||||
async_it('shows notifications for push notifications', async() => {
|
async_it('shows notifications for push notifications', 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;
|
||||||
scope.clients.add('default');
|
|
||||||
await scope.handlePush({
|
await scope.handlePush({
|
||||||
notification: {
|
notification: {
|
||||||
title: 'This is a test',
|
title: 'This is a test',
|
||||||
|
@ -665,7 +664,7 @@ const manifestUpdateHash = sha1(JSON.stringify(manifestUpdate));
|
||||||
server.assertSawRequestFor('/baz');
|
server.assertSawRequestFor('/baz');
|
||||||
});
|
});
|
||||||
|
|
||||||
async_it('does not redirect to index on a request that does not expect HTML', async() => {
|
async_it('does not redirect to index on a request that does not accept HTML', async() => {
|
||||||
expect(await navRequest('/baz', {headers: {}})).toBeNull();
|
expect(await navRequest('/baz', {headers: {}})).toBeNull();
|
||||||
server.assertSawRequestFor('/baz');
|
server.assertSawRequestFor('/baz');
|
||||||
|
|
||||||
|
@ -793,7 +792,6 @@ async function makeRequest(
|
||||||
const [resPromise, done] = scope.handleFetch(new MockRequest(url, init), clientId);
|
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);
|
|
||||||
if (res !== undefined && res.ok) {
|
if (res !== undefined && res.ok) {
|
||||||
return res.text();
|
return res.text();
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,10 +62,7 @@ export class MockClients implements Clients {
|
||||||
|
|
||||||
remove(clientId: string): void { this.clients.delete(clientId); }
|
remove(clientId: string): void { this.clients.delete(clientId); }
|
||||||
|
|
||||||
async get(id: string): Promise<Client> {
|
async get(id: string): Promise<Client> { return this.clients.get(id) !as any as Client; }
|
||||||
this.add(id);
|
|
||||||
return this.clients.get(id) !as any as Client;
|
|
||||||
}
|
|
||||||
|
|
||||||
getMock(id: string): MockClient|undefined { return this.clients.get(id); }
|
getMock(id: string): MockClient|undefined { return this.clients.get(id); }
|
||||||
|
|
||||||
|
@ -197,6 +194,10 @@ export class SwTestHarness implements ServiceWorkerGlobalScope, Adapter, Context
|
||||||
const event = new MockFetchEvent(req, clientId || null);
|
const event = new MockFetchEvent(req, clientId || null);
|
||||||
this.eventHandlers.get('fetch') !.call(this, event);
|
this.eventHandlers.get('fetch') !.call(this, event);
|
||||||
|
|
||||||
|
if (clientId) {
|
||||||
|
this.clients.add(clientId);
|
||||||
|
}
|
||||||
|
|
||||||
return [event.response, event.ready];
|
return [event.response, event.ready];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue