import 'whatwg-fetch'; import React from 'react'; import { fireEvent, render, screen, waitFor } from '@testing-library/react'; import { http, HttpResponse } from 'msw'; import { setupServer } from 'msw/node'; import { Form } from '../form'; import { useForm } from '../useForm'; import { FormProvider } from '../useFormContext'; const server = setupServer( http.post('/success', () => { return HttpResponse.json({ message: 'ok' }); }), http.post('/error', () => { return new Response(null, { status: 500, }); }), http.post('/status', () => { return new HttpResponse(null, { status: 201 }); }), http.post('/get', () => { return new HttpResponse(null, { status: 200 }); }), http.post('/json', ({ request }) => { if (request.headers.get('content-type') === 'application/json') { return new HttpResponse(null, { status: 200 }); } return new HttpResponse(null, { status: 500 }); }), http.post('/formData', async ({ request }) => { if ( request.headers.get('content-type')?.startsWith('multipart/form-data') && request.headers.get('content-type')?.includes('boundary=') ) { try { await request.formData(); return new HttpResponse(null, { status: 200 }); } catch { return new HttpResponse(null, { status: 500 }); } } return new HttpResponse(null, { status: 500 }); }), ); describe('Form', () => { beforeAll(() => server.listen()); afterEach(() => server.resetHandlers()); afterAll(() => server.close()); it('should support render with both form tag and headless', () => { const WithContext = () => { return ( <>
{ return null; }} /> ); }; const App = () => { const methods = useForm(); return (
{ return null; }} />
); }; render(); }); it('should handle success request callback', async () => { const onSubmit = jest.fn(); const onError = jest.fn(); const App = () => { const [message, setMessage] = React.useState(''); const { control, formState: { isSubmitSuccessful }, } = useForm(); return ( { data; formData; formDataJson; onSubmit(); }} control={control} onError={onError} onSuccess={async ({ response }) => { if (response) { const data: { message: string } = await response.json(); setMessage(data.message); } }} >

{isSubmitSuccessful ? 'submitSuccessful' : 'submitFailed'}

{message}

); }; render(); fireEvent.click(screen.getByRole('button')); await waitFor(() => { expect(onSubmit).toBeCalled(); expect(onError).not.toBeCalled(); screen.getByText('submitSuccessful'); screen.getByText('ok'); }); }); it('should handle error request callback', async () => { const onSubmit = jest.fn(); const onSuccess = jest.fn(); const App = () => { const { control, formState: { isSubmitSuccessful, errors }, } = useForm(); return (

{isSubmitSuccessful ? 'submitSuccessful' : 'submitFailed'}

{errors.root?.server && 'This is a server error'}

{errors.root?.server?.type}

); }; render(); fireEvent.click(screen.getByRole('button')); await waitFor(() => { expect(onSubmit).toBeCalled(); expect(onSuccess).not.toBeCalled(); screen.getByText('This is a server error'); screen.getByText('500'); screen.getByText('submitFailed'); }); }); it('should validate custom status code', async () => { const App = () => { const { control, formState: { isSubmitSuccessful }, } = useForm(); return (
status === 200} >

{isSubmitSuccessful ? 'submitSuccessful' : 'submitFailed'}

); }; render(); fireEvent.click(screen.getByRole('button')); await waitFor(() => { screen.getByText('submitFailed'); }); }); it('should support other request type', async () => { const App = () => { const { control, formState: { isSubmitSuccessful }, } = useForm(); return (

{isSubmitSuccessful ? 'submitSuccessful' : 'submitFailed'}

); }; render(); fireEvent.click(screen.getByRole('button')); await waitFor(() => { screen.getByText('submitSuccessful'); }); }); it('should support render props for react native', async () => { const App = () => { const { control, formState: { isSubmitSuccessful }, } = useForm(); return (
{ return ( <>

{isSubmitSuccessful ? 'submitSuccessful' : 'submitFailed'}

); }} /> ); }; render(); fireEvent.click(screen.getByRole('button')); await waitFor(() => { screen.getByText('submitSuccessful'); }); }); it('should support fetcher prop with external request', async () => { const fetcher = jest.fn(); const App = () => { const { control, formState: { isSubmitSuccessful }, } = useForm(); return ( { await fetcher(); }} >

{isSubmitSuccessful ? 'submitSuccessful' : 'submitFailed'}

); }; render(); fireEvent.click(screen.getByRole('button')); await waitFor(() => { screen.getByText('submitSuccessful'); expect(fetcher).toBeCalled(); }); }); it('should include application/json header with encType supplied', async () => { const onSuccess = jest.fn(); const App = () => { const { control, formState: { isSubmitSuccessful }, } = useForm(); return (

{isSubmitSuccessful ? 'submitSuccessful' : 'submitFailed'}

); }; render(); fireEvent.click(screen.getByRole('button')); await waitFor(() => { expect(onSuccess).toBeCalled(); }); }); it('should support explicit "multipart/form-data" encType', async () => { const onSuccess = jest.fn(); const App = () => { const { control, formState: { isSubmitSuccessful }, } = useForm(); return (

{isSubmitSuccessful ? 'submitSuccessful' : 'submitFailed'}

); }; render(); fireEvent.click(screen.getByRole('button')); await waitFor(() => { expect(onSuccess).toBeCalled(); }); }); });