From 2ac1d755b742030a84cfc3e0530816293ed888bf Mon Sep 17 00:00:00 2001 From: exoego Date: Wed, 6 Dec 2023 20:15:01 +0900 Subject: [PATCH] Simplify mocking and assertion --- test/integration/form-data.spec.ts | 49 +++++++++++++++--------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/test/integration/form-data.spec.ts b/test/integration/form-data.spec.ts index 2c5129295..cd162becf 100644 --- a/test/integration/form-data.spec.ts +++ b/test/integration/form-data.spec.ts @@ -11,30 +11,23 @@ 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"); @@ -42,11 +35,17 @@ describe("FormData", () => { 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", + }); }); });