Skip to content

Commit

Permalink
Checks the returned response in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NSeydoux committed Jun 3, 2020
1 parent 1b56737 commit ee40efb
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/nonRdfData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ describe("Non-RDF data fetch", () => {
)
);

await fetchFile("https://some.url");
const response = await fetchFile("https://some.url");

expect(fetcher.fetch.mock.calls).toEqual([["https://some.url", {}]]);
expect(response).toEqual(
new Response("Some data", { status: 200, statusText: "OK" })
);
});

it("should GET a remote resource using the provided fetcher", async () => {
Expand All @@ -45,9 +48,12 @@ describe("Non-RDF data fetch", () => {
)
);

await fetchFile("https://some.url", { fetch: mockFetch });
const response = await fetchFile("https://some.url", { fetch: mockFetch });

expect(mockFetch.mock.calls).toEqual([["https://some.url", {}]]);
expect(response).toEqual(
new Response("Some data", { status: 200, statusText: "OK" })
);
});

it("should pass the request headers through", async () => {
Expand All @@ -59,7 +65,7 @@ describe("Non-RDF data fetch", () => {
)
);

await fetchFile("https://some.url", {
const response = await fetchFile("https://some.url", {
fetch: mockFetch,
headers: new Headers({ Accept: "text/turtle" }),
});
Expand All @@ -72,5 +78,27 @@ describe("Non-RDF data fetch", () => {
},
],
]);
expect(response).toEqual(
new Response("Some data", { status: 200, statusText: "OK" })
);
});

it("should return the response even on failure", async () => {
const mockFetch = jest
.fn(window.fetch)
.mockReturnValue(
Promise.resolve(
new Response(undefined, { status: 400, statusText: "Bad request" })
)
);

const response = await fetchFile("https://some.url", {
fetch: mockFetch,
});

expect(mockFetch.mock.calls).toEqual([["https://some.url", {}]]);
expect(response).toEqual(
new Response(undefined, { status: 400, statusText: "Bad request" })
);
});
});

0 comments on commit ee40efb

Please sign in to comment.