|
| 1 | +import { render, screen } from '@testing-library/react' |
| 2 | +import userEvent from '@testing-library/user-event' |
| 3 | +import { Money } from '@thesis-co/cent' |
| 4 | +import { MoneyInput } from '../../src/components/MoneyInput' |
| 5 | + |
| 6 | +describe('MoneyInput', () => { |
| 7 | + describe('basic rendering', () => { |
| 8 | + it('renders an input element', () => { |
| 9 | + render(<MoneyInput name="amount" currency="USD" />) |
| 10 | + expect(screen.getByRole('textbox')).toBeInTheDocument() |
| 11 | + }) |
| 12 | + |
| 13 | + it('displays controlled value', () => { |
| 14 | + render(<MoneyInput name="amount" currency="USD" value={Money('$100.00')} />) |
| 15 | + expect(screen.getByRole('textbox')).toHaveValue('100.00') |
| 16 | + }) |
| 17 | + |
| 18 | + it('displays placeholder', () => { |
| 19 | + render(<MoneyInput name="amount" currency="USD" placeholder="Enter amount" />) |
| 20 | + expect(screen.getByPlaceholderText('Enter amount')).toBeInTheDocument() |
| 21 | + }) |
| 22 | + }) |
| 23 | + |
| 24 | + describe('user input', () => { |
| 25 | + it('calls onChange with parsed Money value', async () => { |
| 26 | + const user = userEvent.setup() |
| 27 | + const onChange = jest.fn() |
| 28 | + |
| 29 | + render(<MoneyInput name="amount" currency="USD" onChange={onChange} />) |
| 30 | + |
| 31 | + const input = screen.getByRole('textbox') |
| 32 | + await user.type(input, '50.00') |
| 33 | + |
| 34 | + expect(onChange).toHaveBeenCalled() |
| 35 | + const lastCall = onChange.mock.calls[onChange.mock.calls.length - 1][0] |
| 36 | + expect(lastCall.target.name).toBe('amount') |
| 37 | + expect(lastCall.target.value).not.toBeNull() |
| 38 | + }) |
| 39 | + |
| 40 | + it('calls onValueChange with Money value', async () => { |
| 41 | + const user = userEvent.setup() |
| 42 | + const onValueChange = jest.fn() |
| 43 | + |
| 44 | + render(<MoneyInput name="amount" currency="USD" onValueChange={onValueChange} />) |
| 45 | + |
| 46 | + const input = screen.getByRole('textbox') |
| 47 | + await user.type(input, '75') |
| 48 | + |
| 49 | + expect(onValueChange).toHaveBeenCalled() |
| 50 | + }) |
| 51 | + |
| 52 | + it('handles clearing input', async () => { |
| 53 | + const user = userEvent.setup() |
| 54 | + const onChange = jest.fn() |
| 55 | + |
| 56 | + render(<MoneyInput name="amount" currency="USD" value={Money('$100')} onChange={onChange} />) |
| 57 | + |
| 58 | + const input = screen.getByRole('textbox') |
| 59 | + await user.clear(input) |
| 60 | + |
| 61 | + const lastCall = onChange.mock.calls[onChange.mock.calls.length - 1][0] |
| 62 | + expect(lastCall.target.value).toBeNull() |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + describe('formatting', () => { |
| 67 | + it('formats value on blur when formatOnBlur is true with controlled value', () => { |
| 68 | + const value = Money('$1234.50') |
| 69 | + render( |
| 70 | + <MoneyInput name="amount" currency="USD" value={value} formatOnBlur={true} /> |
| 71 | + ) |
| 72 | + |
| 73 | + const input = screen.getByRole('textbox') |
| 74 | + // Controlled value should display formatted (without currency since excludeCurrency) |
| 75 | + expect(input).toHaveValue('1,234.50') |
| 76 | + }) |
| 77 | + |
| 78 | + it('displays unformatted value when controlled value is set', () => { |
| 79 | + const value = Money('$100') |
| 80 | + render( |
| 81 | + <MoneyInput name="amount" currency="USD" value={value} formatOnBlur={true} /> |
| 82 | + ) |
| 83 | + |
| 84 | + const input = screen.getByRole('textbox') |
| 85 | + expect(input).toHaveValue('100.00') |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + describe('validation', () => { |
| 90 | + it('allows negative values by default', async () => { |
| 91 | + const user = userEvent.setup() |
| 92 | + const onChange = jest.fn() |
| 93 | + |
| 94 | + render(<MoneyInput name="amount" currency="USD" onChange={onChange} />) |
| 95 | + |
| 96 | + const input = screen.getByRole('textbox') |
| 97 | + await user.type(input, '-50') |
| 98 | + |
| 99 | + const lastCall = onChange.mock.calls[onChange.mock.calls.length - 1][0] |
| 100 | + expect(lastCall.target.value?.isNegative()).toBe(true) |
| 101 | + }) |
| 102 | + |
| 103 | + it('converts negative to positive when allowNegative is false', async () => { |
| 104 | + const user = userEvent.setup() |
| 105 | + const onChange = jest.fn() |
| 106 | + |
| 107 | + render(<MoneyInput name="amount" currency="USD" onChange={onChange} allowNegative={false} />) |
| 108 | + |
| 109 | + const input = screen.getByRole('textbox') |
| 110 | + await user.type(input, '-50') |
| 111 | + |
| 112 | + const lastCall = onChange.mock.calls[onChange.mock.calls.length - 1][0] |
| 113 | + expect(lastCall.target.value?.isNegative()).toBe(false) |
| 114 | + }) |
| 115 | + }) |
| 116 | + |
| 117 | + describe('props passthrough', () => { |
| 118 | + it('passes disabled prop', () => { |
| 119 | + render(<MoneyInput name="amount" currency="USD" disabled />) |
| 120 | + expect(screen.getByRole('textbox')).toBeDisabled() |
| 121 | + }) |
| 122 | + |
| 123 | + it('passes className', () => { |
| 124 | + render(<MoneyInput name="amount" currency="USD" className="my-input" />) |
| 125 | + expect(screen.getByRole('textbox')).toHaveClass('my-input') |
| 126 | + }) |
| 127 | + |
| 128 | + it('sets inputMode to decimal', () => { |
| 129 | + render(<MoneyInput name="amount" currency="USD" />) |
| 130 | + expect(screen.getByRole('textbox')).toHaveAttribute('inputMode', 'decimal') |
| 131 | + }) |
| 132 | + }) |
| 133 | + |
| 134 | + describe('ref forwarding', () => { |
| 135 | + it('forwards ref to input element', () => { |
| 136 | + const ref = { current: null as HTMLInputElement | null } |
| 137 | + render(<MoneyInput name="amount" currency="USD" ref={ref} />) |
| 138 | + expect(ref.current).toBeInstanceOf(HTMLInputElement) |
| 139 | + }) |
| 140 | + }) |
| 141 | +}) |
0 commit comments