refactor(service-worker): move mock client classes to their own file (#42736)
In the ServiceWorker tests, we use mock implementations of the various client APIs that the ServiceWorker interacts with. Previously, these mock implementations were defined in the `testing/scope.ts` file. This added several extra classes to a file that already contains a few, making it harder to maintain. Therefore, this commit moves these mock client classes to a separate `testing/clients.ts` file. PR Close #42736
This commit is contained in:
parent
22a81231f2
commit
ad9085f3d6
|
@ -12,9 +12,10 @@ import {Driver, DriverReadyState} from '../src/driver';
|
|||
import {AssetGroupConfig, DataGroupConfig, Manifest} from '../src/manifest';
|
||||
import {sha1} from '../src/sha1';
|
||||
import {clearAllCaches, MockCache} from '../testing/cache';
|
||||
import {WindowClientImpl} from '../testing/clients';
|
||||
import {MockRequest, MockResponse} from '../testing/fetch';
|
||||
import {MockFileSystem, MockFileSystemBuilder, MockServerState, MockServerStateBuilder, tmpHashTableForFs} from '../testing/mock';
|
||||
import {MockClient, SwTestHarness, SwTestHarnessBuilder, WindowClientImpl} from '../testing/scope';
|
||||
import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope';
|
||||
import {envIsSupported} from '../testing/utils';
|
||||
|
||||
(function() {
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
/**
|
||||
* @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 {Subject} from 'rxjs';
|
||||
|
||||
|
||||
export class MockClient {
|
||||
queue = new Subject<Object>();
|
||||
|
||||
constructor(readonly id: string) {}
|
||||
|
||||
readonly messages: Object[] = [];
|
||||
|
||||
postMessage(message: Object): void {
|
||||
this.messages.push(message);
|
||||
this.queue.next(message);
|
||||
}
|
||||
}
|
||||
|
||||
export class WindowClientImpl extends MockClient implements WindowClient {
|
||||
readonly ancestorOrigins: ReadonlyArray<string> = [];
|
||||
readonly focused: boolean = false;
|
||||
readonly visibilityState: VisibilityState = 'hidden';
|
||||
frameType: ClientFrameType = 'top-level';
|
||||
url = 'http://localhost/unique';
|
||||
|
||||
constructor(readonly id: string) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
async focus(): Promise<WindowClient> {
|
||||
return this;
|
||||
}
|
||||
|
||||
async navigate(url: string): Promise<WindowClient|null> {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
export class MockClients implements Clients {
|
||||
private clients = new Map<string, MockClient>();
|
||||
|
||||
add(clientId: string): void {
|
||||
if (this.clients.has(clientId)) {
|
||||
return;
|
||||
}
|
||||
this.clients.set(clientId, new MockClient(clientId));
|
||||
}
|
||||
|
||||
remove(clientId: string): void {
|
||||
this.clients.delete(clientId);
|
||||
}
|
||||
|
||||
async get(id: string): Promise<Client> {
|
||||
return this.clients.get(id)! as any as Client;
|
||||
}
|
||||
|
||||
getMock(id: string): MockClient|undefined {
|
||||
return this.clients.get(id);
|
||||
}
|
||||
|
||||
async matchAll<T extends ClientQueryOptions>(options?: T):
|
||||
Promise<ReadonlyArray<T['type'] extends 'window'? WindowClient : Client>> {
|
||||
return Array.from(this.clients.values()) as any[];
|
||||
}
|
||||
|
||||
async openWindow(url: string): Promise<WindowClient|null> {
|
||||
return null;
|
||||
}
|
||||
|
||||
async claim(): Promise<any> {}
|
||||
}
|
|
@ -6,13 +6,12 @@
|
|||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Subject} from 'rxjs';
|
||||
|
||||
import {Adapter} from '../src/adapter';
|
||||
import {AssetGroupConfig, Manifest} from '../src/manifest';
|
||||
import {sha1} from '../src/sha1';
|
||||
|
||||
import {MockCacheStorage} from './cache';
|
||||
import {MockClient, MockClients} from './clients';
|
||||
import {MockActivateEvent, MockExtendableMessageEvent, MockFetchEvent, MockInstallEvent, MockNotificationEvent, MockPushEvent} from './events';
|
||||
import {MockHeaders, MockRequest, MockResponse} from './fetch';
|
||||
import {MockServerState, MockServerStateBuilder} from './mock';
|
||||
|
@ -20,37 +19,6 @@ import {normalizeUrl, parseUrl} from './utils';
|
|||
|
||||
const EMPTY_SERVER_STATE = new MockServerStateBuilder().build();
|
||||
|
||||
export class MockClient {
|
||||
queue = new Subject<Object>();
|
||||
|
||||
constructor(readonly id: string) {}
|
||||
|
||||
readonly messages: Object[] = [];
|
||||
|
||||
postMessage(message: Object): void {
|
||||
this.messages.push(message);
|
||||
this.queue.next(message);
|
||||
}
|
||||
}
|
||||
export class WindowClientImpl extends MockClient implements WindowClient {
|
||||
readonly ancestorOrigins: ReadonlyArray<string> = [];
|
||||
readonly focused: boolean = false;
|
||||
readonly visibilityState: VisibilityState = 'hidden';
|
||||
frameType: ClientFrameType = 'top-level';
|
||||
url = 'http://localhost/unique';
|
||||
|
||||
constructor(readonly id: string) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
async focus(): Promise<WindowClient> {
|
||||
return this;
|
||||
}
|
||||
async navigate(url: string): Promise<WindowClient|null> {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
export class SwTestHarnessBuilder {
|
||||
private origin = parseUrl(this.scopeUrl).origin;
|
||||
private server = EMPTY_SERVER_STATE;
|
||||
|
@ -73,39 +41,6 @@ export class SwTestHarnessBuilder {
|
|||
}
|
||||
}
|
||||
|
||||
export class MockClients implements Clients {
|
||||
private clients = new Map<string, MockClient>();
|
||||
|
||||
add(clientId: string): void {
|
||||
if (this.clients.has(clientId)) {
|
||||
return;
|
||||
}
|
||||
this.clients.set(clientId, new MockClient(clientId));
|
||||
}
|
||||
|
||||
remove(clientId: string): void {
|
||||
this.clients.delete(clientId);
|
||||
}
|
||||
|
||||
async get(id: string): Promise<Client> {
|
||||
return this.clients.get(id)! as any as Client;
|
||||
}
|
||||
|
||||
getMock(id: string): MockClient|undefined {
|
||||
return this.clients.get(id);
|
||||
}
|
||||
|
||||
async matchAll<T extends ClientQueryOptions>(options?: T):
|
||||
Promise<ReadonlyArray<T['type'] extends 'window'? WindowClient : Client>> {
|
||||
return Array.from(this.clients.values()) as any[];
|
||||
}
|
||||
async openWindow(url: string): Promise<WindowClient|null> {
|
||||
return null;
|
||||
}
|
||||
|
||||
async claim(): Promise<any> {}
|
||||
}
|
||||
|
||||
export class SwTestHarness extends Adapter<MockCacheStorage> implements ServiceWorkerGlobalScope {
|
||||
readonly clients = new MockClients();
|
||||
private eventHandlers = new Map<string, Function>();
|
||||
|
|
Loading…
Reference in New Issue