Skip to content

Commit 10f58e0

Browse files
[mui-utils][test] Remove usages of deprecated react-dom APIs (@aarongarciah) (#42813)
Co-authored-by: Aarón García Hervás <[email protected]>
1 parent e3a1cf2 commit 10f58e0

File tree

2 files changed

+34
-44
lines changed

2 files changed

+34
-44
lines changed

packages/mui-utils/src/elementAcceptingRef/elementAcceptingRef.test.tsx

+17-22
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/* eslint-disable react/prefer-stateless-function */
22
import * as React from 'react';
3-
import * as ReactDOM from 'react-dom';
43
import { expect } from 'chai';
54
import PropTypes from 'prop-types';
5+
import { createRenderer, waitFor } from '@mui-internal/test-utils';
66
import elementAcceptingRef from './elementAcceptingRef';
77

88
describe('elementAcceptingRef', () => {
9+
const { render } = createRenderer();
10+
911
function checkPropType(element: any, required = false) {
1012
PropTypes.checkPropTypes(
1113
{ children: required ? elementAcceptingRef.isRequired : elementAcceptingRef },
@@ -20,35 +22,17 @@ describe('elementAcceptingRef', () => {
2022
});
2123

2224
describe('acceptance when not required', () => {
23-
let rootNode: HTMLElement;
24-
2525
function assertPass(element: any, { shouldMount = true } = {}) {
2626
function testAct() {
2727
checkPropType(element);
2828
if (shouldMount) {
29-
// TODO: replace with React 18 implementation after https://github.com/testing-library/react-testing-library/issues/1216 is closed.
30-
// eslint-disable-next-line react/no-deprecated
31-
ReactDOM.render(
32-
<React.Suspense fallback={<p />}>
33-
{React.cloneElement(element, { ref: React.createRef() })}
34-
</React.Suspense>,
35-
rootNode,
36-
);
29+
render(React.cloneElement(element, { ref: React.createRef() }));
3730
}
3831
}
3932

4033
expect(testAct).not.toErrorDev();
4134
}
4235

43-
before(() => {
44-
rootNode = document.createElement('div');
45-
});
46-
47-
afterEach(() => {
48-
// eslint-disable-next-line react/no-deprecated
49-
ReactDOM.unmountComponentAtNode(rootNode);
50-
});
51-
5236
it('accepts nully values', () => {
5337
assertPass(undefined, { shouldMount: false });
5438
assertPass(null, { shouldMount: false });
@@ -90,14 +74,25 @@ describe('elementAcceptingRef', () => {
9074
assertPass(<Component />);
9175
});
9276

93-
it('accepts lazy', () => {
77+
it('accepts lazy', async () => {
9478
const Component = React.lazy(() =>
9579
Promise.resolve({
9680
default: React.forwardRef((props, ref) => <div {...props} ref={ref} />),
9781
}),
9882
);
9983

100-
assertPass(<Component />);
84+
function testAct() {
85+
checkPropType(<Component />);
86+
render(
87+
<React.Suspense fallback={<p />}>
88+
{React.cloneElement(<Component />, { ref: React.createRef() })}
89+
</React.Suspense>,
90+
);
91+
}
92+
93+
await waitFor(() => {
94+
expect(testAct).not.toErrorDev();
95+
});
10196
});
10297

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

packages/mui-utils/src/elementTypeAcceptingRef/elementTypeAcceptingRef.test.tsx

+17-22
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/* eslint-disable react/prefer-stateless-function */
22
import * as React from 'react';
3-
import * as ReactDOM from 'react-dom';
43
import { expect } from 'chai';
54
import PropTypes from 'prop-types';
5+
import { createRenderer, waitFor } from '@mui-internal/test-utils';
66
import elementTypeAcceptingRef from './elementTypeAcceptingRef';
77

88
describe('elementTypeAcceptingRef', () => {
9+
const { render } = createRenderer();
10+
911
function checkPropType(elementType: any) {
1012
PropTypes.checkPropTypes(
1113
{ component: elementTypeAcceptingRef },
@@ -20,20 +22,11 @@ describe('elementTypeAcceptingRef', () => {
2022
});
2123

2224
describe('acceptance', () => {
23-
let rootNode: HTMLElement;
24-
2525
function assertPass(Component: any, { failsOnMount = false, shouldMount = true } = {}) {
2626
function testAct() {
2727
checkPropType(Component);
2828
if (shouldMount) {
29-
// TODO: replace with React 18 implementation after https://github.com/testing-library/react-testing-library/issues/1216 is closed.
30-
// eslint-disable-next-line react/no-deprecated
31-
ReactDOM.render(
32-
<React.Suspense fallback={<p />}>
33-
<Component ref={React.createRef()} />
34-
</React.Suspense>,
35-
rootNode,
36-
);
29+
render(<Component ref={React.createRef()} />);
3730
}
3831
}
3932

@@ -44,15 +37,6 @@ describe('elementTypeAcceptingRef', () => {
4437
}
4538
}
4639

47-
before(() => {
48-
rootNode = document.createElement('div');
49-
});
50-
51-
afterEach(() => {
52-
// eslint-disable-next-line react/no-deprecated
53-
ReactDOM.unmountComponentAtNode(rootNode);
54-
});
55-
5640
it('accepts nully values', () => {
5741
assertPass(undefined, { shouldMount: false });
5842
assertPass(null, { shouldMount: false });
@@ -94,14 +78,25 @@ describe('elementTypeAcceptingRef', () => {
9478
assertPass(Component);
9579
});
9680

97-
it('accepts lazy', () => {
81+
it('accepts lazy', async () => {
9882
const Component = React.lazy(() =>
9983
Promise.resolve({
10084
default: React.forwardRef((props, ref) => <div ref={ref} {...props} />),
10185
}),
10286
);
10387

104-
assertPass(Component);
88+
function testAct() {
89+
checkPropType(Component);
90+
render(
91+
<React.Suspense fallback={<p />}>
92+
<Component ref={React.createRef()} />
93+
</React.Suspense>,
94+
);
95+
}
96+
97+
await waitFor(() => {
98+
expect(testAct).not.toErrorDev();
99+
});
105100
});
106101

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

0 commit comments

Comments
 (0)