Skip to content

Commit bf32989

Browse files
authored
Convert ReactMountDestruction (partially) to createRoot (#28004)
1 parent 2f803b4 commit bf32989

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ describe('ReactDOMRoot', () => {
133133
expect(container.textContent).toEqual('');
134134
});
135135

136+
it('can be immediately unmounted', async () => {
137+
const root = ReactDOMClient.createRoot(container);
138+
await act(() => {
139+
root.unmount();
140+
});
141+
});
142+
136143
it('supports hydration', async () => {
137144
const markup = await new Promise(resolve =>
138145
resolve(
@@ -392,6 +399,18 @@ describe('ReactDOMRoot', () => {
392399
}
393400
});
394401

402+
it('throws if unmounting a root that has had its contents removed', async () => {
403+
const root = ReactDOMClient.createRoot(container);
404+
await act(() => {
405+
root.render(<div>Hi</div>);
406+
});
407+
container.innerHTML = '';
408+
409+
expect(() => {
410+
root.unmount();
411+
}).toThrow('The node to be removed is not a child of this node.');
412+
});
413+
395414
it('opts-in to concurrent default updates', async () => {
396415
const root = ReactDOMClient.createRoot(container, {
397416
unstable_concurrentUpdatesByDefault: true,

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,42 @@
1111

1212
const React = require('react');
1313
const ReactDOM = require('react-dom');
14+
const ReactDOMClient = require('react-dom/client');
15+
const act = require('internal-test-utils').act;
1416

1517
describe('ReactMount', () => {
16-
it('should destroy a react root upon request', () => {
18+
it('should destroy a react root upon request', async () => {
1719
const mainContainerDiv = document.createElement('div');
1820
document.body.appendChild(mainContainerDiv);
1921

2022
const instanceOne = <div className="firstReactDiv" />;
2123
const firstRootDiv = document.createElement('div');
2224
mainContainerDiv.appendChild(firstRootDiv);
23-
ReactDOM.render(instanceOne, firstRootDiv);
25+
const firstRoot = ReactDOMClient.createRoot(firstRootDiv);
26+
await act(() => {
27+
firstRoot.render(instanceOne);
28+
});
2429

2530
const instanceTwo = <div className="secondReactDiv" />;
2631
const secondRootDiv = document.createElement('div');
2732
mainContainerDiv.appendChild(secondRootDiv);
28-
ReactDOM.render(instanceTwo, secondRootDiv);
33+
const secondRoot = ReactDOMClient.createRoot(secondRootDiv);
34+
await act(() => {
35+
secondRoot.render(instanceTwo);
36+
});
2937

3038
// Test that two react roots are rendered in isolation
3139
expect(firstRootDiv.firstChild.className).toBe('firstReactDiv');
3240
expect(secondRootDiv.firstChild.className).toBe('secondReactDiv');
3341

3442
// Test that after unmounting each, they are no longer in the document.
35-
ReactDOM.unmountComponentAtNode(firstRootDiv);
43+
await act(() => {
44+
firstRoot.unmount();
45+
});
3646
expect(firstRootDiv.firstChild).toBeNull();
37-
ReactDOM.unmountComponentAtNode(secondRootDiv);
38-
expect(secondRootDiv.firstChild).toBeNull();
47+
await act(() => {
48+
secondRoot.unmount();
49+
});
3950
});
4051

4152
it('should warn when unmounting a non-container root node', () => {
@@ -46,6 +57,7 @@ describe('ReactMount', () => {
4657
<div />
4758
</div>
4859
);
60+
// Cannot be migrated to createRoot until we remove unmountComponentAtNode i.e. remove this test.
4961
ReactDOM.render(component, mainContainerDiv);
5062

5163
// Test that unmounting at a root node gives a helpful warning
@@ -69,6 +81,7 @@ describe('ReactMount', () => {
6981
</div>
7082
</div>
7183
);
84+
// Cannot be migrated to createRoot until we remove unmountComponentAtNode i.e. remove this test.
7285
ReactDOM.render(component, mainContainerDiv);
7386

7487
// Test that unmounting at a non-root node gives a different warning

0 commit comments

Comments
 (0)