diff --git a/packages/react-router-dom/__tests__/data-browser-router-test.tsx b/packages/react-router-dom/__tests__/data-browser-router-test.tsx index 7d2b0e56b6..15853bfc82 100644 --- a/packages/react-router-dom/__tests__/data-browser-router-test.tsx +++ b/packages/react-router-dom/__tests__/data-browser-router-test.tsx @@ -3188,12 +3188,84 @@ function testDomRouter( } fireEvent.click(screen.getByText("Submit")); - let formData = await actionSpy.mock.calls[0][0].request.formData(); + let request = actionSpy.mock.calls[0][0].request; + expect(request.headers.get("Content-Type")).toBe( + "application/x-www-form-urlencoded;charset=UTF-8" + ); + let formData = await request.formData(); expect(formData.get("a")).toBe("1"); expect(formData.get("b")).toBe("2"); expect(actionSpy.mock.calls[0][0].payload).toBe(undefined); }); + it("serializes JSON on submit(object)/encType:application/json submissions", async () => { + let actionSpy = jest.fn(); + let router = createTestRouter( + createRoutesFromElements( + } /> + ), + { window: getWindow("/") } + ); + render(); + + let payload = { a: "1", b: "2" }; + function FormPage() { + let submit = useSubmit(); + return ( + + ); + } + + fireEvent.click(screen.getByText("Submit")); + let request = actionSpy.mock.calls[0][0].request; + expect(request.headers.get("Content-Type")).toBe("application/json"); + expect(await request.json()).toEqual({ a: "1", b: "2" }); + expect(actionSpy.mock.calls[0][0].payload).toBe(payload); + }); + + it("serializes text on submit(object)/encType:text/plain submissions", async () => { + let actionSpy = jest.fn(); + let router = createTestRouter( + createRoutesFromElements( + } /> + ), + { window: getWindow("/") } + ); + render(); + + let payload = "look ma, no formData!"; + function FormPage() { + let submit = useSubmit(); + return ( + + ); + } + + fireEvent.click(screen.getByText("Submit")); + let request = actionSpy.mock.calls[0][0].request; + expect(request.headers.get("Content-Type")).toBe("text/plain"); + expect(await request.text()).toEqual(payload); + expect(actionSpy.mock.calls[0][0].payload).toBe(payload); + }); + it("does not serialize formData on submit(object)/encType:null submissions", async () => { let actionSpy = jest.fn(); let payload; diff --git a/packages/router/__tests__/router-test.ts b/packages/router/__tests__/router-test.ts index 7ce033f2fc..f257ffcab1 100644 --- a/packages/router/__tests__/router-test.ts +++ b/packages/router/__tests__/router-test.ts @@ -6059,7 +6059,7 @@ describe("a router", () => { expect(request.method).toBe("POST"); expect(request.url).toBe("http://localhost/"); expect(request.headers.get("Content-Type")).toBe("application/json"); - expect(JSON.parse(await request.text())).toEqual(payload); + expect(await request.json()).toEqual(payload); }); it("serializes payload as application/json if specified (array)", async () => { @@ -6085,7 +6085,7 @@ describe("a router", () => { expect(request.method).toBe("POST"); expect(request.url).toBe("http://localhost/"); expect(request.headers.get("Content-Type")).toBe("application/json"); - expect(JSON.parse(await request.text())).toEqual(payload); + expect(await request.json()).toEqual(payload); }); it("serializes payload as text/plain if specified", async () => {