|
1 | 1 | import { JSDOM } from "jsdom"
|
2 | 2 | import { loggerModule } from "../utils/helpers"
|
| 3 | +import { registerServiceWorker } from "../../src/browser/register" |
| 4 | +import { LocationLike } from "./util.test" |
3 | 5 |
|
4 | 6 | describe("register", () => {
|
5 | 7 | describe("when navigator and serviceWorker are defined", () => {
|
@@ -37,6 +39,12 @@ describe("register", () => {
|
37 | 39 | global.navigator = (undefined as unknown) as Navigator & typeof globalThis
|
38 | 40 | global.location = (undefined as unknown) as Location & typeof globalThis
|
39 | 41 | })
|
| 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 | + }) |
40 | 48 |
|
41 | 49 | it("should register a ServiceWorker", () => {
|
42 | 50 | // Load service worker like you would in the browser
|
@@ -84,4 +92,89 @@ describe("register", () => {
|
84 | 92 | expect(spy).toHaveBeenCalledWith("[Service Worker] navigator is undefined")
|
85 | 93 | })
|
86 | 94 | })
|
| 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 | + } |
| 109 | + const { window } = new JSDOM() |
| 110 | + global.window = (window as unknown) as Window & typeof globalThis |
| 111 | + global.document = window.document |
| 112 | + global.navigator = window.navigator |
| 113 | + global.location = location as Location |
| 114 | + |
| 115 | + Object.defineProperty(global.navigator, "serviceWorker", { |
| 116 | + value: { |
| 117 | + register: mockFn, |
| 118 | + }, |
| 119 | + }) |
| 120 | + }) |
| 121 | + |
| 122 | + afterEach(() => { |
| 123 | + mockFn.mockClear() |
| 124 | + jest.resetModules() |
| 125 | + }) |
| 126 | + |
| 127 | + afterAll(() => { |
| 128 | + jest.restoreAllMocks() |
| 129 | + |
| 130 | + // We don't want these to stay around because it can affect other tests |
| 131 | + global.window = (undefined as unknown) as Window & typeof globalThis |
| 132 | + global.document = (undefined as unknown) as Document & typeof globalThis |
| 133 | + global.navigator = (undefined as unknown) as Navigator & typeof globalThis |
| 134 | + global.location = (undefined as unknown) as Location & typeof globalThis |
| 135 | + }) |
| 136 | + it("should register when options.base is undefined", async () => { |
| 137 | + // Mock getElementById |
| 138 | + const csStaticBasePath = "/static/development/Users/jp/Dev/code-server" |
| 139 | + const spy = jest.spyOn(document, "getElementById") |
| 140 | + // Create a fake element and set the attribute |
| 141 | + const mockElement = document.createElement("div") |
| 142 | + mockElement.id = "coder-options" |
| 143 | + mockElement.setAttribute( |
| 144 | + "data-settings", |
| 145 | + `{"csStaticBase":"${csStaticBasePath}","logLevel":2,"disableTelemetry":false,"disableUpdateCheck":false}`, |
| 146 | + ) |
| 147 | + // Return mockElement from the spy |
| 148 | + // this way, when we call "getElementById" |
| 149 | + // it returns the element |
| 150 | + spy.mockImplementation(() => mockElement) |
| 151 | + |
| 152 | + await registerServiceWorker() |
| 153 | + |
| 154 | + expect(mockFn).toBeCalled() |
| 155 | + expect(serviceWorkerPath).toMatch(`${csStaticBasePath}/dist/serviceWorker.js`) |
| 156 | + expect(serviceWorkerScope).toMatch("/") |
| 157 | + }) |
| 158 | + it("should register when options.base is defined", async () => { |
| 159 | + const csStaticBasePath = "/static/development/Users/jp/Dev/code-server" |
| 160 | + const spy = jest.spyOn(document, "getElementById") |
| 161 | + // Create a fake element and set the attribute |
| 162 | + const mockElement = document.createElement("div") |
| 163 | + mockElement.id = "coder-options" |
| 164 | + mockElement.setAttribute( |
| 165 | + "data-settings", |
| 166 | + `{"base":"proxy/","csStaticBase":"${csStaticBasePath}","logLevel":2,"disableTelemetry":false,"disableUpdateCheck":false}`, |
| 167 | + ) |
| 168 | + // Return mockElement from the spy |
| 169 | + // this way, when we call "getElementById" |
| 170 | + // it returns the element |
| 171 | + spy.mockImplementation(() => mockElement) |
| 172 | + |
| 173 | + await registerServiceWorker() |
| 174 | + |
| 175 | + expect(mockFn).toBeCalled() |
| 176 | + expect(serviceWorkerPath).toMatch(`/dist/serviceWorker.js`) |
| 177 | + expect(serviceWorkerScope).toMatch("/") |
| 178 | + }) |
| 179 | + }) |
87 | 180 | })
|
0 commit comments