Skip to content

Commit

Permalink
Few more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Apr 13, 2023
1 parent b18c5c1 commit 6608193
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
74 changes: 73 additions & 1 deletion packages/react-router-dom/__tests__/data-browser-router-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<Route path="/" action={actionSpy} element={<FormPage />} />
),
{ window: getWindow("/") }
);
render(<RouterProvider router={router} />);

let payload = { a: "1", b: "2" };
function FormPage() {
let submit = useSubmit();
return (
<button
onClick={() =>
submit(payload, {
method: "post",
encType: "application/json",
})
}
>
Submit
</button>
);
}

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(
<Route path="/" action={actionSpy} element={<FormPage />} />
),
{ window: getWindow("/") }
);
render(<RouterProvider router={router} />);

let payload = "look ma, no formData!";
function FormPage() {
let submit = useSubmit();
return (
<button
onClick={() =>
submit(payload, {
method: "post",
encType: "text/plain",
})
}
>
Submit
</button>
);
}

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;
Expand Down
4 changes: 2 additions & 2 deletions packages/router/__tests__/router-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand Down

0 comments on commit 6608193

Please sign in to comment.