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

Add support for requestOptions as a method to Poll #113

Merged
merged 2 commits into from
Apr 8, 2019
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
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