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 ReactChildReconciler to createRoot #28192

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 48 additions & 18 deletions packages/react-dom/src/__tests__/ReactChildReconciler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
'use strict';

let React;
let ReactTestUtils;
let ReactDOMClient;
let act;

describe('ReactChildReconciler', () => {
beforeEach(() => {
jest.resetModules();

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

function createIterable(array) {
Expand Down Expand Up @@ -55,37 +57,47 @@ describe('ReactChildReconciler', () => {
return fn;
}

it('does not treat functions as iterables', () => {
let node;
it('does not treat functions as iterables', async () => {
const iterableFunction = makeIterableFunction('foo');

expect(() => {
node = ReactTestUtils.renderIntoDocument(
<div>
<h1>{iterableFunction}</h1>
</div>,
);
const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
await expect(async () => {
await act(() => {
root.render(
<div>
<h1>{iterableFunction}</h1>
</div>,
);
});
}).toErrorDev('Functions are not valid as a React child');
const node = container.firstChild;

expect(node.innerHTML).toContain(''); // h1
});

it('warns for duplicated array keys', () => {
it('warns for duplicated array keys', async () => {
class Component extends React.Component {
render() {
return <div>{[<div key="1" />, <div key="1" />]}</div>;
}
}

expect(() => ReactTestUtils.renderIntoDocument(<Component />)).toErrorDev(
const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
await expect(async () => {
await act(() => {
root.render(<Component />);
});
}).toErrorDev(
'Keys should be unique so that components maintain their identity ' +
'across updates. Non-unique keys may cause children to be ' +
'duplicated and/or omitted — the behavior is unsupported and ' +
'could change in a future version.',
);
});

it('warns for duplicated array keys with component stack info', () => {
it('warns for duplicated array keys with component stack info', async () => {
class Component extends React.Component {
render() {
return <div>{[<div key="1" />, <div key="1" />]}</div>;
Expand All @@ -104,7 +116,13 @@ describe('ReactChildReconciler', () => {
}
}

expect(() => ReactTestUtils.renderIntoDocument(<GrandParent />)).toErrorDev(
const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
await expect(async () => {
await act(() => {
root.render(<GrandParent />);
});
}).toErrorDev(
'Encountered two children with the same key, `1`. ' +
'Keys should be unique so that components maintain their identity ' +
'across updates. Non-unique keys may cause children to be ' +
Expand All @@ -117,22 +135,28 @@ describe('ReactChildReconciler', () => {
);
});

it('warns for duplicated iterable keys', () => {
it('warns for duplicated iterable keys', async () => {
class Component extends React.Component {
render() {
return <div>{createIterable([<div key="1" />, <div key="1" />])}</div>;
}
}

expect(() => ReactTestUtils.renderIntoDocument(<Component />)).toErrorDev(
const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
await expect(async () => {
await act(() => {
root.render(<Component />);
});
}).toErrorDev(
'Keys should be unique so that components maintain their identity ' +
'across updates. Non-unique keys may cause children to be ' +
'duplicated and/or omitted — the behavior is unsupported and ' +
'could change in a future version.',
);
});

it('warns for duplicated iterable keys with component stack info', () => {
it('warns for duplicated iterable keys with component stack info', async () => {
class Component extends React.Component {
render() {
return <div>{createIterable([<div key="1" />, <div key="1" />])}</div>;
Expand All @@ -151,7 +175,13 @@ describe('ReactChildReconciler', () => {
}
}

expect(() => ReactTestUtils.renderIntoDocument(<GrandParent />)).toErrorDev(
const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
await expect(async () => {
await act(() => {
root.render(<GrandParent />);
});
}).toErrorDev(
'Encountered two children with the same key, `1`. ' +
'Keys should be unique so that components maintain their identity ' +
'across updates. Non-unique keys may cause children to be ' +
Expand Down