1
1
/**
2
2
* @jest -environment jsdom
3
3
*/
4
+ import fetchMock from "jest-fetch-mock"
4
5
import { JSDOM } from "jsdom"
5
6
import {
6
7
getNlsConfiguration ,
@@ -20,6 +21,11 @@ describe("vscode", () => {
20
21
// We use underscores to not confuse with global values
21
22
const { window : _window } = new JSDOM ( )
22
23
_document = _window . document
24
+ fetchMock . enableMocks ( )
25
+ } )
26
+
27
+ afterEach ( ( ) => {
28
+ fetchMock . resetMocks ( )
23
29
} )
24
30
25
31
it ( "should throw an error if no nlsConfigElement" , ( ) => {
@@ -60,7 +66,7 @@ describe("vscode", () => {
60
66
61
67
_document . body . removeChild ( mockElement )
62
68
} )
63
- it ( "should return have loadBundle property if _resolvedLangaugePackCoreLocation" , ( ) => {
69
+ it ( "should return and have a loadBundle property if _resolvedLangaugePackCoreLocation" , async ( ) => {
64
70
const mockElement = _document . createElement ( "div" )
65
71
const dataSettings = {
66
72
locale : "en" ,
@@ -76,6 +82,32 @@ describe("vscode", () => {
76
82
expect ( nlsConfig . _resolvedLanguagePackCoreLocation ) . not . toBe ( undefined )
77
83
expect ( nlsConfig . loadBundle ) . not . toBe ( undefined )
78
84
85
+ const mockCallbackFn = jest . fn ( ( _ , bundle ) => {
86
+ return bundle
87
+ } )
88
+
89
+ fetchMock . mockOnce ( JSON . stringify ( { key : "hello world" } ) )
90
+ // Ensure that load bundle works as expected
91
+ // by mocking the fetch response and checking that the callback
92
+ // had the expected value
93
+ await nlsConfig . loadBundle ( "hello" , "en" , mockCallbackFn )
94
+ expect ( mockCallbackFn ) . toHaveBeenCalledTimes ( 1 )
95
+ expect ( mockCallbackFn ) . toHaveBeenCalledWith ( undefined , { key : "hello world" } )
96
+
97
+ // Call it again to ensure it loads from the cache
98
+ // it should return the same value
99
+ await nlsConfig . loadBundle ( "hello" , "en" , mockCallbackFn )
100
+ expect ( mockCallbackFn ) . toHaveBeenCalledTimes ( 2 )
101
+ expect ( mockCallbackFn ) . toHaveBeenCalledWith ( undefined , { key : "hello world" } )
102
+
103
+ fetchMock . mockReject ( new Error ( "fake error message" ) )
104
+ const mockCallbackFn2 = jest . fn ( ( error ) => error )
105
+ // Call it for a different bundle and mock a failed fetch call
106
+ // to ensure we get the expected error
107
+ const error = await nlsConfig . loadBundle ( "goodbye" , "es" , mockCallbackFn2 )
108
+ expect ( error . message ) . toEqual ( "fake error message" )
109
+
110
+ // Clean up
79
111
_document . body . removeChild ( mockElement )
80
112
} )
81
113
} )
@@ -87,6 +119,13 @@ describe("vscode", () => {
87
119
const actual = createBundlePath ( _resolvedLangaugePackCoreLocation , bundle )
88
120
expect ( actual ) . toBe ( expected )
89
121
} )
122
+ it ( "should return the correct path (even if _resolvedLangaugePackCoreLocation is undefined)" , ( ) => {
123
+ const _resolvedLangaugePackCoreLocation = undefined
124
+ const bundle = "/bundle.js"
125
+ const expected = "/!bundle.js.nls.json"
126
+ const actual = createBundlePath ( _resolvedLangaugePackCoreLocation , bundle )
127
+ expect ( actual ) . toBe ( expected )
128
+ } )
90
129
} )
91
130
describe ( "setBodyBackgroundToThemeBackgroundColor" , ( ) => {
92
131
let _document : Document
@@ -228,11 +267,6 @@ describe("vscode", () => {
228
267
} ,
229
268
recordStats : true ,
230
269
231
- // TODO@jsjoeio address trustedTypesPolicy part
232
- // might need to look up types
233
- // and find a way to test the function
234
- // maybe extract function into function
235
- // and test manually
236
270
trustedTypesPolicy : undefined ,
237
271
"vs/nls" : {
238
272
availableLanguages : { } ,
@@ -280,6 +314,11 @@ describe("vscode", () => {
280
314
281
315
expect ( loader . trustedTypesPolicy ) . not . toBe ( undefined )
282
316
expect ( loader . trustedTypesPolicy . name ) . toBe ( "amdLoader" )
317
+
318
+ // Check that we can actually create a script URL
319
+ // using the createScriptURL on the loader object
320
+ const scriptUrl = loader . trustedTypesPolicy . createScriptURL ( "http://localhost/foo.js" )
321
+ expect ( scriptUrl ) . toBe ( "http://localhost/foo.js" )
283
322
} )
284
323
} )
285
324
describe ( "_createScriptURL" , ( ) => {
0 commit comments