@@ -5,6 +5,7 @@ import { JSDOM } from "jsdom"
5
5
import {
6
6
getNlsConfiguration ,
7
7
nlsConfigElementId ,
8
+ registerRequireOnSelf ,
8
9
setBodyBackgroundToThemeBackgroundColor ,
9
10
} from "../../../src/browser/pages/vscode"
10
11
@@ -175,4 +176,179 @@ describe("vscode", () => {
175
176
localStorage . removeItem ( "colorThemeData" )
176
177
} )
177
178
} )
179
+ describe ( "registerRequireOnSelf" , ( ) => {
180
+ beforeAll ( ( ) => {
181
+ const { window } = new JSDOM ( )
182
+ // @ts -expect-error We know these are the exact same type
183
+ // but we need to do this for the test to work
184
+ global . self = window . self
185
+ global . window = window as unknown as Window & typeof globalThis
186
+ global . document = window . document
187
+ global . navigator = window . navigator
188
+ global . location = location as Location
189
+ } )
190
+
191
+ beforeEach ( ( ) => {
192
+ jest . clearAllMocks ( )
193
+ } )
194
+
195
+ afterEach ( ( ) => {
196
+ jest . resetModules ( )
197
+ } )
198
+
199
+ afterAll ( ( ) => {
200
+ jest . restoreAllMocks ( )
201
+
202
+ global . window = undefined as unknown as Window & typeof globalThis
203
+ global . document = undefined as unknown as Document & typeof globalThis
204
+ global . navigator = undefined as unknown as Navigator & typeof globalThis
205
+ global . location = undefined as unknown as Location & typeof globalThis
206
+ } )
207
+ it ( "should throw an error if self is undefined" , ( ) => {
208
+ const options = {
209
+ base : "/" ,
210
+ csStaticBase : "/hello" ,
211
+ logLevel : 1 ,
212
+ }
213
+ const nlsConfig = {
214
+ first : "Jane" ,
215
+ last : "Doe" ,
216
+ locale : "en" ,
217
+ availableLanguages : { } ,
218
+ }
219
+ const errorMsgPrefix = "[vscode]"
220
+ const errorMessage = `${ errorMsgPrefix } Could not register require on self. self is undefined.`
221
+ expect ( ( ) => {
222
+ registerRequireOnSelf ( {
223
+ // @ts -expect-error We are checking what happens if self is undefined.
224
+ self : undefined ,
225
+ window : global . window ,
226
+ nlsConfig : nlsConfig ,
227
+ options,
228
+ } )
229
+ } ) . toThrowError ( errorMessage )
230
+ } )
231
+ it ( "should throw an error if window is undefined" , ( ) => {
232
+ const options = {
233
+ base : "/" ,
234
+ csStaticBase : "/hello" ,
235
+ logLevel : 1 ,
236
+ }
237
+ const nlsConfig = {
238
+ first : "Jane" ,
239
+ last : "Doe" ,
240
+ locale : "en" ,
241
+ availableLanguages : { } ,
242
+ }
243
+ const errorMsgPrefix = "[vscode]"
244
+ const errorMessage = `${ errorMsgPrefix } Could not register require on self. window is undefined.`
245
+ const mockSelf = { } as Window & typeof globalThis
246
+ expect ( ( ) => {
247
+ registerRequireOnSelf ( {
248
+ self : mockSelf ,
249
+ // @ts -expect-error We need to test if window is undefined
250
+ window : undefined ,
251
+ nlsConfig : nlsConfig ,
252
+ options,
253
+ } )
254
+ } ) . toThrowError ( errorMessage )
255
+ } )
256
+ it ( "should throw an error if options.csStaticBase is undefined or an empty string" , ( ) => {
257
+ const options = {
258
+ base : "/" ,
259
+ csStaticBase : "" ,
260
+ logLevel : 1 ,
261
+ }
262
+ const nlsConfig = {
263
+ first : "Jane" ,
264
+ last : "Doe" ,
265
+ locale : "en" ,
266
+ availableLanguages : { } ,
267
+ }
268
+ const errorMsgPrefix = "[vscode]"
269
+ const errorMessage = `${ errorMsgPrefix } Could not register require on self. options or options.csStaticBase is undefined or missing.`
270
+ const mockSelf = { } as Window & typeof globalThis
271
+ expect ( ( ) => {
272
+ registerRequireOnSelf ( {
273
+ self : mockSelf ,
274
+ window : window ,
275
+ nlsConfig : nlsConfig ,
276
+ options,
277
+ } )
278
+ } ) . toThrowError ( errorMessage )
279
+ expect ( ( ) => {
280
+ registerRequireOnSelf ( {
281
+ self : mockSelf ,
282
+ window : window ,
283
+ nlsConfig : nlsConfig ,
284
+ // @ts -expect-error We need to check what happens when options is undefined
285
+ options : undefined ,
286
+ } )
287
+ } ) . toThrowError ( errorMessage )
288
+ } )
289
+ it ( "should throw an error if nlsConfig is undefined" , ( ) => {
290
+ const options = {
291
+ base : "/" ,
292
+ csStaticBase : "/" ,
293
+ logLevel : 1 ,
294
+ }
295
+ const errorMsgPrefix = "[vscode]"
296
+ const errorMessage = `${ errorMsgPrefix } Could not register require on self. nlsConfig is undefined.`
297
+ const mockSelf = { } as Window & typeof globalThis
298
+ expect ( ( ) => {
299
+ registerRequireOnSelf ( {
300
+ self : mockSelf ,
301
+ window : window ,
302
+ // @ts -expect-error We need to check that it works when this is undefined
303
+ nlsConfig : undefined ,
304
+ options,
305
+ } )
306
+ } ) . toThrowError ( errorMessage )
307
+ } )
308
+ it ( "should declare require on self" , ( ) => {
309
+ const options = {
310
+ base : "/" ,
311
+ csStaticBase : "/" ,
312
+ logLevel : 1 ,
313
+ }
314
+ const nlsConfig = {
315
+ first : "Jane" ,
316
+ last : "Doe" ,
317
+ locale : "en" ,
318
+ availableLanguages : { } ,
319
+ }
320
+ const mockSelf = { } as Window & typeof globalThis
321
+ registerRequireOnSelf ( {
322
+ self : mockSelf ,
323
+ window : window ,
324
+ nlsConfig : nlsConfig ,
325
+ options,
326
+ } )
327
+
328
+ const hasRequireProperty = Object . prototype . hasOwnProperty . call ( mockSelf , "require" )
329
+ expect ( hasRequireProperty ) . toBeTruthy ( )
330
+ } )
331
+ it ( "should return true if it registered succesfully" , ( ) => {
332
+ const options = {
333
+ base : "/" ,
334
+ csStaticBase : "/" ,
335
+ logLevel : 1 ,
336
+ }
337
+ const nlsConfig = {
338
+ first : "Jane" ,
339
+ last : "Doe" ,
340
+ locale : "en" ,
341
+ availableLanguages : { } ,
342
+ }
343
+ const mockSelf = { } as Window & typeof globalThis
344
+ const didRegister = registerRequireOnSelf ( {
345
+ self : mockSelf ,
346
+ window : window ,
347
+ nlsConfig : nlsConfig ,
348
+ options,
349
+ } )
350
+
351
+ expect ( didRegister ) . toBe ( true )
352
+ } )
353
+ } )
178
354
} )
0 commit comments