Skip to content

Commit eaba90e

Browse files
committed
feat(testing): add serviceWorker tests
1 parent 76b53d3 commit eaba90e

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

test/serviceWorker.test.ts

+38-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,42 @@
1+
import makeServiceWorkerEnv = require("service-worker-mock")
2+
const makeFetchMock = require("service-worker-mock/fetch")
3+
14
describe("serviceWorker", () => {
2-
it("should add the proper eventListeners", () => {
3-
// make sure install, active and fetch were added as event listeners
5+
beforeEach(() => {
6+
Object.assign(
7+
global,
8+
makeServiceWorkerEnv(),
9+
makeFetchMock(),
10+
// If you're using sinon ur similar you'd probably use below instead of makeFetchMock
11+
// fetch: sinon.stub().returns(Promise.resolve())
12+
)
13+
jest.resetModules()
14+
})
15+
16+
afterEach(() => {
17+
jest.restoreAllMocks()
18+
})
19+
20+
it("should add listeners", () => {
21+
require("../src/browser/serviceWorker.ts")
22+
const _self = (self as unknown) as WorkerGlobalScope
23+
expect(_self.listeners.get("install")).toBeDefined()
24+
expect(_self.listeners.get("activate")).toBeDefined()
25+
expect(_self.listeners.get("fetch")).toBeDefined()
26+
})
27+
28+
it("should call the proper callbacks for 'install'", async () => {
29+
const consoleLogSpy = jest.spyOn(console, "log")
30+
require("../src/browser/serviceWorker.ts")
31+
await self.trigger("install")
32+
expect(consoleLogSpy).toHaveBeenCalledWith("[Service Worker] installed")
433
})
34+
it("should call the proper callbacks for 'activate'", async () => {
35+
const consoleLogSpy = jest.spyOn(console, "log")
36+
require("../src/browser/serviceWorker.ts")
37+
await self.trigger("activate")
538

6-
it("should call the proper callbacks", () => {
7-
// somehow test Line 8 with the events waitUntil..
39+
// Activate serviceWorker
40+
expect(consoleLogSpy).toHaveBeenCalledWith("[Service Worker] activated")
841
})
9-
})
42+
})

0 commit comments

Comments
 (0)