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

Commit

Permalink
Fix Get reload on queryParams change
Browse files Browse the repository at this point in the history
  • Loading branch information
fabien0102 committed Dec 8, 2018
1 parent 180a587 commit 4ccf8b5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/Get.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,33 @@ describe("Get", () => {
);
expect(apiCalls).toEqual(2);
});
it("should refetch when queryParams changes", () => {
let apiCalls = 0;
nock("https://my-awesome-api.fake")
.get("/")
.reply(200, () => ++apiCalls)
.persist();
nock("https://my-awesome-api.fake")
.get("/")
.query({ page: 2 })
.reply(200, () => ++apiCalls)
.persist();
const children = jest.fn();
children.mockReturnValue(<div />);
const { rerender } = render(
<RestfulProvider base="https://my-awesome-api.fake">
<Get path="">{children}</Get>
</RestfulProvider>,
);
rerender(
<RestfulProvider base="https://my-awesome-api.fake">
<Get path="" queryParams={{ page: 2 }}>
{children}
</Get>
</RestfulProvider>,
);
expect(apiCalls).toEqual(2);
});
});
describe("Compose paths and urls", () => {
it("should compose the url with the base", async () => {
Expand Down
3 changes: 2 additions & 1 deletion src/Get.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,12 @@ class ContextlessGet<TData, TError, TQueryParams> extends React.Component<
}

public componentDidUpdate(prevProps: GetProps<TData, TError, TQueryParams>) {
const { base, parentPath, path, resolve } = prevProps;
const { base, parentPath, path, resolve, queryParams } = prevProps;
if (
base !== this.props.base ||
parentPath !== this.props.parentPath ||
path !== this.props.path ||
queryParams !== this.props.queryParams ||
// both `resolve` props need to _exist_ first, and then be equivalent.
(resolve && this.props.resolve && resolve.toString() !== this.props.resolve.toString())
) {
Expand Down

0 comments on commit 4ccf8b5

Please sign in to comment.