Skip to content

Commit

Permalink
Convert ReactChildReconciler to createRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Silbermann committed Feb 1, 2024
1 parent d29f7d9 commit 2509a43
Showing 1 changed file with 48 additions and 18 deletions.
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

0 comments on commit 2509a43

Please sign in to comment.