feat(testing): add serviceWorker tests

This commit is contained in:
Joe Previte
2021-02-18 14:35:41 -07:00
parent ee0973c05a
commit 6b56e6572d
7 changed files with 108 additions and 6 deletions

View File

@@ -1,9 +1,44 @@
import makeServiceWorkerEnv = require("service-worker-mock")
const makeFetchMock = require("service-worker-mock/fetch")
describe("serviceWorker", () => {
it("should add the proper eventListeners", () => {
// make sure install, active and fetch were added as event listeners
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")
})
it("should call the proper callbacks", () => {
// somehow test Line 8 with the events waitUntil..
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")
})
it("should call the proper callbacks for 'activate'", async () => {
require("../src/browser/serviceWorker.ts")
await self.trigger("activate")
// Activate serviceWorker
expect(spy).toHaveBeenCalledWith("[Service Worker] activated")
})
})