ftmocks-utils 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.js +111 -3
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -34,7 +34,7 @@ const areJsonEqual = (jsonObj1, jsonObj2) => {
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
const getDefaultMockDataFromConfig = (testConfig) => {
|
|
37
|
-
const defaultPath = path.join(testConfig.MOCK_DIR, '
|
|
37
|
+
const defaultPath = path.join(testConfig.MOCK_DIR, 'default.json');
|
|
38
38
|
|
|
39
39
|
try {
|
|
40
40
|
const defaultData = fs.readFileSync(defaultPath, 'utf8');
|
|
@@ -87,7 +87,17 @@ const loadMockDataFromConfig = (testConfig, _testName) => {
|
|
|
87
87
|
}
|
|
88
88
|
};
|
|
89
89
|
|
|
90
|
+
const clearNulls = postData => {
|
|
91
|
+
Object.keys(postData || {}).forEach(key => {
|
|
92
|
+
if(postData[key] === null) {
|
|
93
|
+
delete postData[key];
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
|
|
90
98
|
const isSameRequest = (req1, req2) => {
|
|
99
|
+
clearNulls(req1.postData);
|
|
100
|
+
clearNulls(req2.postData);
|
|
91
101
|
let matched = true;
|
|
92
102
|
if(req1.url !== req2.url) {
|
|
93
103
|
matched = false;
|
|
@@ -147,7 +157,7 @@ function getMatchingMockData({testMockData, defaultMockData, url, options, testC
|
|
|
147
157
|
served = mock.fileContent.served;
|
|
148
158
|
return compareMockToFetchRequest(mock, { url, options });
|
|
149
159
|
}) || [];
|
|
150
|
-
let foundMock = matchedMocks.find(mock => !mock.fileContent.served) ? matchedMocks.find(mock => !mock.fileContent.served) : matchedMocks[
|
|
160
|
+
let foundMock = matchedMocks.find(mock => !mock.fileContent.served) ? matchedMocks.find(mock => !mock.fileContent.served) : matchedMocks[matchedMocks.length - 1];
|
|
151
161
|
// updating stats to mock file
|
|
152
162
|
if(foundMock) {
|
|
153
163
|
const mockFilePath = path.join(testConfig.MOCK_DIR, `test_${nameToFolder(testName)}`, `mock_${foundMock.id}.json`);
|
|
@@ -173,6 +183,103 @@ async function resetAllMockStats({testMockData, testConfig, testName}) {
|
|
|
173
183
|
}
|
|
174
184
|
}
|
|
175
185
|
|
|
186
|
+
async function initiateJestFetch (jest, ftmocksConifg, testName) {
|
|
187
|
+
const testMockData = testName ? loadMockDataFromConfig(ftmocksConifg, testName) : [];
|
|
188
|
+
resetAllMockStats({testMockData, testConfig: ftmocksConifg, testName});
|
|
189
|
+
const defaultMockData = getDefaultMockDataFromConfig(ftmocksConifg);
|
|
190
|
+
global.fetch = jest.fn((url, options = {}) => {
|
|
191
|
+
let mockData = getMatchingMockData({testMockData, defaultMockData, url, options, testConfig: ftmocksConifg, testName});
|
|
192
|
+
if (mockData) {
|
|
193
|
+
console.debug('mocked', url, options);
|
|
194
|
+
} else {
|
|
195
|
+
console.debug('missing mock data', url, options);
|
|
196
|
+
return Promise.resolve({
|
|
197
|
+
status: 404,
|
|
198
|
+
headers: new Map([['content-type', 'application/json']]),
|
|
199
|
+
json: () => Promise.resolve({ error: 'Mock data not found' }),
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const { content, headers, status } = mockData.response;
|
|
204
|
+
|
|
205
|
+
return Promise.resolve({
|
|
206
|
+
status,
|
|
207
|
+
headers,
|
|
208
|
+
json: () => Promise.resolve(JSON.parse(content)),
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
global.XMLHttpRequest = jest.fn(function () {
|
|
213
|
+
const xhrMock = {
|
|
214
|
+
open: jest.fn(),
|
|
215
|
+
send: jest.fn(),
|
|
216
|
+
setRequestHeader: jest.fn(),
|
|
217
|
+
getAllResponseHeaders: jest.fn(() => {
|
|
218
|
+
return '';
|
|
219
|
+
}),
|
|
220
|
+
getResponseHeader: jest.fn((header) => {
|
|
221
|
+
return null;
|
|
222
|
+
}),
|
|
223
|
+
readyState: 4,
|
|
224
|
+
status: 0,
|
|
225
|
+
response: null,
|
|
226
|
+
responseText: '',
|
|
227
|
+
onreadystatechange: null,
|
|
228
|
+
onload: null,
|
|
229
|
+
onerror: null,
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
xhrMock.send.mockImplementation(function () {
|
|
233
|
+
const mockData = getMatchingMockData({
|
|
234
|
+
testMockData,
|
|
235
|
+
defaultMockData,
|
|
236
|
+
url: xhrMock._url,
|
|
237
|
+
options: xhrMock._options,
|
|
238
|
+
testConfig: ftmocksConifg,
|
|
239
|
+
testName,
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
if (mockData) {
|
|
243
|
+
console.debug('mocked', xhrMock._url, xhrMock._options);
|
|
244
|
+
const { content, headers, status } = mockData.response;
|
|
245
|
+
|
|
246
|
+
xhrMock.status = status;
|
|
247
|
+
xhrMock.responseText = content;
|
|
248
|
+
xhrMock.response = content;
|
|
249
|
+
xhrMock.headers = headers;
|
|
250
|
+
|
|
251
|
+
if (xhrMock.onreadystatechange) {
|
|
252
|
+
xhrMock.onreadystatechange();
|
|
253
|
+
}
|
|
254
|
+
if (xhrMock.onload) {
|
|
255
|
+
xhrMock.onload();
|
|
256
|
+
}
|
|
257
|
+
} else {
|
|
258
|
+
console.debug('missing mock data', xhrMock._url, xhrMock._options);
|
|
259
|
+
|
|
260
|
+
xhrMock.status = 404;
|
|
261
|
+
xhrMock.responseText = JSON.stringify({ error: 'Mock data not found' });
|
|
262
|
+
xhrMock.response = xhrMock.responseText;
|
|
263
|
+
|
|
264
|
+
if (xhrMock.onreadystatechange) {
|
|
265
|
+
xhrMock.onreadystatechange();
|
|
266
|
+
}
|
|
267
|
+
if (xhrMock.onerror) {
|
|
268
|
+
xhrMock.onerror();
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
xhrMock.open.mockImplementation(function (method, url) {
|
|
274
|
+
xhrMock._options = { method };
|
|
275
|
+
xhrMock._url = url;
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
return xhrMock;
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
return;
|
|
282
|
+
};
|
|
176
283
|
|
|
177
284
|
// Export functions as a module
|
|
178
285
|
module.exports = {
|
|
@@ -184,5 +291,6 @@ module.exports = {
|
|
|
184
291
|
nameToFolder,
|
|
185
292
|
compareMockToFetchRequest,
|
|
186
293
|
getMatchingMockData,
|
|
187
|
-
resetAllMockStats
|
|
294
|
+
resetAllMockStats,
|
|
295
|
+
initiateJestFetch
|
|
188
296
|
};
|