From a86c404f14e28fb1fea34bbf59362da4654a7923 Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Thu, 8 Jul 2021 16:25:15 +0300 Subject: [PATCH] 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 --- .../worker/test/prefetch_spec.ts | 3 +- .../service-worker/worker/testing/events.ts | 118 ++++++++++++++++++ .../service-worker/worker/testing/scope.ts | 107 +--------------- 3 files changed, 121 insertions(+), 107 deletions(-) create mode 100644 packages/service-worker/worker/testing/events.ts diff --git a/packages/service-worker/worker/test/prefetch_spec.ts b/packages/service-worker/worker/test/prefetch_spec.ts index 4f3dc11be9..24cee1d23c 100644 --- a/packages/service-worker/worker/test/prefetch_spec.ts +++ b/packages/service-worker/worker/test/prefetch_spec.ts @@ -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() { diff --git a/packages/service-worker/worker/testing/events.ts b/packages/service-worker/worker/testing/events.ts new file mode 100644 index 0000000000..f42c1698a0 --- /dev/null +++ b/packages/service-worker/worker/testing/events.ts @@ -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[] = []; + + get ready(): Promise { + return (async () => { + while (this.queue.length > 0) { + await this.queue.shift(); + } + })(); + } + + waitUntil(promise: Promise): void { + this.queue.push(promise); + } +} + +export class MockActivateEvent extends MockExtendableEvent { + constructor() { + super('activate'); + } +} + +export class MockFetchEvent extends MockExtendableEvent { + response: Promise = Promise.resolve(undefined); + + constructor( + readonly request: Request, readonly clientId: string, readonly resultingClientId: string) { + super('fetch'); + } + + respondWith(promise: Promise): Promise { + 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'); + } +} diff --git a/packages/service-worker/worker/testing/scope.ts b/packages/service-worker/worker/testing/scope.ts index 8c8b9602d3..2619a8de94 100644 --- a/packages/service-worker/worker/testing/scope.ts +++ b/packages/service-worker/worker/testing/scope.ts @@ -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[] = []; - - waitUntil(promise: Promise): void { - this.queue.push(promise); - } - - get ready(): Promise { - return (async () => { - while (this.queue.length > 0) { - await this.queue.shift(); - } - })(); - } -} - -class MockFetchEvent extends MockExtendableEvent { - response: Promise = Promise.resolve(undefined); - - constructor( - readonly request: Request, readonly clientId: string, readonly resultingClientId: string) { - super('fetch'); - } - - respondWith(promise: Promise): Promise { - 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'); - } -}