Skip to content

Commit 1b1d5d0

Browse files
committed
Add failing test for Suspense layout semantics
1 parent 1a106bd commit 1b1d5d0

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

packages/react-reconciler/src/__tests__/ReactSuspense-test.internal.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,53 @@ describe('ReactSuspense', () => {
663663
expect(root).toMatchRenderedOutput('new value');
664664
});
665665

666+
// @gate enableSuspenseLayoutEffectSemantics
667+
fit('re-fires layout effects when re-showing Suspense', () => {
668+
function TextWithLayout(props) {
669+
Scheduler.unstable_yieldValue(props.text);
670+
React.useLayoutEffect(() => {
671+
Scheduler.unstable_yieldValue('create layout');
672+
return () => {
673+
Scheduler.unstable_yieldValue('destroy layout');
674+
};
675+
}, []);
676+
return props.text;
677+
}
678+
679+
let _setShow;
680+
function App(props) {
681+
const [show, setShow] = React.useState(false);
682+
_setShow = setShow;
683+
return (
684+
<Suspense fallback={<Text text="Loading..." />}>
685+
<TextWithLayout text="Child 1" />
686+
{show && <AsyncText ms={1000} text="Child 2" />}
687+
</Suspense>
688+
);
689+
}
690+
691+
const root = ReactTestRenderer.create(<App />, {
692+
unstable_isConcurrent: true,
693+
});
694+
695+
expect(Scheduler).toFlushAndYield(['Child 1', 'create layout']);
696+
expect(root).toMatchRenderedOutput('Child 1');
697+
698+
ReactTestRenderer.act(() => {
699+
_setShow(true);
700+
});
701+
expect(Scheduler).toHaveYielded([
702+
'Child 1',
703+
'Suspend! [Child 2]',
704+
'Loading...',
705+
'destroy layout',
706+
]);
707+
jest.advanceTimersByTime(1000);
708+
expect(Scheduler).toHaveYielded(['Promise resolved [Child 2]']);
709+
expect(Scheduler).toFlushAndYield(['Child 1', 'Child 2', 'create layout']);
710+
expect(root).toMatchRenderedOutput(['Child 1', 'Child 2'].join(''));
711+
});
712+
666713
describe('outside concurrent mode', () => {
667714
it('a mounted class component can suspend without losing state', () => {
668715
class TextWithLifecycle extends React.Component {

0 commit comments

Comments
 (0)