Skip to content

Commit 24d1c6f

Browse files
authored
Convert ReactDOM-test to createRoot (#28009)
Convert ReactDOM-test to createRoot
1 parent 4c58fc2 commit 24d1c6f

File tree

1 file changed

+52
-24
lines changed

1 file changed

+52
-24
lines changed

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

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,25 @@
1111

1212
let React;
1313
let ReactDOM;
14+
let ReactDOMClient;
1415
let ReactDOMServer;
1516
let ReactTestUtils;
1617

18+
let act;
19+
1720
describe('ReactDOM', () => {
1821
beforeEach(() => {
1922
jest.resetModules();
2023
React = require('react');
2124
ReactDOM = require('react-dom');
25+
ReactDOMClient = require('react-dom/client');
2226
ReactDOMServer = require('react-dom/server');
2327
ReactTestUtils = require('react-dom/test-utils');
28+
29+
act = require('internal-test-utils').act;
2430
});
2531

26-
it('should bubble onSubmit', function () {
32+
it('should bubble onSubmit', async () => {
2733
const container = document.createElement('div');
2834

2935
let count = 0;
@@ -50,8 +56,11 @@ describe('ReactDOM', () => {
5056
}
5157

5258
document.body.appendChild(container);
59+
const root = ReactDOMClient.createRoot(container);
5360
try {
54-
ReactDOM.render(<Parent />, container);
61+
await act(() => {
62+
root.render(<Parent />);
63+
});
5564
buttonRef.click();
5665
expect(count).toBe(1);
5766
} finally {
@@ -228,7 +237,7 @@ describe('ReactDOM', () => {
228237
);
229238
});
230239

231-
it('preserves focus', () => {
240+
it('preserves focus', async () => {
232241
let input;
233242
let input2;
234243
class A extends React.Component {
@@ -255,8 +264,11 @@ describe('ReactDOM', () => {
255264
const log = [];
256265
const container = document.createElement('div');
257266
document.body.appendChild(container);
267+
const root = ReactDOMClient.createRoot(container);
258268
try {
259-
ReactDOM.render(<A showTwo={false} />, container);
269+
await act(() => {
270+
root.render(<A showTwo={false} />);
271+
});
260272
input.focus();
261273

262274
// When the second input is added, let's simulate losing focus, which is
@@ -277,7 +289,9 @@ describe('ReactDOM', () => {
277289
});
278290

279291
expect(document.activeElement.id).toBe('one');
280-
ReactDOM.render(<A showTwo={true} />, container);
292+
await act(() => {
293+
root.render(<A showTwo={true} />);
294+
});
281295
// input2 gets added, which causes input to get blurred. Then
282296
// componentDidUpdate focuses input2 and that should make it down to here,
283297
// not get overwritten by focus restoration.
@@ -288,7 +302,7 @@ describe('ReactDOM', () => {
288302
}
289303
});
290304

291-
it('calls focus() on autoFocus elements after they have been mounted to the DOM', () => {
305+
it('calls focus() on autoFocus elements after they have been mounted to the DOM', async () => {
292306
const originalFocus = HTMLElement.prototype.focus;
293307

294308
try {
@@ -305,14 +319,16 @@ describe('ReactDOM', () => {
305319

306320
const container = document.createElement('div');
307321
document.body.appendChild(container);
308-
ReactDOM.render(
309-
<div>
310-
<h1>Auto-focus Test</h1>
311-
<input autoFocus={true} />
312-
<p>The above input should be focused after mount.</p>
313-
</div>,
314-
container,
315-
);
322+
const root = ReactDOMClient.createRoot(container);
323+
await act(() => {
324+
root.render(
325+
<div>
326+
<h1>Auto-focus Test</h1>
327+
<input autoFocus={true} />
328+
<p>The above input should be focused after mount.</p>
329+
</div>,
330+
);
331+
});
316332

317333
expect(inputFocusedAfterMount).toBe(true);
318334
expect(focusedElement.tagName).toBe('INPUT');
@@ -321,7 +337,7 @@ describe('ReactDOM', () => {
321337
}
322338
});
323339

324-
it("shouldn't fire duplicate event handler while handling other nested dispatch", () => {
340+
it("shouldn't fire duplicate event handler while handling other nested dispatch", async () => {
325341
const actual = [];
326342

327343
class Wrapper extends React.Component {
@@ -352,8 +368,11 @@ describe('ReactDOM', () => {
352368

353369
const container = document.createElement('div');
354370
document.body.appendChild(container);
371+
const root = ReactDOMClient.createRoot(container);
355372
try {
356-
ReactDOM.render(<Wrapper />, container);
373+
await act(() => {
374+
root.render(<Wrapper />);
375+
});
357376

358377
const expected = [
359378
'1st node clicked',
@@ -366,7 +385,7 @@ describe('ReactDOM', () => {
366385
}
367386
});
368387

369-
it('should not crash with devtools installed', () => {
388+
it('should not crash with devtools installed', async () => {
370389
try {
371390
global.__REACT_DEVTOOLS_GLOBAL_HOOK__ = {
372391
inject: function () {},
@@ -382,14 +401,17 @@ describe('ReactDOM', () => {
382401
return <div />;
383402
}
384403
}
385-
ReactDOM.render(<Component />, document.createElement('container'));
404+
const root = ReactDOMClient.createRoot(document.createElement('div'));
405+
await act(() => {
406+
root.render(<Component />);
407+
});
386408
} finally {
387409
delete global.__REACT_DEVTOOLS_GLOBAL_HOOK__;
388410
}
389411
});
390412

391-
it('should not crash calling findDOMNode inside a function component', () => {
392-
const container = document.createElement('div');
413+
it('should not crash calling findDOMNode inside a function component', async () => {
414+
const root = ReactDOMClient.createRoot(document.createElement('div'));
393415

394416
class Component extends React.Component {
395417
render() {
@@ -404,11 +426,13 @@ describe('ReactDOM', () => {
404426
};
405427

406428
if (__DEV__) {
407-
ReactDOM.render(<App />, container);
429+
await act(() => {
430+
root.render(<App />);
431+
});
408432
}
409433
});
410434

411-
it('reports stacks with re-entrant renderToString() calls on the client', () => {
435+
it('reports stacks with re-entrant renderToString() calls on the client', async () => {
412436
function Child2(props) {
413437
return <span ariaTypo3="no">{props.children}</span>;
414438
}
@@ -441,8 +465,12 @@ describe('ReactDOM', () => {
441465
);
442466
}
443467

444-
const container = document.createElement('div');
445-
expect(() => ReactDOM.render(<App />, container)).toErrorDev([
468+
const root = ReactDOMClient.createRoot(document.createElement('div'));
469+
await expect(async () => {
470+
await act(() => {
471+
root.render(<App />);
472+
});
473+
}).toErrorDev([
446474
// ReactDOM(App > div > span)
447475
'Invalid ARIA attribute `ariaTypo`. ARIA attributes follow the pattern aria-* and must be lowercase.\n' +
448476
' in span (at **)\n' +

0 commit comments

Comments
 (0)