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

Commit

Permalink
Add Get classic usage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fabien0102 committed Aug 5, 2018
1 parent 0ca57b3 commit 7701a1d
Showing 1 changed file with 145 additions and 0 deletions.
145 changes: 145 additions & 0 deletions src/Get.test.tsx
Original file line number Diff line number Diff line change
@@ -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(<div />);

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

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

render(
<RestfulProvider base="https://my-awesome-api.fake">
<Get path="/plop">{children}</Get>
</RestfulProvider>,
);

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

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

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

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

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

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

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

render(
<RestfulProvider base="https://my-awesome-api.fake">
<Get path="" resolve={data => ({ ...data, foo: "bar" })}>
{children}
</Get>
</RestfulProvider>,
);

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

render(
<RestfulProvider base="https://my-awesome-api.fake">
<Get path="" lazy>
{children}
</Get>
</RestfulProvider>,
);

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);
});
});
});

0 comments on commit 7701a1d

Please sign in to comment.