2021-02-11 23:02:49 +01:00
|
|
|
import { JSDOM } from "jsdom"
|
2021-02-11 23:23:07 +01:00
|
|
|
// 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"
|
2021-02-11 23:02:49 +01:00
|
|
|
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", () => {
|
2021-02-11 23:23:07 +01:00
|
|
|
describe("registerServiceWorker", () => {
|
|
|
|
const spy = jest.fn()
|
|
|
|
let loggerSpy: jest.SpyInstance
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
Object.defineProperty(global.navigator, "serviceWorker", {
|
|
|
|
value: {
|
|
|
|
register: () => {
|
|
|
|
return "hello"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2021-02-11 23:02:49 +01:00
|
|
|
})
|
|
|
|
|
2021-02-11 23:23:07 +01:00
|
|
|
beforeEach(() => {
|
|
|
|
loggerSpy = jest.spyOn(logger, "error")
|
|
|
|
})
|
2021-02-11 23:02:49 +01:00
|
|
|
|
2021-02-11 23:23:07 +01:00
|
|
|
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}`)
|
|
|
|
})
|
2021-02-11 23:02:49 +01:00
|
|
|
})
|
|
|
|
})
|