Skip to content

Commit

Permalink
Merge pull request #6 from fac20/fetch-helper
Browse files Browse the repository at this point in the history
test for fetchHelper + test passes
  • Loading branch information
fairyaksh authored Sep 17, 2020
2 parents 6740ac7 + a7418ca commit 76ca1cd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/modules/fetch-helper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const API_BASE = "https://fishbase.ropensci.org/";

const fetchHelper = (url) => {
fetch(`${API_BASE}${url}`).then(checkResponse);
return fetch(`${API_BASE}${url}`).then(checkResponse);
};

const checkResponse = (res) => {
Expand Down
18 changes: 18 additions & 0 deletions src/modules/fetch-helper.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { fetchHelper } from "./fetch-helper";

const mockResponse = { data: [{ SpeciesRefNo: 168 }] };
global.fetch = jest // any time fetch runs, we're going to replace it with the anonymous func @ line 6
.fn()
.mockImplementation(() => {
return Promise.resolve({
// promise will resolve into lines 8 + 9
ok: true,
json: () => Promise.resolve(mockResponse),
});
});

test("Get input and receive a response", () => {
return fetchHelper("species/132").then((res) => {
expect(res.data[0].SpeciesRefNo).toEqual(168);
});
});

0 comments on commit 76ca1cd

Please sign in to comment.