From 44b9874fbe19caa9ef8e546dd6624c67794b1d8e Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Thu, 11 Feb 2021 15:23:07 -0700 Subject: [PATCH] feat: test failure to register service worker --- src/browser/register.ts | 7 ++-- test/register.test.ts | 88 +++++++++++++++++++++++++++-------------- 2 files changed, 61 insertions(+), 34 deletions(-) diff --git a/src/browser/register.ts b/src/browser/register.ts index 607c8e927..c773f776f 100644 --- a/src/browser/register.ts +++ b/src/browser/register.ts @@ -17,8 +17,7 @@ export async function registerServiceWorker(navigator: Navigator, path: string, } if (typeof navigator !== "undefined" && "serviceWorker" in navigator) { - const options = getOptions() - const path = normalize(`${options.csStaticBase}/dist/serviceWorker.js`) - registerServiceWorker(navigator, path, options) - } + const options = getOptions() + const path = normalize(`${options.csStaticBase}/dist/serviceWorker.js`) + registerServiceWorker(navigator, path, options) } diff --git a/test/register.test.ts b/test/register.test.ts index 54c8d9e46..8f84b7eb1 100644 --- a/test/register.test.ts +++ b/test/register.test.ts @@ -1,4 +1,7 @@ import { JSDOM } from "jsdom" +// Note: we need to import logger from the root +// because this is the logger used in logError in ../src/common/util +import { logger } from "../node_modules/@coder/logger" import { registerServiceWorker } from "../src/browser/register" const { window } = new JSDOM() global.window = (window as unknown) as Window & typeof globalThis @@ -6,38 +9,63 @@ 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, - }, + describe("registerServiceWorker", () => { + const spy = jest.fn() + let loggerSpy: jest.SpyInstance + + beforeAll(() => { + Object.defineProperty(global.navigator, "serviceWorker", { + value: { + register: () => { + return "hello" + }, + }, + }) }) - // global.navigator.serviceWorker.register = (spy as unknown) as ServiceWorkerContainer["register"] - }) - afterEach(() => { - jest.resetAllMocks() - }) + beforeEach(() => { + loggerSpy = jest.spyOn(logger, "error") + }) - 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) + afterEach(() => { + jest.resetAllMocks() + }) + + afterAll(() => { + jest.restoreAllMocks() + }) + + it("should register a ServiceWorker", () => { + global.navigator.serviceWorker.register = spy + // 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) + }) + + it("should log an error if something goes work", () => { + const message = "Can't find browser" + const path = "/hello" + const mockOptions = { + base: "", + csStaticBase: "", + logLevel: 0, + } + global.navigator.serviceWorker.register = () => { + throw new Error(message) + } + + registerServiceWorker(navigator, path, mockOptions) + expect(loggerSpy).toHaveBeenCalled() + expect(loggerSpy).toHaveBeenCalledTimes(1) + expect(loggerSpy).toHaveBeenCalledWith(`[Service Worker] failed to register: ${message}`) + }) }) })