Skip to content

Commit

Permalink
Convert ReactMountDestruction (partially) to createRoot (#28004)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored Jan 23, 2024
1 parent 2f803b4 commit bf32989
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
19 changes: 19 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMRoot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ describe('ReactDOMRoot', () => {
expect(container.textContent).toEqual('');
});

it('can be immediately unmounted', async () => {
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.unmount();
});
});

it('supports hydration', async () => {
const markup = await new Promise(resolve =>
resolve(
Expand Down Expand Up @@ -392,6 +399,18 @@ describe('ReactDOMRoot', () => {
}
});

it('throws if unmounting a root that has had its contents removed', async () => {
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<div>Hi</div>);
});
container.innerHTML = '';

expect(() => {
root.unmount();
}).toThrow('The node to be removed is not a child of this node.');
});

it('opts-in to concurrent default updates', async () => {
const root = ReactDOMClient.createRoot(container, {
unstable_concurrentUpdatesByDefault: true,
Expand Down
25 changes: 19 additions & 6 deletions packages/react-dom/src/__tests__/ReactMountDestruction-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,42 @@

const React = require('react');
const ReactDOM = require('react-dom');
const ReactDOMClient = require('react-dom/client');
const act = require('internal-test-utils').act;

describe('ReactMount', () => {
it('should destroy a react root upon request', () => {
it('should destroy a react root upon request', async () => {
const mainContainerDiv = document.createElement('div');
document.body.appendChild(mainContainerDiv);

const instanceOne = <div className="firstReactDiv" />;
const firstRootDiv = document.createElement('div');
mainContainerDiv.appendChild(firstRootDiv);
ReactDOM.render(instanceOne, firstRootDiv);
const firstRoot = ReactDOMClient.createRoot(firstRootDiv);
await act(() => {
firstRoot.render(instanceOne);
});

const instanceTwo = <div className="secondReactDiv" />;
const secondRootDiv = document.createElement('div');
mainContainerDiv.appendChild(secondRootDiv);
ReactDOM.render(instanceTwo, secondRootDiv);
const secondRoot = ReactDOMClient.createRoot(secondRootDiv);
await act(() => {
secondRoot.render(instanceTwo);
});

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

// Test that after unmounting each, they are no longer in the document.
ReactDOM.unmountComponentAtNode(firstRootDiv);
await act(() => {
firstRoot.unmount();
});
expect(firstRootDiv.firstChild).toBeNull();
ReactDOM.unmountComponentAtNode(secondRootDiv);
expect(secondRootDiv.firstChild).toBeNull();
await act(() => {
secondRoot.unmount();
});
});

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

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

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

0 comments on commit bf32989

Please sign in to comment.