From a922a7a865e44885353a25490b0b05e68b7b8e34 Mon Sep 17 00:00:00 2001 From: Sergei Baranovski <120570511+sergei-deriv@users.noreply.github.com> Date: Fri, 15 Dec 2023 05:37:21 +0300 Subject: [PATCH] feat: write unit tests (#12276) --- .../useInputDecimalFormatter.spec.ts | 108 ++++++++++++++++++ .../src/hooks/useInputDecimalFormatter.ts | 2 +- 2 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 packages/wallets/src/hooks/__tests__/useInputDecimalFormatter.spec.ts diff --git a/packages/wallets/src/hooks/__tests__/useInputDecimalFormatter.spec.ts b/packages/wallets/src/hooks/__tests__/useInputDecimalFormatter.spec.ts new file mode 100644 index 000000000000..dcf0347609e8 --- /dev/null +++ b/packages/wallets/src/hooks/__tests__/useInputDecimalFormatter.spec.ts @@ -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'); + }); +}); diff --git a/packages/wallets/src/hooks/useInputDecimalFormatter.ts b/packages/wallets/src/hooks/useInputDecimalFormatter.ts index 87b5195c2f71..f5700c2f11a1 100644 --- a/packages/wallets/src/hooks/useInputDecimalFormatter.ts +++ b/packages/wallets/src/hooks/useInputDecimalFormatter.ts @@ -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;