|
| 1 | +import * as v from '@badrap/valita'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import user from '@testing-library/user-event'; |
| 4 | +import React from 'react'; |
| 5 | +import { useForm } from 'react-hook-form'; |
| 6 | +import { valitaResolver } from '..'; |
| 7 | + |
| 8 | +const USERNAME_REQUIRED_MESSAGE = 'username field is required'; |
| 9 | +const PASSWORD_REQUIRED_MESSAGE = 'password field is required'; |
| 10 | +const USERNAME_LENGTH_TOO_SHORT = 'username is too short'; |
| 11 | + |
| 12 | +function strRequired(message: string) { |
| 13 | + return (value: string) => { |
| 14 | + if (value === '') { |
| 15 | + return v.err(message); |
| 16 | + } |
| 17 | + return v.ok(value); |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +function strMinLength(min: number) { |
| 22 | + return (value: string) => { |
| 23 | + if (value.length < min) { |
| 24 | + return v.err(USERNAME_LENGTH_TOO_SHORT); |
| 25 | + } |
| 26 | + return v.ok(value); |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +const schema = v.object({ |
| 31 | + username: v |
| 32 | + .string() |
| 33 | + .chain(strRequired(USERNAME_REQUIRED_MESSAGE)) |
| 34 | + .chain(strMinLength(2)), |
| 35 | + password: v |
| 36 | + .string() |
| 37 | + .chain(strRequired(PASSWORD_REQUIRED_MESSAGE)) |
| 38 | + .chain(strMinLength(2)), |
| 39 | +}); |
| 40 | + |
| 41 | +type FormData = { username: string; password: string }; |
| 42 | + |
| 43 | +interface Props { |
| 44 | + onSubmit: (data: FormData) => void; |
| 45 | +} |
| 46 | + |
| 47 | +function TestComponent({ onSubmit }: Props) { |
| 48 | + const { |
| 49 | + register, |
| 50 | + handleSubmit, |
| 51 | + formState: { errors }, |
| 52 | + } = useForm<FormData>({ |
| 53 | + resolver: valitaResolver(schema), |
| 54 | + }); |
| 55 | + |
| 56 | + return ( |
| 57 | + <form onSubmit={handleSubmit(onSubmit)}> |
| 58 | + <input {...register('username')} placeholder="username" /> |
| 59 | + {errors.username && <span role="alert">{errors.username.message}</span>} |
| 60 | + |
| 61 | + <input {...register('password')} /> |
| 62 | + {errors.password && <span role="alert">{errors.password.message}</span>} |
| 63 | + |
| 64 | + <button type="submit">submit</button> |
| 65 | + </form> |
| 66 | + ); |
| 67 | +} |
| 68 | + |
| 69 | +describe('valita form validation errors', () => { |
| 70 | + test('ensure custom validation messages are shown', async () => { |
| 71 | + const handleSubmit = vi.fn(); |
| 72 | + render(<TestComponent onSubmit={handleSubmit} />); |
| 73 | + |
| 74 | + expect(screen.queryAllByRole('alert')).toHaveLength(0); |
| 75 | + |
| 76 | + await user.type(screen.getByPlaceholderText('username'), 'a'); |
| 77 | + await user.click(screen.getByText(/submit/i)); |
| 78 | + |
| 79 | + expect(screen.getByText(/username is too short/i)).toBeInTheDocument(); |
| 80 | + expect(screen.getByText(/password field is required/i)).toBeInTheDocument(); |
| 81 | + expect(handleSubmit).not.toHaveBeenCalled(); |
| 82 | + }); |
| 83 | +}); |
| 84 | + |
| 85 | +export function TestComponentManualType({ |
| 86 | + onSubmit, |
| 87 | +}: { |
| 88 | + onSubmit: (data: FormData) => void; |
| 89 | +}) { |
| 90 | + const { |
| 91 | + register, |
| 92 | + handleSubmit, |
| 93 | + formState: { errors }, |
| 94 | + } = useForm<v.Infer<typeof schema>, undefined, FormData>({ |
| 95 | + resolver: valitaResolver(schema), |
| 96 | + }); |
| 97 | + |
| 98 | + return ( |
| 99 | + <form onSubmit={handleSubmit(onSubmit)}> |
| 100 | + <input {...register('username')} /> |
| 101 | + {errors.username && <span role="alert">{errors.username.message}</span>} |
| 102 | + |
| 103 | + <input {...register('password')} /> |
| 104 | + {errors.password && <span role="alert">{errors.password.message}</span>} |
| 105 | + |
| 106 | + <button type="submit">submit</button> |
| 107 | + </form> |
| 108 | + ); |
| 109 | +} |
0 commit comments