feat(test): add test handleRegisterServiceWorker

This commit is contained in:
Joe Previte 2021-02-11 16:51:48 -07:00
parent 21f577d471
commit 38891de0d9
No known key found for this signature in database
GPG Key ID: 2C91590C6B742C24
1 changed files with 67 additions and 4 deletions

View File

@ -2,7 +2,8 @@ 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, handleServiceWorkerRegistration } from "../src/browser/register"
import { Options } from "../src/common/util"
const { window } = new JSDOM()
global.window = (window as unknown) as Window & typeof globalThis
global.document = window.document
@ -10,7 +11,10 @@ global.navigator = window.navigator
describe("register", () => {
describe("registerServiceWorker", () => {
const spy = jest.fn()
let spy: jest.MockedFunction<(
scriptURL: string,
options?: RegistrationOptions | undefined,
) => Promise<ServiceWorkerRegistration>>
let loggerSpy: jest.SpyInstance
beforeAll(() => {
@ -25,6 +29,8 @@ describe("register", () => {
beforeEach(() => {
loggerSpy = jest.spyOn(logger, "error")
spy = jest.fn()
global.navigator.serviceWorker.register = spy
})
afterEach(() => {
@ -37,7 +43,6 @@ describe("register", () => {
it("should register a ServiceWorker", () => {
global.navigator.serviceWorker.register = spy
// call registerServiceWorker
const path = "/hello"
const mockOptions = {
base: "",
@ -50,7 +55,7 @@ describe("register", () => {
expect(spy).toHaveBeenCalledTimes(1)
})
it("should log an error if something goes work", () => {
it("should log an error if something doesn't work", () => {
const message = "Can't find browser"
const path = "/hello"
const mockOptions = {
@ -67,5 +72,63 @@ describe("register", () => {
expect(loggerSpy).toHaveBeenCalledTimes(1)
expect(loggerSpy).toHaveBeenCalledWith(`[Service Worker] failed to register: ${message}`)
})
it("should work when base is undefined", () => {
const path = "/hello"
// We want to test some code that checks if options.base is undefined
// so we leave it off mockOptions
// but assert it as Options so TS is happy
const mockOptions = {
csStaticBase: "",
logLevel: 0,
} as Options
registerServiceWorker(navigator, path, mockOptions)
// expect spy to have been called
expect(spy).toHaveBeenCalled()
expect(spy).toHaveBeenCalledTimes(1)
})
})
describe("handleServiceWorkerRegistration", () => {
let getOptionsMock: jest.MockedFunction<() => {
base: string
csStaticBase: string
logLevel: number
}>
let normalizeMock: jest.MockedFunction<(v: string) => string>
let registerServiceWorkerMock: jest.MockedFunction<(
navigator: Navigator,
path: string,
mockOptions: Options,
) => Promise<void>>
beforeEach(() => {
getOptionsMock = jest.fn(() => ({
base: "",
csStaticBase: "",
logLevel: 0,
}))
normalizeMock = jest.fn((url: string) => "qux///")
registerServiceWorkerMock = jest
.fn()
.mockImplementation((navigator: Navigator, path: string, mockOptions: Options) => Promise.resolve())
})
it("should work when called", () => {
handleServiceWorkerRegistration({
getOptions: getOptionsMock,
normalize: normalizeMock,
registerServiceWorker: registerServiceWorkerMock,
})
const mocks = [getOptionsMock, normalizeMock, registerServiceWorkerMock]
mocks.forEach((mock) => {
expect(mock).toHaveBeenCalled()
expect(mock).toHaveBeenCalledTimes(1)
})
})
})
})