Skip to content

Commit

Permalink
馃悶 fix <Form /> component content-type json type missing (#10454)
Browse files Browse the repository at this point in the history
* 馃悶 fix header component json type check

* update with unit test
  • Loading branch information
bluebill1049 committed May 27, 2023
1 parent 11fa730 commit ba03685
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
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

0 comments on commit ba03685

Please sign in to comment.