Skip to content

Commit

Permalink
[mui-utils][test] Remove usages of deprecated react-dom APIs (#42780)
Browse files Browse the repository at this point in the history
  • Loading branch information
aarongarciah authored and web-flow committed Jul 1, 2024
1 parent e3a1cf2 commit 9f80277
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* eslint-disable react/prefer-stateless-function */
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { expect } from 'chai';
import PropTypes from 'prop-types';
import { createRenderer, waitFor } from '@mui/internal-test-utils';
import elementAcceptingRef from './elementAcceptingRef';

describe('elementAcceptingRef', () => {
const { render } = createRenderer();

function checkPropType(element: any, required = false) {
PropTypes.checkPropTypes(
{ children: required ? elementAcceptingRef.isRequired : elementAcceptingRef },
Expand All @@ -20,35 +22,17 @@ describe('elementAcceptingRef', () => {
});

describe('acceptance when not required', () => {
let rootNode: HTMLElement;

function assertPass(element: any, { shouldMount = true } = {}) {
function testAct() {
checkPropType(element);
if (shouldMount) {
// TODO: replace with React 18 implementation after https://github.com/testing-library/react-testing-library/issues/1216 is closed.
// eslint-disable-next-line react/no-deprecated
ReactDOM.render(
<React.Suspense fallback={<p />}>
{React.cloneElement(element, { ref: React.createRef() })}
</React.Suspense>,
rootNode,
);
render(React.cloneElement(element, { ref: React.createRef() }));
}
}

expect(testAct).not.toErrorDev();
}

before(() => {
rootNode = document.createElement('div');
});

afterEach(() => {
// eslint-disable-next-line react/no-deprecated
ReactDOM.unmountComponentAtNode(rootNode);
});

it('accepts nully values', () => {
assertPass(undefined, { shouldMount: false });
assertPass(null, { shouldMount: false });
Expand Down Expand Up @@ -90,14 +74,25 @@ describe('elementAcceptingRef', () => {
assertPass(<Component />);
});

it('accepts lazy', () => {
it('accepts lazy', async () => {
const Component = React.lazy(() =>
Promise.resolve({
default: React.forwardRef((props, ref) => <div {...props} ref={ref} />),
}),
);

assertPass(<Component />);
function testAct() {
checkPropType(<Component />);
render(
<React.Suspense fallback={<p />}>
{React.cloneElement(<Component />, { ref: React.createRef() })}
</React.Suspense>,
);
}

await waitFor(() => {
expect(testAct).not.toErrorDev();
});
});

it('technically allows other exotics like strict mode', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* eslint-disable react/prefer-stateless-function */
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { expect } from 'chai';
import PropTypes from 'prop-types';
import { createRenderer, waitFor } from '@mui/internal-test-utils';
import elementTypeAcceptingRef from './elementTypeAcceptingRef';

describe('elementTypeAcceptingRef', () => {
const { render } = createRenderer();

function checkPropType(elementType: any) {
PropTypes.checkPropTypes(
{ component: elementTypeAcceptingRef },
Expand All @@ -20,20 +22,11 @@ describe('elementTypeAcceptingRef', () => {
});

describe('acceptance', () => {
let rootNode: HTMLElement;

function assertPass(Component: any, { failsOnMount = false, shouldMount = true } = {}) {
function testAct() {
checkPropType(Component);
if (shouldMount) {
// TODO: replace with React 18 implementation after https://github.com/testing-library/react-testing-library/issues/1216 is closed.
// eslint-disable-next-line react/no-deprecated
ReactDOM.render(
<React.Suspense fallback={<p />}>
<Component ref={React.createRef()} />
</React.Suspense>,
rootNode,
);
render(<Component ref={React.createRef()} />);
}
}

Expand All @@ -44,15 +37,6 @@ describe('elementTypeAcceptingRef', () => {
}
}

before(() => {
rootNode = document.createElement('div');
});

afterEach(() => {
// eslint-disable-next-line react/no-deprecated
ReactDOM.unmountComponentAtNode(rootNode);
});

it('accepts nully values', () => {
assertPass(undefined, { shouldMount: false });
assertPass(null, { shouldMount: false });
Expand Down Expand Up @@ -94,14 +78,25 @@ describe('elementTypeAcceptingRef', () => {
assertPass(Component);
});

it('accepts lazy', () => {
it('accepts lazy', async () => {
const Component = React.lazy(() =>
Promise.resolve({
default: React.forwardRef((props, ref) => <div ref={ref} {...props} />),
}),
);

assertPass(Component);
function testAct() {
checkPropType(Component);
render(
<React.Suspense fallback={<p />}>
<Component ref={React.createRef()} />
</React.Suspense>,
);
}

await waitFor(() => {
expect(testAct).not.toErrorDev();
});
});

it('technically allows other exotics like strict mode', () => {
Expand Down

0 comments on commit 9f80277

Please sign in to comment.