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