feat: test failure to register service worker
This commit is contained in:
parent
a9f88affa9
commit
44b9874fbe
@ -20,5 +20,4 @@ if (typeof navigator !== "undefined" && "serviceWorker" in navigator) {
|
|||||||
const options = getOptions()
|
const options = getOptions()
|
||||||
const path = normalize(`${options.csStaticBase}/dist/serviceWorker.js`)
|
const path = normalize(`${options.csStaticBase}/dist/serviceWorker.js`)
|
||||||
registerServiceWorker(navigator, path, options)
|
registerServiceWorker(navigator, path, options)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
import { JSDOM } from "jsdom"
|
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"
|
import { registerServiceWorker } from "../src/browser/register"
|
||||||
const { window } = new JSDOM()
|
const { window } = new JSDOM()
|
||||||
global.window = (window as unknown) as Window & typeof globalThis
|
global.window = (window as unknown) as Window & typeof globalThis
|
||||||
@ -6,18 +9,22 @@ global.document = window.document
|
|||||||
global.navigator = window.navigator
|
global.navigator = window.navigator
|
||||||
|
|
||||||
describe("register", () => {
|
describe("register", () => {
|
||||||
|
describe("registerServiceWorker", () => {
|
||||||
const spy = jest.fn()
|
const spy = jest.fn()
|
||||||
|
let loggerSpy: jest.SpyInstance
|
||||||
|
|
||||||
beforeAll(() => {
|
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", {
|
Object.defineProperty(global.navigator, "serviceWorker", {
|
||||||
value: {
|
value: {
|
||||||
register: spy,
|
register: () => {
|
||||||
|
return "hello"
|
||||||
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
// global.navigator.serviceWorker.register = (spy as unknown) as ServiceWorkerContainer["register"]
|
})
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
loggerSpy = jest.spyOn(logger, "error")
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
@ -27,7 +34,9 @@ describe("register", () => {
|
|||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
jest.restoreAllMocks()
|
jest.restoreAllMocks()
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should register a ServiceWorker", () => {
|
it("should register a ServiceWorker", () => {
|
||||||
|
global.navigator.serviceWorker.register = spy
|
||||||
// call registerServiceWorker
|
// call registerServiceWorker
|
||||||
const path = "/hello"
|
const path = "/hello"
|
||||||
const mockOptions = {
|
const mockOptions = {
|
||||||
@ -40,4 +49,23 @@ describe("register", () => {
|
|||||||
expect(spy).toHaveBeenCalled()
|
expect(spy).toHaveBeenCalled()
|
||||||
expect(spy).toHaveBeenCalledTimes(1)
|
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}`)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user