diff --git a/src/Mutate.test.tsx b/src/Mutate.test.tsx index 08c53be1..5bd37e62 100644 --- a/src/Mutate.test.tsx +++ b/src/Mutate.test.tsx @@ -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(
); + + // 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(); + + // 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(
); + + // 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(); + + // 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(
); + + // 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(); + + // 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(
); + + // 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(); + + // 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", + }); + }); + }); + }); }); diff --git a/src/Mutate.tsx b/src/Mutate.tsx index 3fbc87ec..b215ea4f 100644 --- a/src/Mutate.tsx +++ b/src/Mutate.tsx @@ -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 @@ -122,18 +123,17 @@ class ContextlessMutate extends React.Component