mirror of
https://github.com/coder/code-server.git
synced 2024-12-04 23:03:06 +08:00
02b9489489
This PR refactors part of vscode.ts and adds a function to get the NLS Configuration. This makes the code more readable and easier to test. And it adds multiple tests for this part of the codebase.
62 lines
2.1 KiB
TypeScript
62 lines
2.1 KiB
TypeScript
/**
|
|
* @jest-environment jsdom
|
|
*/
|
|
import { JSDOM } from "jsdom"
|
|
import { getNlsConfiguration, nlsConfigElementId } from "../../../src/browser/pages/vscode"
|
|
|
|
describe("vscode", () => {
|
|
describe("getNlsConfiguration", () => {
|
|
beforeEach(() => {
|
|
const { window } = new JSDOM()
|
|
global.document = window.document
|
|
})
|
|
|
|
it("should throw an error if Document is undefined", () => {
|
|
const errorMsgPrefix = "[vscode]"
|
|
const errorMessage = `${errorMsgPrefix} Could not parse NLS configuration. document is undefined.`
|
|
|
|
expect(() => {
|
|
getNlsConfiguration(undefined as any as Document)
|
|
}).toThrowError(errorMessage)
|
|
})
|
|
it("should throw an error if no nlsConfigElement", () => {
|
|
const errorMsgPrefix = "[vscode]"
|
|
const errorMessage = `${errorMsgPrefix} Could not parse NLS configuration. Could not find nlsConfigElement with id: ${nlsConfigElementId}`
|
|
|
|
expect(() => {
|
|
getNlsConfiguration(document)
|
|
}).toThrowError(errorMessage)
|
|
})
|
|
it("should throw an error if no nlsConfig", () => {
|
|
const mockElement = document.createElement("div")
|
|
mockElement.setAttribute("id", nlsConfigElementId)
|
|
document.body.appendChild(mockElement)
|
|
|
|
const errorMsgPrefix = "[vscode]"
|
|
const errorMessage = `${errorMsgPrefix} Could not parse NLS configuration. Found nlsConfigElement but missing data-settings attribute.`
|
|
|
|
expect(() => {
|
|
getNlsConfiguration(document)
|
|
}).toThrowError(errorMessage)
|
|
|
|
document.body.removeChild(mockElement)
|
|
})
|
|
it("should return the correct configuration", () => {
|
|
const mockElement = document.createElement("div")
|
|
const dataSettings = {
|
|
first: "Jane",
|
|
last: "Doe",
|
|
}
|
|
|
|
mockElement.setAttribute("id", nlsConfigElementId)
|
|
mockElement.setAttribute("data-settings", JSON.stringify(dataSettings))
|
|
document.body.appendChild(mockElement)
|
|
const actual = getNlsConfiguration(global.document)
|
|
|
|
expect(actual).toStrictEqual(dataSettings)
|
|
|
|
document.body.removeChild(mockElement)
|
|
})
|
|
})
|
|
})
|