@@ -14,56 +14,54 @@ import {
14
14
15
15
describe ( "vscode" , ( ) => {
16
16
describe ( "getNlsConfiguration" , ( ) => {
17
- beforeEach ( ( ) => {
18
- const { window } = new JSDOM ( )
19
- global . document = window . document
20
- } )
17
+ let _document : Document
21
18
22
- afterEach ( ( ) => {
23
- global . window = undefined as unknown as Window & typeof globalThis
24
- global . document = undefined as unknown as Document & typeof globalThis
19
+ beforeEach ( ( ) => {
20
+ // We use underscores to not confuse with global values
21
+ const { window : _window } = new JSDOM ( )
22
+ _document = _window . document
25
23
} )
26
24
27
25
it ( "should throw an error if no nlsConfigElement" , ( ) => {
28
26
const errorMsgPrefix = "[vscode]"
29
27
const errorMessage = `${ errorMsgPrefix } Could not parse NLS configuration. Could not find nlsConfigElement with id: ${ nlsConfigElementId } `
30
28
31
29
expect ( ( ) => {
32
- getNlsConfiguration ( document , "" )
30
+ getNlsConfiguration ( _document , "" )
33
31
} ) . toThrowError ( errorMessage )
34
32
} )
35
33
it ( "should throw an error if no nlsConfig" , ( ) => {
36
- const mockElement = document . createElement ( "div" )
34
+ const mockElement = _document . createElement ( "div" )
37
35
mockElement . setAttribute ( "id" , nlsConfigElementId )
38
- document . body . appendChild ( mockElement )
36
+ _document . body . appendChild ( mockElement )
39
37
40
38
const errorMsgPrefix = "[vscode]"
41
39
const errorMessage = `${ errorMsgPrefix } Could not parse NLS configuration. Found nlsConfigElement but missing data-settings attribute.`
42
40
43
41
expect ( ( ) => {
44
- getNlsConfiguration ( document , "" )
42
+ getNlsConfiguration ( _document , "" )
45
43
} ) . toThrowError ( errorMessage )
46
44
47
- document . body . removeChild ( mockElement )
45
+ _document . body . removeChild ( mockElement )
48
46
} )
49
47
it ( "should return the correct configuration" , ( ) => {
50
- const mockElement = document . createElement ( "div" )
48
+ const mockElement = _document . createElement ( "div" )
51
49
const dataSettings = {
52
50
first : "Jane" ,
53
51
last : "Doe" ,
54
52
}
55
53
56
54
mockElement . setAttribute ( "id" , nlsConfigElementId )
57
55
mockElement . setAttribute ( "data-settings" , JSON . stringify ( dataSettings ) )
58
- document . body . appendChild ( mockElement )
59
- const actual = getNlsConfiguration ( global . document , "" )
56
+ _document . body . appendChild ( mockElement )
57
+ const actual = getNlsConfiguration ( _document , "" )
60
58
61
59
expect ( actual ) . toStrictEqual ( dataSettings )
62
60
63
- document . body . removeChild ( mockElement )
61
+ _document . body . removeChild ( mockElement )
64
62
} )
65
63
it ( "should return have loadBundle property if _resolvedLangaugePackCoreLocation" , ( ) => {
66
- const mockElement = document . createElement ( "div" )
64
+ const mockElement = _document . createElement ( "div" )
67
65
const dataSettings = {
68
66
locale : "en" ,
69
67
availableLanguages : [ "en" , "de" ] ,
@@ -72,13 +70,13 @@ describe("vscode", () => {
72
70
73
71
mockElement . setAttribute ( "id" , nlsConfigElementId )
74
72
mockElement . setAttribute ( "data-settings" , JSON . stringify ( dataSettings ) )
75
- document . body . appendChild ( mockElement )
76
- const nlsConfig = getNlsConfiguration ( global . document , "" )
73
+ _document . body . appendChild ( mockElement )
74
+ const nlsConfig = getNlsConfiguration ( _document , "" )
77
75
78
76
expect ( nlsConfig . _resolvedLanguagePackCoreLocation ) . not . toBe ( undefined )
79
77
expect ( nlsConfig . loadBundle ) . not . toBe ( undefined )
80
78
81
- document . body . removeChild ( mockElement )
79
+ _document . body . removeChild ( mockElement )
82
80
} )
83
81
} )
84
82
describe ( "createBundlePath" , ( ) => {
@@ -91,45 +89,48 @@ describe("vscode", () => {
91
89
} )
92
90
} )
93
91
describe ( "setBodyBackgroundToThemeBackgroundColor" , ( ) => {
92
+ let _document : Document
93
+ let _localStorage : Storage
94
+
94
95
beforeEach ( ( ) => {
95
96
// We need to set the url in the JSDOM constructor
96
97
// to prevent this error "SecurityError: localStorage is not available for opaque origins"
97
98
// See: https://github.com/jsdom/jsdom/issues/2304#issuecomment-622314949
98
- const { window } = new JSDOM ( "" , { url : "http://localhost" } )
99
- global . document = window . document
100
- global . localStorage = window . localStorage
99
+ const { window : _window } = new JSDOM ( "" , { url : "http://localhost" } )
100
+ _document = _window . document
101
+ _localStorage = _window . localStorage
101
102
} )
102
103
it ( "should return null" , ( ) => {
103
104
const test = {
104
105
colorMap : {
105
106
[ `editor.background` ] : "#ff3270" ,
106
107
} ,
107
108
}
108
- localStorage . setItem ( "colorThemeData" , JSON . stringify ( test ) )
109
+ _localStorage . setItem ( "colorThemeData" , JSON . stringify ( test ) )
109
110
110
- expect ( setBodyBackgroundToThemeBackgroundColor ( document , localStorage ) ) . toBeNull ( )
111
+ expect ( setBodyBackgroundToThemeBackgroundColor ( _document , _localStorage ) ) . toBeNull ( )
111
112
112
- localStorage . removeItem ( "colorThemeData" )
113
+ _localStorage . removeItem ( "colorThemeData" )
113
114
} )
114
115
it ( "should throw an error if it can't find colorThemeData in localStorage" , ( ) => {
115
116
const errorMsgPrefix = "[vscode]"
116
117
const errorMessage = `${ errorMsgPrefix } Could not set body background to theme background color. Could not find colorThemeData in localStorage.`
117
118
118
119
expect ( ( ) => {
119
- setBodyBackgroundToThemeBackgroundColor ( document , localStorage )
120
+ setBodyBackgroundToThemeBackgroundColor ( _document , _localStorage )
120
121
} ) . toThrowError ( errorMessage )
121
122
} )
122
123
it ( "should throw an error if there is an error parsing colorThemeData from localStorage" , ( ) => {
123
124
const errorMsgPrefix = "[vscode]"
124
125
const errorMessage = `${ errorMsgPrefix } Could not set body background to theme background color. Could not parse colorThemeData from localStorage.`
125
126
126
- localStorage . setItem (
127
+ _localStorage . setItem (
127
128
"colorThemeData" ,
128
129
'{"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}' ,
129
130
)
130
131
131
132
expect ( ( ) => {
132
- setBodyBackgroundToThemeBackgroundColor ( document , localStorage )
133
+ setBodyBackgroundToThemeBackgroundColor ( _document , _localStorage )
133
134
} ) . toThrowError ( errorMessage )
134
135
135
136
localStorage . removeItem ( "colorThemeData" )
@@ -141,13 +142,13 @@ describe("vscode", () => {
141
142
const test = {
142
143
id : "hey-joe" ,
143
144
}
144
- localStorage . setItem ( "colorThemeData" , JSON . stringify ( test ) )
145
+ _localStorage . setItem ( "colorThemeData" , JSON . stringify ( test ) )
145
146
146
147
expect ( ( ) => {
147
- setBodyBackgroundToThemeBackgroundColor ( document , localStorage )
148
+ setBodyBackgroundToThemeBackgroundColor ( _document , _localStorage )
148
149
} ) . toThrowError ( errorMessage )
149
150
150
- localStorage . removeItem ( "colorThemeData" )
151
+ _localStorage . removeItem ( "colorThemeData" )
151
152
} )
152
153
it ( "should throw an error if there is no editor.background color" , ( ) => {
153
154
const errorMsgPrefix = "[vscode]"
@@ -159,44 +160,44 @@ describe("vscode", () => {
159
160
editor : "#fff" ,
160
161
} ,
161
162
}
162
- localStorage . setItem ( "colorThemeData" , JSON . stringify ( test ) )
163
+ _localStorage . setItem ( "colorThemeData" , JSON . stringify ( test ) )
163
164
164
165
expect ( ( ) => {
165
- setBodyBackgroundToThemeBackgroundColor ( document , localStorage )
166
+ setBodyBackgroundToThemeBackgroundColor ( _document , _localStorage )
166
167
} ) . toThrowError ( errorMessage )
167
168
168
- localStorage . removeItem ( "colorThemeData" )
169
+ _localStorage . removeItem ( "colorThemeData" )
169
170
} )
170
171
it ( "should set the body background to the editor background color" , ( ) => {
171
172
const test = {
172
173
colorMap : {
173
174
[ `editor.background` ] : "#ff3270" ,
174
175
} ,
175
176
}
176
- localStorage . setItem ( "colorThemeData" , JSON . stringify ( test ) )
177
+ _localStorage . setItem ( "colorThemeData" , JSON . stringify ( test ) )
177
178
178
- setBodyBackgroundToThemeBackgroundColor ( document , localStorage )
179
+ setBodyBackgroundToThemeBackgroundColor ( _document , _localStorage )
179
180
180
181
// When the body.style.backgroundColor is set using hex
181
182
// it is converted to rgb
182
183
// which is why we use that in the assertion
183
- expect ( document . body . style . backgroundColor ) . toBe ( "rgb(255, 50, 112)" )
184
+ expect ( _document . body . style . backgroundColor ) . toBe ( "rgb(255, 50, 112)" )
184
185
185
- localStorage . removeItem ( "colorThemeData" )
186
+ _localStorage . removeItem ( "colorThemeData" )
186
187
} )
187
188
} )
188
189
describe ( "getConfigurationForLoader" , ( ) => {
190
+ let _window : Window
191
+
189
192
beforeEach ( ( ) => {
190
- const { window } = new JSDOM ( )
191
- global . document = window . document
192
- } )
193
- afterEach ( ( ) => {
194
- global . window = undefined as unknown as Window & typeof globalThis
195
- global . document = undefined as unknown as Document & typeof globalThis
193
+ const { window : __window } = new JSDOM ( )
194
+ // @ts -expect-error the Window from JSDOM is not exactly the same as Window
195
+ // so we expect an error here
196
+ _window = __window
196
197
} )
197
198
it ( "should return a loader object (with undefined trustedTypesPolicy)" , ( ) => {
198
199
const options = {
199
- base : "/ " ,
200
+ base : ". " ,
200
201
csStaticBase : "/" ,
201
202
logLevel : 1 ,
202
203
}
@@ -208,12 +209,12 @@ describe("vscode", () => {
208
209
}
209
210
const loader = getConfigurationForLoader ( {
210
211
options,
212
+ _window,
211
213
nlsConfig : nlsConfig ,
212
- _window : global . window ,
213
214
} )
214
215
215
216
expect ( loader ) . toStrictEqual ( {
216
- baseUrl : "localhost//lib/vscode/out" ,
217
+ baseUrl : "http:// localhost//lib/vscode/out" ,
217
218
paths : {
218
219
"iconv-lite-umd" : "../node_modules/iconv-lite-umd/lib/iconv-lite-umd.js" ,
219
220
jschardet : "../node_modules/jschardet/dist/jschardet.min.js" ,
@@ -256,7 +257,7 @@ describe("vscode", () => {
256
257
const mockFn = jest . fn ( mockCreatePolicy )
257
258
258
259
// @ts -expect-error we are adding a custom property to window
259
- global . window . trustedTypes = {
260
+ _window . trustedTypes = {
260
261
createPolicy : mockFn ,
261
262
}
262
263
@@ -273,8 +274,8 @@ describe("vscode", () => {
273
274
}
274
275
const loader = getConfigurationForLoader ( {
275
276
options,
277
+ _window,
276
278
nlsConfig : nlsConfig ,
277
- _window : global . window ,
278
279
} )
279
280
280
281
expect ( loader . trustedTypesPolicy ) . not . toBe ( undefined )
@@ -294,59 +295,66 @@ describe("vscode", () => {
294
295
} )
295
296
} )
296
297
describe ( "main" , ( ) => {
298
+ let _window : Window
299
+ let _document : Document
300
+ let _localStorage : Storage
301
+
297
302
beforeEach ( ( ) => {
298
303
// We need to set the url in the JSDOM constructor
299
304
// to prevent this error "SecurityError: localStorage is not available for opaque origins"
300
305
// See: https://github.com/jsdom/jsdom/issues/2304#issuecomment-62231494
301
- const { window } = new JSDOM ( "" , { url : "http://localhost" } )
302
- global . document = window . document
303
- global . localStorage = window . localStorage
304
-
305
- const mockElement = document . createElement ( "div" )
306
+ const { window : __window } = new JSDOM ( "" , { url : "http://localhost" } )
307
+ // @ts -expect-error the Window from JSDOM is not exactly the same as Window
308
+ // so we expect an error here
309
+ _window = __window
310
+ _document = __window . document
311
+ _localStorage = __window . localStorage
312
+
313
+ const mockElement = _document . createElement ( "div" )
306
314
const dataSettings = {
307
315
first : "Jane" ,
308
316
last : "Doe" ,
309
317
}
310
318
311
319
mockElement . setAttribute ( "id" , nlsConfigElementId )
312
320
mockElement . setAttribute ( "data-settings" , JSON . stringify ( dataSettings ) )
313
- document . body . appendChild ( mockElement )
321
+ _document . body . appendChild ( mockElement )
314
322
315
323
const test = {
316
324
colorMap : {
317
325
[ `editor.background` ] : "#ff3270" ,
318
326
} ,
319
327
}
320
- localStorage . setItem ( "colorThemeData" , JSON . stringify ( test ) )
328
+ _localStorage . setItem ( "colorThemeData" , JSON . stringify ( test ) )
321
329
} )
322
330
afterEach ( ( ) => {
323
- localStorage . removeItem ( "colorThemeData" )
331
+ _localStorage . removeItem ( "colorThemeData" )
324
332
} )
325
333
it ( "should throw if document is missing" , ( ) => {
326
334
expect ( ( ) => {
327
- main ( undefined , window , localStorage )
335
+ main ( undefined , _window , _localStorage )
328
336
} ) . toThrow ( "document is undefined." )
329
337
} )
330
338
it ( "should throw if window is missing" , ( ) => {
331
339
expect ( ( ) => {
332
- main ( document , undefined , localStorage )
340
+ main ( _document , undefined , _localStorage )
333
341
} ) . toThrow ( "window is undefined." )
334
342
} )
335
343
it ( "should throw if localStorage is missing" , ( ) => {
336
344
expect ( ( ) => {
337
- main ( document , window , undefined )
345
+ main ( _document , _window , undefined )
338
346
} ) . toThrow ( "localStorage is undefined." )
339
347
} )
340
348
it ( "should add loader to self.require" , ( ) => {
341
- main ( document , window , localStorage )
349
+ main ( _document , _window , _localStorage )
342
350
343
351
expect ( Object . prototype . hasOwnProperty . call ( self , "require" ) ) . toBe ( true )
344
352
} )
345
353
it ( "should not throw in browser context" , ( ) => {
346
354
// Assuming we call it in a normal browser context
347
355
// where everything is defined
348
356
expect ( ( ) => {
349
- main ( global . document , global . window , global . localStorage )
357
+ main ( _document , _window , _localStorage )
350
358
} ) . not . toThrow ( )
351
359
} )
352
360
} )
0 commit comments