-
Notifications
You must be signed in to change notification settings - Fork 0
/
setupTests.ts
73 lines (60 loc) · 1.84 KB
/
setupTests.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
import "@testing-library/jest-dom";
import { parseHTML } from "linkedom";
const defaultHtml =
'<!doctype html><html><head><meta charset="utf-8"></head><body></body></html>';
const dom = parseHTML(defaultHtml);
const { window } = dom;
const { document } = window;
// add missing window.location property
if (!window.location) {
// @ts-ignore
window.location = { protocol: "http" };
}
if (!document.getSelection) {
// @ts-ignore
document.getSelection = () => ({});
}
// add missing window.getComputedStyled property
if (!window.getComputedStyle) {
// https://github.com/mikemadest/jest-environment-linkedom/blob/main/src/get-computed-style-polyfill.js
function getComputedStyle(element) {
this.el = element;
this.getPropertyValue = function (prop) {
const regExp = /(\-([a-z]){1})/g;
let updatedProp = prop === "float" ? "styleFloat" : prop;
if (regExp.test(updatedProp)) {
updatedProp = updatedProp.replace(regExp, function (match, ...parts) {
return parts[1].toUpperCase();
});
}
return element?.currentStyle?.[updatedProp]
? element.currentStyle[updatedProp]
: null;
};
return this;
}
window.getComputedStyle = getComputedStyle;
}
// add missing default width/height of window (1024x768 is JSDOM's default)
if (!window.innerWidth) {
window.innerWidth = 1024;
}
if (!window.outerWidth) {
window.outerWidth = 1024;
}
if (!window.innerHeight) {
window.innerHeight = 768;
}
if (!window.outerHeight) {
window.outerHeight = 768;
}
// add in global all window's properties that are not already defined
for (const key of Object.getOwnPropertyNames(window).filter(
(k) => !k.startsWith("_") && !global[k]
)) {
global[key] = window[key];
}
global.document = document;
global.window = window;
global.navigator = window.navigator;
window.console = global.console;