Skip to content

Commit 08cd087

Browse files
authored
Convert ReactPureComponent-test to createRoot (facebook#27917)
Convert ReactPureComponent-test to createRoot
1 parent 2594caa commit 08cd087

File tree

1 file changed

+46
-17
lines changed

1 file changed

+46
-17
lines changed

‎packages/react/src/__tests__/ReactPureComponent-test.js‎

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,20 @@
99

1010
'use strict';
1111

12+
let act;
13+
1214
let React;
13-
let ReactDOM;
15+
let ReactDOMClient;
1416

1517
describe('ReactPureComponent', () => {
1618
beforeEach(() => {
19+
act = require('internal-test-utils').act;
20+
1721
React = require('react');
18-
ReactDOM = require('react-dom');
22+
ReactDOMClient = require('react-dom/client');
1923
});
2024

21-
it('should render', () => {
25+
it('should render', async () => {
2226
let renders = 0;
2327
class Component extends React.PureComponent {
2428
constructor() {
@@ -32,36 +36,47 @@ describe('ReactPureComponent', () => {
3236
}
3337

3438
const container = document.createElement('div');
39+
const root = ReactDOMClient.createRoot(container);
3540
let text;
36-
let component;
41+
const componentRef = React.createRef();
3742

3843
text = ['porcini'];
39-
component = ReactDOM.render(<Component text={text} />, container);
44+
await act(() => {
45+
root.render(<Component ref={componentRef} text={text} />);
46+
});
4047
expect(container.textContent).toBe('porcini');
4148
expect(renders).toBe(1);
4249

4350
text = ['morel'];
44-
component = ReactDOM.render(<Component text={text} />, container);
51+
await act(() => {
52+
root.render(<Component ref={componentRef} text={text} />);
53+
});
4554
expect(container.textContent).toBe('morel');
4655
expect(renders).toBe(2);
4756

4857
text[0] = 'portobello';
49-
component = ReactDOM.render(<Component text={text} />, container);
58+
await act(() => {
59+
root.render(<Component ref={componentRef} text={text} />);
60+
});
5061
expect(container.textContent).toBe('morel');
5162
expect(renders).toBe(2);
5263

5364
// Setting state without changing it doesn't cause a rerender.
54-
component.setState({type: 'mushrooms'});
65+
await act(() => {
66+
componentRef.current.setState({type: 'mushrooms'});
67+
});
5568
expect(container.textContent).toBe('morel');
5669
expect(renders).toBe(2);
5770

5871
// But changing state does.
59-
component.setState({type: 'portobello mushrooms'});
72+
await act(() => {
73+
componentRef.current.setState({type: 'portobello mushrooms'});
74+
});
6075
expect(container.textContent).toBe('portobello');
6176
expect(renders).toBe(3);
6277
});
6378

64-
it('can override shouldComponentUpdate', () => {
79+
it('can override shouldComponentUpdate', async () => {
6580
let renders = 0;
6681
class Component extends React.PureComponent {
6782
render() {
@@ -74,17 +89,24 @@ describe('ReactPureComponent', () => {
7489
}
7590

7691
const container = document.createElement('div');
77-
expect(() => ReactDOM.render(<Component />, container)).toErrorDev(
92+
const root = ReactDOMClient.createRoot(container);
93+
await expect(async () => {
94+
await act(() => {
95+
root.render(<Component />);
96+
});
97+
}).toErrorDev(
7898
'Warning: ' +
7999
'Component has a method called shouldComponentUpdate(). ' +
80100
'shouldComponentUpdate should not be used when extending React.PureComponent. ' +
81101
'Please extend React.Component if shouldComponentUpdate is used.',
82102
);
83-
ReactDOM.render(<Component />, container);
103+
await act(() => {
104+
root.render(<Component />);
105+
});
84106
expect(renders).toBe(2);
85107
});
86108

87-
it('extends React.Component', () => {
109+
it('extends React.Component', async () => {
88110
let renders = 0;
89111
class Component extends React.PureComponent {
90112
render() {
@@ -94,11 +116,14 @@ describe('ReactPureComponent', () => {
94116
return <div />;
95117
}
96118
}
97-
ReactDOM.render(<Component />, document.createElement('div'));
119+
const root = ReactDOMClient.createRoot(document.createElement('div'));
120+
await act(() => {
121+
root.render(<Component />);
122+
});
98123
expect(renders).toBe(1);
99124
});
100125

101-
it('should warn when shouldComponentUpdate is defined on React.PureComponent', () => {
126+
it('should warn when shouldComponentUpdate is defined on React.PureComponent', async () => {
102127
class PureComponent extends React.PureComponent {
103128
shouldComponentUpdate() {
104129
return true;
@@ -107,8 +132,12 @@ describe('ReactPureComponent', () => {
107132
return <div />;
108133
}
109134
}
110-
const container = document.createElement('div');
111-
expect(() => ReactDOM.render(<PureComponent />, container)).toErrorDev(
135+
const root = ReactDOMClient.createRoot(document.createElement('div'));
136+
await expect(async () => {
137+
await act(() => {
138+
root.render(<PureComponent />);
139+
});
140+
}).toErrorDev(
112141
'Warning: ' +
113142
'PureComponent has a method called shouldComponentUpdate(). ' +
114143
'shouldComponentUpdate should not be used when extending React.PureComponent. ' +

0 commit comments

Comments
 (0)