code-server/test/serviceWorker.test.ts

45 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-02-18 22:35:41 +01:00
import makeServiceWorkerEnv = require("service-worker-mock")
const makeFetchMock = require("service-worker-mock/fetch")
describe("serviceWorker", () => {
2021-02-18 22:35:41 +01: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-18 22:35:41 +01:00
it("should call the proper callbacks for 'activate'", async () => {
require("../src/browser/serviceWorker.ts")
await self.trigger("activate")
2021-02-18 22:35:41 +01:00
// Activate serviceWorker
expect(spy).toHaveBeenCalledWith("[Service Worker] activated")
})
2021-02-18 22:35:41 +01:00
})