11/**
22 * @jest -environment jsdom
33 */
4+ import fetchMock from "jest-fetch-mock"
45import { JSDOM } from "jsdom"
56import {
67 getNlsConfiguration ,
@@ -20,6 +21,11 @@ describe("vscode", () => {
2021 // We use underscores to not confuse with global values
2122 const { window : _window } = new JSDOM ( )
2223 _document = _window . document
24+ fetchMock . enableMocks ( )
25+ } )
26+
27+ afterEach ( ( ) => {
28+ fetchMock . resetMocks ( )
2329 } )
2430
2531 it ( "should throw an error if no nlsConfigElement" , ( ) => {
@@ -60,7 +66,7 @@ describe("vscode", () => {
6066
6167 _document . body . removeChild ( mockElement )
6268 } )
63- it ( "should return have loadBundle property if _resolvedLangaugePackCoreLocation" , ( ) => {
69+ it ( "should return and have a loadBundle property if _resolvedLangaugePackCoreLocation" , async ( ) => {
6470 const mockElement = _document . createElement ( "div" )
6571 const dataSettings = {
6672 locale : "en" ,
@@ -76,6 +82,32 @@ describe("vscode", () => {
7682 expect ( nlsConfig . _resolvedLanguagePackCoreLocation ) . not . toBe ( undefined )
7783 expect ( nlsConfig . loadBundle ) . not . toBe ( undefined )
7884
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
79111 _document . body . removeChild ( mockElement )
80112 } )
81113 } )
@@ -87,6 +119,13 @@ describe("vscode", () => {
87119 const actual = createBundlePath ( _resolvedLangaugePackCoreLocation , bundle )
88120 expect ( actual ) . toBe ( expected )
89121 } )
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+ } )
90129 } )
91130 describe ( "setBodyBackgroundToThemeBackgroundColor" , ( ) => {
92131 let _document : Document
@@ -228,11 +267,6 @@ describe("vscode", () => {
228267 } ,
229268 recordStats : true ,
230269
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
236270 trustedTypesPolicy : undefined ,
237271 "vs/nls" : {
238272 availableLanguages : { } ,
@@ -280,6 +314,11 @@ describe("vscode", () => {
280314
281315 expect ( loader . trustedTypesPolicy ) . not . toBe ( undefined )
282316 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" )
283322 } )
284323 } )
285324 describe ( "_createScriptURL" , ( ) => {
0 commit comments