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

Add a failing test for remounting fallback in sync mode #14078

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -631,5 +631,65 @@ describe('ReactSuspense', () => {
expect(root).toFlushAndYield(['Step: 3']);
expect(root).toMatchRenderedOutput('Step: 3');
});

it('does not remount the fallback while suspended children resolve in sync mode', () => {
let mounts = 0;
class ShouldMountOnce extends React.Component {
componentDidMount() {
mounts++;
}
render() {
return <Text text="Loading..." />;
}
}

function App(props) {
return (
<Suspense maxDuration={10} fallback={<ShouldMountOnce />}>
<AsyncText ms={1000} text="Child 1" />
<AsyncText ms={2000} text="Child 2" />
<AsyncText ms={3000} text="Child 3" />
</Suspense>
);
}

const root = ReactTestRenderer.create(<App />);

// Initial render
expect(ReactTestRenderer).toHaveYielded([
'Suspend! [Child 1]',
'Suspend! [Child 2]',
'Suspend! [Child 3]',
'Loading...',
]);
expect(root).toFlushAndYield([]);
jest.advanceTimersByTime(1000);
expect(ReactTestRenderer).toHaveYielded([
'Promise resolved [Child 1]',
'Child 1',
'Suspend! [Child 2]',
'Suspend! [Child 3]',
'Loading...',
]);
jest.advanceTimersByTime(1000);
expect(ReactTestRenderer).toHaveYielded([
'Promise resolved [Child 2]',
'Child 2',
'Suspend! [Child 3]',
'Loading...',
'Suspend! [Child 3]', // TODO: why does this suspend twice?
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was also weird

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's a known flaw with sync mode. Pings aren't batched, so we get a synchronous retry for every individual ping. Added a comment in the rebased version of your test:

// TODO: This suspends twice because there were two pings, once for each
// time Child 2 suspended. This is only an issue in sync mode because
// pings aren't batched.

'Loading...',
]);
jest.advanceTimersByTime(1000);
expect(ReactTestRenderer).toHaveYielded([
'Promise resolved [Child 3]',
'Child 3',
]);
expect(root).toMatchRenderedOutput(
['Child 1', 'Child 2', 'Child 3'].join(''),
);
// TODO: this fails. Is it by design?
expect(mounts).toBe(1);
});
});
});