go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/testing_tools/fakes/fake_service_worker.ts (about)

     1  // Copyright 2022 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  /* eslint-disable @typescript-eslint/no-explicit-any */
    16  
    17  /**
    18   * A fake service worker with none of its method implemented.
    19   * The method should be stubbed according to the test requirement.
    20   * We cannot stub `ServiceWorker` because it doesn't have a legal constructor.
    21   */
    22  export class FakeServiceWorker implements ServiceWorker {
    23    constructor(
    24      public state: ServiceWorkerState,
    25      public scriptURL: string,
    26    ) {}
    27  
    28    onstatechange: ((this: ServiceWorker, ev: Event) => any) | null = null;
    29  
    30    postMessage(message: any, transfer: Transferable[]): void;
    31    postMessage(
    32      message: any,
    33      options?: StructuredSerializeOptions | undefined,
    34    ): void;
    35    postMessage(_message: unknown, _options?: unknown): void {
    36      throw new Error('Method not implemented.');
    37    }
    38  
    39    addEventListener<K extends keyof ServiceWorkerEventMap>(
    40      type: K,
    41      listener: (this: ServiceWorker, ev: ServiceWorkerEventMap[K]) => any,
    42      options?: boolean | AddEventListenerOptions | undefined,
    43    ): void;
    44    addEventListener(
    45      type: string,
    46      listener: EventListenerOrEventListenerObject,
    47      options?: boolean | AddEventListenerOptions | undefined,
    48    ): void;
    49    addEventListener(
    50      _type: string,
    51      _listener: unknown,
    52      _options?: unknown,
    53    ): void {
    54      throw new Error('Method not implemented.');
    55    }
    56  
    57    removeEventListener<K extends keyof ServiceWorkerEventMap>(
    58      type: K,
    59      listener: (this: ServiceWorker, ev: ServiceWorkerEventMap[K]) => any,
    60      options?: boolean | EventListenerOptions | undefined,
    61    ): void;
    62    removeEventListener(
    63      type: string,
    64      listener: EventListenerOrEventListenerObject,
    65      options?: boolean | EventListenerOptions | undefined,
    66    ): void;
    67    removeEventListener(
    68      _type: string,
    69      _listener: unknown,
    70      _options?: unknown,
    71    ): void {
    72      throw new Error('Method not implemented.');
    73    }
    74  
    75    dispatchEvent(event: Event): boolean;
    76    dispatchEvent(_event: unknown): boolean {
    77      throw new Error('Method not implemented.');
    78    }
    79  
    80    onerror: ((this: AbstractWorker, ev: ErrorEvent) => any) | null = null;
    81  }