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 (
<>
);
};
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 (
);
};
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 (
);
};
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 (
);
};
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 (
);
};
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 (
);
};
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 (
);
};
render();
fireEvent.click(screen.getByRole('button'));
await waitFor(() => {
expect(onSuccess).toBeCalled();
});
});
});