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 t from 'typanion'; import { typanionResolver } from '..'; const schema = t.isObject({ username: t.cascade(t.isString(), [t.hasMinLength(1)]), password: t.cascade(t.isString(), [t.hasMinLength(1)]), }); function TestComponent({ onSubmit, }: { onSubmit: (data: t.InferType) => void }) { const { register, formState: { errors }, handleSubmit, } = useForm({ resolver: typanionResolver(schema), // Useful to check TypeScript regressions }); return (
{errors.username && {errors.username.message}} {errors.password && {errors.password.message}}
); } test("form's validation with Typanion 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.getAllByText( 'Expected to have a length of at least 1 elements (got 0)', ), ).toHaveLength(2); expect(handleSubmit).not.toHaveBeenCalled(); });