From 06aeca0a83b67a96f4f6c6b44c9c23c84d1876e3 Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Thu, 11 Feb 2021 15:02:49 -0700 Subject: [PATCH] feat(testing): add register test --- test/register.test.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 test/register.test.ts diff --git a/test/register.test.ts b/test/register.test.ts new file mode 100644 index 00000000..54c8d9e4 --- /dev/null +++ b/test/register.test.ts @@ -0,0 +1,43 @@ +import { JSDOM } from "jsdom" +import { registerServiceWorker } from "../src/browser/register" +const { window } = new JSDOM() +global.window = (window as unknown) as Window & typeof globalThis +global.document = window.document +global.navigator = window.navigator + +describe("register", () => { + const spy = jest.fn() + beforeAll(() => { + // register relies on navigator to be defined globally + // this is because the code is called on the browser + // so we're sure it will be defined + // We have to cast/assert so that TS thinks it's the correct type + Object.defineProperty(global.navigator, "serviceWorker", { + value: { + register: spy, + }, + }) + // global.navigator.serviceWorker.register = (spy as unknown) as ServiceWorkerContainer["register"] + }) + + afterEach(() => { + jest.resetAllMocks() + }) + + afterAll(() => { + jest.restoreAllMocks() + }) + it("should register a ServiceWorker", () => { + // call registerServiceWorker + const path = "/hello" + const mockOptions = { + base: "", + csStaticBase: "", + logLevel: 0, + } + registerServiceWorker(navigator, path, mockOptions) + // expect spy to have been called + expect(spy).toHaveBeenCalled() + expect(spy).toHaveBeenCalledTimes(1) + }) +})