Skip to content
This repository has been archived by the owner on Nov 11, 2023. It is now read-only.

Commit

Permalink
Fix delete with id
Browse files Browse the repository at this point in the history
  • Loading branch information
fabien0102 committed Nov 7, 2018
1 parent 13f118e commit ef00272
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
64 changes: 64 additions & 0 deletions src/Mutate.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<div />);

// setup - first render
render(
<RestfulProvider base="https://my-awesome-api.fake">
<Mutate verb="DELETE" path="" base="https://my-awesome-api.fake">
{children}
</Mutate>
</RestfulProvider>,
);

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(<div />);

// setup - first render
render(
<RestfulProvider base="https://my-awesome-api.fake">
<Mutate verb="DELETE" path="user">
{children}
</Mutate>
</RestfulProvider>,
);

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("/")
Expand Down
4 changes: 3 additions & 1 deletion src/Mutate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ class ContextlessMutate<TData, TError> 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))
Expand Down

0 comments on commit ef00272

Please sign in to comment.