Skip to content

Commit 6f2709b

Browse files
committed
feat: add tests for registerServiceWorker
1 parent 5ad8e68 commit 6f2709b

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

test/unit/register.test.ts

+94
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { JSDOM } from "jsdom"
22
import { loggerModule } from "../utils/helpers"
3+
import { registerServiceWorker } from "../../src/browser/register"
4+
import { LocationLike } from "./util.test"
35

46
describe("register", () => {
57
describe("when navigator and serviceWorker are defined", () => {
@@ -37,6 +39,12 @@ describe("register", () => {
3739
global.navigator = (undefined as unknown) as Navigator & typeof globalThis
3840
global.location = (undefined as unknown) as Location & typeof globalThis
3941
})
42+
it("test should have access to browser globals from beforeAll", () => {
43+
expect(typeof global.window).not.toBeFalsy()
44+
expect(typeof global.document).not.toBeFalsy()
45+
expect(typeof global.navigator).not.toBeFalsy()
46+
expect(typeof global.location).not.toBeFalsy()
47+
})
4048

4149
it("should register a ServiceWorker", () => {
4250
// Load service worker like you would in the browser
@@ -84,4 +92,90 @@ describe("register", () => {
8492
expect(spy).toHaveBeenCalledWith("[Service Worker] navigator is undefined")
8593
})
8694
})
95+
describe("registerServiceWorker", () => {
96+
let serviceWorkerPath: string
97+
let serviceWorkerScope: string
98+
const mockFn = jest.fn((path: string, options: { scope: string }) => {
99+
serviceWorkerPath = path
100+
serviceWorkerScope = options.scope
101+
return undefined
102+
})
103+
104+
beforeAll(() => {
105+
const location: LocationLike = {
106+
pathname: "",
107+
origin: "http://localhost:8080",
108+
// search: "?environmentId=600e0187-0909d8a00cb0a394720d4dce",
109+
}
110+
const { window } = new JSDOM()
111+
global.window = (window as unknown) as Window & typeof globalThis
112+
global.document = window.document
113+
global.navigator = window.navigator
114+
global.location = location as Location
115+
116+
Object.defineProperty(global.navigator, "serviceWorker", {
117+
value: {
118+
register: mockFn,
119+
},
120+
})
121+
})
122+
123+
afterEach(() => {
124+
mockFn.mockClear()
125+
jest.resetModules()
126+
})
127+
128+
afterAll(() => {
129+
jest.restoreAllMocks()
130+
131+
// We don't want these to stay around because it can affect other tests
132+
global.window = (undefined as unknown) as Window & typeof globalThis
133+
global.document = (undefined as unknown) as Document & typeof globalThis
134+
global.navigator = (undefined as unknown) as Navigator & typeof globalThis
135+
global.location = (undefined as unknown) as Location & typeof globalThis
136+
})
137+
it("should register when options.base is undefined", async () => {
138+
// Mock getElementById
139+
const csStaticBasePath = "/static/development/Users/jp/Dev/code-server"
140+
const spy = jest.spyOn(document, "getElementById")
141+
// Create a fake element and set the attribute
142+
const mockElement = document.createElement("div")
143+
mockElement.id = "coder-options"
144+
mockElement.setAttribute(
145+
"data-settings",
146+
`{"csStaticBase":"${csStaticBasePath}","logLevel":2,"disableTelemetry":false,"disableUpdateCheck":false}`,
147+
)
148+
// Return mockElement from the spy
149+
// this way, when we call "getElementById"
150+
// it returns the element
151+
spy.mockImplementation(() => mockElement)
152+
153+
await registerServiceWorker()
154+
155+
expect(mockFn).toBeCalled()
156+
expect(serviceWorkerPath).toMatch(`${csStaticBasePath}/dist/serviceWorker.js`)
157+
expect(serviceWorkerScope).toMatch("/")
158+
})
159+
it("should register when options.base is defined", async () => {
160+
const csStaticBasePath = "/static/development/Users/jp/Dev/code-server"
161+
const spy = jest.spyOn(document, "getElementById")
162+
// Create a fake element and set the attribute
163+
const mockElement = document.createElement("div")
164+
mockElement.id = "coder-options"
165+
mockElement.setAttribute(
166+
"data-settings",
167+
`{"base":"proxy/","csStaticBase":"${csStaticBasePath}","logLevel":2,"disableTelemetry":false,"disableUpdateCheck":false}`,
168+
)
169+
// Return mockElement from the spy
170+
// this way, when we call "getElementById"
171+
// it returns the element
172+
spy.mockImplementation(() => mockElement)
173+
174+
await registerServiceWorker()
175+
176+
expect(mockFn).toBeCalled()
177+
expect(serviceWorkerPath).toMatch(`/dist/serviceWorker.js`)
178+
expect(serviceWorkerScope).toMatch("/")
179+
})
180+
})
87181
})

test/unit/util.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { loggerModule } from "../utils/helpers"
1616
const dom = new JSDOM()
1717
global.document = dom.window.document
1818

19-
type LocationLike = Pick<Location, "pathname" | "origin">
19+
export type LocationLike = Pick<Location, "pathname" | "origin">
2020

2121
// jest.mock is hoisted above the imports so we must use `require` here.
2222
jest.mock("@coder/logger", () => require("../utils/helpers").loggerModule)

0 commit comments

Comments
 (0)