Skip to content

Commit 7ae2a63

Browse files
authored
Merge pull request #191 from JoaoBrlt/master
feat: add options to ignore query params
2 parents e7b15b7 + 9e1a104 commit 7ae2a63

5 files changed

Lines changed: 153 additions & 11 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# IDE files
2+
.idea
23
.vscode
34

45
# dependencies

packages/mock-addon-docs/stories/docs/advanced-setup.stories.mdx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ You can set <strong>global configuration</strong> for the addon. Go to the `.sto
2020

2121

2222

23-
| Property | Description | Default |
24-
| ---------- | :------------------------------------------------------------------------------------------ | :------ |
25-
| `globalMockData` | An array of mock objects which will add in every story | [] |
26-
| `refreshStoryOnUpdate` | This property re-renders the story if there's any data changes | false |
27-
| `disableUsingOriginal` | This property disables the toggle (on/off) option to use the original endpoint | false |
28-
| `disable` | This property disables the panel from all the stories | false |
23+
| Property | Description | Default |
24+
| ------------------------ | :----------------------------------------------------------------------------- | :------ |
25+
| `globalMockData` | An array of mock objects which will add in every story | [] |
26+
| `ignoreQueryParams` | Whether or not to ignore query parameters globally | false |
27+
| `refreshStoryOnUpdate` | This property re-renders the story if there's any data changes | false |
28+
| `disableUsingOriginal` | This property disables the toggle (on/off) option to use the original endpoint | false |
29+
| `disable` | This property disables the panel from all the stories | false |
2930

3031

3132
```js
@@ -39,6 +40,7 @@ export const parameters = {
3940
status: 201,
4041
response: {},
4142
}],
43+
ignoreQueryParams: true, // Whether or not to ignore query parameters globally
4244
refreshStoryOnUpdate: true, // This property re-renders the story if there's any data changes
4345
disableUsingOriginal: false, // This property disables the toggle (on/off) option to use the original endpoint
4446
disable: true, // This property disables the panel from all the stories

packages/mock-addon/src/utils/faker.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export class Faker {
3131
global.XMLHttpRequest = this.MockXhr;
3232

3333
this.requestMap = {};
34+
this.ignoreQueryParams = false;
3435
}
3536

3637
getRequests = () => Object.values(this.requestMap);
@@ -51,6 +52,10 @@ export class Faker {
5152
});
5253
};
5354

