Skip to content

Commit 031309e

Browse files
kassensAndyPengc12
authored andcommitted
Convert ReactElement-test to createRoot (facebook#27918)
Convert ReactElement-test to createRoot
1 parent d7764f3 commit 031309e

File tree

1 file changed

+51
-24
lines changed

1 file changed

+51
-24
lines changed

packages/react/src/__tests__/ReactElement-test.js

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99

1010
'use strict';
1111

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

1618
describe('ReactElement', () => {
@@ -19,8 +21,10 @@ describe('ReactElement', () => {
1921
beforeEach(() => {
2022
jest.resetModules();
2123

24+
act = require('internal-test-utils').act;
25+
2226
React = require('react');
23-
ReactDOM = require('react-dom');
27+
ReactDOMClient = require('react-dom/client');
2428
ReactTestUtils = require('react-dom/test-utils');
2529
// NOTE: We're explicitly not using JSX here. This is intended to test
2630
// classic JS without JSX.
@@ -43,11 +47,10 @@ describe('ReactElement', () => {
4347
expect(element.props).toEqual({});
4448
});
4549

46-
it('should warn when `key` is being accessed on composite element', () => {
47-
const container = document.createElement('div');
50+
it('should warn when `key` is being accessed on composite element', async () => {
4851
class Child extends React.Component {
4952
render() {
50-
return <div> {this.props.key} </div>;
53+
return <div>{this.props.key}</div>;
5154
}
5255
}
5356
class Parent extends React.Component {
@@ -61,7 +64,12 @@ describe('ReactElement', () => {
6164
);
6265
}
6366
}
64-
expect(() => ReactDOM.render(<Parent />, container)).toErrorDev(
67+
const root = ReactDOMClient.createRoot(document.createElement('div'));
68+
await expect(async () => {
69+
await act(() => {
70+
root.render(<Parent />);
71+
});
72+
}).toErrorDev(
6573
'Child: `key` is not a prop. Trying to access it will result ' +
6674
'in `undefined` being returned. If you need to access the same ' +
6775
'value within the child component, you should pass it as a different ' +
@@ -80,8 +88,7 @@ describe('ReactElement', () => {
8088
);
8189
});
8290

83-
it('should warn when `ref` is being accessed', () => {
84-
const container = document.createElement('div');
91+
it('should warn when `ref` is being accessed', async () => {
8592
class Child extends React.Component {
8693
render() {
8794
return <div> {this.props.ref} </div>;
@@ -96,7 +103,13 @@ describe('ReactElement', () => {
96103
);
97104
}
98105
}
99-
expect(() => ReactDOM.render(<Parent />, container)).toErrorDev(
106+
const root = ReactDOMClient.createRoot(document.createElement('div'));
107+
108+
await expect(async () => {
109+
await act(() => {
110+
root.render(<Parent />);
111+
});
112+
}).toErrorDev(
100113
'Child: `ref` is not a prop. Trying to access it will result ' +
101114
'in `undefined` being returned. If you need to access the same ' +
102115
'value within the child component, you should pass it as a different ' +
@@ -288,7 +301,7 @@ describe('ReactElement', () => {
288301

289302
// NOTE: We're explicitly not using JSX here. This is intended to test
290303
// classic JS without JSX.
291-
it('should use default prop value when removing a prop', () => {
304+
it('should use default prop value when removing a prop', async () => {
292305
class Component extends React.Component {
293306
render() {
294307
return React.createElement('span');
@@ -297,13 +310,18 @@ describe('ReactElement', () => {
297310
Component.defaultProps = {fruit: 'persimmon'};
298311

299312
const container = document.createElement('div');
300-
const instance = ReactDOM.render(
301-
React.createElement(Component, {fruit: 'mango'}),
302-
container,
303-
);
313+
const root = ReactDOMClient.createRoot(container);
314+
315+
const ref = React.createRef();
316+
await act(() => {
317+
root.render(React.createElement(Component, {ref, fruit: 'mango'}));
318+
});
319+
const instance = ref.current;
304320
expect(instance.props.fruit).toBe('mango');
305321

306-
ReactDOM.render(React.createElement(Component), container);
322+
await act(() => {
323+
root.render(React.createElement(Component));
324+
});
307325
expect(instance.props.fruit).toBe('persimmon');
308326
});
309327

@@ -328,7 +346,7 @@ describe('ReactElement', () => {
328346
expect(inst2.props.prop).toBe(null);
329347
});
330348

331-
it('throws when changing a prop (in dev) after element creation', () => {
349+
it('throws when changing a prop (in dev) after element creation', async () => {
332350
class Outer extends React.Component {
333351
render() {
334352
const el = <div className="moo" />;
@@ -346,15 +364,21 @@ describe('ReactElement', () => {
346364
return el;
347365
}
348366
}
349-
const outer = ReactTestUtils.renderIntoDocument(<Outer color="orange" />);
367+
368+
const container = document.createElement('div');
369+
const root = ReactDOMClient.createRoot(container);
370+
371+
await act(() => {
372+
root.render(<Outer color="orange" />);
373+
});
350374
if (__DEV__) {
351-
expect(ReactDOM.findDOMNode(outer).className).toBe('moo');
375+
expect(container.firstChild.className).toBe('moo');
352376
} else {
353-
expect(ReactDOM.findDOMNode(outer).className).toBe('quack');
377+
expect(container.firstChild.className).toBe('quack');
354378
}
355379
});
356380

357-
it('throws when adding a prop (in dev) after element creation', () => {
381+
it('throws when adding a prop (in dev) after element creation', async () => {
358382
const container = document.createElement('div');
359383
class Outer extends React.Component {
360384
render() {
@@ -374,12 +398,15 @@ describe('ReactElement', () => {
374398
}
375399
}
376400
Outer.defaultProps = {sound: 'meow'};
377-
const outer = ReactDOM.render(<Outer />, container);
378-
expect(ReactDOM.findDOMNode(outer).textContent).toBe('meow');
401+
const root = ReactDOMClient.createRoot(container);
402+
await act(() => {
403+
root.render(<Outer />);
404+
});
405+
expect(container.firstChild.textContent).toBe('meow');
379406
if (__DEV__) {
380-
expect(ReactDOM.findDOMNode(outer).className).toBe('');
407+
expect(container.firstChild.className).toBe('');
381408
} else {
382-
expect(ReactDOM.findDOMNode(outer).className).toBe('quack');
409+
expect(container.firstChild.className).toBe('quack');
383410
}
384411
});
385412

0 commit comments

Comments
 (0)