Skip to content

Commit

Permalink
Add failing test for passive effects cleanup functio not being called…
Browse files Browse the repository at this point in the history
… for memoized components
  • Loading branch information
Brian Vaughn committed Sep 2, 2020
1 parent 2cfd73c commit d7b6f87
Showing 1 changed file with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2734,6 +2734,75 @@ describe('ReactHooksWithNoopRenderer', () => {
expect(ReactNoop.getChildren()).toEqual([]);
});
});

it('calls passive effect destroy functions for memoized components', () => {
const Wrapper = ({children}) => children;
function Child({prop}) {
React.useEffect(() => {
Scheduler.unstable_yieldValue('passive create');
return () => {
Scheduler.unstable_yieldValue('passive destroy');
};
}, [prop]);
React.useLayoutEffect(() => {
Scheduler.unstable_yieldValue('layout create');
return () => {
Scheduler.unstable_yieldValue('layout destroy');
};
}, [prop]);
Scheduler.unstable_yieldValue('render');
return null;
}

const isEqual = (prevProps, nextProps) =>
prevProps.prop === nextProps.prop;
const MemoizedChild = React.memo(Child, isEqual);

act(() => {
ReactNoop.render(
<Wrapper>
<MemoizedChild key={1} />
</Wrapper>,
);
});
expect(Scheduler).toHaveYielded([
'render',
'layout create',
'passive create',
]);

act(() => {
ReactNoop.render(
<Wrapper>
<MemoizedChild key={1} />
</Wrapper>,
);
});
expect(Scheduler).toHaveYielded([]);

// This update is exists to test an internal implementation detail:
// Effects without updating dependencies lose their layout/passive tag during an update.
act(() => {
ReactNoop.render(
<Wrapper>
<MemoizedChild key={2} />
</Wrapper>,
);
});
expect(Scheduler).toHaveYielded([
'render',
'layout destroy',
'layout create',
'passive destroy',
'passive create',
]);

// Unmount the component and verify that passive destroy functions are deferred until post-commit.
act(() => {
ReactNoop.render(null);
});
expect(Scheduler).toHaveYielded(['layout destroy', 'passive destroy']);
});
});

describe('useLayoutEffect', () => {
Expand Down

0 comments on commit d7b6f87

Please sign in to comment.