|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | +import { JSDOM } from "jsdom" |
| 5 | +import { getNlsConfiguration, nlsConfigElementId } from "../../../src/browser/pages/vscode" |
| 6 | +import { createLoggerMock } from "../../utils/helpers" |
| 7 | + |
| 8 | +describe("vscode", () => { |
| 9 | + describe("getNlsConfiguration", () => { |
| 10 | + const loggerModule = createLoggerMock() |
| 11 | + beforeEach(() => { |
| 12 | + jest.clearAllMocks() |
| 13 | + jest.mock("@coder/logger", () => loggerModule) |
| 14 | + const { window } = new JSDOM() |
| 15 | + global.window = window as unknown as Window & typeof globalThis |
| 16 | + global.document = window.document |
| 17 | + global.navigator = window.navigator |
| 18 | + global.location = window.location |
| 19 | + }) |
| 20 | + |
| 21 | + afterAll(() => { |
| 22 | + jest.restoreAllMocks() |
| 23 | + }) |
| 24 | + |
| 25 | + it("should return undefined if Document is undefined", () => { |
| 26 | + const actual = getNlsConfiguration(undefined as any as Document) |
| 27 | + const expected = undefined |
| 28 | + expect(actual).toBe(expected) |
| 29 | + }) |
| 30 | + it("should return undefined if no nlsConfigElement", () => { |
| 31 | + const actual = getNlsConfiguration(global.document) |
| 32 | + const expected = undefined |
| 33 | + expect(actual).toBe(expected) |
| 34 | + }) |
| 35 | + it("should return undefined if no nlsConfig", () => { |
| 36 | + const mockElement = document.createElement("div") |
| 37 | + mockElement.setAttribute("id", nlsConfigElementId) |
| 38 | + document.body.appendChild(mockElement) |
| 39 | + |
| 40 | + const actual = getNlsConfiguration(document) |
| 41 | + const expected = undefined |
| 42 | + expect(actual).toBe(expected) |
| 43 | + |
| 44 | + document.body.removeChild(mockElement) |
| 45 | + }) |
| 46 | + it("should log an error if it can't parse the nlsConfig", () => { |
| 47 | + const consoleError = console.error |
| 48 | + let errorMessage = "" |
| 49 | + |
| 50 | + console.error = (x) => { |
| 51 | + errorMessage = x |
| 52 | + } |
| 53 | + |
| 54 | + const mockElement = document.createElement("div") |
| 55 | + const dataSettings = { |
| 56 | + first: "Jane", |
| 57 | + last: "Doe", |
| 58 | + } |
| 59 | + |
| 60 | + mockElement.setAttribute("id", nlsConfigElementId) |
| 61 | + mockElement.setAttribute("data-settings", JSON.stringify(dataSettings) + ",") |
| 62 | + document.body.appendChild(mockElement) |
| 63 | + |
| 64 | + getNlsConfiguration(global.document) |
| 65 | + |
| 66 | + expect(errorMessage).toBe("[vscode] Failed to read or parse the NLS configuration.") |
| 67 | + |
| 68 | + console.error = consoleError |
| 69 | + document.body.removeChild(mockElement) |
| 70 | + }) |
| 71 | + it("should return the correct configuration", () => { |
| 72 | + const mockElement = document.createElement("div") |
| 73 | + const dataSettings = { |
| 74 | + first: "Jane", |
| 75 | + last: "Doe", |
| 76 | + } |
| 77 | + |
| 78 | + mockElement.setAttribute("id", nlsConfigElementId) |
| 79 | + mockElement.setAttribute("data-settings", JSON.stringify(dataSettings)) |
| 80 | + document.body.appendChild(mockElement) |
| 81 | + // TODO add element to the Dom |
| 82 | + // TODO test when DOM is there b |
| 83 | + const actual = getNlsConfiguration(global.document) |
| 84 | + expect(actual).toStrictEqual(dataSettings) |
| 85 | + document.body.removeChild(mockElement) |
| 86 | + }) |
| 87 | + }) |
| 88 | +}) |
0 commit comments