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

Commit

Permalink
[breaking] Improve mutation response
Browse files Browse the repository at this point in the history
  • Loading branch information
fabien0102 authored and Tejas Kumar committed Sep 10, 2018
1 parent b5474b2 commit ea075f0
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 5 deletions.
120 changes: 120 additions & 0 deletions src/Mutate.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,124 @@ describe("Mutate", () => {
expect(children.mock.calls[2][1].loading).toEqual(false);
});
});
describe("POST", () => {
it("should call the correct url", async () => {
nock("https://my-awesome-api.fake")
.post("/")
.reply(200, { id: 1 });

const children = jest.fn();
children.mockReturnValue(<div />);

// setup - first render
render(
<RestfulProvider base="https://my-awesome-api.fake">
<Mutate verb="POST" path="">
{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();

// post action
children.mock.calls[0][0]();
await wait(() => expect(children.mock.calls.length).toBe(3));

// transition state
expect(children.mock.calls[1][1].loading).toEqual(true);

// after post state
expect(children.mock.calls[2][1].loading).toEqual(false);
});

it("should send the correct body", async () => {
nock("https://my-awesome-api.fake")
.post("/", { foo: "bar" })
.reply(200, { id: 1 });

const children = jest.fn();
children.mockReturnValue(<div />);

// setup - first render
render(
<RestfulProvider base="https://my-awesome-api.fake">
<Mutate verb="POST" path="">
{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();

// post action
children.mock.calls[0][0]({ foo: "bar" });
await wait(() => expect(children.mock.calls.length).toBe(3));

// transition state
expect(children.mock.calls[1][1].loading).toEqual(true);

// after post state
expect(children.mock.calls[2][1].loading).toEqual(false);
});

it("should return the correct data", async () => {
nock("https://my-awesome-api.fake")
.post("/")
.reply(200, { id: 1 });

const children = jest.fn();
children.mockReturnValue(<div />);

// setup - first render
render(
<RestfulProvider base="https://my-awesome-api.fake">
<Mutate verb="POST" path="">
{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();

// post action
expect(await children.mock.calls[0][0]()).toEqual({ id: 1 });
});

it("should return the data and the message on error", async () => {
nock("https://my-awesome-api.fake")
.post("/")
.reply(500, { error: "oh no… not again…" });

const children = jest.fn();
children.mockReturnValue(<div />);

// setup - first render
render(
<RestfulProvider base="https://my-awesome-api.fake">
<Mutate verb="POST" path="">
{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();

// post action
return children.mock.calls[0][0]().catch(error => {
expect(error).toEqual({
data: { error: "oh no… not again…" },
message: "Failed to fetch: 500 Internal Server Error",
});
});
});
});
});
10 changes: 5 additions & 5 deletions src/Mutate.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from "react";
import RestfulReactProvider, { RestfulReactConsumer, RestfulReactProviderProps } from "./Context";
import { GetState } from "./Get";
import { processResponse } from "./util/processResponse";

/**
* An enumeration of states that a fetchable
Expand Down Expand Up @@ -122,18 +123,17 @@ class ContextlessMutate<TData, TError> extends React.Component<MutateProps<TData
});

const response = await fetch(request);
const { data, responseError } = await processResponse(response);

if (!response.ok) {
const responseData = await response.json();
if (!response.ok || responseError) {
this.setState({
loading: false,
error: { data: responseData, message: `Failed to fetch: ${response.status} ${response.statusText}` },
});
throw response;
throw { data, message: `Failed to fetch: ${response.status} ${response.statusText}` };
}

this.setState({ loading: false });
return response;
return data;
};

public render() {
Expand Down

0 comments on commit ea075f0

Please sign in to comment.