Skip to content

Commit dddbab2

Browse files
Added mockFetch and cleaned up http tests but the test are currently not running. I think this is because of the babel version, eslint or something build related.
1 parent 2742b58 commit dddbab2

File tree

2 files changed

+150
-4
lines changed

2 files changed

+150
-4
lines changed

src/modules/http/tests/http.test.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@ import * as store from "../../store";
22
import localStorage from "localStorage";
33
import http from "../http";
44
import fetch from "cross-fetch";
5-
import axios from "axios";
6-
import fileDownload from "react-file-download";
75
import mockFetch from "../../../test/mockFetch";
86

97
const expectedHostName = "expectedHostName";
108

119
jest.mock("../../store");
1210
jest.mock("cross-fetch");
13-
jest.mock("axios");
14-
jest.mock("react-file-download");
1511
jest.mock("../http.constants", () => ({
1612
API_URL: "http://expectedHostName/api"
1713
}));

src/test/mockFetch.js

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import _ from "lodash";
2+
3+
const buildFetchErrorResponse = (data, status) =>
4+
Promise.resolve({
5+
ok: false,
6+
status: status || 400,
7+
headers: {
8+
get: () => null,
9+
map: {
10+
"content-type": ""
11+
}
12+
},
13+
json: () => Promise.resolve(data)
14+
});
15+
16+
const buildFetchResponse = data =>
17+
Promise.resolve({
18+
ok: {},
19+
headers: {
20+
get: () => null,
21+
map: {
22+
"content-type": ""
23+
}
24+
},
25+
json: () => Promise.resolve(data)
26+
});
27+
28+
const mockFetch = (stubConfig, { calls } = {}) => {
29+
stubConfig.sort((a, b) => {
30+
const aIsArray = !!a.reduce;
31+
const bIsArray = !!b.reduce;
32+
33+
if (!aIsArray === bIsArray) {
34+
return 0;
35+
}
36+
37+
if (aIsArray) {
38+
return -1;
39+
}
40+
41+
return 1;
42+
});
43+
44+
if (!stubConfig) {
45+
throw new Error("stubConfig is required!");
46+
}
47+
48+
const NOT_FOUND = -1;
49+
if (
50+
!stubConfig.length ||
51+
stubConfig.findIndex(
52+
c =>
53+
!c.url ||
54+
(!c.response && typeof c.response === "function") ||
55+
(!c.errorResponse && typeof c.errorResponse === "function")
56+
) > NOT_FOUND
57+
) {
58+
throw new Error(
59+
'You must specify at least one config as part of the stubConfig argument and each config must have a valid format. Example: [{url:"/foo", response: fooResponseGetter}]'
60+
);
61+
}
62+
63+
if (calls) {
64+
calls.get = function(url, method) {
65+
return this.find(
66+
c =>
67+
((!c.config.method && (!method || method.toLowerCase() === "get")) ||
68+
(c.config.method &&
69+
c.config.method.toLowerCase() === method.toLowerCase())) &&
70+
c.url.includes(url)
71+
);
72+
};
73+
74+
calls.expect = function(url, method) {
75+
expect(!!this.get(url, method)).toBeTruthy();
76+
};
77+
78+
calls.getBody = function(url, method) {
79+
const call = this.get(url, method);
80+
81+
const body = _.get(call, "config.body");
82+
83+
return body && JSON.parse(body);
84+
};
85+
}
86+
87+
return (url, config) => {
88+
let foundUrl;
89+
90+
const isGetOrDefault = (stubUrlConfig, curConfig) =>
91+
(!curConfig.method || curConfig.method.toLocaleString() === "get") &&
92+
(!stubUrlConfig.method || stubUrlConfig.method.toLowerCase() === "get");
93+
94+
const stubMethodMatchesConfig = stubUrlConfig =>
95+
stubUrlConfig.method &&
96+
config.method &&
97+
config.method.toLowerCase() === stubUrlConfig.method.toLowerCase();
98+
99+
const methodIsGetOrMethodsMatch = (stubUrlConfig, curConfig) =>
100+
isGetOrDefault(stubUrlConfig, curConfig) ||
101+
stubMethodMatchesConfig(stubUrlConfig);
102+
103+
const urlIncludesFragments = (url, fragments) => {
104+
if (fragments.reduce) {
105+
return fragments.reduce((acc, cur) => {
106+
if (!acc) {
107+
return acc;
108+
}
109+
return url.includes(cur);
110+
}, true);
111+
}
112+
113+
return url.includes(fragments);
114+
};
115+
116+
foundUrl = stubConfig.find(
117+
stubUrlConfig =>
118+
methodIsGetOrMethodsMatch(stubUrlConfig, config) &&
119+
urlIncludesFragments(url, stubUrlConfig.url)
120+
);
121+
122+
if (foundUrl) {
123+
if (calls && calls.length !== undefined) {
124+
calls.push({
125+
url,
126+
config
127+
});
128+
}
129+
130+
if (foundUrl.errorResponse) {
131+
return buildFetchErrorResponse(
132+
foundUrl.response(),
133+
foundUrl.statusCode
134+
);
135+
} else {
136+
return buildFetchResponse(foundUrl.response());
137+
}
138+
}
139+
140+
throw new Error(
141+
`There was an unexpected ajax call. Details follow...
142+
url: ${url}
143+
method: ${config.method || "GET"}
144+
config: ${JSON.stringify(config)}.
145+
stubConfig: ${JSON.stringify(stubConfig)}`
146+
);
147+
};
148+
};
149+
150+
export default mockFetch;

0 commit comments

Comments
 (0)