55+
setIgnoreQueryParams = (value) => {
56+
this.ignoreQueryParams = value;
57+
};
58+
5459
add = (request) => {
5560
const { path, searchParamKeys } = getNormalizedUrl(request.url);
5661
const key = this.getKey(path, searchParamKeys, request.method);
@@ -102,7 +107,7 @@ export class Faker {
102107
if (
103108
match(requestPath)(path) &&
104109
method == requestMethod &&
105-
arrayEquals(searchParamKeys, requestSearchKeys) &&
110+
this.matchQueryParams(searchParamKeys, requestSearchKeys) &&
106111
!this.requestMap[key].skip
107112
) {
108113
return this.requestMap[key];
@@ -112,6 +117,13 @@ export class Faker {
112117
return null;
113118
};
114119

120+
matchQueryParams = (searchParams, requestSearchParams) => {
121+
return (
122+
this.ignoreQueryParams ||
123+
arrayEquals(searchParams, requestSearchParams)
124+
);
125+
};
126+
115127
mockFetch = (input, options) => {
116128
const request = new Request(input, options);
117129
const { url, method } = request;
@@ -237,6 +249,7 @@ export class Faker {
237249

238250
restore = () => {
239251
this.requestMap = {};
252+
this.ignoreQueryParams = false;
240253
};
241254
}
242255

packages/mock-addon/src/utils/faker.test.js

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,38 @@ describe('Faker - matchMock', () => {
200200
response: {},
201201
delay: 0,
202202
},
203+
{
204+
url: 'http://request3.com?foo=1&bar=2',
205+
method: 'GET',
206+
status: 200,
207+
response: {},
208+
delay: 0,
209+
},
210+
{
211+
url: 'http://request4.com',
212+
method: 'GET',
213+
status: 200,
214+
response: {},
215+
delay: 0,
216+
ignoreQueryParams: true,
217+
},
218+
{
219+
url: 'http://request5.com?foo=1&bar=2',
220+
method: 'GET',
221+
status: 200,
222+
response: {},
223+
delay: 0,
224+
ignoreQueryParams: true,
225+
},
203226
];
204227

205228
const [faker, resetMock] = setupMockFaker();
206229

207-
beforeAll(() => {
230+
beforeEach(() => {
208231
faker.makeInitialRequestMap(requests);
209232
});
210233

211-
afterAll(() => {
234+
afterEach(() => {
212235
resetMock();
213236
});
214237

@@ -230,6 +253,100 @@ describe('Faker - matchMock', () => {
230253
expect(actual.method).toEqual(requests[2].method);
231254
expect(actual.skip).toEqual(false);
232255
});
256+
257+
it('should return request if url and query parameters match', () => {
258+
const actual = faker.matchMock(
259+
'http://request3.com?foo=1&bar=2',
260+
'GET'
261+
);
262+
expect(actual.url).toEqual(requests[3].url);
263+
expect(actual.method).toEqual(requests[3].method);
264+
expect(actual.skip).toEqual(false);
265+
});
266+
267+
it('should return request if url and query parameters match with different order', () => {
268+
const actual = faker.matchMock(
269+
'http://request3.com?bar=2&foo=1',
270+
'GET'
271+
);
272+
expect(actual.url).toEqual(requests[3].url);
273+
expect(actual.method).toEqual(requests[3].method);
274+
expect(actual.skip).toEqual(false);
275+
});
276+
277+
it('should return null if unexpected query parameters are provided', () => {
278+
const actual = faker.matchMock('http://request.com?foo=1', 'GET');
279+
expect(actual).toBeNull();
280+
});
281+
282+
it('should return null if query parameters are missing', () => {
283+
const actual = faker.matchMock('http://request3.com?baz=1', 'GET');
284+
expect(actual).toBeNull();
285+
});
286+
287+
it('should return request if unexpected query parameters are provided but are globally ignored', () => {
288+
faker.setIgnoreQueryParams(true);
289+
const actual = faker.matchMock('http://request.com?foo=1', 'GET');
290+
expect(actual.url).toEqual(requests[0].url);
291+
expect(actual.method).toEqual(requests[0].method);
292+
expect(actual.skip).toEqual(false);
293+
});
294+
295+
it('should return request if query parameters are missing but are globally ignored', () => {
296+
faker.setIgnoreQueryParams(true);
297+
const actual = faker.matchMock('http://request3.com?baz=1', 'GET');
298+
expect(actual.url).toEqual(requests[3].url);
299+
expect(actual.method).toEqual(requests[3].method);
300+
expect(actual.skip).toEqual(false);
301+
});
302+
});
303+
304+
describe('Faker - matchQueryParams', () => {
305+
const [faker, resetMock] = setupMockFaker();
306+
307+
afterEach(() => {
308+
resetMock();
309+
});
310+
311+
it('should return true if query parameters match', () => {
312+
const actual = faker.matchQueryParams(
313+
['foo', 'bar'],
314+
['foo', 'bar'],
315+
false
316+
);
317+
expect(actual).toBe(true);
318+
});
319+
320+
it('should return true if query parameters match with different order', () => {
321+
const actual = faker.matchQueryParams(
322+
['foo', 'bar'],
323+
['bar', 'foo'],
324+
false
325+
);
326+
expect(actual).toBe(true);
327+
});
328+
329+
it('should return false if unexpected query parameters are provided', () => {
330+
const actual = faker.matchQueryParams([], ['foo'], false);
331+
expect(actual).toBe(false);
332+
});
333+
334+
it('should return false if query parameters are missing', () => {
335+
const actual = faker.matchQueryParams(['foo', 'bar'], ['baz'], false);
336+
expect(actual).toBe(false);
337+
});
338+
339+
it('should return true if unexpected query parameters are provided but are globally ignored', () => {
340+
faker.setIgnoreQueryParams(true);
341+
const actual = faker.matchQueryParams([], ['foo'], false);
342+
expect(actual).toBe(true);
343+
});
344+
345+
it('should return true if query parameters are missing but are globally ignored', () => {
346+
faker.setIgnoreQueryParams(true);
347+
const actual = faker.matchQueryParams([], ['foo'], false);
348+
expect(actual).toBe(true);
349+
});
233350
});
234351

235352
describe('restore', () => {

packages/mock-addon/src/withRoundTrip.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ export const withRoundTrip = (storyFn, context) => {
1919
refreshStoryOnUpdate: false,
2020
globalMockData: [],
2121
disableUsingOriginal: false,
22+
ignoreQueryParams: false,
2223
});
23-
const { globalMockData, refreshStoryOnUpdate, disableUsingOriginal } =
24-
mockAddonConfigs;
24+
const {
25+
globalMockData,
26+
refreshStoryOnUpdate,
27+
disableUsingOriginal,
28+
ignoreQueryParams,
29+
} = mockAddonConfigs;
2530
const data = [...globalMockData, ...paramData];
2631

2732
/**
@@ -30,6 +35,7 @@ export const withRoundTrip = (storyFn, context) => {
3035
*/
3136
if (INITIAL_MOUNT_STATE) {
3237
faker.makeInitialRequestMap(data);
38+
faker.setIgnoreQueryParams(ignoreQueryParams);
3339

3440
channel.emit(EVENTS.SEND, {
3541
mockData: faker.getRequests(),
@@ -59,10 +65,13 @@ export const withRoundTrip = (storyFn, context) => {
5965
*/
6066
if (STORY_CHANGED_STATE) {
6167
faker.makeInitialRequestMap(data);
68+
faker.setIgnoreQueryParams(ignoreQueryParams);
69+
6270
channel.emit(EVENTS.SEND, {
6371
mockData: faker.getRequests(),
6472
disableUsingOriginal,
6573
});
74+
6675
STORY_CHANGED_STATE = false;
6776
}
6877
return storyFn(context);

0 commit comments

Comments
 (0)