From 7701a1d8330144f8a8710782ccf87207190ad07a Mon Sep 17 00:00:00 2001 From: Fabien BERNARD Date: Sun, 5 Aug 2018 16:41:43 +0200 Subject: [PATCH] Add Get classic usage tests --- src/Get.test.tsx | 145 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 src/Get.test.tsx diff --git a/src/Get.test.tsx b/src/Get.test.tsx new file mode 100644 index 00000000..9b7c5e86 --- /dev/null +++ b/src/Get.test.tsx @@ -0,0 +1,145 @@ +import "isomorphic-fetch"; +import "jest-dom/extend-expect"; +import nock from "nock"; +import React from "react"; +import { cleanup, render, wait } from "react-testing-library"; + +import { Get, RestfulProvider } from "./index"; + +afterEach(() => { + cleanup(); + nock.cleanAll(); +}); + +describe("Get", () => { + describe("classic usage", () => { + it("should call the url set in provider", async () => { + nock("https://my-awesome-api.fake") + .get("/") + .reply(200); + + const children = jest.fn(); + children.mockReturnValue(
); + + render( + + {children} + , + ); + + await wait(() => expect(children.mock.calls.length).toBe(3)); + }); + + it("should compose the url with the base", async () => { + nock("https://my-awesome-api.fake") + .get("/plop") + .reply(200); + + const children = jest.fn(); + children.mockReturnValue(
); + + render( + + {children} + , + ); + + await wait(() => expect(children.mock.calls.length).toBe(3)); + }); + + it("should set loading to `true` on mount", async () => { + nock("https://my-awesome-api.fake") + .get("/") + .reply(200); + + const children = jest.fn(); + children.mockReturnValue(
); + + render( + + {children} + , + ); + + await wait(() => expect(children.mock.calls.length).toBe(3)); + expect(children.mock.calls[0][1].loading).toEqual(true); + }); + + it("should set loading to `false` on data", async () => { + nock("https://my-awesome-api.fake") + .get("/") + .reply(200, { hello: "world" }); + + const children = jest.fn(); + children.mockReturnValue(
); + + render( + + {children} + , + ); + + await wait(() => expect(children.mock.calls.length).toBe(3)); + expect(children.mock.calls[2][1].loading).toEqual(false); + }); + + it("should send data on data", async () => { + nock("https://my-awesome-api.fake") + .get("/") + .reply(200, { hello: "world" }); + + const children = jest.fn(); + children.mockReturnValue(
); + + render( + + {children} + , + ); + + await wait(() => expect(children.mock.calls.length).toBe(3)); + expect(children.mock.calls[2][0]).toEqual({ hello: "world" }); + }); + }); + + describe("with custom resolver", () => { + it("should transform data", async () => { + nock("https://my-awesome-api.fake") + .get("/") + .reply(200, { hello: "world" }); + + const children = jest.fn(); + children.mockReturnValue(
); + + render( + + ({ ...data, foo: "bar" })}> + {children} + + , + ); + + await wait(() => expect(children.mock.calls.length).toBe(3)); + expect(children.mock.calls[2][0]).toEqual({ hello: "world", foo: "bar" }); + }); + }); + + describe("with lazy", () => { + it("should not fetch on mount", async () => { + const children = jest.fn(); + children.mockReturnValue(
); + + render( + + + {children} + + , + ); + + await wait(() => expect(children.mock.calls.length).toBe(1)); + expect(children.mock.calls[0][1].loading).toBe(false); + expect(children.mock.calls[0][0]).toBe(null); + }); + }); +});