Skip to content

Commit

Permalink
r19 compat in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Dec 9, 2024
1 parent d42ec4e commit 25c3413
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions test/util/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import React from 'react';
import ReactDOM from 'react-dom';
// Doing this after React is loaded makes React do a bit less DOM work
import 'min-react-env/install';
import env from 'min-react-env';
import createYouTube from './createYouTube';

Object.assign(global, env);
const reactMajor = parseInt((ReactDOM.version || '16').split('.')[0], 10);

const render = (initialProps) => {
async function render(initialProps) {
const { YouTube, sdkMock, playerMock } = createYouTube();

let component;
Expand All @@ -36,18 +35,33 @@ const render = (initialProps) => {
}
}

const div = env.document.createElement('div');
const container = new Promise((resolve) => {
// eslint-disable-next-line react/no-deprecated
ReactDOM.render(<Container {...initialProps} ref={resolve} />, div);
const div = document.createElement('div');
let root;
if (reactMajor >= 18) {
const { createRoot } = await import('react-dom/client');
root = createRoot(div);
} else {
root = {
render(element) {
// eslint-disable-next-line react/no-deprecated
ReactDOM.render(element, div);
},
unmount() {
// eslint-disable-next-line react/no-deprecated
ReactDOM.unmountComponentAtNode(div);
},
};
}
const container = await new Promise((resolve) => {
root.render(<Container {...initialProps} ref={resolve} />);
});

function rerender(newProps) {
return container.then((wrapper) => new Promise((resolve) => {
wrapper.setState({ props: newProps }, () => {
return new Promise((resolve) => {
container.setState({ props: newProps }, () => {
Promise.resolve().then(resolve);
});
}));
});
}

function unmount() {
Expand All @@ -62,6 +76,6 @@ const render = (initialProps) => {
rerender,
unmount,
}));
};
}

export default render;

0 comments on commit 25c3413

Please sign in to comment.