Skip to content

Commit

Permalink
#940@trivial: Fixes failing unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
capricorn86 committed Oct 2, 2023
1 parent 96336ce commit 7c595e0
Showing 1 changed file with 41 additions and 30 deletions.
71 changes: 41 additions & 30 deletions packages/global-registrator/test/react/React.test.tsx
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();

0 comments on commit 7c595e0

Please sign in to comment.