ftmocks-utils 1.0.4 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +99 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ftmocks-utils",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Util functions for FtMocks",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -183,6 +183,103 @@ async function resetAllMockStats({testMockData, testConfig, testName}) {
183
183
  }
184
184
  }
185
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
+ };
186
283
 
187
284
  // Export functions as a module
188
285
  module.exports = {
@@ -194,5 +291,6 @@ module.exports = {
194
291
  nameToFolder,
195
292
  compareMockToFetchRequest,
196
293
  getMatchingMockData,
197
- resetAllMockStats
294
+ resetAllMockStats,
295
+ initiateJestFetch
198
296
  };