Skip to content

Commit

Permalink
Bug: Effect clean up when deleting suspended tree
Browse files Browse the repository at this point in the history
Adds a failing unit test.
  • Loading branch information
acdlite committed Sep 2, 2020
1 parent 99cae88 commit a24569d
Showing 1 changed file with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3956,4 +3956,53 @@ describe('ReactSuspenseWithNoopRenderer', () => {
</>,
);
});

it('should fire effect clean-up when deleting suspended tree', async () => {
const {useEffect} = React;

function App({show}) {
return (
<Suspense fallback={<Text text="Loading..." />}>
<Child />
{show && <AsyncText text="Async" />}
</Suspense>
);
}

function Child() {
useEffect(() => {
Scheduler.unstable_yieldValue('Mount Child');
return () => {
Scheduler.unstable_yieldValue('Unmount Child');
};
}, []);
return <span prop="Child" />;
}

const root = ReactNoop.createRoot();

await ReactNoop.act(async () => {
root.render(<App show={false} />);
});
expect(Scheduler).toHaveYielded(['Mount Child']);
expect(root).toMatchRenderedOutput(<span prop="Child" />);

await ReactNoop.act(async () => {
root.render(<App show={true} />);
});
// TODO: `act` should have flushed the placeholder
jest.runAllTimers();
expect(Scheduler).toHaveYielded(['Suspend! [Async]', 'Loading...']);
expect(root).toMatchRenderedOutput(
<>
<span hidden={true} prop="Child" />
<span prop="Loading..." />
</>,
);

await ReactNoop.act(async () => {
root.render(null);
});
expect(Scheduler).toHaveYielded(['Unmount Child']);
});
});

0 comments on commit a24569d

Please sign in to comment.