import { render, screen } from '@testing-library/react'; import user from '@testing-library/user-event'; import { Validator } from 'fluentvalidation-ts'; import React from 'react'; import { SubmitHandler, useForm } from 'react-hook-form'; import { fluentValidationResolver } from '../fluentvalidation-ts'; type FormData = { username: string; password: string; }; class FormDataValidator extends Validator { constructor() { super(); this.ruleFor('username') .notEmpty() .withMessage('username is a required field'); this.ruleFor('password') .notEmpty() .withMessage('password is a required field'); } } interface Props { onSubmit: SubmitHandler; } function TestComponent({ onSubmit }: Props) { const { register, formState: { errors }, handleSubmit, } = useForm({ resolver: fluentValidationResolver(new FormDataValidator()), // Useful to check TypeScript regressions }); return (
{errors.username && {errors.username.message}} {errors.password && {errors.password.message}}
); } test("form's validation with Yup 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 is a required field/i)).toBeInTheDocument(); expect(screen.getByText(/password is a required field/i)).toBeInTheDocument(); expect(handleSubmit).not.toHaveBeenCalled(); });