|
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | +import user from '@testing-library/user-event'; |
| 3 | +import { type } from 'arktype'; |
| 4 | +import React from 'react'; |
| 5 | +import { useForm } from 'react-hook-form'; |
| 6 | +import { standardSchemaResolver } from '..'; |
| 7 | + |
| 8 | +const schema = type({ |
| 9 | + username: 'string>1', |
| 10 | + password: 'string>1', |
| 11 | +}); |
| 12 | + |
| 13 | +type FormData = typeof schema.infer; |
| 14 | + |
| 15 | +interface Props { |
| 16 | + onSubmit: (data: FormData) => void; |
| 17 | +} |
| 18 | + |
| 19 | +function TestComponent({ onSubmit }: Props) { |
| 20 | + const { register, handleSubmit } = useForm<FormData>({ |
| 21 | + resolver: standardSchemaResolver(schema), |
| 22 | + shouldUseNativeValidation: true, |
| 23 | + }); |
| 24 | + |
| 25 | + return ( |
| 26 | + <form onSubmit={handleSubmit(onSubmit)}> |
| 27 | + <input {...register('username')} placeholder="username" /> |
| 28 | + |
| 29 | + <input {...register('password')} placeholder="password" /> |
| 30 | + |
| 31 | + <button type="submit">submit</button> |
| 32 | + </form> |
| 33 | + ); |
| 34 | +} |
| 35 | + |
| 36 | +test("form's native validation with arkType", async () => { |
| 37 | + const handleSubmit = vi.fn(); |
| 38 | + render(<TestComponent onSubmit={handleSubmit} />); |
| 39 | + |
| 40 | + // username |
| 41 | + let usernameField = screen.getByPlaceholderText( |
| 42 | + /username/i, |
| 43 | + ) as HTMLInputElement; |
| 44 | + expect(usernameField.validity.valid).toBe(true); |
| 45 | + expect(usernameField.validationMessage).toBe(''); |
| 46 | + |
| 47 | + // password |
| 48 | + let passwordField = screen.getByPlaceholderText( |
| 49 | + /password/i, |
| 50 | + ) as HTMLInputElement; |
| 51 | + expect(passwordField.validity.valid).toBe(true); |
| 52 | + expect(passwordField.validationMessage).toBe(''); |
| 53 | + |
| 54 | + await user.click(screen.getByText(/submit/i)); |
| 55 | + |
| 56 | + // username |
| 57 | + usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement; |
| 58 | + expect(usernameField.validity.valid).toBe(false); |
| 59 | + expect(usernameField.validationMessage).toBe( |
| 60 | + 'username must be at least length 2', |
| 61 | + ); |
| 62 | + |
| 63 | + // password |
| 64 | + passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement; |
| 65 | + expect(passwordField.validity.valid).toBe(false); |
| 66 | + expect(passwordField.validationMessage).toBe( |
| 67 | + 'password must be at least length 2', |
| 68 | + ); |
| 69 | + |
| 70 | + await user.type(screen.getByPlaceholderText(/username/i), 'joe'); |
| 71 | + await user.type(screen.getByPlaceholderText(/password/i), 'password'); |
| 72 | + |
| 73 | + // username |
| 74 | + usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement; |
| 75 | + expect(usernameField.validity.valid).toBe(true); |
| 76 | + expect(usernameField.validationMessage).toBe(''); |
| 77 | + |
| 78 | + // password |
| 79 | + passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement; |
| 80 | + expect(passwordField.validity.valid).toBe(true); |
| 81 | + expect(passwordField.validationMessage).toBe(''); |
| 82 | +}); |
0 commit comments