feat(testing): add register test

This commit is contained in:
Joe Previte 2021-02-11 15:02:49 -07:00
parent 72b05cacac
commit 06aeca0a83
No known key found for this signature in database
GPG Key ID: 2C91590C6B742C24

43
test/register.test.ts Normal file
View File

@ -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)
})
})