Skip to content

Commit

Permalink
chore(useSetState): check useSetState for memoized callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Swavely committed Aug 23, 2019
1 parent 16f023f commit c60ee96
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/__tests__/useSetState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,21 @@ it('should merge changes into current state when providing function', () => {

expect(result.current[0]).toEqual({ foo: 'bar', count: 2, someBool: true });
});

/**
* Enforces cases where a hook can safely depend on the callback without
* causing an endless rerender cycle: useEffect(() => setState({ data }), [setState]);
*/
it('should return a memoized setState callback', () => {
const { result, rerender } = setUp({ ok: false });
const [, setState1] = result.current;

act(() => {
setState1({ ok: true });
});
rerender();

const [, setState2] = result.current;

expect(setState1).toBe(setState2);
});

0 comments on commit c60ee96

Please sign in to comment.