forked from capricorn86/happy-dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlobalRegistrator.ts
39 lines (36 loc) · 871 Bytes
/
GlobalRegistrator.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
import { GlobalWindow } from 'happy-dom';
/**
*
*/
export default class GlobalRegistrator {
private static registered = [];
/**
* Registers Happy DOM globally.
*/
public static register(): void {
if (this.registered.length) {
throw new Error('Failed to register. Happy DOM has already been globally registered.');
}
const window = new GlobalWindow();
for (const key of Object.keys(window)) {
if (global[key] === undefined && key !== 'undefined') {
global[key] = window[key];
this.registered.push(key);
}
}
}
/**
* Registers Happy DOM globally.
*/
public static unregister(): void {
if (!this.registered.length) {
throw new Error(
'Failed to unregister. Happy DOM has not previously been globally registered.'
);
}
for (const key of this.registered) {
delete global[key];
this.registered.pop();
}
}
}