From 51786ecfcaa94e921e88e2178c883e6b8f58b24e Mon Sep 17 00:00:00 2001 From: Rick Hanlon Date: Thu, 13 May 2021 13:22:16 -0400 Subject: [PATCH] Add test starting from hidden --- .../src/__tests__/ReactOffscreen-test.js | 56 ++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/packages/react-reconciler/src/__tests__/ReactOffscreen-test.js b/packages/react-reconciler/src/__tests__/ReactOffscreen-test.js index 1a3c5eb00af53..afc645ce37b8b 100644 --- a/packages/react-reconciler/src/__tests__/ReactOffscreen-test.js +++ b/packages/react-reconciler/src/__tests__/ReactOffscreen-test.js @@ -218,7 +218,7 @@ describe('ReactOffscreen', () => { // @gate experimental // @gate enableSuspenseLayoutEffectSemantics - it('mounts/unmounts layout effects when visibility changes', async () => { + it('mounts/unmounts layout effects when visibility changes (starting visible)', async () => { function Child({text}) { useLayoutEffect(() => { Scheduler.unstable_yieldValue('Mount layout'); @@ -265,4 +265,58 @@ describe('ReactOffscreen', () => { expect(Scheduler).toHaveYielded(['Child', 'Mount layout']); expect(root).toMatchRenderedOutput(); }); + + // @gate experimental + // @gate enableSuspenseLayoutEffectSemantics + it('mounts/unmounts layout effects when visibility changes (starting hidden)', async () => { + function Child({text}) { + useLayoutEffect(() => { + Scheduler.unstable_yieldValue('Mount layout'); + return () => { + Scheduler.unstable_yieldValue('Unmount layout'); + }; + }, []); + return ; + } + + const root = ReactNoop.createRoot(); + await ReactNoop.act(async () => { + // Start the tree hidden. The layout effect is not mounted. + root.render( + + + , + ); + }); + expect(Scheduler).toHaveYielded(['Child']); + // TODO: Offscreen does not yet hide/unhide children correctly. Until we do, + // it should only be used inside a host component wrapper whose visibility + // is toggled simultaneously. + expect(root).toMatchRenderedOutput(); + + // Show the tree. The layout effect is mounted. + await ReactNoop.act(async () => { + root.render( + + + , + ); + }); + expect(Scheduler).toHaveYielded(['Child', 'Mount layout']); + expect(root).toMatchRenderedOutput(); + + // Hide the tree again. The layout effect is un-mounted. + await ReactNoop.act(async () => { + root.render( + + + , + ); + }); + expect(Scheduler).toHaveYielded(['Unmount layout', 'Child']); + // TODO: Offscreen does not yet hide/unhide children correctly. Until we do, + // it should only be used inside a host component wrapper whose visibility + // is toggled simultaneously. + expect(root).toMatchRenderedOutput(); + }); });