From 041a3a84374c8c7bbdb1f245a5fda95e27f4443a Mon Sep 17 00:00:00 2001 From: Vadim Sikora Date: Sun, 7 Apr 2019 22:52:15 +0200 Subject: [PATCH 1/2] Add support for requestOptions as a method to Poll --- src/Get.test.tsx | 21 +++++++++++++++++++++ src/Poll.test.tsx | 21 +++++++++++++++++++++ src/Poll.tsx | 29 +++++++++++++++++++---------- 3 files changed, 61 insertions(+), 10 deletions(-) diff --git a/src/Get.test.tsx b/src/Get.test.tsx index 63534de2..cae61f82 100644 --- a/src/Get.test.tsx +++ b/src/Get.test.tsx @@ -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(
); + + render( + + ({ headers: { foo: "bar" } })}> + {children} + + , + ); + + 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", () => { diff --git a/src/Poll.test.tsx b/src/Poll.test.tsx index 3ec7f377..600dfb2f 100644 --- a/src/Poll.test.tsx +++ b/src/Poll.test.tsx @@ -657,5 +657,26 @@ 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(
); + + render( + + ({ headers: { foo: "bar" } })}> + {children} + + , + ); + + 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 }); + }); }); }); diff --git a/src/Poll.tsx b/src/Poll.tsx index 6bec59a5..16491fb9 100644 --- a/src/Poll.tsx +++ b/src/Poll.tsx @@ -339,16 +339,25 @@ function Poll( // Compose Contexts to allow for URL nesting return ( - {contextProps => ( - - )} + {contextProps => { + const contextRequestOptions = + typeof contextProps.requestOptions === "function" + ? contextProps.requestOptions() + : contextProps.requestOptions || {}; + const propsRequestOptions = + typeof props.requestOptions === "function" ? props.requestOptions() : props.requestOptions || {}; + + return ( + + ); + }} ); } From 1914b509aa07a222bc19407172741d9fd93580c6 Mon Sep 17 00:00:00 2001 From: Vadim Sikora Date: Mon, 8 Apr 2019 15:15:17 +0200 Subject: [PATCH 2/2] Fixed a case with merging request options in Poll --- src/Poll.test.tsx | 21 +++++++++++++++++++++ src/Poll.tsx | 6 ++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Poll.test.tsx b/src/Poll.test.tsx index 600dfb2f..856482ad 100644 --- a/src/Poll.test.tsx +++ b/src/Poll.test.tsx @@ -678,5 +678,26 @@ describe("Poll", () => { 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(
); + + render( + + ({ headers: { foo: "bar" } })}> + {children} + + , + ); + + 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 }); + }); }); }); diff --git a/src/Poll.tsx b/src/Poll.tsx index 16491fb9..921b2a87 100644 --- a/src/Poll.tsx +++ b/src/Poll.tsx @@ -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"; @@ -351,10 +352,7 @@ function Poll( ); }}