Skip to content

Commit 02820d9

Browse files
authored
feat: [#1670] Adds property isRegistered to GlobalRegistrator (#1674)
1 parent a68a9cc commit 02820d9

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

packages/global-registrator/src/GlobalRegistrator.ts

+19-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@ const IGNORE_LIST = ['constructor', 'undefined', 'NaN', 'global', 'globalThis'];
77
*
88
*/
99
export default class GlobalRegistrator {
10-
private static registered: { [key: string | symbol]: PropertyDescriptor } | null = null;
10+
static #registered: { [key: string | symbol]: PropertyDescriptor } | null = null;
11+
12+
/**
13+
* Returns the registered state.
14+
*
15+
* @returns Registered state.
16+
*/
17+
public static get isRegistered(): boolean {
18+
return this.#registered !== null;
19+
}
1120

1221
/**
1322
* Registers Happy DOM globally.
@@ -24,13 +33,13 @@ export default class GlobalRegistrator {
2433
url?: string;
2534
settings?: IOptionalBrowserSettings;
2635
}): void {
27-
if (this.registered !== null) {
36+
if (this.#registered !== null) {
2837
throw new Error('Failed to register. Happy DOM has already been globally registered.');
2938
}
3039

3140
const window = new GlobalWindow({ ...options, console: globalThis.console });
3241

33-
this.registered = {};
42+
this.#registered = {};
3443

3544
// Define properties on the global object
3645
const propertyDescriptors = Object.getOwnPropertyDescriptors(window);
@@ -44,7 +53,7 @@ export default class GlobalRegistrator {
4453
globalPropertyDescriptor?.value === undefined ||
4554
globalPropertyDescriptor?.value !== windowPropertyDescriptor.value
4655
) {
47-
this.registered[key] = globalPropertyDescriptor || null;
56+
this.#registered[key] = globalPropertyDescriptor || null;
4857

4958
// If the property is the window object, replace it with the global object
5059
if (windowPropertyDescriptor.value === window) {
@@ -65,7 +74,7 @@ export default class GlobalRegistrator {
6574

6675
for (const key of propertySymbols) {
6776
const propertyDescriptor = Object.getOwnPropertyDescriptor(window, key);
68-
this.registered[key] = null;
77+
this.#registered[key] = null;
6978

7079
// If the property is the window object, replace it with the global object
7180
if (propertyDescriptor.value === window) {
@@ -87,23 +96,23 @@ export default class GlobalRegistrator {
8796
* Closes the window and unregisters Happy DOM from being global.
8897
*/
8998
public static async unregister(): Promise<void> {
90-
if (this.registered === null) {
99+
if (this.#registered === null) {
91100
throw new Error(
92101
'Failed to unregister. Happy DOM has not previously been globally registered.'
93102
);
94103
}
95104

96105
const happyDOM = globalThis.happyDOM;
97106

98-
for (const key of Object.keys(this.registered)) {
99-
if (this.registered[key] !== null) {
100-
Object.defineProperty(globalThis, key, this.registered[key]);
107+
for (const key of Object.keys(this.#registered)) {
108+
if (this.#registered[key] !== null) {
109+
Object.defineProperty(globalThis, key, this.#registered[key]);
101110
} else {
102111
delete globalThis[key];
103112
}
104113
}
105114

106-
this.registered = null;
115+
this.#registered = null;
107116

108117
if (happyDOM) {
109118
await happyDOM.close();

packages/global-registrator/test/react/React.test.tsx

+19
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,25 @@ async function main(): Promise<void> {
232232
}
233233

234234
testWindowOptions();
235+
236+
/**
237+
* Test is registered property.
238+
*/
239+
function testIsRegisteredProperty(): void {
240+
GlobalRegistrator.register();
241+
242+
if (!GlobalRegistrator.isRegistered) {
243+
throw Error('The "isRegistered" property is incorrect.');
244+
}
245+
246+
GlobalRegistrator.unregister();
247+
248+
if (GlobalRegistrator.isRegistered) {
249+
throw Error('The "isRegistered" property is incorrect.');
250+
}
251+
}
252+
253+
testIsRegisteredProperty();
235254
}
236255

237256
main();

0 commit comments

Comments
 (0)