Archived
1
0

Replace console with logger in sw register

This commit is contained in:
Asher 2021-05-03 15:56:53 -05:00
parent a48ac5080b
commit 10babb4a0c
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
2 changed files with 11 additions and 10 deletions

View File

@ -14,7 +14,7 @@ export async function registerServiceWorker(): Promise<void> {
await navigator.serviceWorker.register(path, { await navigator.serviceWorker.register(path, {
scope: options.base + "/", scope: options.base + "/",
}) })
console.log("[Service Worker] registered") logger.info(`[Service Worker] registered`)
} catch (error) { } catch (error) {
logError(logger, `[Service Worker] registration`, error) logError(logger, `[Service Worker] registration`, error)
} }
@ -23,5 +23,5 @@ export async function registerServiceWorker(): Promise<void> {
if (typeof navigator !== "undefined" && "serviceWorker" in navigator) { if (typeof navigator !== "undefined" && "serviceWorker" in navigator) {
registerServiceWorker() registerServiceWorker()
} else { } else {
console.error(`[Service Worker] navigator is undefined`) logger.error(`[Service Worker] navigator is undefined`)
} }

View File

@ -22,11 +22,11 @@ describe("register", () => {
}) })
beforeEach(() => { beforeEach(() => {
jest.clearAllMocks()
jest.mock("@coder/logger", () => loggerModule) jest.mock("@coder/logger", () => loggerModule)
}) })
afterEach(() => { afterEach(() => {
mockRegisterFn.mockClear()
jest.resetModules() jest.resetModules()
}) })
@ -39,6 +39,7 @@ describe("register", () => {
global.navigator = (undefined as unknown) as Navigator & typeof globalThis global.navigator = (undefined as unknown) as Navigator & typeof globalThis
global.location = (undefined as unknown) as Location & typeof globalThis global.location = (undefined as unknown) as Location & typeof globalThis
}) })
it("test should have access to browser globals from beforeAll", () => { it("test should have access to browser globals from beforeAll", () => {
expect(typeof global.window).not.toBeFalsy() expect(typeof global.window).not.toBeFalsy()
expect(typeof global.document).not.toBeFalsy() expect(typeof global.document).not.toBeFalsy()
@ -74,24 +75,24 @@ describe("register", () => {
}) })
describe("when navigator and serviceWorker are NOT defined", () => { describe("when navigator and serviceWorker are NOT defined", () => {
let spy: jest.SpyInstance
beforeEach(() => { beforeEach(() => {
spy = jest.spyOn(console, "error") jest.clearAllMocks()
jest.mock("@coder/logger", () => loggerModule)
}) })
afterAll(() => { afterAll(() => {
jest.restoreAllMocks() jest.restoreAllMocks()
}) })
it("should log an error to the console", () => { it("should log an error", () => {
// Load service worker like you would in the browser // Load service worker like you would in the browser
require("../../src/browser/register") require("../../src/browser/register")
expect(spy).toHaveBeenCalled() expect(loggerModule.logger.error).toHaveBeenCalled()
expect(spy).toHaveBeenCalledTimes(1) expect(loggerModule.logger.error).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith("[Service Worker] navigator is undefined") expect(loggerModule.logger.error).toHaveBeenCalledWith("[Service Worker] navigator is undefined")
}) })
}) })
describe("registerServiceWorker", () => { describe("registerServiceWorker", () => {
let serviceWorkerPath: string let serviceWorkerPath: string
let serviceWorkerScope: string let serviceWorkerScope: string