2021-02-25 01:50:08 +08:00
|
|
|
interface MockEvent {
|
|
|
|
claim: jest.Mock<any, any>
|
|
|
|
waitUntil?: jest.Mock<any, any>
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Listener {
|
|
|
|
event: string
|
|
|
|
cb: (event?: MockEvent) => void
|
|
|
|
}
|
2021-02-19 05:35:41 +08:00
|
|
|
|
2021-02-11 06:37:43 +08:00
|
|
|
describe("serviceWorker", () => {
|
2021-02-25 01:50:08 +08:00
|
|
|
let listeners: Listener[] = []
|
2021-02-19 05:35:41 +08:00
|
|
|
let spy: jest.SpyInstance
|
2021-02-25 01:50:08 +08:00
|
|
|
let claimSpy: jest.Mock<any, any>
|
|
|
|
let waitUntilSpy: jest.Mock<any, any>
|
|
|
|
|
|
|
|
function emit(event: string) {
|
|
|
|
listeners
|
|
|
|
.filter((listener) => listener.event === event)
|
|
|
|
.forEach((listener) => {
|
|
|
|
switch (event) {
|
|
|
|
case "activate":
|
|
|
|
listener.cb({
|
|
|
|
claim: jest.fn(),
|
|
|
|
waitUntil: jest.fn(() => waitUntilSpy()),
|
|
|
|
})
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
listener.cb()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-02-19 05:35:41 +08:00
|
|
|
|
2021-02-25 01:50:08 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
claimSpy = jest.fn()
|
2021-02-19 05:35:41 +08:00
|
|
|
spy = jest.spyOn(console, "log")
|
2021-02-25 01:50:08 +08:00
|
|
|
waitUntilSpy = jest.fn()
|
|
|
|
|
|
|
|
Object.assign(global, {
|
|
|
|
self: global,
|
|
|
|
addEventListener: (event: string, cb: () => void) => {
|
|
|
|
listeners.push({ event, cb })
|
|
|
|
},
|
|
|
|
clients: {
|
|
|
|
claim: claimSpy.mockResolvedValue("claimed"),
|
|
|
|
},
|
|
|
|
})
|
2021-02-19 05:35:41 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.restoreAllMocks()
|
2021-02-25 01:50:08 +08:00
|
|
|
jest.resetModules()
|
|
|
|
spy.mockClear()
|
|
|
|
claimSpy.mockClear()
|
|
|
|
|
|
|
|
// Clear all the listeners
|
|
|
|
listeners = []
|
2021-02-19 05:35:41 +08:00
|
|
|
})
|
|
|
|
|
2021-02-25 01:50:08 +08:00
|
|
|
it("should add 3 listeners: install, activate and fetch", () => {
|
2021-07-13 07:54:19 +08:00
|
|
|
require("../../../src/browser/serviceWorker.ts")
|
2021-02-25 01:50:08 +08:00
|
|
|
const listenerEventNames = listeners.map((listener) => listener.event)
|
|
|
|
|
|
|
|
expect(listeners).toHaveLength(3)
|
|
|
|
expect(listenerEventNames).toContain("install")
|
|
|
|
expect(listenerEventNames).toContain("activate")
|
|
|
|
expect(listenerEventNames).toContain("fetch")
|
2021-02-19 05:35:41 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should call the proper callbacks for 'install'", async () => {
|
2021-07-13 07:54:19 +08:00
|
|
|
require("../../../src/browser/serviceWorker.ts")
|
2021-02-25 01:50:08 +08:00
|
|
|
emit("install")
|
2021-02-19 05:35:41 +08:00
|
|
|
expect(spy).toHaveBeenCalledWith("[Service Worker] installed")
|
2021-02-25 01:50:08 +08:00
|
|
|
expect(spy).toHaveBeenCalledTimes(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should do nothing when 'fetch' is called", async () => {
|
2021-07-13 07:54:19 +08:00
|
|
|
require("../../../src/browser/serviceWorker.ts")
|
2021-02-25 01:50:08 +08:00
|
|
|
emit("fetch")
|
|
|
|
expect(spy).not.toHaveBeenCalled()
|
2021-02-11 06:37:43 +08:00
|
|
|
})
|
2021-02-25 01:50:08 +08:00
|
|
|
|
2021-02-19 05:35:41 +08:00
|
|
|
it("should call the proper callbacks for 'activate'", async () => {
|
2021-07-13 07:54:19 +08:00
|
|
|
require("../../../src/browser/serviceWorker.ts")
|
2021-02-25 01:50:08 +08:00
|
|
|
emit("activate")
|
2021-02-11 06:37:43 +08:00
|
|
|
|
2021-02-19 05:35:41 +08:00
|
|
|
// Activate serviceWorker
|
|
|
|
expect(spy).toHaveBeenCalledWith("[Service Worker] activated")
|
2021-02-25 01:50:08 +08:00
|
|
|
expect(waitUntilSpy).toHaveBeenCalled()
|
|
|
|
expect(claimSpy).toHaveBeenCalled()
|
2021-02-11 06:37:43 +08:00
|
|
|
})
|
2021-02-19 05:35:41 +08:00
|
|
|
})
|