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

Commit

Permalink
Merge pull request #56 from contiamo/fix-error-on-wait
Browse files Browse the repository at this point in the history
Fix the error case on Get with `wait` property
  • Loading branch information
Tejas Kumar authored Sep 25, 2018
2 parents cf13011 + d30cb81 commit a70c412
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
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

0 comments on commit a70c412

Please sign in to comment.