From ef00272d58f24e8f8735120002298deb212c5857 Mon Sep 17 00:00:00 2001 From: Fabien BERNARD Date: Wed, 7 Nov 2018 14:21:22 +0100 Subject: [PATCH] Fix delete with id --- src/Mutate.test.tsx | 64 +++++++++++++++++++++++++++++++++++++++++++++ src/Mutate.tsx | 4 ++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/src/Mutate.test.tsx b/src/Mutate.test.tsx index d82d3887..46a53ade 100644 --- a/src/Mutate.test.tsx +++ b/src/Mutate.test.tsx @@ -46,6 +46,70 @@ describe("Mutate", () => { expect(children.mock.calls[2][1].loading).toEqual(false); }); + it("should call the correct url with a specific id (with base in Mutate)", async () => { + nock("https://my-awesome-api.fake") + .delete("/plop") + .reply(200, { id: 1 }); + + const children = jest.fn(); + children.mockReturnValue(
); + + // setup - first render + render( + + + {children} + + , + ); + + await wait(() => expect(children.mock.calls.length).toBe(1)); + expect(children.mock.calls[0][1].loading).toEqual(false); + expect(children.mock.calls[0][0]).toBeDefined(); + + // delete action + children.mock.calls[0][0]("plop"); + await wait(() => expect(children.mock.calls.length).toBe(3)); + + // transition state + expect(children.mock.calls[1][1].loading).toEqual(true); + + // after delete state + expect(children.mock.calls[2][1].loading).toEqual(false); + }); + + it("should call the correct url with a specific id (with base in Mutate)", async () => { + nock("https://my-awesome-api.fake") + .delete("/plop") + .reply(200, { id: 1 }); + + const children = jest.fn(); + children.mockReturnValue(
); + + // setup - first render + render( + + + {children} + + , + ); + + await wait(() => expect(children.mock.calls.length).toBe(1)); + expect(children.mock.calls[0][1].loading).toEqual(false); + expect(children.mock.calls[0][0]).toBeDefined(); + + // delete action + children.mock.calls[0][0]("plop"); + await wait(() => expect(children.mock.calls.length).toBe(3)); + + // transition state + expect(children.mock.calls[1][1].loading).toEqual(true); + + // after delete state + expect(children.mock.calls[2][1].loading).toEqual(false); + }); + it("should call the correct url without id", async () => { nock("https://my-awesome-api.fake") .delete("/") diff --git a/src/Mutate.tsx b/src/Mutate.tsx index aa26e4b3..4247ea35 100644 --- a/src/Mutate.tsx +++ b/src/Mutate.tsx @@ -144,7 +144,9 @@ class ContextlessMutate extends React.Component< const makeRequestPath = () => { if (__internal_hasExplicitBase) { - return composeUrl(base!, "", path || ""); + return verb === "DELETE" && typeof body === "string" + ? composeUrl(base!, "", composePathWithBody(path!, body)) + : composeUrl(base!, "", path || ""); } else { return verb === "DELETE" && typeof body === "string" ? composeUrl(base!, parentPath!, composePathWithBody(path!, body))