Skip to content

Commit 37bdff6

Browse files
Convert ReactIdentity-test.js to createRoot (#28106)
1 parent 759811b commit 37bdff6

File tree

1 file changed

+70
-49
lines changed

1 file changed

+70
-49
lines changed

packages/react-dom/src/__tests__/ReactIdentity-test.js

+70-49
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@
1010
'use strict';
1111

1212
let React;
13-
let ReactDOM;
13+
let ReactDOMClient;
1414
let ReactTestUtils;
15+
let act;
1516

1617
describe('ReactIdentity', () => {
1718
beforeEach(() => {
1819
jest.resetModules();
1920
React = require('react');
20-
ReactDOM = require('react-dom');
21+
ReactDOMClient = require('react-dom/client');
2122
ReactTestUtils = require('react-dom/test-utils');
23+
act = require('internal-test-utils').act;
2224
});
2325

24-
it('should allow key property to express identity', () => {
26+
it('should allow key property to express identity', async () => {
2527
let node;
2628
const Component = props => (
2729
<div ref={c => (node = c)}>
@@ -31,41 +33,48 @@ describe('ReactIdentity', () => {
3133
);
3234

3335
const container = document.createElement('div');
34-
ReactDOM.render(<Component />, container);
36+
const root = ReactDOMClient.createRoot(container);
37+
await act(async () => {
38+
root.render(<Component />);
39+
});
3540
const origChildren = Array.from(node.childNodes);
36-
ReactDOM.render(<Component swap={true} />, container);
41+
await act(async () => {
42+
root.render(<Component swap={true} />);
43+
});
3744
const newChildren = Array.from(node.childNodes);
3845
expect(origChildren[0]).toBe(newChildren[1]);
3946
expect(origChildren[1]).toBe(newChildren[0]);
4047
});
4148

42-
it('should use composite identity', () => {
49+
it('should use composite identity', async () => {
4350
class Wrapper extends React.Component {
4451
render() {
4552
return <a>{this.props.children}</a>;
4653
}
4754
}
4855

4956
const container = document.createElement('div');
57+
const root = ReactDOMClient.createRoot(container);
5058
let node1;
5159
let node2;
52-
ReactDOM.render(
53-
<Wrapper key="wrap1">
54-
<span ref={c => (node1 = c)} />
55-
</Wrapper>,
56-
container,
57-
);
58-
ReactDOM.render(
59-
<Wrapper key="wrap2">
60-
<span ref={c => (node2 = c)} />
61-
</Wrapper>,
62-
container,
63-
);
64-
60+
await act(async () => {
61+
root.render(
62+
<Wrapper key="wrap1">
63+
<span ref={c => (node1 = c)} />
64+
</Wrapper>,
65+
);
66+
});
67+
await act(async () => {
68+
root.render(
69+
<Wrapper key="wrap2">
70+
<span ref={c => (node2 = c)} />
71+
</Wrapper>,
72+
);
73+
});
6574
expect(node1).not.toBe(node2);
6675
});
6776

68-
function renderAComponentWithKeyIntoContainer(key, container) {
77+
async function renderAComponentWithKeyIntoContainer(key, root) {
6978
class Wrapper extends React.Component {
7079
spanRef = React.createRef();
7180
render() {
@@ -76,36 +85,41 @@ describe('ReactIdentity', () => {
7685
);
7786
}
7887
}
79-
80-
const instance = ReactDOM.render(<Wrapper />, container);
81-
const span = instance.spanRef.current;
88+
const wrapperRef = React.createRef();
89+
await act(async () => {
90+
root.render(<Wrapper ref={wrapperRef} />);
91+
});
92+
const span = wrapperRef.current.spanRef.current;
8293
expect(span).not.toBe(null);
8394
}
8495

85-
it('should allow any character as a key, in a detached parent', () => {
96+
it('should allow any character as a key, in a detached parent', async () => {
8697
const detachedContainer = document.createElement('div');
87-
renderAComponentWithKeyIntoContainer("<'WEIRD/&\\key'>", detachedContainer);
98+
const root = ReactDOMClient.createRoot(detachedContainer);
99+
await renderAComponentWithKeyIntoContainer("<'WEIRD/&\\key'>", root);
88100
});
89101

90-
it('should allow any character as a key, in an attached parent', () => {
102+
it('should allow any character as a key, in an attached parent', async () => {
91103
// This test exists to protect against implementation details that
92104
// incorrectly query escaped IDs using DOM tools like getElementById.
93105
const attachedContainer = document.createElement('div');
106+
const root = ReactDOMClient.createRoot(attachedContainer);
94107
document.body.appendChild(attachedContainer);
95108

96-
renderAComponentWithKeyIntoContainer("<'WEIRD/&\\key'>", attachedContainer);
109+
await renderAComponentWithKeyIntoContainer("<'WEIRD/&\\key'>", root);
97110

98111
document.body.removeChild(attachedContainer);
99112
});
100113

101-
it('should not allow scripts in keys to execute', () => {
114+
it('should not allow scripts in keys to execute', async () => {
102115
const h4x0rKey =
103116
'"><script>window[\'YOUVEBEENH4X0RED\']=true;</script><div id="';
104117

105118
const attachedContainer = document.createElement('div');
119+
const root = ReactDOMClient.createRoot(attachedContainer);
106120
document.body.appendChild(attachedContainer);
107121

108-
renderAComponentWithKeyIntoContainer(h4x0rKey, attachedContainer);
122+
await renderAComponentWithKeyIntoContainer(h4x0rKey, root);
109123

110124
document.body.removeChild(attachedContainer);
111125

@@ -209,7 +223,7 @@ describe('ReactIdentity', () => {
209223
}).not.toThrow();
210224
});
211225

212-
it('should retain key during updates in composite components', () => {
226+
it('should retain key during updates in composite components', async () => {
213227
class TestComponent extends React.Component {
214228
render() {
215229
return <div>{this.props.children}</div>;
@@ -236,16 +250,23 @@ describe('ReactIdentity', () => {
236250
const instance0 = <span key="A" />;
237251
const instance1 = <span key="B" />;
238252

239-
let wrapped = <TestContainer first={instance0} second={instance1} />;
240-
241-
wrapped = ReactDOM.render(wrapped, document.createElement('div'));
242-
const div = ReactDOM.findDOMNode(wrapped);
253+
const container = document.createElement('div');
254+
const root = ReactDOMClient.createRoot(container);
255+
const wrappedRef = React.createRef();
256+
await act(async () => {
257+
root.render(
258+
<TestContainer first={instance0} second={instance1} ref={wrappedRef} />,
259+
);
260+
});
261+
const div = container.firstChild;
243262

244-
const beforeA = div.childNodes[0];
245-
const beforeB = div.childNodes[1];
246-
wrapped.swap();
247-
const afterA = div.childNodes[1];
248-
const afterB = div.childNodes[0];
263+
const beforeA = div.firstChild;
264+
const beforeB = div.lastChild;
265+
await act(async () => {
266+
wrappedRef.current.swap();
267+
});
268+
const afterA = div.lastChild;
269+
const afterB = div.firstChild;
249270

250271
expect(beforeA).toBe(afterA);
251272
expect(beforeB).toBe(afterB);
@@ -264,7 +285,7 @@ describe('ReactIdentity', () => {
264285
}).not.toThrow();
265286
});
266287

267-
it('should throw if key is a Temporal-like object', () => {
288+
it('should throw if key is a Temporal-like object', async () => {
268289
class TemporalLike {
269290
valueOf() {
270291
// Throwing here is the behavior of ECMAScript "Temporal" date/time API.
@@ -277,15 +298,15 @@ describe('ReactIdentity', () => {
277298
}
278299

279300
const el = document.createElement('div');
280-
const test = () =>
281-
ReactDOM.render(
282-
<div>
283-
<span key={new TemporalLike()} />
284-
</div>,
285-
el,
286-
);
287-
expect(() =>
288-
expect(test).toThrowError(new TypeError('prod message')),
301+
const root = ReactDOMClient.createRoot(el);
302+
await expect(() =>
303+
expect(() => {
304+
root.render(
305+
<div>
306+
<span key={new TemporalLike()} />
307+
</div>,
308+
);
309+
}).toThrowError(new TypeError('prod message')),
289310
).toErrorDev(
290311
'The provided key is an unsupported type TemporalLike.' +
291312
' This value must be coerced to a string before using it here.',

0 commit comments

Comments
 (0)