import { Static, Type } from '@sinclair/typebox'; 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 { typeboxResolver } from '..'; const schema = Type.Object({ username: Type.String({ minLength: 1 }), password: Type.String({ minLength: 1 }), }); type FormData = Static & { unusedProperty: string }; interface Props { onSubmit: (data: FormData) => void; } function TestComponent({ onSubmit }: Props) { const { register, handleSubmit, formState: { errors }, } = useForm({ resolver: typeboxResolver(schema), // Useful to check TypeScript regressions }); return (
{errors.username && {errors.username.message}} {errors.password && {errors.password.message}}
); } test("form's validation with Typebox 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 string length greater or equal to 1/i), ).toHaveLength(2); expect(handleSubmit).not.toHaveBeenCalled(); });