Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悶 fix <Form /> component content-type json type missing #10454

Merged
merged 2 commits into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/__tests__/form.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ const server = setupServer(
rest.post('/get', (_, res: ResponseComposition, ctx: RestContext) => {
return res(ctx.status(200));
}),
rest.post('/json', (req, res: ResponseComposition, ctx: RestContext) => {
if (req.headers.get('content-type') === 'application/json') {
return res(ctx.status(200));
}

return res(ctx.status(500));
}),
);

describe('Form', () => {
Expand Down Expand Up @@ -264,4 +271,34 @@ describe('Form', () => {
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 (
<Form
action={'/json'}
control={control}
encType="application/json"
onSuccess={onSuccess}
>
<button>Submit</button>
<p>{isSubmitSuccessful ? 'submitSuccessful' : 'submitFailed'}</p>
</Form>
);
};

render(<App />);

fireEvent.click(screen.getByRole('button'));

await waitFor(() => {
expect(onSuccess).toBeCalled();
});
});
});
2 changes: 1 addition & 1 deletion src/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export function Form<
const shouldStringifySubmissionData = [
headers && headers['Content-Type'],
encType,
].includes('json');
].some((value) => value && value.includes('json'));

const response = await fetch(action, {
method,
Expand Down