|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | +import { JSDOM } from "jsdom" |
| 5 | +import { |
| 6 | + getNlsConfiguration, |
| 7 | + nlsConfigElementId, |
| 8 | + getLoader, |
| 9 | + setBodyBackgroundToThemeBackgroundColor, |
| 10 | +} from "../../../src/browser/pages/vscode" |
| 11 | + |
| 12 | +describe("vscode", () => { |
| 13 | + describe("getNlsConfiguration", () => { |
| 14 | + beforeEach(() => { |
| 15 | + const { window } = new JSDOM() |
| 16 | + global.document = window.document |
| 17 | + }) |
| 18 | + |
| 19 | + it("should throw an error if Document is undefined", () => { |
| 20 | + const errorMsgPrefix = "[vscode]" |
| 21 | + const errorMessage = `${errorMsgPrefix} Could not parse NLS configuration. document is undefined.` |
| 22 | + |
| 23 | + expect(() => { |
| 24 | + getNlsConfiguration(undefined as any as Document) |
| 25 | + }).toThrowError(errorMessage) |
| 26 | + }) |
| 27 | + it("should throw an error if no nlsConfigElement", () => { |
| 28 | + const errorMsgPrefix = "[vscode]" |
| 29 | + const errorMessage = `${errorMsgPrefix} Could not parse NLS configuration. Could not find nlsConfigElement with id: ${nlsConfigElementId}` |
| 30 | + |
| 31 | + expect(() => { |
| 32 | + getNlsConfiguration(document) |
| 33 | + }).toThrowError(errorMessage) |
| 34 | + }) |
| 35 | + it("should throw an error if no nlsConfig", () => { |
| 36 | + const mockElement = document.createElement("div") |
| 37 | + mockElement.setAttribute("id", nlsConfigElementId) |
| 38 | + document.body.appendChild(mockElement) |
| 39 | + |
| 40 | + const errorMsgPrefix = "[vscode]" |
| 41 | + const errorMessage = `${errorMsgPrefix} Could not parse NLS configuration. Found nlsConfigElement but missing data-settings attribute.` |
| 42 | + |
| 43 | + expect(() => { |
| 44 | + getNlsConfiguration(document) |
| 45 | + }).toThrowError(errorMessage) |
| 46 | + |
| 47 | + document.body.removeChild(mockElement) |
| 48 | + }) |
| 49 | + it("should return the correct configuration", () => { |
| 50 | + const mockElement = document.createElement("div") |
| 51 | + const dataSettings = { |
| 52 | + first: "Jane", |
| 53 | + last: "Doe", |
| 54 | + } |
| 55 | + |
| 56 | + mockElement.setAttribute("id", nlsConfigElementId) |
| 57 | + mockElement.setAttribute("data-settings", JSON.stringify(dataSettings)) |
| 58 | + document.body.appendChild(mockElement) |
| 59 | + const actual = getNlsConfiguration(global.document) |
| 60 | + |
| 61 | + expect(actual).toStrictEqual(dataSettings) |
| 62 | + |
| 63 | + document.body.removeChild(mockElement) |
| 64 | + }) |
| 65 | + }) |
| 66 | + describe("setBodyBackgroundToThemeBackgroundColor", () => { |
| 67 | + beforeEach(() => { |
| 68 | + // We need to set the url in the JSDOM constructor |
| 69 | + // to prevent this error "SecurityError: localStorage is not available for opaque origins" |
| 70 | + // See: https://github.com/jsdom/jsdom/issues/2304#issuecomment-622314949 |
| 71 | + const { window } = new JSDOM("", { url: "http://localhost" }) |
| 72 | + global.document = window.document |
| 73 | + global.localStorage = window.localStorage |
| 74 | + }) |
| 75 | + it("should return null", () => { |
| 76 | + const test = { |
| 77 | + colorMap: { |
| 78 | + [`editor.background`]: "#ff3270", |
| 79 | + }, |
| 80 | + } |
| 81 | + localStorage.setItem("colorThemeData", JSON.stringify(test)) |
| 82 | + |
| 83 | + expect(setBodyBackgroundToThemeBackgroundColor(document, localStorage)).toBeNull() |
| 84 | + |
| 85 | + localStorage.removeItem("colorThemeData") |
| 86 | + }) |
| 87 | + it("should throw an error if Document is undefined", () => { |
| 88 | + const errorMsgPrefix = "[vscode]" |
| 89 | + const errorMessage = `${errorMsgPrefix} Could not set body background to theme background color. Document is undefined.` |
| 90 | + |
| 91 | + expect(() => { |
| 92 | + // @ts-expect-error We need to test when document is undefined |
| 93 | + setBodyBackgroundToThemeBackgroundColor(undefined, localStorage) |
| 94 | + }).toThrowError(errorMessage) |
| 95 | + }) |
| 96 | + it("should throw an error if localStorage is undefined", () => { |
| 97 | + const errorMsgPrefix = "[vscode]" |
| 98 | + const errorMessage = `${errorMsgPrefix} Could not set body background to theme background color. localStorage is undefined.` |
| 99 | + |
| 100 | + expect(() => { |
| 101 | + // @ts-expect-error We need to test when localStorage is undefined |
| 102 | + setBodyBackgroundToThemeBackgroundColor(document, undefined) |
| 103 | + }).toThrowError(errorMessage) |
| 104 | + }) |
| 105 | + it("should throw an error if it can't find colorThemeData in localStorage", () => { |
| 106 | + const errorMsgPrefix = "[vscode]" |
| 107 | + const errorMessage = `${errorMsgPrefix} Could not set body background to theme background color. Could not find colorThemeData in localStorage.` |
| 108 | + |
| 109 | + expect(() => { |
| 110 | + setBodyBackgroundToThemeBackgroundColor(document, localStorage) |
| 111 | + }).toThrowError(errorMessage) |
| 112 | + }) |
| 113 | + it("should throw an error if there is an error parsing colorThemeData from localStorage", () => { |
| 114 | + const errorMsgPrefix = "[vscode]" |
| 115 | + const errorMessage = `${errorMsgPrefix} Could not set body background to theme background color. Could not parse colorThemeData from localStorage.` |
| 116 | + |
| 117 | + localStorage.setItem( |
| 118 | + "colorThemeData", |
| 119 | + '{"id":"vs-dark max-SS-Cyberpunk-themes-cyberpunk-umbra-color-theme-json","label":"Activate UMBRA protocol","settingsId":"Activate "errorForeground":"#ff3270","foreground":"#ffffff","sideBarTitle.foreground":"#bbbbbb"},"watch\\":::false}', |
| 120 | + ) |
| 121 | + |
| 122 | + expect(() => { |
| 123 | + setBodyBackgroundToThemeBackgroundColor(document, localStorage) |
| 124 | + }).toThrowError(errorMessage) |
| 125 | + |
| 126 | + localStorage.removeItem("colorThemeData") |
| 127 | + }) |
| 128 | + it("should throw an error if there is no colorMap property", () => { |
| 129 | + const errorMsgPrefix = "[vscode]" |
| 130 | + const errorMessage = `${errorMsgPrefix} Could not set body background to theme background color. colorThemeData is missing colorMap.` |
| 131 | + |
| 132 | + const test = { |
| 133 | + id: "hey-joe", |
| 134 | + } |
| 135 | + localStorage.setItem("colorThemeData", JSON.stringify(test)) |
| 136 | + |
| 137 | + expect(() => { |
| 138 | + setBodyBackgroundToThemeBackgroundColor(document, localStorage) |
| 139 | + }).toThrowError(errorMessage) |
| 140 | + |
| 141 | + localStorage.removeItem("colorThemeData") |
| 142 | + }) |
| 143 | + it("should throw an error if there is no editor.background color", () => { |
| 144 | + const errorMsgPrefix = "[vscode]" |
| 145 | + const errorMessage = `${errorMsgPrefix} Could not set body background to theme background color. colorThemeData.colorMap["editor.background"] is undefined.` |
| 146 | + |
| 147 | + const test = { |
| 148 | + id: "hey-joe", |
| 149 | + colorMap: { |
| 150 | + editor: "#fff", |
| 151 | + }, |
| 152 | + } |
| 153 | + localStorage.setItem("colorThemeData", JSON.stringify(test)) |
| 154 | + |
| 155 | + expect(() => { |
| 156 | + setBodyBackgroundToThemeBackgroundColor(document, localStorage) |
| 157 | + }).toThrowError(errorMessage) |
| 158 | + |
| 159 | + localStorage.removeItem("colorThemeData") |
| 160 | + }) |
| 161 | + it("should set the body background to the editor background color", () => { |
| 162 | + const test = { |
| 163 | + colorMap: { |
| 164 | + [`editor.background`]: "#ff3270", |
| 165 | + }, |
| 166 | + } |
| 167 | + localStorage.setItem("colorThemeData", JSON.stringify(test)) |
| 168 | + |
| 169 | + setBodyBackgroundToThemeBackgroundColor(document, localStorage) |
| 170 | + |
| 171 | + // When the body.style.backgroundColor is set using hex |
| 172 | + // it is converted to rgb |
| 173 | + // which is why we use that in the assertion |
| 174 | + expect(document.body.style.backgroundColor).toBe("rgb(255, 50, 112)") |
| 175 | + |
| 176 | + localStorage.removeItem("colorThemeData") |
| 177 | + }) |
| 178 | + }) |
| 179 | + describe("getLoader", () => { |
| 180 | + it("should throw an error if window is undefined in context", () => { |
| 181 | + const options = { |
| 182 | + base: "/", |
| 183 | + csStaticBase: "/hello", |
| 184 | + logLevel: 1, |
| 185 | + } |
| 186 | + const nlsConfig = { |
| 187 | + first: "Jane", |
| 188 | + last: "Doe", |
| 189 | + locale: "en", |
| 190 | + availableLanguages: {}, |
| 191 | + } |
| 192 | + const errorMsgPrefix = "[vscode]" |
| 193 | + const errorMessage = `${errorMsgPrefix} Could not get loader. window is undefined or missing.` |
| 194 | + /* eslint-disable no-global-assign */ |
| 195 | + window = undefined as unknown as Window & typeof globalThis |
| 196 | + expect(() => { |
| 197 | + if (typeof window === "undefined") { |
| 198 | + throw new Error(errorMessage) |
| 199 | + } |
| 200 | + |
| 201 | + getLoader({ |
| 202 | + origin: "localhost", |
| 203 | + nlsConfig: nlsConfig, |
| 204 | + options, |
| 205 | + }) |
| 206 | + }).toThrowError(errorMessage) |
| 207 | + }) |
| 208 | + it("should throw an error if options.csStaticBase is undefined or an empty string in context", () => { |
| 209 | + let options: |
| 210 | + | { |
| 211 | + base: string |
| 212 | + csStaticBase: string |
| 213 | + logLevel: number |
| 214 | + } |
| 215 | + | undefined = { |
| 216 | + base: "/", |
| 217 | + csStaticBase: "", |
| 218 | + logLevel: 1, |
| 219 | + } |
| 220 | + const nlsConfig = { |
| 221 | + first: "Jane", |
| 222 | + last: "Doe", |
| 223 | + locale: "en", |
| 224 | + availableLanguages: {}, |
| 225 | + } |
| 226 | + const errorMsgPrefix = "[vscode]" |
| 227 | + const errorMessage = `${errorMsgPrefix} Could not get loader. options or options.csStaticBase is undefined or missing.` |
| 228 | + expect(() => { |
| 229 | + if (!options?.csStaticBase) { |
| 230 | + throw new Error(errorMessage) |
| 231 | + } |
| 232 | + |
| 233 | + getLoader({ |
| 234 | + origin: "localhost", |
| 235 | + nlsConfig: nlsConfig, |
| 236 | + options, |
| 237 | + }) |
| 238 | + }).toThrowError(errorMessage) |
| 239 | + expect(() => { |
| 240 | + options = undefined |
| 241 | + if (!options) { |
| 242 | + throw new Error(errorMessage) |
| 243 | + } |
| 244 | + getLoader({ |
| 245 | + origin: "localhost", |
| 246 | + nlsConfig: nlsConfig, |
| 247 | + options, |
| 248 | + }) |
| 249 | + }).toThrowError(errorMessage) |
| 250 | + }) |
| 251 | + it("should throw an error if nlsConfig is undefined", () => { |
| 252 | + const options = { |
| 253 | + base: "/", |
| 254 | + csStaticBase: "/", |
| 255 | + logLevel: 1, |
| 256 | + } |
| 257 | + const errorMsgPrefix = "[vscode]" |
| 258 | + const errorMessage = `${errorMsgPrefix} Could not get loader. nlsConfig is undefined.` |
| 259 | + expect(() => { |
| 260 | + let nlsConfig = undefined |
| 261 | + |
| 262 | + if (!nlsConfig) { |
| 263 | + throw new Error(errorMessage) |
| 264 | + } |
| 265 | + |
| 266 | + nlsConfig = { |
| 267 | + locale: "en", |
| 268 | + availableLanguages: {}, |
| 269 | + } |
| 270 | + |
| 271 | + getLoader({ |
| 272 | + nlsConfig, |
| 273 | + options, |
| 274 | + origin: "localthost", |
| 275 | + }) |
| 276 | + }).toThrowError(errorMessage) |
| 277 | + }) |
| 278 | + it("should return a loader object", () => { |
| 279 | + const options = { |
| 280 | + base: "/", |
| 281 | + csStaticBase: "/", |
| 282 | + logLevel: 1, |
| 283 | + } |
| 284 | + const nlsConfig = { |
| 285 | + first: "Jane", |
| 286 | + last: "Doe", |
| 287 | + locale: "en", |
| 288 | + availableLanguages: {}, |
| 289 | + } |
| 290 | + const loader = getLoader({ |
| 291 | + origin: "localhost", |
| 292 | + nlsConfig: nlsConfig, |
| 293 | + options, |
| 294 | + }) |
| 295 | + |
| 296 | + expect(loader).toStrictEqual({ |
| 297 | + baseUrl: "localhost//lib/vscode/out", |
| 298 | + paths: { |
| 299 | + "iconv-lite-umd": "../node_modules/iconv-lite-umd/lib/iconv-lite-umd.js", |
| 300 | + jschardet: "../node_modules/jschardet/dist/jschardet.min.js", |
| 301 | + "tas-client-umd": "../node_modules/tas-client-umd/lib/tas-client-umd.js", |
| 302 | + "vscode-oniguruma": "../node_modules/vscode-oniguruma/release/main", |
| 303 | + "vscode-textmate": "../node_modules/vscode-textmate/release/main", |
| 304 | + xterm: "../node_modules/xterm/lib/xterm.js", |
| 305 | + "xterm-addon-search": "../node_modules/xterm-addon-search/lib/xterm-addon-search.js", |
| 306 | + "xterm-addon-unicode11": "../node_modules/xterm-addon-unicode11/lib/xterm-addon-unicode11.js", |
| 307 | + "xterm-addon-webgl": "../node_modules/xterm-addon-webgl/lib/xterm-addon-webgl.js", |
| 308 | + }, |
| 309 | + recordStats: true, |
| 310 | + "vs/nls": { |
| 311 | + availableLanguages: {}, |
| 312 | + first: "Jane", |
| 313 | + last: "Doe", |
| 314 | + locale: "en", |
| 315 | + }, |
| 316 | + }) |
| 317 | + }) |
| 318 | + }) |
| 319 | +}) |
0 commit comments