-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
GlobalWindow.test.ts
522 lines (448 loc) · 15.1 KB
/
GlobalWindow.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
import CSSStyleDeclaration from '../../src/css/CSSStyleDeclaration';
import IDocument from '../../src/nodes/document/IDocument';
import IHTMLLinkElement from '../../src/nodes/html-link-element/IHTMLLinkElement';
import IHTMLElement from '../../src/nodes/html-element/IHTMLElement';
import ResourceFetchHandler from '../../src/fetch/ResourceFetchHandler';
import IHTMLScriptElement from '../../src/nodes/html-script-element/IHTMLScriptElement';
import GlobalWindow from '../../src/window/GlobalWindow';
import IWindow from '../../src/window/IWindow';
import Navigator from '../../src/navigator/Navigator';
import Headers from '../../src/fetch/Headers';
import Response from '../../src/fetch/Response';
import Request from '../../src/fetch/Request';
const MOCKED_NODE_FETCH = global['mockedModules']['node-fetch'];
describe('GlobalWindow', () => {
let window: IWindow;
let document: IDocument;
beforeEach(() => {
MOCKED_NODE_FETCH.url = null;
MOCKED_NODE_FETCH.init = null;
MOCKED_NODE_FETCH.error = null;
window = new GlobalWindow();
document = window.document;
});
afterEach(() => {
jest.restoreAllMocks();
});
describe('get Object()', () => {
it('Is the same as {}.constructor.', () => {
expect({}.constructor).toBe(window.Object);
});
it('Is the same as {}.constructor when using eval().', () => {
global['window'] = window;
expect(window.eval('({}).constructor === window.Object')).toBe(true);
delete global['window'];
});
});
describe('get Function()', () => {
it('Is the same as (() => {}).constructor.', () => {
expect((() => {}).constructor).toBe(window.Function);
});
it('Is the same as (() => {}).constructor when using eval().', () => {
global['window'] = window;
expect(window.eval('(() => {}).constructor === window.Function')).toBe(true);
delete global['window'];
});
});
describe('get Array()', () => {
it('Is the same as [].constructor.', () => {
expect([].constructor).toBe(window.Array);
});
it('Is the same as [].constructor when using eval().', () => {
global['window'] = window;
expect(window.eval('[].constructor === window.Array')).toBe(true);
delete global['window'];
});
});
describe('get Headers()', () => {
it('Returns Headers class.', () => {
expect(window.Headers).toBe(Headers);
});
});
describe('get Response()', () => {
it('Returns Response class.', () => {
expect(window.Response['_ownerDocument']).toBe(document);
expect(window.Response).toBe(Response);
});
for (const method of ['arrayBuffer', 'blob', 'buffer', 'json', 'text', 'textConverted']) {
it(`Handles the "${method}" method with the async task manager.`, async () => {
const response = new window.Response();
const result = await response[method]();
expect(result).toBe(MOCKED_NODE_FETCH.response[method]);
});
}
});
describe('get Request()', () => {
it('Returns Request class.', () => {
expect(window.Request['_ownerDocument']).toBe(document);
expect(window.Request).toBe(Request);
});
for (const method of ['arrayBuffer', 'blob', 'buffer', 'json', 'text', 'textConverted']) {
it(`Handles the "${method}" method with the async task manager.`, async () => {
const request = new window.Request('test');
const result = await request[method]();
expect(result).toBe(MOCKED_NODE_FETCH.response[method]);
});
}
});
describe('get performance()', () => {
it('Exposes "performance" from the NodeJS perf_hooks package.', () => {
expect(typeof window.performance.now()).toBe('number');
});
});
describe('get navigator()', () => {
it('Returns an instance of Navigator with browser data.', () => {
expect(window.navigator instanceof Navigator).toBe(true);
expect(window.navigator).toEqual({
appCodeName: 'Mozilla',
appName: 'Netscape',
appVersion: '5.0 (Windows)',
cookieEnabled: true,
credentials: null,
doNotTrack: 'unspecified',
geolocation: null,
hardwareConcurrency: 8,
language: 'en-US',
languages: ['en-US', 'en'],
locks: null,
maxTouchPoints: 0,
mimeTypes: {
length: 0
},
onLine: true,
permissions: null,
platform: 'Win32',
plugins: {
length: 0
},
product: 'Gecko',
productSub: '20100101',
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0',
vendor: '',
vendorSub: '',
webdriver: true
});
});
});
describe('getComputedStyle()', () => {
it('Returns a CSSStyleDeclaration object with computed styles that are live updated whenever the element styles are changed.', () => {
const element = <IHTMLElement>window.document.createElement('div');
const computedStyle = window.getComputedStyle(element);
element.style.direction = 'rtl';
expect(computedStyle instanceof CSSStyleDeclaration).toBe(true);
expect(computedStyle.direction).toBe('');
window.document.body.appendChild(element);
expect(computedStyle.direction).toBe('rtl');
});
});
describe('eval()', () => {
it('Evaluates code and returns the result.', () => {
const result = <() => number>window.eval('() => 5');
expect(result()).toBe(5);
});
});
describe('setTimeout()', () => {
it('Sets a timeout.', (done) => {
const timeoutId = window.setTimeout(() => done());
expect(timeoutId.constructor.name).toBe('Timeout');
});
});
describe('clearTimeout()', () => {
it('Clears a timeout.', () => {
const timeoutId = window.setTimeout(() => {
throw new Error('This timeout should have been canceled.');
});
window.clearTimeout(timeoutId);
});
});
describe('setInterval()', () => {
it('Sets an interval.', (done) => {
let count = 0;
const intervalId = window.setInterval(() => {
count++;
if (count > 2) {
clearInterval(intervalId);
done();
}
});
});
});
describe('clearInterval()', () => {
it('Clears an interval.', () => {
const intervalId = window.setInterval(() => {
throw new Error('This interval should have been canceled.');
});
window.clearInterval(intervalId);
});
});
describe('requestAnimationFrame()', () => {
it('Requests an animation frame.', (done) => {
const timeoutId = window.requestAnimationFrame(() => done());
expect(timeoutId.constructor.name).toBe('Timeout');
});
});
describe('cancelAnimationFrame()', () => {
it('Cancels an animation frame.', () => {
const timeoutId = window.requestAnimationFrame(() => {
throw new Error('This timeout should have been canceled.');
});
window.cancelAnimationFrame(timeoutId);
});
});
describe('matchMedia()', () => {
it('Returns a new MediaQueryList object that can then be used to determine if the document matches the media query string.', () => {
const mediaQueryString = '(max-width: 600px)';
const mediaQueryList = window.matchMedia(mediaQueryString);
expect(mediaQueryList.matches).toBe(false);
expect(mediaQueryList.media).toBe(mediaQueryString);
expect(mediaQueryList.onchange).toBe(null);
expect(typeof mediaQueryList.addEventListener).toBe('function');
expect(typeof mediaQueryList.removeEventListener).toBe('function');
});
});
describe('fetch()', () => {
for (const method of ['arrayBuffer', 'blob', 'buffer', 'json', 'text', 'textConverted']) {
it(`Handles successful "${method}" request.`, async () => {
const expectedUrl = 'https://localhost:8080/path/';
const expectedOptions = {};
const response = await window.fetch(expectedUrl, expectedOptions);
const result = await response[method]();
expect(MOCKED_NODE_FETCH.url).toBe(expectedUrl);
expect(MOCKED_NODE_FETCH.init).toBe(expectedOptions);
expect(result).toEqual(MOCKED_NODE_FETCH.response[method]);
});
}
it('Handles relative URL.', async () => {
const expectedPath = '/path/';
const expectedOptions = {};
window.location.href = 'https://localhost:8080';
const response = await window.fetch(expectedPath, expectedOptions);
const textResponse = await response.text();
expect(MOCKED_NODE_FETCH.url).toBe('https://localhost:8080' + expectedPath);
expect(MOCKED_NODE_FETCH.init).toBe(expectedOptions);
expect(textResponse).toEqual(MOCKED_NODE_FETCH.response.text);
});
it('Handles error JSON request.', async () => {
MOCKED_NODE_FETCH.error = new Error('error');
try {
await window.fetch('/url/', {});
} catch (error) {
expect(error).toBe(MOCKED_NODE_FETCH.error);
}
});
});
describe('happyDOM.whenAsyncComplete()', () => {
it('Resolves the Promise returned by whenAsyncComplete() when all async tasks has been completed.', async () => {
let isFirstWhenAsyncCompleteCalled = false;
window.happyDOM.whenAsyncComplete().then(() => {
isFirstWhenAsyncCompleteCalled = true;
});
let tasksDone = 0;
const intervalID = window.setInterval(() => {
tasksDone++;
});
window.clearInterval(intervalID);
window.setTimeout(() => {
tasksDone++;
});
window.setTimeout(() => {
tasksDone++;
});
window.requestAnimationFrame(() => {
tasksDone++;
});
window.requestAnimationFrame(() => {
tasksDone++;
});
window.fetch('/url/').then((response) =>
response.json().then(() => {
tasksDone++;
})
);
window.fetch('/url/').then((response) =>
response.text().then(() => {
tasksDone++;
})
);
await window.happyDOM.whenAsyncComplete();
expect(tasksDone).toBe(6);
expect(isFirstWhenAsyncCompleteCalled).toBe(true);
});
});
describe('happyDOM.cancelAsync()', () => {
it('Cancels all ongoing asynchrounous tasks.', (done) => {
let isFirstWhenAsyncCompleteCalled = false;
window.happyDOM.whenAsyncComplete().then(() => {
isFirstWhenAsyncCompleteCalled = true;
});
let tasksDone = 0;
const intervalID = window.setInterval(() => {
tasksDone++;
});
window.clearInterval(intervalID);
window.setTimeout(() => {
tasksDone++;
});
window.setTimeout(() => {
tasksDone++;
});
window.requestAnimationFrame(() => {
tasksDone++;
});
window.requestAnimationFrame(() => {
tasksDone++;
});
window
.fetch('/url/')
.then((response) =>
response
.json()
.then(() => {
tasksDone++;
})
.catch(() => {})
)
.catch(() => {});
window
.fetch('/url/')
.then((response) =>
response
.json()
.then(() => {
tasksDone++;
})
.catch(() => {})
)
.catch(() => {});
let isSecondWhenAsyncCompleteCalled = false;
window.happyDOM.whenAsyncComplete().then(() => {
isSecondWhenAsyncCompleteCalled = true;
});
window.happyDOM.cancelAsync();
expect(tasksDone).toBe(0);
global.setTimeout(() => {
expect(isFirstWhenAsyncCompleteCalled).toBe(true);
expect(isSecondWhenAsyncCompleteCalled).toBe(true);
done();
}, 1);
});
});
for (const functionName of ['scroll', 'scrollTo']) {
describe(`${functionName}()`, () => {
it('Sets the properties scrollTop and scrollLeft.', () => {
window[functionName](50, 60);
expect(window.document.documentElement.scrollLeft).toBe(50);
expect(window.document.documentElement.scrollTop).toBe(60);
});
it('Sets the properties scrollTop and scrollLeft using object.', () => {
window[functionName]({ left: 50, top: 60 });
expect(window.document.documentElement.scrollLeft).toBe(50);
expect(window.document.documentElement.scrollTop).toBe(60);
});
it('Sets only the property scrollTop.', () => {
window[functionName]({ top: 60 });
expect(window.document.documentElement.scrollLeft).toBe(0);
expect(window.document.documentElement.scrollTop).toBe(60);
});
it('Sets only the property scrollLeft.', () => {
window[functionName]({ left: 60 });
expect(window.document.documentElement.scrollLeft).toBe(60);
expect(window.document.documentElement.scrollTop).toBe(0);
});
it('Sets the properties scrollTop and scrollLeft with animation.', async () => {
window[functionName]({ left: 50, top: 60, behavior: 'smooth' });
expect(window.document.documentElement.scrollLeft).toBe(0);
expect(window.document.documentElement.scrollTop).toBe(0);
await window.happyDOM.whenAsyncComplete();
expect(window.document.documentElement.scrollLeft).toBe(50);
expect(window.document.documentElement.scrollTop).toBe(60);
});
});
}
describe('addEventListener()', () => {
it('Triggers "load" event if no resources needs to be loaded.', (done) => {
let loadEvent = null;
window.addEventListener('load', (event) => {
loadEvent = event;
});
setTimeout(() => {
expect(loadEvent.target).toBe(window);
done();
}, 1);
});
it('Triggers "load" event when all resources have been loaded.', (done) => {
const cssURL = '/path/to/file.css';
const jsURL = '/path/to/file.js';
const cssResponse = 'body { background-color: red; }';
const jsResponse = 'globalThis.test = "test";';
let resourceFetchCSSDocument = null;
let resourceFetchCSSURL = null;
let resourceFetchJSDocument = null;
let resourceFetchJSURL = null;
let loadEvent = null;
jest
.spyOn(ResourceFetchHandler, 'fetch')
.mockImplementation(async (document: IDocument, url: string) => {
if (url.endsWith('.css')) {
resourceFetchCSSDocument = document;
resourceFetchCSSURL = url;
return cssResponse;
}
resourceFetchJSDocument = document;
resourceFetchJSURL = url;
return jsResponse;
});
window.addEventListener('load', (event) => {
loadEvent = event;
});
const script = <IHTMLScriptElement>document.createElement('script');
script.async = true;
script.src = jsURL;
const link = <IHTMLLinkElement>document.createElement('link');
link.href = cssURL;
link.rel = 'stylesheet';
document.body.appendChild(script);
document.body.appendChild(link);
setTimeout(() => {
expect(resourceFetchCSSDocument).toBe(document);
expect(resourceFetchCSSURL).toBe(cssURL);
expect(resourceFetchJSDocument).toBe(document);
expect(resourceFetchJSURL).toBe(jsURL);
expect(loadEvent.target).toBe(window);
expect(document.styleSheets.length).toBe(1);
expect(document.styleSheets[0].cssRules[0].cssText).toBe(cssResponse);
expect(globalThis['test']).toBe('test');
done();
}, 0);
});
it('Triggers "error" event if there are problems loading resources.', (done) => {
const cssURL = '/path/to/file.css';
const jsURL = '/path/to/file.js';
const errorEvents = [];
jest
.spyOn(ResourceFetchHandler, 'fetch')
.mockImplementation(async (_document: IDocument, url: string) => {
throw new Error(url);
});
window.addEventListener('error', (event) => {
errorEvents.push(event);
});
const script = <IHTMLScriptElement>document.createElement('script');
script.async = true;
script.src = jsURL;
const link = <IHTMLLinkElement>document.createElement('link');
link.href = cssURL;
link.rel = 'stylesheet';
document.body.appendChild(script);
document.body.appendChild(link);
setTimeout(() => {
expect(errorEvents.length).toBe(2);
expect(errorEvents[0].target).toBe(window);
expect(errorEvents[0].error.message).toBe(jsURL);
expect(errorEvents[1].target).toBe(window);
expect(errorEvents[1].error.message).toBe(cssURL);
done();
}, 0);
});
});
});