import React from 'react'; import { render, screen, act } from '@testing-library/react'; import user from '@testing-library/user-event'; import { useForm } from 'react-hook-form'; import * as t from 'io-ts'; import * as tt from 'io-ts-types'; import { ioTsResolver } from '..'; const schema = t.type({ username: tt.withMessage( tt.NonEmptyString, () => 'username is a required field', ), password: tt.withMessage( tt.NonEmptyString, () => 'password is a required field', ), }); interface FormData { username: string; password: string; } interface Props { onSubmit: (data: FormData) => void; } function TestComponent({ onSubmit }: Props) { const { register, formState: { errors }, handleSubmit, } = useForm({ resolver: ioTsResolver(schema), criteriaMode: 'all', }); return (
{errors.username && {errors.username.message}} {errors.password && {errors.password.message}}
); } test("form's validation with io-ts and TypeScript's integration", async () => { const handleSubmit = jest.fn(); render(); expect(screen.queryAllByRole(/alert/i)).toHaveLength(0); await act(async () => { 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(); });