From cb373f951fb3f34b9e54793687de14000a2dc08e Mon Sep 17 00:00:00 2001 From: xobotyi Date: Mon, 4 Nov 2019 11:10:37 +0300 Subject: [PATCH] feat(usePreviousDistinct): add tests for undefined value behaviour; --- src/__tests__/usePreviousDistinct.test.tsx | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/__tests__/usePreviousDistinct.test.tsx b/src/__tests__/usePreviousDistinct.test.tsx index 8bb7c04016..1a24d7b4c1 100644 --- a/src/__tests__/usePreviousDistinct.test.tsx +++ b/src/__tests__/usePreviousDistinct.test.tsx @@ -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' };