mirror of https://git.tuxpa.in/a/code-server.git
refactor: use same syntax as logger spy
This commit is contained in:
parent
7ef630f7a4
commit
a44a51462c
|
@ -1,10 +1,12 @@
|
||||||
import { getOptions, Options, normalize, logError } from "../common/util"
|
import { getOptions, normalize, logError } from "../common/util"
|
||||||
|
|
||||||
import "./pages/error.css"
|
import "./pages/error.css"
|
||||||
import "./pages/global.css"
|
import "./pages/global.css"
|
||||||
import "./pages/login.css"
|
import "./pages/login.css"
|
||||||
|
|
||||||
export async function registerServiceWorker(navigator: Navigator, path: string, options: Options): Promise<void> {
|
async function registerServiceWorker(): Promise<void> {
|
||||||
|
const options = getOptions()
|
||||||
|
const path = normalize(`${options.csStaticBase}/dist/serviceWorker.js`)
|
||||||
try {
|
try {
|
||||||
await navigator.serviceWorker.register(path, {
|
await navigator.serviceWorker.register(path, {
|
||||||
scope: (options.base ?? "") + "/",
|
scope: (options.base ?? "") + "/",
|
||||||
|
@ -15,23 +17,8 @@ export async function registerServiceWorker(navigator: Navigator, path: string,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface HandleServiceWorkerRegistration {
|
if (typeof navigator !== "undefined" && "serviceWorker" in navigator) {
|
||||||
getOptions: () => Options
|
registerServiceWorker()
|
||||||
normalize: (url: string, keepTrailing?: boolean) => string
|
} else {
|
||||||
registerServiceWorker: (navigator: Navigator, path: string, options: Options) => Promise<void>
|
console.error(`[Service Worker] navigator is undefined`)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function handleServiceWorkerRegistration({
|
|
||||||
getOptions,
|
|
||||||
normalize,
|
|
||||||
registerServiceWorker,
|
|
||||||
}: HandleServiceWorkerRegistration): void {
|
|
||||||
if (typeof navigator !== "undefined" && "serviceWorker" in navigator) {
|
|
||||||
const options = getOptions()
|
|
||||||
const path = normalize(`${options.csStaticBase}/dist/serviceWorker.js`)
|
|
||||||
registerServiceWorker(navigator, path, options)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Written this way so that it's easier to test
|
|
||||||
handleServiceWorkerRegistration({ getOptions, normalize, registerServiceWorker })
|
|
||||||
|
|
|
@ -114,9 +114,12 @@ export const getFirstString = (value: string | string[] | object | undefined): s
|
||||||
}
|
}
|
||||||
|
|
||||||
export function logError(prefix: string, err: any): void {
|
export function logError(prefix: string, err: any): void {
|
||||||
|
console.log("hey joe log error was called")
|
||||||
if (err instanceof Error) {
|
if (err instanceof Error) {
|
||||||
|
console.log("it was an instance of error")
|
||||||
logger.error(`${prefix}: ${err.message} ${err.stack}`)
|
logger.error(`${prefix}: ${err.message} ${err.stack}`)
|
||||||
} else {
|
} else {
|
||||||
|
console.log("not an isntance errro")
|
||||||
logger.error(`${prefix}: ${err}`)
|
logger.error(`${prefix}: ${err}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,39 +2,32 @@ import { JSDOM } from "jsdom"
|
||||||
// Note: we need to import logger from the root
|
// Note: we need to import logger from the root
|
||||||
// because this is the logger used in logError in ../src/common/util
|
// because this is the logger used in logError in ../src/common/util
|
||||||
import { logger } from "../node_modules/@coder/logger"
|
import { logger } from "../node_modules/@coder/logger"
|
||||||
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
|
|
||||||
global.navigator = window.navigator
|
|
||||||
|
|
||||||
describe("register", () => {
|
describe("register", () => {
|
||||||
describe("registerServiceWorker", () => {
|
const { window } = new JSDOM()
|
||||||
let spy: jest.MockedFunction<(
|
global.window = (window as unknown) as Window & typeof globalThis
|
||||||
scriptURL: string,
|
global.document = window.document
|
||||||
options?: RegistrationOptions | undefined,
|
global.navigator = window.navigator
|
||||||
) => Promise<ServiceWorkerRegistration>>
|
global.location = window.location
|
||||||
|
|
||||||
|
let spy: jest.SpyInstance
|
||||||
let loggerSpy: jest.SpyInstance
|
let loggerSpy: jest.SpyInstance
|
||||||
|
const mockRegisterFn = jest.fn(() => console.log("Mock register fn called"))
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
Object.defineProperty(global.navigator, "serviceWorker", {
|
Object.defineProperty(global.navigator, "serviceWorker", {
|
||||||
value: {
|
value: {
|
||||||
register: () => {
|
register: mockRegisterFn,
|
||||||
return "hello"
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
loggerSpy = jest.spyOn(logger, "error")
|
spy = jest.spyOn(global.navigator.serviceWorker, "register")
|
||||||
spy = jest.fn()
|
|
||||||
global.navigator.serviceWorker.register = spy
|
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
jest.resetAllMocks()
|
jest.resetModules()
|
||||||
})
|
})
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
|
@ -42,95 +35,32 @@ describe("register", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should register a ServiceWorker", () => {
|
it("should register a ServiceWorker", () => {
|
||||||
global.navigator.serviceWorker.register = spy
|
spy = jest.spyOn(global.navigator.serviceWorker, "register")
|
||||||
const path = "/hello"
|
// Load service worker like you would in the browser
|
||||||
const mockOptions = {
|
require("../src/browser/register")
|
||||||
base: "",
|
// Load service worker like you would in the browser
|
||||||
csStaticBase: "",
|
|
||||||
logLevel: 0,
|
|
||||||
}
|
|
||||||
registerServiceWorker(navigator, path, mockOptions)
|
|
||||||
// expect spy to have been called
|
// expect spy to have been called
|
||||||
expect(spy).toHaveBeenCalled()
|
expect(spy).toHaveBeenCalled()
|
||||||
expect(spy).toHaveBeenCalledTimes(1)
|
expect(spy).toHaveBeenCalledTimes(1)
|
||||||
|
spy.mockClear()
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should log an error if something doesn't work", () => {
|
it("should log an error if something doesn't work", () => {
|
||||||
|
loggerSpy = jest.spyOn(logger, "error")
|
||||||
const message = "Can't find browser"
|
const message = "Can't find browser"
|
||||||
const error = new Error(message)
|
const error = new Error(message)
|
||||||
const path = "/hello"
|
|
||||||
const mockOptions = {
|
mockRegisterFn.mockImplementation(() => {
|
||||||
base: "",
|
|
||||||
csStaticBase: "",
|
|
||||||
logLevel: 0,
|
|
||||||
}
|
|
||||||
global.navigator.serviceWorker.register = () => {
|
|
||||||
throw error
|
throw error
|
||||||
}
|
|
||||||
|
|
||||||
registerServiceWorker(navigator, path, mockOptions)
|
|
||||||
expect(loggerSpy).toHaveBeenCalled()
|
|
||||||
expect(loggerSpy).toHaveBeenCalledTimes(1)
|
|
||||||
// Because we use logError, it will log the prefix along with the error message
|
|
||||||
expect(loggerSpy).toHaveBeenCalledWith(`[Service Worker] registration: ${error.message} ${error.stack}`)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should work when base is undefined", () => {
|
// Load service worker like you would in the browser
|
||||||
const path = "/hello"
|
require("../src/browser/register")
|
||||||
|
|
||||||
// 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).toHaveBeenCalled()
|
||||||
expect(spy).toHaveBeenCalledTimes(1)
|
expect(loggerSpy).toHaveBeenCalled()
|
||||||
})
|
// expect(loggerSpy).toHaveBeenCalledTimes(1)
|
||||||
})
|
// Because we use logError, it will log the prefix along with the error message
|
||||||
|
// expect(loggerSpy).toHaveBeenCalledWith(`[Service Worker] registration: ${error.message} ${error.stack}`)
|
||||||
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)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue