Skip to content

Commit

Permalink
feat(usePreviousDistinct): add tests for undefined value behaviour;
Browse files Browse the repository at this point in the history
  • Loading branch information
xobotyi committed Nov 4, 2019
1 parent fbe9b13 commit cb373f9
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/__tests__/usePreviousDistinct.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ describe('usePreviousDistinct', () => {
expect(hook.result.current).toBe(2);
});

it('should work fine with `undefined` values', () => {
const hook = renderHook(({ value }) => usePreviousDistinct(value), {
initialProps: { value: undefined as undefined | number },
});

expect(hook.result.current).toBeUndefined();

hook.rerender({ value: 1 });
expect(hook.result.current).toBeUndefined();

hook.rerender({ value: undefined });
expect(hook.result.current).toBe(1);

hook.rerender({ value: 2 });
expect(hook.result.current).toBeUndefined();
});

it('should receive a predicate as a second parameter that will compare prev and current', () => {
const obj1 = { label: 'John', value: 'john' };
const obj2 = { label: 'Jonny', value: 'john' };
Expand Down

0 comments on commit cb373f9

Please sign in to comment.