From e3b36ce6de043e622715f73bfcd439e90efd0998 Mon Sep 17 00:00:00 2001 From: theborakompanioni Date: Mon, 8 Jan 2024 17:53:44 +0100 Subject: [PATCH] test: add tests for BitcoinAmountInput --- src/components/BitcoinAmountInput.test.tsx | 98 ++++++++++++++++++++++ src/components/BitcoinAmountInput.tsx | 27 +++--- 2 files changed, 113 insertions(+), 12 deletions(-) create mode 100644 src/components/BitcoinAmountInput.test.tsx diff --git a/src/components/BitcoinAmountInput.test.tsx b/src/components/BitcoinAmountInput.test.tsx new file mode 100644 index 000000000..aab58e3ae --- /dev/null +++ b/src/components/BitcoinAmountInput.test.tsx @@ -0,0 +1,98 @@ +import user from '@testing-library/user-event' +import { render, screen } from '../testUtils' + +import { noop } from '../utils' + +import BitcoinAmountInput, { AmountValue, BitcoinAmountInputProps } from './BitcoinAmountInput' +import { Formik } from 'formik' + +describe('', () => { + const setup = (props: Omit) => { + render( + + {(form) => { + const valueField = form.getFieldProps('value') + return + }} + , + ) + } + + it('should render without errors', () => { + setup({ + label: 'test-label', + }) + const inputElement = screen.getByLabelText('test-label') + + expect(inputElement).toBeVisible() + expect(inputElement.dataset.value).toBe(undefined) + expect(inputElement.dataset.displayUnit).toBe(undefined) + expect(inputElement.dataset.displayValue).toBe(undefined) + }) + + it('amount can be entered in sats', async () => { + setup({ + label: 'test-label', + }) + + const inputElement = screen.getByLabelText('test-label') + + await user.type(inputElement, '123456') + + expect(inputElement).toHaveFocus() + expect(inputElement.dataset.value).toBe(`123456`) + expect(inputElement.dataset.displayUnit).toBe(`sats`) + expect(inputElement.dataset.displayValue).toBe(`123456`) + + // changes to display unit to BTC after element loses focus + await user.tab() + + expect(inputElement).not.toHaveFocus() + expect(inputElement.dataset.value).toBe(`123456`) + expect(inputElement.dataset.displayUnit).toBe(`BTC`) + expect(inputElement.dataset.displayValue).toBe(`0.00 123 456`) + }) + + it('amount can be entered in BTC', async () => { + setup({ + label: 'test-label', + }) + const inputElement = screen.getByLabelText('test-label') + + await user.type(inputElement, '1.234') + + expect(inputElement).toHaveFocus() + expect(inputElement.dataset.value).toBe(`123400000`) + expect(inputElement.dataset.displayUnit).toBe(`BTC`) + expect(inputElement.dataset.displayValue).toBe(`1.234`) + + // keeps display unit in BTC after element loses focus + await user.tab() + + expect(inputElement).not.toHaveFocus() + expect(inputElement.dataset.value).toBe(`123400000`) + expect(inputElement.dataset.displayUnit).toBe(`BTC`) + expect(inputElement.dataset.displayValue).toBe(`1.23 400 000`) + }) + + it('given input must be a number', async () => { + setup({ + label: 'test-label', + }) + const inputElement = screen.getByLabelText('test-label') + + await user.type(inputElement, 'test') + + expect(inputElement).toHaveFocus() + expect(inputElement.dataset.value).toBe(undefined) + expect(inputElement.dataset.displayUnit).toBe(undefined) + expect(inputElement.dataset.displayValue).toBe(undefined) + + await user.tab() + + expect(inputElement).not.toHaveFocus() + expect(inputElement.dataset.value).toBe(undefined) + expect(inputElement.dataset.displayUnit).toBe(undefined) + expect(inputElement.dataset.displayValue).toBe('') + }) +}) diff --git a/src/components/BitcoinAmountInput.tsx b/src/components/BitcoinAmountInput.tsx index 35da133a0..ba09028c2 100644 --- a/src/components/BitcoinAmountInput.tsx +++ b/src/components/BitcoinAmountInput.tsx @@ -24,7 +24,7 @@ const unitFromValue = (value: string | undefined): Unit | undefined => { return value !== undefined && value !== '' ? (value?.includes('.') ? 'BTC' : 'sats') : undefined } -type BitcoinAmountInputProps = { +export type BitcoinAmountInputProps = { label: string className?: string inputGroupTextClassName?: string @@ -53,25 +53,28 @@ const BitcoinAmountInput = forwardRef( inputMode: 'decimal', }) - const displayInputUnit = useMemo(() => unitFromValue(field.value?.userRawInputValue), [field]) + const displayInputUnit = useMemo(() => { + return inputType.type === 'number' + ? unitFromValue(field.value?.userRawInputValue) + : field.value?.displayValue + ? 'BTC' + : undefined + }, [field, inputType]) return ( <> - {inputType.type === 'number' ? ( - <> - {displayInputUnit === undefined && <>…} - {displayInputUnit === 'sats' && } - {displayInputUnit === 'BTC' && {BITCOIN_SYMBOL}} - - ) : ( - <>{field.value?.displayValue ? {BITCOIN_SYMBOL} : '…'} - )} + {displayInputUnit === undefined && <>…} + {displayInputUnit === 'sats' && } + {displayInputUnit === 'BTC' && {BITCOIN_SYMBOL}}