From 5c343dbdb410dc07822e533c56866c48d3a8168d Mon Sep 17 00:00:00 2001 From: David Ortner Date: Wed, 8 Jan 2025 01:13:17 +0100 Subject: [PATCH] feat: [#1670] Adds property isRegistered to GlobalRegistrator --- .../src/GlobalRegistrator.ts | 29 ++++++++++++------- .../test/react/React.test.tsx | 19 ++++++++++++ 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/packages/global-registrator/src/GlobalRegistrator.ts b/packages/global-registrator/src/GlobalRegistrator.ts index 5796bbc29..4f7fd6c20 100644 --- a/packages/global-registrator/src/GlobalRegistrator.ts +++ b/packages/global-registrator/src/GlobalRegistrator.ts @@ -7,7 +7,16 @@ const IGNORE_LIST = ['constructor', 'undefined', 'NaN', 'global', 'globalThis']; * */ export default class GlobalRegistrator { - private static registered: { [key: string | symbol]: PropertyDescriptor } | null = null; + static #registered: { [key: string | symbol]: PropertyDescriptor } | null = null; + + /** + * Returns the registered state. + * + * @returns Registered state. + */ + public static get isRegistered(): boolean { + return this.#registered !== null; + } /** * Registers Happy DOM globally. @@ -24,13 +33,13 @@ export default class GlobalRegistrator { url?: string; settings?: IOptionalBrowserSettings; }): void { - if (this.registered !== null) { + if (this.#registered !== null) { throw new Error('Failed to register. Happy DOM has already been globally registered.'); } const window = new GlobalWindow({ ...options, console: globalThis.console }); - this.registered = {}; + this.#registered = {}; // Define properties on the global object const propertyDescriptors = Object.getOwnPropertyDescriptors(window); @@ -44,7 +53,7 @@ export default class GlobalRegistrator { globalPropertyDescriptor?.value === undefined || globalPropertyDescriptor?.value !== windowPropertyDescriptor.value ) { - this.registered[key] = globalPropertyDescriptor || null; + this.#registered[key] = globalPropertyDescriptor || null; // If the property is the window object, replace it with the global object if (windowPropertyDescriptor.value === window) { @@ -65,7 +74,7 @@ export default class GlobalRegistrator { for (const key of propertySymbols) { const propertyDescriptor = Object.getOwnPropertyDescriptor(window, key); - this.registered[key] = null; + this.#registered[key] = null; // If the property is the window object, replace it with the global object if (propertyDescriptor.value === window) { @@ -87,7 +96,7 @@ export default class GlobalRegistrator { * Closes the window and unregisters Happy DOM from being global. */ public static async unregister(): Promise { - if (this.registered === null) { + if (this.#registered === null) { throw new Error( 'Failed to unregister. Happy DOM has not previously been globally registered.' ); @@ -95,15 +104,15 @@ export default class GlobalRegistrator { const happyDOM = globalThis.happyDOM; - for (const key of Object.keys(this.registered)) { - if (this.registered[key] !== null) { - Object.defineProperty(globalThis, key, this.registered[key]); + for (const key of Object.keys(this.#registered)) { + if (this.#registered[key] !== null) { + Object.defineProperty(globalThis, key, this.#registered[key]); } else { delete globalThis[key]; } } - this.registered = null; + this.#registered = null; if (happyDOM) { await happyDOM.close(); diff --git a/packages/global-registrator/test/react/React.test.tsx b/packages/global-registrator/test/react/React.test.tsx index f2e087640..21c88b95a 100644 --- a/packages/global-registrator/test/react/React.test.tsx +++ b/packages/global-registrator/test/react/React.test.tsx @@ -232,6 +232,25 @@ async function main(): Promise { } testWindowOptions(); + + /** + * Test is registered property. + */ + function testIsRegisteredProperty(): void { + GlobalRegistrator.register(); + + if (!GlobalRegistrator.isRegistered) { + throw Error('The "isRegistered" property is incorrect.'); + } + + GlobalRegistrator.unregister(); + + if (GlobalRegistrator.isRegistered) { + throw Error('The "isRegistered" property is incorrect.'); + } + } + + testIsRegisteredProperty(); } main();