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 ReactDOMFiberAsync to createRoot #28067

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Changes from all commits
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
58 changes: 42 additions & 16 deletions packages/react-dom/src/__tests__/ReactDOMFiberAsync-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('ReactDOMFiberAsync', () => {
document.body.removeChild(container);
});

it('renders synchronously by default', () => {
it('renders synchronously by default in legacy mode', () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably need to keep this, so added "in legacy mode" to test description.

const ops = [];
ReactDOM.render(<div>Hi</div>, container, () => {
ops.push(container.textContent);
Expand All @@ -61,12 +61,16 @@ describe('ReactDOMFiberAsync', () => {
expect(ops).toEqual(['Hi', 'Bye']);
});

it('flushSync batches sync updates and flushes them at the end of the batch', () => {
it('flushSync batches sync updates and flushes them at the end of the batch', async () => {
const ops = [];
let instance;

class Component extends React.Component {
state = {text: ''};
componentDidMount() {
instance = this;
}

push(val) {
this.setState(state => ({text: state.text + val}));
}
Expand All @@ -79,9 +83,13 @@ describe('ReactDOMFiberAsync', () => {
}
}

ReactDOM.render(<Component />, container);
const root = ReactDOMClient.createRoot(container);
await act(() => root.render(<Component />));

await act(() => {
instance.push('A');
});

instance.push('A');
expect(ops).toEqual(['A']);
expect(container.textContent).toEqual('A');

Expand All @@ -92,19 +100,26 @@ describe('ReactDOMFiberAsync', () => {
expect(container.textContent).toEqual('A');
expect(ops).toEqual(['A']);
});

expect(container.textContent).toEqual('ABC');
expect(ops).toEqual(['A', 'ABC']);
instance.push('D');
await act(() => {
instance.push('D');
});
expect(container.textContent).toEqual('ABCD');
expect(ops).toEqual(['A', 'ABC', 'ABCD']);
});

it('flushSync flushes updates even if nested inside another flushSync', () => {
it('flushSync flushes updates even if nested inside another flushSync', async () => {
const ops = [];
let instance;

class Component extends React.Component {
state = {text: ''};
componentDidMount() {
instance = this;
}

push(val) {
this.setState(state => ({text: state.text + val}));
}
Expand All @@ -117,9 +132,12 @@ describe('ReactDOMFiberAsync', () => {
}
}

ReactDOM.render(<Component />, container);
const root = ReactDOMClient.createRoot(container);
await act(() => root.render(<Component />));

instance.push('A');
await act(() => {
instance.push('A');
});
expect(ops).toEqual(['A']);
expect(container.textContent).toEqual('A');

Expand All @@ -141,7 +159,7 @@ describe('ReactDOMFiberAsync', () => {
expect(ops).toEqual(['A', 'ABCD']);
});

it('flushSync logs an error if already performing work', () => {
it('flushSync logs an error if already performing work', async () => {
class Component extends React.Component {
componentDidUpdate() {
ReactDOM.flushSync();
Expand All @@ -152,11 +170,16 @@ describe('ReactDOMFiberAsync', () => {
}

// Initial mount
ReactDOM.render(<Component />, container);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<Component />);
});
// Update
expect(() => ReactDOM.render(<Component />, container)).toErrorDev(
'flushSync was called from inside a lifecycle method',
);
expect(() => {
ReactDOM.flushSync(() => {
root.render(<Component />);
});
}).toErrorDev('flushSync was called from inside a lifecycle method');
});

describe('concurrent mode', () => {
Expand Down Expand Up @@ -492,11 +515,14 @@ describe('ReactDOMFiberAsync', () => {
const containerA = document.createElement('div');
const containerB = document.createElement('div');
const containerC = document.createElement('div');
const rootA = ReactDOMClient.createRoot(containerA);
const rootB = ReactDOMClient.createRoot(containerB);
const rootC = ReactDOMClient.createRoot(containerC);

await act(() => {
ReactDOM.render(<App label="A" />, containerA);
ReactDOM.render(<App label="B" />, containerB);
ReactDOM.render(<App label="C" />, containerC);
rootA.render(<App label="A" />);
rootB.render(<App label="B" />);
rootC.render(<App label="C" />);
});

expect(containerA.textContent).toEqual('Finished');
Expand Down