refactor(service-worker): move mock event classes to their own file (#42736)
In the ServiceWorker tests, we use mock implementations of the various events emitted during the ServiceWorker lifecycle. 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 event classes to a separate `testing/events.ts` file. PR Close #42736
This commit is contained in:
parent
7df1fa5411
commit
a86c404f14
|
@ -10,9 +10,10 @@ import {PrefetchAssetGroup} from '../src/assets';
|
|||
import {CacheDatabase} from '../src/db-cache';
|
||||
import {IdleScheduler} from '../src/idle';
|
||||
import {MockCache} from '../testing/cache';
|
||||
import {MockExtendableEvent} from '../testing/events';
|
||||
import {MockRequest} from '../testing/fetch';
|
||||
import {MockFileSystemBuilder, MockServerStateBuilder, tmpHashTable, tmpManifestSingleAssetGroup} from '../testing/mock';
|
||||
import {MockExtendableEvent, SwTestHarnessBuilder} from '../testing/scope';
|
||||
import {SwTestHarnessBuilder} from '../testing/scope';
|
||||
import {envIsSupported} from '../testing/utils';
|
||||
|
||||
(function() {
|
||||
|
|
|
@ -0,0 +1,118 @@
|
|||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
export class MockEvent implements Event {
|
||||
readonly AT_TARGET = -1;
|
||||
readonly BUBBLING_PHASE = -1;
|
||||
readonly CAPTURING_PHASE = -1;
|
||||
readonly NONE = -1;
|
||||
|
||||
readonly bubbles = false;
|
||||
cancelBubble = false;
|
||||
readonly cancelable = false;
|
||||
readonly composed = false;
|
||||
readonly currentTarget = null;
|
||||
readonly defaultPrevented = false;
|
||||
readonly eventPhase = -1;
|
||||
readonly isTrusted = false;
|
||||
returnValue = false;
|
||||
readonly srcElement = null;
|
||||
readonly target = null;
|
||||
readonly timeStamp = Date.now();
|
||||
|
||||
constructor(readonly type: string) {}
|
||||
|
||||
composedPath(): EventTarget[] {
|
||||
this.notImplemented();
|
||||
}
|
||||
initEvent(type: string, bubbles?: boolean, cancelable?: boolean): void {
|
||||
this.notImplemented();
|
||||
}
|
||||
preventDefault(): void {
|
||||
this.notImplemented();
|
||||
}
|
||||
stopImmediatePropagation(): void {
|
||||
this.notImplemented();
|
||||
}
|
||||
stopPropagation(): void {
|
||||
this.notImplemented();
|
||||
}
|
||||
|
||||
private notImplemented(): never {
|
||||
throw new Error('Method not implemented in `MockEvent`.');
|
||||
}
|
||||
}
|
||||
|
||||
export class MockExtendableEvent extends MockEvent implements ExtendableEvent {
|
||||
private queue: Promise<void>[] = [];
|
||||
|
||||
get ready(): Promise<void> {
|
||||
return (async () => {
|
||||
while (this.queue.length > 0) {
|
||||
await this.queue.shift();
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
waitUntil(promise: Promise<void>): void {
|
||||
this.queue.push(promise);
|
||||
}
|
||||
}
|
||||
|
||||
export class MockActivateEvent extends MockExtendableEvent {
|
||||
constructor() {
|
||||
super('activate');
|
||||
}
|
||||
}
|
||||
|
||||
export class MockFetchEvent extends MockExtendableEvent {
|
||||
response: Promise<Response|undefined> = Promise.resolve(undefined);
|
||||
|
||||
constructor(
|
||||
readonly request: Request, readonly clientId: string, readonly resultingClientId: string) {
|
||||
super('fetch');
|
||||
}
|
||||
|
||||
respondWith(promise: Promise<Response>): Promise<Response> {
|
||||
this.response = promise;
|
||||
return promise;
|
||||
}
|
||||
}
|
||||
|
||||
export class MockInstallEvent extends MockExtendableEvent {
|
||||
constructor() {
|
||||
super('install');
|
||||
}
|
||||
}
|
||||
|
||||
export class MockMessageEvent extends MockExtendableEvent {
|
||||
constructor(readonly data: Object, readonly source: MockClient|null) {
|
||||
super('message');
|
||||
}
|
||||
}
|
||||
|
||||
export class MockNotificationEvent extends MockExtendableEvent {
|
||||
readonly notification = {
|
||||
...this._notification,
|
||||
close: () => undefined,
|
||||
};
|
||||
|
||||
constructor(private _notification: any, readonly action?: string) {
|
||||
super('notification');
|
||||
}
|
||||
}
|
||||
|
||||
export class MockPushEvent extends MockExtendableEvent {
|
||||
data = {
|
||||
json: () => this._data,
|
||||
};
|
||||
|
||||
constructor(private _data: object) {
|
||||
super('push');
|
||||
}
|
||||
}
|
|
@ -13,6 +13,7 @@ import {AssetGroupConfig, Manifest} from '../src/manifest';
|
|||
import {sha1} from '../src/sha1';
|
||||
|
||||
import {MockCacheStorage} from './cache';
|
||||
import {MockActivateEvent, MockFetchEvent, MockInstallEvent, MockMessageEvent, MockNotificationEvent, MockPushEvent} from './events';
|
||||
import {MockHeaders, MockRequest, MockResponse} from './fetch';
|
||||
import {MockServerState, MockServerStateBuilder} from './mock';
|
||||
import {normalizeUrl, parseUrl} from './utils';
|
||||
|
@ -356,109 +357,3 @@ export class ConfigBuilder {
|
|||
};
|
||||
}
|
||||
}
|
||||
|
||||
class MockEvent implements Event {
|
||||
readonly AT_TARGET = -1;
|
||||
readonly BUBBLING_PHASE = -1;
|
||||
readonly CAPTURING_PHASE = -1;
|
||||
readonly NONE = -1;
|
||||
|
||||
readonly bubbles = false;
|
||||
cancelBubble = false;
|
||||
readonly cancelable = false;
|
||||
readonly composed = false;
|
||||
readonly currentTarget = null;
|
||||
readonly defaultPrevented = false;
|
||||
readonly eventPhase = -1;
|
||||
readonly isTrusted = false;
|
||||
returnValue = false;
|
||||
readonly srcElement = null;
|
||||
readonly target = null;
|
||||
readonly timeStamp = Date.now();
|
||||
|
||||
constructor(readonly type: string) {}
|
||||
|
||||
composedPath(): EventTarget[] {
|
||||
this.notImplemented();
|
||||
}
|
||||
initEvent(type: string, bubbles?: boolean, cancelable?: boolean): void {
|
||||
this.notImplemented();
|
||||
}
|
||||
preventDefault(): void {
|
||||
this.notImplemented();
|
||||
}
|
||||
stopImmediatePropagation(): void {
|
||||
this.notImplemented();
|
||||
}
|
||||
stopPropagation(): void {
|
||||
this.notImplemented();
|
||||
}
|
||||
|
||||
private notImplemented(): never {
|
||||
throw new Error('Method not implemented in `MockEvent`.');
|
||||
}
|
||||
}
|
||||
|
||||
export class MockExtendableEvent extends MockEvent implements ExtendableEvent {
|
||||
private queue: Promise<void>[] = [];
|
||||
|
||||
waitUntil(promise: Promise<void>): void {
|
||||
this.queue.push(promise);
|
||||
}
|
||||
|
||||
get ready(): Promise<void> {
|
||||
return (async () => {
|
||||
while (this.queue.length > 0) {
|
||||
await this.queue.shift();
|
||||
}
|
||||
})();
|
||||
}
|
||||
}
|
||||
|
||||
class MockFetchEvent extends MockExtendableEvent {
|
||||
response: Promise<Response|undefined> = Promise.resolve(undefined);
|
||||
|
||||
constructor(
|
||||
readonly request: Request, readonly clientId: string, readonly resultingClientId: string) {
|
||||
super('fetch');
|
||||
}
|
||||
|
||||
respondWith(promise: Promise<Response>): Promise<Response> {
|
||||
this.response = promise;
|
||||
return promise;
|
||||
}
|
||||
}
|
||||
|
||||
class MockMessageEvent extends MockExtendableEvent {
|
||||
constructor(readonly data: Object, readonly source: MockClient|null) {
|
||||
super('message');
|
||||
}
|
||||
}
|
||||
|
||||
class MockPushEvent extends MockExtendableEvent {
|
||||
constructor(private _data: Object) {
|
||||
super('push');
|
||||
}
|
||||
data = {
|
||||
json: () => this._data,
|
||||
};
|
||||
}
|
||||
|
||||
class MockNotificationEvent extends MockExtendableEvent {
|
||||
constructor(private _notification: any, readonly action?: string) {
|
||||
super('notification');
|
||||
}
|
||||
readonly notification = {...this._notification, close: () => undefined};
|
||||
}
|
||||
|
||||
class MockInstallEvent extends MockExtendableEvent {
|
||||
constructor() {
|
||||
super('install');
|
||||
}
|
||||
}
|
||||
|
||||
class MockActivateEvent extends MockExtendableEvent {
|
||||
constructor() {
|
||||
super('activate');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue