Skip to content

Commit

Permalink
feat: write unit tests (#12276)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-deriv committed Dec 15, 2023
1 parent 7a170a0 commit a922a7a
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
108 changes: 108 additions & 0 deletions packages/wallets/src/hooks/__tests__/useInputDecimalFormatter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { act, renderHook } from '@testing-library/react-hooks';
import useInputDecimalFormatter from '../useInputDecimalFormatter';

describe('useInputDecimalFormatter', () => {
it('should update the input value correctly when onChange is called', () => {
const { result } = renderHook(() => useInputDecimalFormatter());

act(() => {
result.current.onChange({ target: { value: '123' } });
});

expect(result.current.value).toBe('123');
});

it('should handle fractional digits and sign options correctly', () => {
const { result } = renderHook(() => useInputDecimalFormatter(123, { fractionDigits: 2, withSign: true }));

act(() => {
result.current.onChange({ target: { value: '-789.12345' } });
});

expect(result.current.value).toBe('-789.12');
});

it('should round initial value to 2 digits when fractionDigits is undefined', () => {
const { result } = renderHook(() => useInputDecimalFormatter(123.456));

expect(result.current.value).toBe('123.45');
});

it('should return empty string when inital is undefined', () => {
const { result } = renderHook(() => useInputDecimalFormatter());

expect(result.current.value).toBe('');
});

it('should return empty string when an user clear the unput', () => {
const { result } = renderHook(() => useInputDecimalFormatter(10));

expect(result.current.value).toBe('10');

act(() => {
result.current.onChange({ target: { value: '' } });
});

expect(result.current.value).toBe('');
});

it('should return old value when an user add 2 dots', () => {
const { result } = renderHook(() => useInputDecimalFormatter(10.25));

expect(result.current.value).toBe('10.25');

act(() => {
result.current.onChange({ target: { value: '56.754.78' } });
});

expect(result.current.value).toBe('10.25');
});

it('should return value with sign after adding sign for integer number', () => {
const { result } = renderHook(() => useInputDecimalFormatter(1, { withSign: true }));

expect(result.current.value).toBe('1');

act(() => {
result.current.onChange({ target: { value: '-1' } });
});

expect(result.current.value).toBe('-1');
});

it('should return 0 if an user type 0', () => {
const { result } = renderHook(() => useInputDecimalFormatter());

expect(result.current.value).toBe('');

act(() => {
result.current.onChange({ target: { value: '0' } });
});

expect(result.current.value).toBe('0');
});

it('should return previous value if an user type char', () => {
const { result } = renderHook(() => useInputDecimalFormatter(10));

expect(result.current.value).toBe('10');

act(() => {
result.current.onChange({ target: { value: 'test' } });
});

expect(result.current.value).toBe('10');
});

it('should return previous value if an user type integer part like this pattern 0*', () => {
const { result } = renderHook(() => useInputDecimalFormatter(10));

expect(result.current.value).toBe('10');

act(() => {
result.current.onChange({ target: { value: '03' } });
});

expect(result.current.value).toBe('10');
});
});
2 changes: 1 addition & 1 deletion packages/wallets/src/hooks/useInputDecimalFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const useInputDecimalFormatter = (initial?: number, options?: TOptions) => {

// The field value is positive or negative sign, So we return the new value without
// any calculations.
if ((left === '-' || left === '+') && !hasRight) return newValue;
if ((text[0] === '-' || text[0] === '+') && !hasRight) return newValue;

// The field value is 0, So we return the new value without any calculations.
if (left === '0' && !hasRight) return newValue;
Expand Down

0 comments on commit a922a7a

Please sign in to comment.