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

Commit

Permalink
Remove originalResponse flag
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinavrastogi authored and fabien0102 committed Oct 12, 2020
1 parent 93c5a0c commit e1896fa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 67 deletions.
92 changes: 31 additions & 61 deletions src/useGet.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,37 @@ describe("useGet hook", () => {
expect(onResponse).toBeCalled();
expect(body).toMatchObject({ oh: "my god 😍" });
});

it("should return the original response, including headers", async () => {
nock("https://my-awesome-api.fake")
.get("/")
.reply(200, { oh: "my god 😍" }, { "X-custom-header": "custom value" });

const MyAwesomeComponent = () => {
const { loading, response } = useGet({
path: "/",
});

return loading ? (
<div data-testid="loading">Loading…</div>
) : (
<>
<div data-testid="response">{response ? JSON.stringify(response) : null}</div>
<div data-testid="custom-header">{response?.headers.get("X-custom-header") || ""}</div>
</>
);
};

const { getByTestId } = render(
<RestfulProvider base="https://my-awesome-api.fake">
<MyAwesomeComponent />
</RestfulProvider>,
);

await waitForElement(() => getByTestId("response"));
expect(getByTestId("response")).not.toBeEmpty();
expect(getByTestId("custom-header")).toHaveTextContent("custom value");
});
});

describe("url composition", () =>
Expand Down Expand Up @@ -645,67 +676,6 @@ describe("useGet hook", () => {
});
});

describe("originalResponse", () => {
it("should return original response when flag is enabled", async () => {
nock("https://my-awesome-api.fake")
.get("/")
.reply(200, { oh: "my god 😍" }, { "X-custom-header": "custom value" });

const MyAwesomeComponent = () => {
const { loading, response } = useGet({
path: "/",
originalResponse: true,
});

return loading ? (
<div data-testid="loading">Loading…</div>
) : (
<>
<div data-testid="response">{response ? JSON.stringify(response) : null}</div>
<div data-testid="header">{response?.headers.get("X-custom-header") || ""}</div>
</>
);
};

const { getByTestId } = render(
<RestfulProvider base="https://my-awesome-api.fake">
<MyAwesomeComponent />
</RestfulProvider>,
);

await waitForElement(() => getByTestId("response"));
expect(getByTestId("response")).not.toBeEmpty();
expect(getByTestId("header")).toHaveTextContent("custom value");
});

it("should not return original response when flag is disabled", async () => {
nock("https://my-awesome-api.fake")
.get("/")
.reply(200, { oh: "my god 😍" });

const MyAwesomeComponent = () => {
const { loading, response } = useGet({
path: "/",
});

return loading ? (
<div data-testid="loading">Loading…</div>
) : (
<div data-testid="response">{response ? JSON.stringify(response) : null}</div>
);
};

const { getByTestId } = render(
<RestfulProvider base="https://my-awesome-api.fake">
<MyAwesomeComponent />
</RestfulProvider>,
);

await waitForElement(() => getByTestId("response"));
expect(getByTestId("response")).toBeEmpty();
});
});

describe("actions", () => {
it("should refetch", async () => {
nock("https://my-awesome-api.fake")
Expand Down
8 changes: 2 additions & 6 deletions src/useGet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ export interface UseGetProps<TData, TError, TQueryParams, TPathParams> {
}
| boolean
| number;
/**
* Should the original response object be returned?
*/
originalResponse?: boolean;
}

async function _fetchData<TData, TError, TQueryParams, TPathParams>(
Expand Down Expand Up @@ -147,7 +143,7 @@ async function _fetchData<TData, TError, TQueryParams, TPathParams>(
loading: false,
data: null,
error,
response: props.originalResponse ? originalResponse : null,
response: originalResponse,
});

if (!props.localErrorOnly && context.onError) {
Expand All @@ -161,7 +157,7 @@ async function _fetchData<TData, TError, TQueryParams, TPathParams>(
error: null,
loading: false,
data: resolve(data),
response: props.originalResponse ? originalResponse : null,
response: originalResponse,
});
} catch (e) {
// avoid state updates when component has been unmounted
Expand Down

0 comments on commit e1896fa

Please sign in to comment.