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 #113 from vxsx/bugfix/poll-request-options
Browse files Browse the repository at this point in the history
Add support for requestOptions as a method to Poll
  • Loading branch information
Tejas Kumar authored Apr 8, 2019
2 parents eaa37d2 + 1914b50 commit b5c75ab
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 10 deletions.
21 changes: 21 additions & 0 deletions src/Get.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,27 @@ describe("Get", () => {
expect(children.mock.calls[1][1].loading).toEqual(false);
expect(children.mock.calls[1][0]).toEqual({ id: 1 });
});

it("should add a custom header with requestOptions method", async () => {
nock("https://my-awesome-api.fake", { reqheaders: { foo: "bar" } })
.get("/")
.reply(200, { id: 1 });

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

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

await wait(() => expect(children.mock.calls.length).toBe(2));
expect(children.mock.calls[1][1].loading).toEqual(false);
expect(children.mock.calls[1][0]).toEqual({ id: 1 });
});
});

describe("actions", () => {
Expand Down
42 changes: 42 additions & 0 deletions src/Poll.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -657,5 +657,47 @@ describe("Poll", () => {
expect(children.mock.calls[1][1].loading).toEqual(false);
expect(children.mock.calls[1][0]).toEqual({ id: 1 });
});

it("should add a custom header with requestOptions method", async () => {
nock("https://my-awesome-api.fake", { reqheaders: { foo: "bar" } })
.get("/")
.reply(200, { id: 1 });

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

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

await wait(() => expect(children.mock.calls.length).toBe(2));
expect(children.mock.calls[1][1].loading).toEqual(false);
expect(children.mock.calls[1][0]).toEqual({ id: 1 });
});

it("should merge headers with providers", async () => {
nock("https://my-awesome-api.fake", { reqheaders: { foo: "bar", baz: "qux" } })
.get("/")
.reply(200, { id: 1 });

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

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

await wait(() => expect(children.mock.calls.length).toBe(2));
expect(children.mock.calls[1][1].loading).toEqual(false);
expect(children.mock.calls[1][0]).toEqual({ id: 1 });
});
});
});
27 changes: 17 additions & 10 deletions src/Poll.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import merge from "lodash/merge";
import * as qs from "qs";
import * as React from "react";
import equal from "react-fast-compare";
Expand Down Expand Up @@ -339,16 +340,22 @@ function Poll<TData = any, TError = any, TQueryParams = { [key: string]: any }>(
// Compose Contexts to allow for URL nesting
return (
<RestfulReactConsumer>
{contextProps => (
<ContextlessPoll
{...contextProps}
{...props}
requestOptions={{
...contextProps.requestOptions,
...props.requestOptions,
}}
/>
)}
{contextProps => {
const contextRequestOptions =
typeof contextProps.requestOptions === "function"
? contextProps.requestOptions()
: contextProps.requestOptions || {};
const propsRequestOptions =
typeof props.requestOptions === "function" ? props.requestOptions() : props.requestOptions || {};

return (
<ContextlessPoll
{...contextProps}
{...props}
requestOptions={merge(contextRequestOptions, propsRequestOptions)}
/>
);
}}
</RestfulReactConsumer>
);
}
Expand Down

0 comments on commit b5c75ab

Please sign in to comment.