/
createDOM.js
71 lines (61 loc) · 1.52 KB
/
createDOM.js
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
const { JSDOM } = require('jsdom');
// We can use jsdom-global at some point if maintaining these lists is a burden.
const whitelist = [
// used by React's experimental cache API
// Always including it to reduce churn when switching between React builds
'AbortController',
// required for fake getComputedStyle
'CSSStyleDeclaration',
'Element',
'Event',
'TouchEvent',
'Image',
'HTMLElement',
'HTMLInputElement',
'Node',
'Performance',
'document',
'DocumentFragment',
];
const blacklist = ['sessionStorage', 'localStorage'];
function createDOM() {
const dom = new JSDOM('', {
pretendToBeVisual: true,
url: 'http://localhost', // https://github.com/jsdom/jsdom/issues/2383
});
global.window = dom.window;
// Not yet supported: https://github.com/jsdom/jsdom/issues/2152
class Touch {
constructor(instance) {
this.instance = instance;
}
get identifier() {
return this.instance.identifier;
}
get pageX() {
return this.instance.pageX;
}
get pageY() {
return this.instance.pageY;
}
get clientX() {
return this.instance.clientX;
}
get clientY() {
return this.instance.clientY;
}
}
global.window.Touch = Touch;
global.navigator = {
userAgent: 'node.js',
};
Object.keys(dom.window)
.filter((key) => !blacklist.includes(key))
.concat(whitelist)
.forEach((key) => {
if (typeof global[key] === 'undefined') {
global[key] = dom.window[key];
}
});
}
module.exports = createDOM;