ftmocks-utils 1.0.1 → 1.0.2

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 +69 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ftmocks-utils",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Util functions for FtMocks",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -5,6 +5,34 @@ export const nameToFolder = name => {
5
5
  return name.replaceAll(' ', '_');
6
6
  };
7
7
 
8
+ const areJsonEqual = (jsonObj1, jsonObj2) => {
9
+ // Check if both are objects and not null
10
+ if (typeof jsonObj1 === 'object' && jsonObj1 !== null &&
11
+ typeof jsonObj2 === 'object' && jsonObj2 !== null) {
12
+
13
+ // Get the keys of both objects
14
+ const keys1 = Object.keys(jsonObj1);
15
+ const keys2 = Object.keys(jsonObj2);
16
+
17
+ // Check if the number of keys is different
18
+ if (keys1.length !== keys2.length) {
19
+ return false;
20
+ }
21
+
22
+ // Recursively check each key-value pair
23
+ for (let key of keys1) {
24
+ if (!keys2.includes(key) || !areJsonEqual(jsonObj1[key], jsonObj2[key])) {
25
+ return false;
26
+ }
27
+ }
28
+
29
+ return true;
30
+ } else {
31
+ // For non-object types, use strict equality comparison
32
+ return jsonObj1 === jsonObj2;
33
+ }
34
+ }
35
+
8
36
  const getDefaultMockDataFromConfig = (testConfig) => {
9
37
  const defaultPath = path.join(testConfig.MOCK_DIR, testConfig.MOCK_DEFAULT_FILE);
10
38
 
@@ -51,9 +79,10 @@ const loadMockDataFromConfig = (testConfig, _testName) => {
51
79
  mock.fileContent = fileContent;
52
80
  });
53
81
 
82
+
54
83
  return mocks;
55
84
  } catch (error) {
56
- console.error('Error loading test data:', error.message);
85
+ console.debug('Error loading test data:', error.message);
57
86
  return [];
58
87
  }
59
88
  };
@@ -104,11 +133,46 @@ function compareMockToFetchRequest(mock, fetchReq) {
104
133
  const postData = mock.fileContent.request?.postData?.text ? JSON.parse(mock.fileContent.request?.postData?.text) : mock.fileContent.request?.postData;
105
134
  return isSameRequest({url: mockURL, method: mock.fileContent.method, postData}, {
106
135
  method: fetchReq.options.method || 'GET',
107
- postData: fetchReq.options.body,
136
+ postData: fetchReq.options.body?.length ? JSON.parse(fetchReq.options.body) : fetchReq.options.body,
108
137
  url: reqURL,
109
138
  });
110
139
  }
111
140
 
141
+ function getMatchingMockData({testMockData, defaultMockData, url, options, testConfig, testName}) {
142
+ let served = false;
143
+ let matchedMocks = testMockData?.filter(mock => {
144
+ if (mock.fileContent.waitForPrevious && !served) {
145
+ return false;
146
+ }
147
+ served = mock.fileContent.served;
148
+ return compareMockToFetchRequest(mock, { url, options });
149
+ }) || [];
150
+ let foundMock = matchedMocks.find(mock => !mock.fileContent.served) ? matchedMocks.find(mock => !mock.fileContent.served) : matchedMocks[0];
151
+ // updating stats to mock file
152
+ if(foundMock) {
153
+ const mockFilePath = path.join(testConfig.MOCK_DIR, `test_${nameToFolder(testName)}`, `mock_${foundMock.id}.json`);
154
+ foundMock.fileContent.served = true;
155
+ fs.writeFileSync(mockFilePath, JSON.stringify(foundMock.fileContent, null, 2));
156
+ }
157
+
158
+ if(!foundMock) {
159
+ foundMock = defaultMockData.find(tm => compareMockToFetchRequest(tm, {
160
+ url,
161
+ options
162
+ }));
163
+ }
164
+ return foundMock ? foundMock.fileContent : null;
165
+ }
166
+
167
+ async function resetAllMockStats({testMockData, testConfig, testName}) {
168
+ for(let i=0; i<testMockData.length; i++) {
169
+ const tmd = testMockData[i];
170
+ const mockFilePath = path.join(testConfig.MOCK_DIR, `test_${nameToFolder(testName)}`, `mock_${tmd.id}.json`);
171
+ tmd.fileContent.served = false;
172
+ await fs.writeFileSync(mockFilePath, JSON.stringify(tmd.fileContent, null, 2));
173
+ }
174
+ }
175
+
112
176
 
113
177
  // Export functions as a module
114
178
  module.exports = {
@@ -118,5 +182,7 @@ module.exports = {
118
182
  loadMockDataFromConfig,
119
183
  getDefaultMockDataFromConfig,
120
184
  nameToFolder,
121
- compareMockToFetchRequest
185
+ compareMockToFetchRequest,
186
+ getMatchingMockData,
187
+ resetAllMockStats
122
188
  };