Skip to content

Commit

Permalink
Simplify mocking and assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
exoego committed Dec 6, 2023
1 parent de894d0 commit 2ac1d75
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions test/integration/form-data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,41 @@ describe("FormData", () => {
afterEach(() => server.stop());

it("should parse application/x-www-form-urlencoded", async () => {
await server.forPost("/mocked-endpoint").thenCallback(async (req) => {
const formData = await req.body.getFormData() ;
return {
statusCode: 200,
body: JSON.stringify(formData),
};
const endpoint = await server.forPost("/mocked-endpoint").thenReply(200);

await fetch(server.urlFor("/mocked-endpoint"), {
method: "POST",
body: "id=123&id=456&id=789&order=desc"
});

await expect(
fetch(server.urlFor("/mocked-endpoint"), {
method: "POST",
body: "id=123&id=456&id=789&order=desc"
})
).to.responseText("{\"id\":[\"123\",\"456\",\"789\"],\"order\":\"desc\"}");
const requests = await endpoint.getSeenRequests();
expect(requests.length).to.equal(1);
expect(await requests[0].body.getFormData()).to.deep.equal({
id: ["123", "456", "789"],
order: "desc",
});
});

it("should parse multipart/form-data", async () => {
await server.forPost("/mocked-endpoint").thenCallback(async (req) => {
const formData = await req.body.getFormData() ;
return {
statusCode: 200,
body: JSON.stringify(formData),
};
});
const endpoint = await server.forPost("/mocked-endpoint").thenReply(200);

const formData = new FormData();
formData.append("id", "123");
formData.append("id", "456");
formData.append("id", "789");
formData.append("order", "desc");
formData.append("readme", new File(["file content"], "file.txt", { type: "text/plain" }));
await expect(
fetch(server.urlFor("/mocked-endpoint"), {
method: "POST",
body: formData,
})
).to.responseText("{\"id\":[\"123\",\"456\",\"789\"],\"order\":\"desc\",\"readme\":\"file content\"}");
await fetch(server.urlFor("/mocked-endpoint"), {
method: "POST",
body: formData,
});

const requests = await endpoint.getSeenRequests();
expect(requests.length).to.equal(1);
expect(await requests[0].body.getFormData()).to.deep.equal({
id: ["123", "456", "789"],
order: "desc",
readme: "file content",
});
});
});

0 comments on commit 2ac1d75

Please sign in to comment.