1
1
import { JSDOM } from "jsdom"
2
2
import * as util from "../../../src/common/util"
3
- import { createLoggerMock } from "../../utils/helpers"
3
+ import * as helpers from "../../utils/helpers"
4
4
5
5
const dom = new JSDOM ( )
6
6
global . document = dom . window . document
@@ -111,6 +111,8 @@ describe("util", () => {
111
111
} )
112
112
113
113
describe ( "getOptions" , ( ) => {
114
+ let getOptions : typeof import ( "../../../src/common/util" ) . getOptions
115
+
114
116
beforeEach ( ( ) => {
115
117
const location : LocationLike = {
116
118
pathname : "/healthz" ,
@@ -124,54 +126,80 @@ describe("util", () => {
124
126
// and tell TS that our location should be looked at
125
127
// as Location (even though it's missing some properties)
126
128
global . location = location as Location
129
+
130
+ // Reset and re-import since the options are cached.
131
+ jest . resetModules ( )
132
+ getOptions = require ( "../../../src/common/util" ) . getOptions
133
+
134
+ helpers . spyOnConsole ( )
127
135
} )
128
136
129
137
afterEach ( ( ) => {
130
138
jest . restoreAllMocks ( )
131
139
} )
132
140
133
141
it ( "should return options with base and cssStaticBase even if it doesn't exist" , ( ) => {
134
- expect ( util . getOptions ( ) ) . toStrictEqual ( {
142
+ expect ( getOptions ( ) ) . toStrictEqual ( {
135
143
base : "" ,
136
144
csStaticBase : "" ,
137
145
} )
146
+ expect ( console . error ) . toBeCalledTimes ( 1 )
138
147
} )
139
148
140
149
it ( "should return options when they do exist" , ( ) => {
150
+ const expected = {
151
+ base : "." ,
152
+ csStaticBase : "./static/development/Users/jp/Dev/code-server" ,
153
+ logLevel : 2 ,
154
+ disableTelemetry : false ,
155
+ disableUpdateCheck : false ,
156
+ }
157
+
141
158
// Mock getElementById
142
159
const spy = jest . spyOn ( document , "getElementById" )
143
- // Create a fake element and set the attribute
160
+ // Create a fake element and set the attribute. Options are expected to be
161
+ // stringified JSON.
144
162
const mockElement = document . createElement ( "div" )
145
- mockElement . setAttribute (
146
- "data-settings" ,
147
- '{"base":".","csStaticBase":"./static/development/Users/jp/Dev/code-server","logLevel":2,"disableUpdateCheck":false}' ,
148
- )
163
+ mockElement . setAttribute ( "data-settings" , JSON . stringify ( expected ) )
149
164
// Return mockElement from the spy
150
165
// this way, when we call "getElementById"
151
166
// it returns the element
152
167
spy . mockImplementation ( ( ) => mockElement )
153
168
154
- expect ( util . getOptions ( ) ) . toStrictEqual ( {
169
+ expect ( getOptions ( ) ) . toStrictEqual ( {
170
+ ...expected ,
171
+ // The two bases should get resolved. The rest should be unchanged.
155
172
base : "" ,
156
173
csStaticBase : "/static/development/Users/jp/Dev/code-server" ,
157
- disableUpdateCheck : false ,
174
+ } )
175
+ expect ( console . error ) . toBeCalledTimes ( 0 )
176
+ } )
177
+
178
+ it ( "should merge options in the query" , ( ) => {
179
+ // Options provided in the query will override any options provided in the
180
+ // HTML. Options are expected to be stringified JSON (same as the
181
+ // element).
182
+ const expected = {
158
183
logLevel : 2 ,
184
+ }
185
+ location . search = `?options=${ JSON . stringify ( expected ) } `
186
+ expect ( getOptions ( ) ) . toStrictEqual ( {
187
+ ...expected ,
188
+ base : "" ,
189
+ csStaticBase : "" ,
159
190
} )
191
+ // Once for the element.
192
+ expect ( console . error ) . toBeCalledTimes ( 1 )
160
193
} )
161
194
162
- it ( "should include queryOpts" , ( ) => {
163
- // Trying to understand how the implementation works
164
- // 1. It grabs the search params from location.search (i.e. ?)
165
- // 2. it then grabs the "options" param if it exists
166
- // 3. then it creates a new options object
167
- // spreads the original options
168
- // then parses the queryOpts
169
- location . search = '?options={"logLevel":2}'
170
- expect ( util . getOptions ( ) ) . toStrictEqual ( {
195
+ it ( "should skip bad query options" , ( ) => {
196
+ location . search = "?options=invalidJson"
197
+ expect ( getOptions ( ) ) . toStrictEqual ( {
171
198
base : "" ,
172
199
csStaticBase : "" ,
173
- logLevel : 2 ,
174
200
} )
201
+ // Once for the element, once for the query.
202
+ expect ( console . error ) . toBeCalledTimes ( 2 )
175
203
} )
176
204
} )
177
205
@@ -217,7 +245,7 @@ describe("util", () => {
217
245
jest . restoreAllMocks ( )
218
246
} )
219
247
220
- const loggerModule = createLoggerMock ( )
248
+ const loggerModule = helpers . createLoggerMock ( )
221
249
222
250
it ( "should log an error with the message and stack trace" , ( ) => {
223
251
const message = "You don't have access to that folder."
0 commit comments