Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert renderSubtreeIntoContainer-test.js to createRoot #28114

Merged
merged 3 commits into from
Jan 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 48 additions & 17 deletions packages/react-dom/src/__tests__/renderSubtreeIntoContainer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

const React = require('react');
const PropTypes = require('prop-types');
const ReactDOM = require('react-dom');
const ReactDOMClient = require('react-dom/client');
const ReactTestUtils = require('react-dom/test-utils');
const act = require('internal-test-utils').act;
const renderSubtreeIntoContainer =
require('react-dom').unstable_renderSubtreeIntoContainer;

Expand Down Expand Up @@ -101,7 +102,7 @@ describe('renderSubtreeIntoContainer', () => {
});

// @gate !disableLegacyContext
it('should update context if it changes due to setState', () => {
it('should update context if it changes due to setState', async () => {
const container = document.createElement('div');
document.body.appendChild(container);
const portal = document.createElement('div');
Expand Down Expand Up @@ -154,15 +155,22 @@ describe('renderSubtreeIntoContainer', () => {
);
}
}
const root = ReactDOMClient.createRoot(container);
const parentRef = React.createRef();
await act(async () => {
root.render(<Parent ref={parentRef} />);
});
const instance = parentRef.current;

const instance = ReactDOM.render(<Parent />, container);
expect(portal.firstChild.innerHTML).toBe('initial-initial');
instance.setState({bar: 'changed'});
await act(async () => {
instance.setState({bar: 'changed'});
});
expect(portal.firstChild.innerHTML).toBe('changed-changed');
});

// @gate !disableLegacyContext
it('should update context if it changes due to re-render', () => {
it('should update context if it changes due to re-render', async () => {
const container = document.createElement('div');
document.body.appendChild(container);
const portal = document.createElement('div');
Expand Down Expand Up @@ -212,13 +220,18 @@ describe('renderSubtreeIntoContainer', () => {
}
}

ReactDOM.render(<Parent bar="initial" />, container);
const root = ReactDOMClient.createRoot(container);
await act(async () => {
root.render(<Parent bar="initial" />);
});
expect(portal.firstChild.innerHTML).toBe('initial-initial');
ReactDOM.render(<Parent bar="changed" />, container);
await act(async () => {
root.render(<Parent bar="changed" />);
});
expect(portal.firstChild.innerHTML).toBe('changed-changed');
});

it('should render portal with non-context-provider parent', () => {
it('should render portal with non-context-provider parent', async () => {
const container = document.createElement('div');
document.body.appendChild(container);
const portal = document.createElement('div');
Expand All @@ -237,12 +250,15 @@ describe('renderSubtreeIntoContainer', () => {
}
}

ReactDOM.render(<Parent bar="initial" />, container);
const root = ReactDOMClient.createRoot(container);
await act(async () => {
root.render(<Parent bar="initial" />);
});
expect(portal.firstChild.innerHTML).toBe('hello');
});

// @gate !disableLegacyContext
it('should get context through non-context-provider parent', () => {
it('should get context through non-context-provider parent', async () => {
const container = document.createElement('div');
document.body.appendChild(container);
const portal = document.createElement('div');
Expand Down Expand Up @@ -281,12 +297,15 @@ describe('renderSubtreeIntoContainer', () => {
}
}

ReactDOM.render(<Parent value="foo" />, container);
const root = ReactDOMClient.createRoot(container);
await act(async () => {
root.render(<Parent value="foo" />);
});
expect(portal.textContent).toBe('foo');
});

// @gate !disableLegacyContext
it('should get context through middle non-context-provider layer', () => {
it('should get context through middle non-context-provider layer', async () => {
const container = document.createElement('div');
document.body.appendChild(container);
const portal1 = document.createElement('div');
Expand Down Expand Up @@ -333,21 +352,33 @@ describe('renderSubtreeIntoContainer', () => {
}
}

ReactDOM.render(<Parent value="foo" />, container);
const root = ReactDOMClient.createRoot(container);
await act(async () => {
root.render(<Parent value="foo" />);
});
expect(portal2.textContent).toBe('foo');
});

it('fails gracefully when mixing React 15 and 16', () => {
it('fails gracefully when mixing React 15 and 16', async () => {
class C extends React.Component {
render() {
return <div />;
}
}
const c = ReactDOM.render(<C />, document.createElement('div'));

const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
const componentRef = React.createRef();
await act(async () => {
root.render(<C ref={componentRef} />);
});
// React 15 calls this:
// https://github.com/facebook/react/blob/77b71fc3c4/src/renderers/dom/client/ReactMount.js#L478-L479
expect(() => {
c._reactInternalInstance._processChildContext({});
const component = componentRef.current;
mattcarrollcode marked this conversation as resolved.
Show resolved Hide resolved
await expect(async () => {
act(async () => {
component._reactInternalInstance._processChildContext({});
});
}).toThrow(
__DEV__
? '_processChildContext is not available in React 16+. This likely ' +
Expand Down