Skip to content

Commit 5e68264

Browse files
jackpopeAndyPengc12
authored andcommitted
Use createRoot in ReactEventIndependence-test (facebook#28052)
1 parent 8b9afd4 commit 5e68264

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

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

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,68 +10,75 @@
1010
'use strict';
1111

1212
let React;
13-
let ReactDOM;
13+
let ReactDOMClient;
14+
let act;
1415

1516
describe('ReactEventIndependence', () => {
1617
beforeEach(() => {
1718
jest.resetModules();
1819

1920
React = require('react');
20-
ReactDOM = require('react-dom');
21+
ReactDOMClient = require('react-dom/client');
22+
act = require('internal-test-utils').act;
2123
});
2224

23-
it('does not crash with other react inside', () => {
25+
it('does not crash with other react inside', async () => {
2426
let clicks = 0;
2527
const container = document.createElement('div');
2628
document.body.appendChild(container);
29+
const root = ReactDOMClient.createRoot(container);
2730
try {
28-
const div = ReactDOM.render(
29-
<div
30-
onClick={() => clicks++}
31-
dangerouslySetInnerHTML={{
32-
__html: '<button data-reactid=".z">click me</div>',
33-
}}
34-
/>,
35-
container,
36-
);
31+
await act(() => {
32+
root.render(
33+
<div
34+
onClick={() => clicks++}
35+
dangerouslySetInnerHTML={{
36+
__html: '<button data-reactid=".z">click me</div>',
37+
}}
38+
/>,
39+
);
40+
});
3741

38-
div.firstChild.click();
42+
container.firstElementChild.click();
3943
expect(clicks).toBe(1);
4044
} finally {
4145
document.body.removeChild(container);
4246
}
4347
});
4448

45-
it('does not crash with other react outside', () => {
49+
it('does not crash with other react outside', async () => {
4650
let clicks = 0;
4751
const outer = document.createElement('div');
4852
document.body.appendChild(outer);
53+
const root = ReactDOMClient.createRoot(outer);
4954
try {
5055
outer.setAttribute('data-reactid', '.z');
51-
const inner = ReactDOM.render(
52-
<button onClick={() => clicks++}>click me</button>,
53-
outer,
54-
);
55-
inner.click();
56+
await act(() => {
57+
root.render(<button onClick={() => clicks++}>click me</button>);
58+
});
59+
outer.firstElementChild.click();
5660
expect(clicks).toBe(1);
5761
} finally {
5862
document.body.removeChild(outer);
5963
}
6064
});
6165

62-
it('does not when event fired on unmounted tree', () => {
66+
it('does not when event fired on unmounted tree', async () => {
6367
let clicks = 0;
6468
const container = document.createElement('div');
6569
document.body.appendChild(container);
6670
try {
67-
const button = ReactDOM.render(
68-
<button onClick={() => clicks++}>click me</button>,
69-
container,
70-
);
71+
const root = ReactDOMClient.createRoot(container);
72+
73+
await act(() => {
74+
root.render(<button onClick={() => clicks++}>click me</button>);
75+
});
76+
77+
const button = container.firstChild;
7178

7279
// Now we unmount the component, as if caused by a non-React event handler
7380
// for the same click we're about to simulate, like closing a layer:
74-
ReactDOM.unmountComponentAtNode(container);
81+
root.unmount();
7582
button.click();
7683

7784
// Since the tree is unmounted, we don't dispatch the click event.

0 commit comments

Comments
 (0)