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

Commit

Permalink
Add prevData in the Poll resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
fabien0102 committed Sep 11, 2018
1 parent 7912452 commit ee2839b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 9 deletions.
75 changes: 69 additions & 6 deletions src/Poll.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ describe("Poll", () => {
it("should send data on data", async () => {
nock("https://my-awesome-api.fake", {
reqheaders: {
prefer: "wait=60s;"
prefer: "wait=0s;"
}
})
.get("/")
.reply(200, { data: "hello" }, { "x-polling-index": "1" });

nock("https://my-awesome-api.fake", {
reqheaders: {
prefer: "wait=60s;index=1"
prefer: "wait=0s;index=1"
}
})
.get("/")
Expand All @@ -153,7 +153,9 @@ describe("Poll", () => {

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

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

nock("https://my-awesome-api.fake", {
reqheaders: {
prefer: "wait=60s;index=1"
prefer: "wait=0s;index=1"
}
})
.get("/")
Expand All @@ -183,7 +185,9 @@ describe("Poll", () => {

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

Expand Down Expand Up @@ -243,6 +247,27 @@ describe("Poll", () => {
});

describe("with custom resolver", () => {
it("should use the provider resolver", 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"
resolve={data => ({ ...data, foo: "bar" })}
>
<Poll path="">{children}</Poll>
</RestfulProvider>
);

await wait(() => expect(children.mock.calls.length).toBe(2));
expect(children.mock.calls[1][0]).toEqual({ hello: "world", foo: "bar" });
});

it("should transform data", async () => {
nock("https://my-awesome-api.fake")
.get("/")
Expand All @@ -262,6 +287,44 @@ describe("Poll", () => {
await wait(() => expect(children.mock.calls.length).toBe(2));
expect(children.mock.calls[1][0]).toEqual({ hello: "world", foo: "bar" });
});

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

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

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

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

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

describe("with lazy", () => {
Expand Down
6 changes: 3 additions & 3 deletions src/Poll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export interface PollProps<TData, TError> {
/**
* Should the data be transformed in any way?
*/
resolve?: GetProps<TData, TError>["resolve"];
resolve?: (data: any, prevData: TData) => TData;
/**
* We can request foreign URLs with this prop.
*/
Expand Down Expand Up @@ -245,10 +245,10 @@ class ContextlessPoll<TData, TError> extends React.Component<
}

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

0 comments on commit ee2839b

Please sign in to comment.