|
| 1 | +import { renderHook, screen, waitFor } from '@testing-library/react' |
| 2 | +import { userEvent } from '@testing-library/user-event' |
| 3 | +import { mockFormErrors, renderWithForm } from '@utils/test' |
| 4 | +import { useForm } from 'react-hook-form' |
| 5 | +import { describe, expect, test, vi } from 'vitest' |
| 6 | + |
| 7 | +import { PhoneField } from '..' |
| 8 | +import { Submit } from '../..' |
| 9 | +import { Form } from '../../Form' |
| 10 | + |
| 11 | +describe('PhoneField', () => { |
| 12 | + test('should render correctly', () => { |
| 13 | + const { asFragment } = renderWithForm( |
| 14 | + <PhoneField |
| 15 | + label="Phone Number" |
| 16 | + name="phone" |
| 17 | + parseNumberErrorMessage="Invalid phone number" |
| 18 | + />, |
| 19 | + ) |
| 20 | + expect(asFragment()).toMatchSnapshot() |
| 21 | + }) |
| 22 | + |
| 23 | + test('should validate phone number', async () => { |
| 24 | + const onSubmit = vi.fn() |
| 25 | + const { result } = renderHook(() => |
| 26 | + useForm<{ phone: string }>(), |
| 27 | + ) |
| 28 | + |
| 29 | + renderWithForm( |
| 30 | + <Form |
| 31 | + errors={mockFormErrors} |
| 32 | + methods={result.current} |
| 33 | + onSubmit={onSubmit} |
| 34 | + > |
| 35 | + <PhoneField |
| 36 | + label="Phone Number" |
| 37 | + name="phone" |
| 38 | + parseNumberErrorMessage="Invalid phone number" |
| 39 | + required |
| 40 | + /> |
| 41 | + <Submit>Submit</Submit> |
| 42 | + </Form>, |
| 43 | + ) |
| 44 | + |
| 45 | + await userEvent.click(screen.getByText('Submit')) |
| 46 | + await waitFor(() => { |
| 47 | + expect(onSubmit).toHaveBeenCalledTimes(0) |
| 48 | + }) |
| 49 | + |
| 50 | + const phoneInput = screen.getByRole('textbox') |
| 51 | + await userEvent.type(phoneInput, '+33612345678') |
| 52 | + await userEvent.click(screen.getByText('Submit')) |
| 53 | + await waitFor(() => { |
| 54 | + expect(onSubmit).toHaveBeenCalledTimes(1) |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + test('should work with default country', async () => { |
| 59 | + const onSubmit = vi.fn() |
| 60 | + const { result } = renderHook(() => |
| 61 | + useForm<{ phone: string }>(), |
| 62 | + ) |
| 63 | + |
| 64 | + const { asFragment } = renderWithForm( |
| 65 | + <Form |
| 66 | + errors={mockFormErrors} |
| 67 | + methods={result.current} |
| 68 | + onSubmit={onSubmit} |
| 69 | + > |
| 70 | + <PhoneField |
| 71 | + defaultCountry="US" |
| 72 | + label="Phone Number" |
| 73 | + name="phone" |
| 74 | + parseNumberErrorMessage="Invalid phone number" |
| 75 | + /> |
| 76 | + <Submit>Submit</Submit> |
| 77 | + </Form>, |
| 78 | + ) |
| 79 | + |
| 80 | + const phoneInput = screen.getByRole('textbox') |
| 81 | + await userEvent.type(phoneInput, '+12025551234') |
| 82 | + await userEvent.click(screen.getByText('Submit')) |
| 83 | + await waitFor(() => { |
| 84 | + expect(onSubmit.mock.calls[0][0]).toEqual({ |
| 85 | + phone: '+12025551234', |
| 86 | + }) |
| 87 | + }) |
| 88 | + expect(asFragment()).toMatchSnapshot() |
| 89 | + }) |
| 90 | + |
| 91 | + test('should show error for invalid phone number', async () => { |
| 92 | + const onSubmit = vi.fn() |
| 93 | + const { result } = renderHook(() => |
| 94 | + useForm<{ phone: string }>(), |
| 95 | + ) |
| 96 | + |
| 97 | + renderWithForm( |
| 98 | + <Form |
| 99 | + errors={mockFormErrors} |
| 100 | + methods={result.current} |
| 101 | + onSubmit={onSubmit} |
| 102 | + > |
| 103 | + <PhoneField |
| 104 | + label="Phone Number" |
| 105 | + name="phone" |
| 106 | + parseNumberErrorMessage="This doesn't appear to be a valid phone number." |
| 107 | + required |
| 108 | + /> |
| 109 | + <Submit>Submit</Submit> |
| 110 | + </Form>, |
| 111 | + ) |
| 112 | + |
| 113 | + const phoneInput = screen.getByRole('textbox') |
| 114 | + await userEvent.type(phoneInput, 'invalid') |
| 115 | + await userEvent.click(screen.getByText('Submit')) |
| 116 | + await waitFor(() => { |
| 117 | + expect(onSubmit).toHaveBeenCalledTimes(0) |
| 118 | + }) |
| 119 | + expect( |
| 120 | + screen.getByText("This doesn't appear to be a valid phone number."), |
| 121 | + ).toBeDefined() |
| 122 | + }) |
| 123 | + |
| 124 | + test('should be disabled when disabled prop is true', () => { |
| 125 | + const { asFragment } = renderWithForm( |
| 126 | + <PhoneField |
| 127 | + disabled |
| 128 | + label="Phone Number" |
| 129 | + name="phone" |
| 130 | + parseNumberErrorMessage="Invalid phone number" |
| 131 | + />, |
| 132 | + ) |
| 133 | + expect(asFragment()).toMatchSnapshot() |
| 134 | + }) |
| 135 | +}) |
0 commit comments