angular/packages/service-worker/worker/testing/events.ts
Kristiyan Kostadinov ea61ec2562 feat(core): support TypeScript 4.4 (#43281)
Adds support for TypeScript 4.4. High-level overview of the changes made in this PR:

* Bumps the various packages to `typescript@4.4.2` and `tslib@2.3.0`.
* The `useUnknownInCatchVariables` compiler option has been disabled so that we don't have to cast error objects explicitly everywhere.
* TS now passes in a third argument to the `__spreadArray` call inside child class constructors. I had to update a couple of places in the runtime and ngcc to be able to pick up the calls correctly.
* TS now generates code like `(0, foo)(arg1, arg2)` for imported function calls. I had to update a few of our tests to account for it. See https://github.com/microsoft/TypeScript/pull/44624.
* Our `ngtsc` test setup calls the private `matchFiles` function from TS. I had to update our usage, because a new parameter was added.
* There was one place where we were setting the readonly `hasTrailingComma` property. I updated the usage to pass in the value when constructing the object instead.
* Some browser types were updated which meant that I had to resolve some trivial type errors.
* The downlevel decorators tranform was running into an issue where the Closure synthetic comments were being emitted twice. I've worked around it by recreating the class declaration node instead of cloning it.

PR Close #43281
2021-09-23 14:49:19 -07:00

125 lines
3.1 KiB
TypeScript

/**
* @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<unknown>[] = [];
get ready(): Promise<void> {
return (async () => {
while (this.queue.length > 0) {
await this.queue.shift();
}
})();
}
waitUntil(promise: Promise<unknown>): void {
this.queue.push(promise);
}
}
export class MockActivateEvent extends MockExtendableEvent {
constructor() {
super('activate');
}
}
export class MockFetchEvent extends MockExtendableEvent implements FetchEvent {
readonly preloadResponse = Promise.resolve();
handled = Promise.resolve(undefined);
response: Promise<Response|undefined> = Promise.resolve(undefined);
constructor(
readonly request: Request, readonly clientId: string, readonly resultingClientId: string) {
super('fetch');
}
respondWith(r: Response|Promise<Response>): void {
this.response = Promise.resolve(r);
}
}
export class MockInstallEvent extends MockExtendableEvent {
constructor() {
super('install');
}
}
export class MockExtendableMessageEvent extends MockExtendableEvent implements
ExtendableMessageEvent {
readonly lastEventId = '';
readonly origin = '';
readonly ports: ReadonlyArray<MessagePort> = [];
constructor(readonly data: any, readonly source: Client|MessagePort|ServiceWorker|null) {
super('message');
}
}
export class MockNotificationEvent extends MockExtendableEvent implements NotificationEvent {
readonly notification = {
...this._notification,
close: () => undefined,
} as Notification;
constructor(private _notification: Partial<Notification>, readonly action = '') {
super('notification');
}
}
export class MockPushEvent extends MockExtendableEvent implements PushEvent {
readonly data = {
json: () => this._data,
text: () => JSON.stringify(this._data),
} as PushMessageData;
constructor(private _data: object) {
super('push');
}
}