ftmocks-utils 1.0.8 → 1.0.9

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 +45 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ftmocks-utils",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "Util functions for FtMocks",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -198,6 +198,49 @@ async function resetAllMockStats({testMockData, testConfig, testName}) {
198
198
  }
199
199
  }
200
200
 
201
+ async function initiatePlaywrightRoutes (page, ftmocksConifg, testName) {
202
+ const testMockData = testName ? loadMockDataFromConfig(ftmocksConifg, testName) : [];
203
+ resetAllMockStats({testMockData, testConfig: ftmocksConifg, testName});
204
+ const defaultMockData = getDefaultMockDataFromConfig(ftmocksConifg);
205
+ console.debug('calling initiatePlaywrightRoutes fetch');
206
+ await page.route('**/*', async (route, request) => {
207
+ const url = request.url();
208
+ const options = {
209
+ options: {
210
+ url,
211
+ method: request.method(),
212
+ body: request.postData(),
213
+ }
214
+ }
215
+ console.debug('got fetch request', request.method(), request.url(), request.postData());
216
+ let mockData = getMatchingMockData({testMockData, defaultMockData, url, options, testConfig: ftmocksConifg, testName});
217
+ if (mockData) {
218
+ console.debug('mocked', url, options);
219
+ } else {
220
+ console.debug('missing mock data', url, options);
221
+ return route.fulfill({
222
+ status: 404,
223
+ headers: new Map([['content-type', 'application/json']]),
224
+ json: () => Promise.resolve({ error: 'Mock data not found' }),
225
+ });
226
+ }
227
+
228
+ const { content, headers, status } = mockData.response;
229
+
230
+ const json = {
231
+ status,
232
+ headers,
233
+ json: () => Promise.resolve(JSON.parse(content)),
234
+ };
235
+
236
+ if(json) {
237
+ await route.fulfill(json);
238
+ } else {
239
+ await route.fallback();
240
+ }
241
+ });
242
+ }
243
+
201
244
  async function initiateJestFetch (jest, ftmocksConifg, testName) {
202
245
  const testMockData = testName ? loadMockDataFromConfig(ftmocksConifg, testName) : [];
203
246
  resetAllMockStats({testMockData, testConfig: ftmocksConifg, testName});
@@ -403,5 +446,6 @@ module.exports = {
403
446
  saveSnap,
404
447
  deleteAllSnaps,
405
448
  deleteAllLogs,
406
- initiateConsoleLogs
449
+ initiateConsoleLogs,
450
+ initiatePlaywrightRoutes
407
451
  };