-
-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#940@trivial: Fixes failing unit test.
- Loading branch information
1 parent
96336ce
commit 7c595e0
Showing
1 changed file
with
41 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<void> { | ||
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(<ReactComponent />); | ||
async function mountReactComponent(): Promise<void> { | ||
act(() => { | ||
root = ReactDOM.createRoot(appElement); | ||
root.render(<ReactComponent />); | ||
}); | ||
|
||
if (appElement.innerHTML !== '<div>Test</div>') { | ||
throw Error('React not rendered correctly.'); | ||
await new Promise((resolve) => setTimeout(resolve, 2)); | ||
|
||
if (appElement.innerHTML !== '<div>Test</div>') { | ||
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(); |