import { render, screen } from '@testing-library/react'; import user from '@testing-library/user-event'; import React from 'react'; import { useForm } from 'react-hook-form'; import * as v from 'valibot'; import { valibotResolver } from '..'; const USERNAME_REQUIRED_MESSAGE = 'username field is required'; const PASSWORD_REQUIRED_MESSAGE = 'password field is required'; const schema = v.object({ username: v.pipe( v.string(USERNAME_REQUIRED_MESSAGE), v.minLength(2, USERNAME_REQUIRED_MESSAGE), ), password: v.pipe( v.string(PASSWORD_REQUIRED_MESSAGE), v.minLength(2, PASSWORD_REQUIRED_MESSAGE), ), }); type FormData = { username: string; password: string }; interface Props { onSubmit: (data: FormData) => void; } function TestComponent({ onSubmit }: Props) { const { register, handleSubmit, formState: { errors }, } = useForm({ resolver: valibotResolver(schema), // Useful to check TypeScript regressions }); return (
{errors.username && {errors.username.message}} {errors.password && {errors.password.message}}
); } test("form's validation with Valibot and TypeScript's integration", async () => { const handleSubmit = vi.fn(); render(); expect(screen.queryAllByRole('alert')).toHaveLength(0); await user.click(screen.getByText(/submit/i)); expect(screen.getByText(/username field is required/i)).toBeInTheDocument(); expect(screen.getByText(/password field is required/i)).toBeInTheDocument(); expect(handleSubmit).not.toHaveBeenCalled(); }); export function TestComponentManualType({ onSubmit, }: { onSubmit: (data: FormData) => void; }) { const { register, handleSubmit, formState: { errors }, } = useForm, undefined, FormData>({ resolver: valibotResolver(schema), // Useful to check TypeScript regressions }); return (
{errors.username && {errors.username.message}} {errors.password && {errors.password.message}}
); }