|
| 1 | +interface MockEvent { |
| 2 | + claim: jest.Mock<any, any> |
| 3 | + waitUntil?: jest.Mock<any, any> |
| 4 | +} |
| 5 | + |
| 6 | +interface Listener { |
| 7 | + event: string |
| 8 | + cb: (event?: MockEvent) => void |
| 9 | +} |
| 10 | + |
| 11 | +describe("serviceWorker", () => { |
| 12 | + let listeners: Listener[] = [] |
| 13 | + let spy: jest.SpyInstance |
| 14 | + let claimSpy: jest.Mock<any, any> |
| 15 | + let waitUntilSpy: jest.Mock<any, any> |
| 16 | + |
| 17 | + function emit(event: string) { |
| 18 | + listeners |
| 19 | + .filter((listener) => listener.event === event) |
| 20 | + .forEach((listener) => { |
| 21 | + switch (event) { |
| 22 | + case "activate": |
| 23 | + listener.cb({ |
| 24 | + claim: jest.fn(), |
| 25 | + waitUntil: jest.fn(() => waitUntilSpy()), |
| 26 | + }) |
| 27 | + break |
| 28 | + default: |
| 29 | + listener.cb() |
| 30 | + } |
| 31 | + }) |
| 32 | + } |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + claimSpy = jest.fn() |
| 36 | + spy = jest.spyOn(console, "log") |
| 37 | + waitUntilSpy = jest.fn() |
| 38 | + |
| 39 | + Object.assign(global, { |
| 40 | + self: global, |
| 41 | + addEventListener: (event: string, cb: () => void) => { |
| 42 | + listeners.push({ event, cb }) |
| 43 | + }, |
| 44 | + clients: { |
| 45 | + claim: claimSpy.mockResolvedValue("claimed"), |
| 46 | + }, |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + afterEach(() => { |
| 51 | + jest.restoreAllMocks() |
| 52 | + jest.resetModules() |
| 53 | + spy.mockClear() |
| 54 | + claimSpy.mockClear() |
| 55 | + |
| 56 | + // Clear all the listeners |
| 57 | + listeners = [] |
| 58 | + }) |
| 59 | + |
| 60 | + it("should add 3 listeners: install, activate and fetch", () => { |
| 61 | + require("../src/browser/serviceWorker.ts") |
| 62 | + const listenerEventNames = listeners.map((listener) => listener.event) |
| 63 | + |
| 64 | + expect(listeners).toHaveLength(3) |
| 65 | + expect(listenerEventNames).toContain("install") |
| 66 | + expect(listenerEventNames).toContain("activate") |
| 67 | + expect(listenerEventNames).toContain("fetch") |
| 68 | + }) |
| 69 | + |
| 70 | + it("should call the proper callbacks for 'install'", async () => { |
| 71 | + require("../src/browser/serviceWorker.ts") |
| 72 | + emit("install") |
| 73 | + expect(spy).toHaveBeenCalledWith("[Service Worker] installed") |
| 74 | + expect(spy).toHaveBeenCalledTimes(1) |
| 75 | + }) |
| 76 | + |
| 77 | + it("should do nothing when 'fetch' is called", async () => { |
| 78 | + require("../src/browser/serviceWorker.ts") |
| 79 | + emit("fetch") |
| 80 | + expect(spy).not.toHaveBeenCalled() |
| 81 | + }) |
| 82 | + |
| 83 | + it("should call the proper callbacks for 'activate'", async () => { |
| 84 | + require("../src/browser/serviceWorker.ts") |
| 85 | + emit("activate") |
| 86 | + |
| 87 | + // Activate serviceWorker |
| 88 | + expect(spy).toHaveBeenCalledWith("[Service Worker] activated") |
| 89 | + expect(waitUntilSpy).toHaveBeenCalled() |
| 90 | + expect(claimSpy).toHaveBeenCalled() |
| 91 | + }) |
| 92 | +}) |
0 commit comments