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 #46 from contiamo/poll-errors
Browse files Browse the repository at this point in the history
Improve the error management for Poll
  • Loading branch information
Tejas Kumar authored Sep 13, 2018
2 parents 419fb67 + 985c64a commit 2b90bea
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 58 deletions.
141 changes: 89 additions & 52 deletions src/Poll.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ describe("Poll", () => {
it("should call the url set in provider", async () => {
nock("https://my-awesome-api.fake", {
reqheaders: {
prefer: "wait=60s;"
}
prefer: "wait=60s;",
},
})
.get("/")
.reply(200, { data: "hello" }, { "x-polling-index": "1" });

nock("https://my-awesome-api.fake", {
reqheaders: {
prefer: "wait=60s;index=1"
}
prefer: "wait=60s;index=1",
},
})
.get("/")
.reply(200, { data: "hello" }, { "x-polling-index": "2" });
Expand All @@ -36,7 +36,7 @@ describe("Poll", () => {
render(
<RestfulProvider base="https://my-awesome-api.fake">
<Poll path="">{children}</Poll>
</RestfulProvider>
</RestfulProvider>,
);

await wait(() => expect(children.mock.calls.length).toBe(2));
Expand All @@ -45,16 +45,16 @@ describe("Poll", () => {
it("should compose the url with the base", async () => {
nock("https://my-awesome-api.fake", {
reqheaders: {
prefer: "wait=60s;"
}
prefer: "wait=60s;",
},
})
.get("/plop")
.reply(200, { data: "hello" }, { "x-polling-index": "1" });

nock("https://my-awesome-api.fake", {
reqheaders: {
prefer: "wait=60s;index=1"
}
prefer: "wait=60s;index=1",
},
})
.get("/plop")
.reply(200, { data: "hello" }, { "x-polling-index": "2" });
Expand All @@ -65,7 +65,7 @@ describe("Poll", () => {
render(
<RestfulProvider base="https://my-awesome-api.fake">
<Poll path="/plop">{children}</Poll>
</RestfulProvider>
</RestfulProvider>,
);

await wait(() => expect(children.mock.calls.length).toBe(2));
Expand All @@ -74,16 +74,16 @@ describe("Poll", () => {
it("should set loading to `true` on mount", async () => {
nock("https://my-awesome-api.fake", {
reqheaders: {
prefer: "wait=60s;"
}
prefer: "wait=60s;",
},
})
.get("/")
.reply(200, { data: "hello" }, { "x-polling-index": "1" });

nock("https://my-awesome-api.fake", {
reqheaders: {
prefer: "wait=60s;index=1"
}
prefer: "wait=60s;index=1",
},
})
.get("/")
.reply(200, { data: "hello" }, { "x-polling-index": "2" });
Expand All @@ -94,7 +94,7 @@ describe("Poll", () => {
render(
<RestfulProvider base="https://my-awesome-api.fake">
<Poll path="">{children}</Poll>
</RestfulProvider>
</RestfulProvider>,
);

await wait(() => expect(children.mock.calls.length).toBe(2));
Expand All @@ -104,16 +104,16 @@ describe("Poll", () => {
it("should set loading to `false` on data", async () => {
nock("https://my-awesome-api.fake", {
reqheaders: {
prefer: "wait=60s;"
}
prefer: "wait=60s;",
},
})
.get("/")
.reply(200, { data: "hello" }, { "x-polling-index": "1" });

nock("https://my-awesome-api.fake", {
reqheaders: {
prefer: "wait=60s;index=1"
}
prefer: "wait=60s;index=1",
},
})
.get("/")
.reply(200, { data: "hello" }, { "x-polling-index": "2" });
Expand All @@ -124,7 +124,7 @@ describe("Poll", () => {
render(
<RestfulProvider base="https://my-awesome-api.fake">
<Poll path="">{children}</Poll>
</RestfulProvider>
</RestfulProvider>,
);

await wait(() => expect(children.mock.calls.length).toBe(2));
Expand All @@ -134,16 +134,16 @@ describe("Poll", () => {
it("should send data on data", async () => {
nock("https://my-awesome-api.fake", {
reqheaders: {
prefer: "wait=0s;"
}
prefer: "wait=0s;",
},
})
.get("/")
.reply(200, { data: "hello" }, { "x-polling-index": "1" });

nock("https://my-awesome-api.fake", {
reqheaders: {
prefer: "wait=0s;index=1"
}
prefer: "wait=0s;index=1",
},
})
.get("/")
.reply(200, { data: "hello" }, { "x-polling-index": "2" });
Expand All @@ -156,7 +156,7 @@ describe("Poll", () => {
<Poll path="" wait={0}>
{children}
</Poll>
</RestfulProvider>
</RestfulProvider>,
);

await wait(() => expect(children.mock.calls.length).toBe(2));
Expand All @@ -166,16 +166,16 @@ describe("Poll", () => {
it("should update data if the response change", async () => {
nock("https://my-awesome-api.fake", {
reqheaders: {
prefer: "wait=0s;"
}
prefer: "wait=0s;",
},
})
.get("/")
.reply(200, { data: "hello" }, { "x-polling-index": "1" });

nock("https://my-awesome-api.fake", {
reqheaders: {
prefer: "wait=0s;index=1"
}
prefer: "wait=0s;index=1",
},
})
.get("/")
.reply(200, { data: "hello you" }, { "x-polling-index": "2" });
Expand All @@ -188,15 +188,15 @@ describe("Poll", () => {
<Poll path="" wait={0}>
{children}
</Poll>
</RestfulProvider>
</RestfulProvider>,
);

await wait(() => expect(children.mock.calls.length).toBe(3));
expect(children.mock.calls[2][0]).toEqual({ data: "hello you" });
});
});

describe.skip("with error", () => {
describe("with error", () => {
it("should set the `error` object properly", async () => {
nock("https://my-awesome-api.fake")
.get("/")
Expand All @@ -208,22 +208,22 @@ describe("Poll", () => {
render(
<RestfulProvider base="https://my-awesome-api.fake">
<Poll path="">{children}</Poll>
</RestfulProvider>
</RestfulProvider>,
);

await wait(() => expect(children.mock.calls.length).toBe(2));
expect(children.mock.calls[1][0]).toEqual(null);
expect(children.mock.calls[1][1].error).toEqual({
data: { message: "You shall not pass!" },
message: "Failed to fetch: 401 Unauthorized"
message: "Failed to poll: 401 Unauthorized",
});
});

it("should deal with non standard server error response (nginx style)", async () => {
nock("https://my-awesome-api.fake")
.get("/")
.reply(200, "<html>404 - this is not a json!</html>", {
"content-type": "application/json"
"content-type": "application/json",
});

const children = jest.fn();
Expand All @@ -232,7 +232,7 @@ describe("Poll", () => {
render(
<RestfulProvider base="https://my-awesome-api.fake">
<Poll path="">{children}</Poll>
</RestfulProvider>
</RestfulProvider>,
);

await wait(() => expect(children.mock.calls.length).toBe(2));
Expand All @@ -241,9 +241,49 @@ describe("Poll", () => {
data:
"invalid json response body at https://my-awesome-api.fake reason: Unexpected token < in JSON at position 0",
message:
"Failed to fetch: 200 OK - invalid json response body at https://my-awesome-api.fake reason: Unexpected token < in JSON at position 0"
"Failed to poll: 200 OK - invalid json response body at https://my-awesome-api.fake reason: Unexpected token < in JSON at position 0",
});
});

it("should continue polling after an error", async () => {
nock("https://my-awesome-api.fake")
.get("/")
.reply(504, "<html>504 Gateway Time-out</html>", {
"content-type": "text/html",
});

nock("https://my-awesome-api.fake", {
reqheaders: {
prefer: "wait=0s;",
},
})
.get("/")
.reply(200, { data: "hello" }, { "x-polling-index": "1" });

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

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

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

// first res (error)
expect(children.mock.calls[1][0]).toEqual(null);
expect(children.mock.calls[1][1].error).toEqual({
data: "<html>504 Gateway Time-out</html>",
message: "Failed to poll: 504 Gateway Timeout",
});

// second res (success)
expect(children.mock.calls[2][0]).toEqual({ data: "hello" });
expect(children.mock.calls[2][1].error).toEqual(null);
});
});

describe("with custom resolver", () => {
Expand All @@ -256,12 +296,9 @@ describe("Poll", () => {
children.mockReturnValue(<div />);

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

await wait(() => expect(children.mock.calls.length).toBe(2));
Expand All @@ -281,7 +318,7 @@ describe("Poll", () => {
<Poll path="" resolve={data => ({ ...data, foo: "bar" })}>
{children}
</Poll>
</RestfulProvider>
</RestfulProvider>,
);

await wait(() => expect(children.mock.calls.length).toBe(2));
Expand All @@ -291,16 +328,16 @@ describe("Poll", () => {
it("should be able to consolidate data", async () => {
nock("https://my-awesome-api.fake", {
reqheaders: {
prefer: "wait=0s;"
}
prefer: "wait=0s;",
},
})
.get("/")
.reply(200, { data: "hello" }, { "x-polling-index": "1" });

nock("https://my-awesome-api.fake", {
reqheaders: {
prefer: "wait=0s;index=1"
}
prefer: "wait=0s;index=1",
},
})
.get("/")
.reply(200, { data: " you" }, { "x-polling-index": "2" });
Expand All @@ -314,12 +351,12 @@ describe("Poll", () => {
path=""
wait={0}
resolve={(data, prevData) => ({
data: (prevData || { data: "" }).data + data.data
data: (prevData || { data: "" }).data + data.data,
})}
>
{children}
</Poll>
</RestfulProvider>
</RestfulProvider>,
);

await wait(() => expect(children.mock.calls.length).toBe(3));
Expand All @@ -337,7 +374,7 @@ describe("Poll", () => {
<Poll path="" lazy>
{children}
</Poll>
</RestfulProvider>
</RestfulProvider>,
);

await wait(() => expect(children.mock.calls.length).toBe(1));
Expand All @@ -360,7 +397,7 @@ describe("Poll", () => {
<Poll path="" base="https://my-awesome-api.fake">
{children}
</Poll>
</RestfulProvider>
</RestfulProvider>,
);

await wait(() => expect(children.mock.calls.length).toBe(2));
Expand All @@ -381,7 +418,7 @@ describe("Poll", () => {
<Poll path="/plop" base="https://my-awesome-api.fake">
{children}
</Poll>
</RestfulProvider>
</RestfulProvider>,
);

await wait(() => expect(children.mock.calls.length).toBe(2));
Expand All @@ -404,7 +441,7 @@ describe("Poll", () => {
<Poll path="" requestOptions={{ headers: { foo: "bar" } }}>
{children}
</Poll>
</RestfulProvider>
</RestfulProvider>,
);

await wait(() => expect(children.mock.calls.length).toBe(2));
Expand Down
10 changes: 4 additions & 6 deletions src/Poll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,18 +222,16 @@ class ContextlessPoll<TData, TError> extends React.Component<

if (!this.isResponseOk(response) || responseError) {
const error = {
message: `${response.status} ${response.statusText}${responseError ? " - " + data : ""}`,
message: `Failed to poll: ${response.status} ${response.statusText}${responseError ? " - " + data : ""}`,
data,
};
this.setState({ loading: false, lastResponse: response, data, error });
throw new Error(`Failed to Poll: ${error}`);
}

if (this.isModified(response, data)) {
this.setState({ loading: false, lastResponse: response, error });
} else if (this.isModified(response, data)) {
this.setState(prevState => ({
loading: false,
lastResponse: response,
data: resolve ? resolve(data, prevState.data) : data,
error: null,
lastPollIndex: response.headers.get("x-polling-index") || undefined,
}));
}
Expand Down

0 comments on commit 2b90bea

Please sign in to comment.