-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhappy-dom.js
More file actions
77 lines (66 loc) · 2.38 KB
/
Copy pathhappy-dom.js
File metadata and controls
77 lines (66 loc) · 2.38 KB
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
/// <reference path="../../types/index.d.ts" />
import { Window } from 'happy-dom';
const BROWSER_GLOBAL_KEYS = [
'Event',
'HTMLElement',
'KeyboardEvent',
'document',
'getComputedStyle',
'localStorage',
'window',
];
/** @param {string} key @param {unknown} value */
function assignTestGlobal(key, value) {
Reflect.set(globalThis, key, value);
}
/** @param {string} [pathname='/'] */
function createHappyDomWindow(pathname = '/') {
return new Window({ url: `https://example.com${pathname}` });
}
/** @param {import('happy-dom').Window} happyDomWindow */
function installBrowserGlobals(happyDomWindow) {
assignTestGlobal('window', happyDomWindow);
assignTestGlobal('document', happyDomWindow.document);
assignTestGlobal('localStorage', happyDomWindow.localStorage);
assignTestGlobal('HTMLElement', happyDomWindow.HTMLElement);
assignTestGlobal('KeyboardEvent', happyDomWindow.KeyboardEvent);
assignTestGlobal('getComputedStyle', happyDomWindow.getComputedStyle.bind(happyDomWindow));
assignTestGlobal('Event', happyDomWindow.Event);
}
function uninstallBrowserGlobals() {
for (const key of BROWSER_GLOBAL_KEYS) {
Reflect.deleteProperty(globalThis, key);
}
}
/** @param {import('happy-dom').Window} happyDomWindow */
function installDocumentGlobals(happyDomWindow) {
assignTestGlobal('document', happyDomWindow.document);
assignTestGlobal('localStorage', happyDomWindow.localStorage);
assignTestGlobal('HTMLElement', happyDomWindow.HTMLElement);
assignTestGlobal('KeyboardEvent', happyDomWindow.KeyboardEvent);
assignTestGlobal('getComputedStyle', happyDomWindow.getComputedStyle.bind(happyDomWindow));
}
/** @param {import('happy-dom').Window} happyDomWindow */
function installVueGlobals(happyDomWindow) {
installBrowserGlobals(happyDomWindow);
assignTestGlobal('SVGElement', happyDomWindow.SVGElement);
assignTestGlobal('Node', happyDomWindow.Node);
assignTestGlobal('Text', happyDomWindow.Text);
assignTestGlobal('Comment', happyDomWindow.Comment);
assignTestGlobal('Element', happyDomWindow.Element);
}
const VUE_GLOBAL_KEYS = ['Comment', 'Element', 'Node', 'SVGElement', 'Text', ...BROWSER_GLOBAL_KEYS];
function uninstallVueGlobals() {
for (const key of VUE_GLOBAL_KEYS) {
Reflect.deleteProperty(globalThis, key);
}
}
export {
BROWSER_GLOBAL_KEYS,
createHappyDomWindow,
installBrowserGlobals,
installDocumentGlobals,
installVueGlobals,
uninstallBrowserGlobals,
uninstallVueGlobals,
};