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

Fix the error case on Get with wait property #56

Merged
merged 1 commit into from
Sep 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions src/Get.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,88 @@ describe("Get", () => {
});
});

describe("with wait", () => {
it("should render nothing if until we have data", async () => {
const children = jest.fn();
children.mockReturnValue(<div />);

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

await wait(() => expect(children.mock.calls.length).toBe(0));
});

it("should render if we have 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="" wait>
{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]).toEqual({ hello: "world" });
});
it("should render if we have 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="" wait>
{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]).toEqual({ hello: "world" });
});

it("should render if we have an error", async () => {
nock("https://my-awesome-api.fake")
.get("/")
.reply(401, "Go away!");

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

render(
<RestfulProvider base="https://my-awesome-api.fake">
<Get path="" wait>
{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][1].error).toEqual({
data: "Go away!",
message: "Failed to fetch: 401 Unauthorized",
});
});
});

describe("with base", () => {
it("should override the base url", async () => {
nock("https://my-awesome-api.fake")
Expand Down
2 changes: 1 addition & 1 deletion src/Get.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class ContextlessGet<TData, TError> extends React.Component<
const { children, wait, path, base } = this.props;
const { data, error, loading, response } = this.state;

if (wait && data === null) {
if (wait && data === null && !error) {
return <></>; // Show nothing until we have data.
}

Expand Down