diff --git a/src/array/index.test.ts b/src/array/index.test.ts index 7574477..ffeebf8 100644 --- a/src/array/index.test.ts +++ b/src/array/index.test.ts @@ -19,6 +19,15 @@ describe('useNumber array', () => { // then expect(result.current[0]).toBe(10); }); + it('should increase value with concrete value with respect to upperLimit', () => { + // given + const { result } = renderHook(() => useNumber(5, { upperLimit: 10 })); + const [, actions] = result.current; + // when + act(() => actions.increase(10)); + // then + expect(result.current[0]).toBe(10); + }); it('should increase value by default step', () => { // given const { result } = renderHook(() => useNumber(5)); @@ -28,6 +37,15 @@ describe('useNumber array', () => { // then expect(result.current[0]).toBe(6); }); + it('should increase value by default step with respect to upperLimit', () => { + // given + const { result } = renderHook(() => useNumber(5, { upperLimit: 5 })); + const [, actions] = result.current; + // when + act(() => actions.increase()); + // then + expect(result.current[0]).toBe(5); + }); it('should increase value by predefined step', () => { // given const { result } = renderHook(() => useNumber(5, { step: 3 })); @@ -37,6 +55,15 @@ describe('useNumber array', () => { // then expect(result.current[0]).toBe(8); }); + it('should increase value by predefined step with respect to upperLimit', () => { + // given + const { result } = renderHook(() => useNumber(5, { step: 3, upperLimit: 7 })); + const [, actions] = result.current; + // when + act(() => actions.increase()); + // then + expect(result.current[0]).toBe(7); + }); it('should decrease value with concrete value', () => { // given const { result } = renderHook(() => useNumber(5)); @@ -46,6 +73,15 @@ describe('useNumber array', () => { // then expect(result.current[0]).toBe(0); }); + it('should decrease value with concrete value with respect to lowerLimit', () => { + // given + const { result } = renderHook(() => useNumber(5, { lowerLimit: 0 })); + const [, actions] = result.current; + // when + act(() => actions.decrease(10)); + // then + expect(result.current[0]).toBe(0); + }); it('should decrease value by default step', () => { // given const { result } = renderHook(() => useNumber(5)); @@ -55,6 +91,15 @@ describe('useNumber array', () => { // then expect(result.current[0]).toBe(4); }); + it('should decrease value by default step with respect to lowerLimit', () => { + // given + const { result } = renderHook(() => useNumber(5, { lowerLimit: 5})); + const [, actions] = result.current; + // when + act(() => actions.decrease()); + // then + expect(result.current[0]).toBe(5); + }); it('should decrease value by predefined step', () => { // given const { result } = renderHook(() => useNumber(5, { step: 3 })); @@ -64,6 +109,16 @@ describe('useNumber array', () => { // then expect(result.current[0]).toBe(2); }); + it('should decrease value by predefined step with respect to lowerLimit', () => { + // given + const { result } = renderHook(() => useNumber(5, { step: 3, lowerLimit: 3 })); + const [, actions] = result.current; + // when + act(() => actions.decrease()); + // then + expect(result.current[0]).toBe(3); + }); + describe('hooks optimizations', () => { it('should keep actions reference equality after value change', () => { // given @@ -226,6 +281,7 @@ describe('useArray array', () => { const modifiedElement = result.current[0].find( (element: { id: number; foo: boolean }) => element.id === 2, ); + expect(modifiedElement.foo).toBe(true); }); diff --git a/src/array/useNumber.ts b/src/array/useNumber.ts index bc6ef08..c17ca2c 100644 --- a/src/array/useNumber.ts +++ b/src/array/useNumber.ts @@ -29,14 +29,15 @@ export function useNumber( const nextValue = aValue - decreaseBy; if (lowerLimit !== undefined) { - if (nextValue + decreaseBy > lowerLimit) { - return nextValue; - } - if (loop && upperLimit) { - return upperLimit; + if (nextValue < lowerLimit) { + if (loop && upperLimit) { + return upperLimit; + } + + return lowerLimit; } - return aValue; } + return nextValue; }); }, @@ -49,14 +50,14 @@ export function useNumber( const nextValue = aValue + increaseBy; if (upperLimit !== undefined) { - if (nextValue - increaseBy < upperLimit) { - return nextValue; - } - if (loop) { - return initial; + if (nextValue > upperLimit) { + if (loop) { + return initial; + } + return upperLimit; } - return aValue; } + return nextValue; }); }, diff --git a/src/index.test.ts b/src/index.test.ts index 03719c7..4d71786 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -30,6 +30,15 @@ describe('useNumber', () => { // then expect(result.current.value).toBe(10); }); + it('should increase value with concrete value with respect to upperLimit', () => { + // given + const { result } = renderHook(() => useNumber(5, { upperLimit: 10 })); + const { increase } = result.current; + // when + act(() => increase(10)); + // then + expect(result.current.value).toBe(10); + }); it('should increase value by default step', () => { // given const { result } = renderHook(() => useNumber(5)); @@ -39,6 +48,15 @@ describe('useNumber', () => { // then expect(result.current.value).toBe(6); }); + it('should increase value by default step with respect to upperLimit', () => { + // given + const { result } = renderHook(() => useNumber(5, { upperLimit: 5 })); + const { increase } = result.current; + // when + act(() => increase()); + // then + expect(result.current.value).toBe(5); + }); it('should increase value by predefined step', () => { // given const { result } = renderHook(() => useNumber(5, { step: 3 })); @@ -48,7 +66,16 @@ describe('useNumber', () => { // then expect(result.current.value).toBe(8); }); - it('should decrease value with concrete value', () => { + it('should increase value by predefined step with respect to upperLimit', () => { + // given + const { result } = renderHook(() => useNumber(5, { step: 3, upperLimit: 7 })); + const { increase } = result.current; + // when + act(() => increase()); + // then + expect(result.current.value).toBe(7); + }); + it('should decrease value with concrete value with', () => { // given const { result } = renderHook(() => useNumber(5)); const { decrease } = result.current; @@ -57,6 +84,15 @@ describe('useNumber', () => { // then expect(result.current.value).toBe(0); }); + it('should decrease value with concrete value with respect to lowerLimit', () => { + // given + const { result } = renderHook(() => useNumber(5, { lowerLimit: 0 })); + const { decrease } = result.current; + // when + act(() => decrease(10)); + // then + expect(result.current.value).toBe(0); + }); it('should decrease value by default step', () => { // given const { result } = renderHook(() => useNumber(5)); @@ -66,6 +102,15 @@ describe('useNumber', () => { // then expect(result.current.value).toBe(4); }); + it('should decrease value by default step with respect to lowerLimit', () => { + // given + const { result } = renderHook(() => useNumber(5, { lowerLimit: 5 })); + const { decrease } = result.current; + // when + act(() => decrease()); + // then + expect(result.current.value).toBe(5); + }); it('should decrease value by predefined step', () => { // given const { result } = renderHook(() => useNumber(5, { step: 3 })); @@ -75,6 +120,16 @@ describe('useNumber', () => { // then expect(result.current.value).toBe(2); }); + it('should decrease value by predefined step with respect to lowerLimit', () => { + // given + const { result } = renderHook(() => useNumber(5, { step: 3, lowerLimit: 3 })); + const { decrease } = result.current; + // when + act(() => decrease()); + // then + expect(result.current.value).toBe(3); + }); + describe('hooks optimizations', () => { it('should keep actions reference equality after value change', () => { // given @@ -202,6 +257,7 @@ describe('useArray', () => { const modifiedElement = result.current.value.find( (element: { id: number; foo: boolean }) => element.id === 2, ); + expect(modifiedElement.foo).toBe(true); });