Skip to content

Commit

Permalink
Merge pull request #48 from OrLavy/feature/respectUseNumberLimits
Browse files Browse the repository at this point in the history
Makes 'useNumber' respect lower and upper limits
  • Loading branch information
kitze authored Feb 3, 2020
2 parents 7ecad74 + 189066e commit 37f2628
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 13 deletions.
56 changes: 56 additions & 0 deletions src/array/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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 }));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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 }));
Expand All @@ -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
Expand Down Expand Up @@ -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);
});

Expand Down
25 changes: 13 additions & 12 deletions src/array/useNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
},
Expand All @@ -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;
});
},
Expand Down
58 changes: 57 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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 }));
Expand All @@ -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;
Expand All @@ -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));
Expand All @@ -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 }));
Expand All @@ -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
Expand Down Expand Up @@ -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);
});

Expand Down

0 comments on commit 37f2628

Please sign in to comment.