From 7c595e06413a4a1607656f3b0af72ae6c80d10b0 Mon Sep 17 00:00:00 2001 From: David Ortner Date: Mon, 2 Oct 2023 16:33:22 +0200 Subject: [PATCH] #940@trivial: Fixes failing unit test. --- .../test/react/React.test.tsx | 71 +++++++++++-------- 1 file changed, 41 insertions(+), 30 deletions(-) diff --git a/packages/global-registrator/test/react/React.test.tsx b/packages/global-registrator/test/react/React.test.tsx index 0fc5dac77..a17fa9d52 100644 --- a/packages/global-registrator/test/react/React.test.tsx +++ b/packages/global-registrator/test/react/React.test.tsx @@ -1,51 +1,62 @@ import GlobalRegistrator from '../../cjs/GlobalRegistrator.cjs'; import React from 'react'; import ReactDOM from 'react-dom/client'; +import { act } from 'react-dom/test-utils'; import ReactComponent from './ReactComponent.js'; -const selfReferringProperties = ['self', 'top', 'parent', 'window']; +async function main(): Promise { + const selfReferringProperties = ['self', 'top', 'parent', 'window']; -// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -const originalSetTimeout = global.setTimeout; + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions + const originalSetTimeout = global.setTimeout; -GlobalRegistrator.register(); + GlobalRegistrator.register(); -const appElement = document.createElement('app'); -let root; -document.body.appendChild(appElement); + const appElement = document.createElement('app'); + let root; + document.body.appendChild(appElement); -function mountReactComponent(): void { - root = ReactDOM.createRoot(appElement); - root.render(); + async function mountReactComponent(): Promise { + act(() => { + root = ReactDOM.createRoot(appElement); + root.render(); + }); - if (appElement.innerHTML !== '
Test
') { - throw Error('React not rendered correctly.'); + await new Promise((resolve) => setTimeout(resolve, 2)); + + if (appElement.innerHTML !== '
Test
') { + throw Error('React not rendered correctly.'); + } } -} -function unmountReactComponent(): void { - root.unmount(); + function unmountReactComponent(): void { + act(() => { + root.unmount(); + }); - if (appElement.innerHTML !== '') { - throw Error('React not unmounted correctly.'); + if (appElement.innerHTML !== '') { + throw Error('React not unmounted correctly.'); + } } -} -if (global.setTimeout === originalSetTimeout) { - throw Error('Happy DOM function not registered.'); -} + if (global.setTimeout === originalSetTimeout) { + throw Error('Happy DOM function not registered.'); + } -for (const property of selfReferringProperties) { - if (global[property] !== global) { - throw Error('Self referring property property was not registered.'); + for (const property of selfReferringProperties) { + if (global[property] !== global) { + throw Error('Self referring property property was not registered.'); + } } -} -mountReactComponent(); -unmountReactComponent(); + await mountReactComponent(); + unmountReactComponent(); -GlobalRegistrator.unregister(); + GlobalRegistrator.unregister(); -if (global.setTimeout !== originalSetTimeout) { - throw Error('Global property was not restored.'); + if (global.setTimeout !== originalSetTimeout) { + throw Error('Global property was not restored.'); + } } + +main();