2021-02-18 21:35:41 +00:00
|
|
|
import makeServiceWorkerEnv = require("service-worker-mock")
|
|
|
|
const makeFetchMock = require("service-worker-mock/fetch")
|
|
|
|
|
2021-02-10 22:37:43 +00:00
|
|
|
describe("serviceWorker", () => {
|
2021-02-18 21:35:41 +00:00
|
|
|
let spy: jest.SpyInstance
|
|
|
|
beforeEach(() => {
|
|
|
|
Object.assign(
|
|
|
|
global,
|
|
|
|
makeServiceWorkerEnv(),
|
|
|
|
makeFetchMock(),
|
|
|
|
// If you're using sinon ur similar you'd probably use below instead of makeFetchMock
|
|
|
|
// fetch: sinon.stub().returns(Promise.resolve())
|
|
|
|
)
|
|
|
|
jest.resetModules()
|
|
|
|
|
|
|
|
spy = jest.spyOn(console, "log")
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.restoreAllMocks()
|
|
|
|
spy.mockRestore()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should add listeners", () => {
|
|
|
|
require("../src/browser/serviceWorker.ts")
|
|
|
|
const _self = (self as unknown) as WorkerGlobalScope
|
|
|
|
expect(_self.listeners.get("install")).toBeDefined()
|
|
|
|
expect(_self.listeners.get("activate")).toBeDefined()
|
|
|
|
expect(_self.listeners.get("fetch")).toBeDefined()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should call the proper callbacks for 'install'", async () => {
|
|
|
|
require("../src/browser/serviceWorker.ts")
|
|
|
|
await self.trigger("install")
|
|
|
|
expect(spy).toHaveBeenCalledWith("[Service Worker] installed")
|
2021-02-10 22:37:43 +00:00
|
|
|
})
|
2021-02-18 21:35:41 +00:00
|
|
|
it("should call the proper callbacks for 'activate'", async () => {
|
|
|
|
require("../src/browser/serviceWorker.ts")
|
|
|
|
await self.trigger("activate")
|
2021-02-10 22:37:43 +00:00
|
|
|
|
2021-02-18 21:35:41 +00:00
|
|
|
// Activate serviceWorker
|
|
|
|
expect(spy).toHaveBeenCalledWith("[Service Worker] activated")
|
2021-02-10 22:37:43 +00:00
|
|
|
})
|
2021-02-18 21:35:41 +00:00
|
|
|
})
|