axios-mock-adapter
axios-mock-adapter copied to clipboard
how to ignore queryString (params) when doing a mock
tried:
mock.onGet( apiPath('/Facility') ).reply(config=>{
console.log(config);
return [
200,newItem(1)
]
})
mock.onGet( apiPath('/Facility'),{ params: { facilityId: /\d+/ } } ).reply(config=>{
console.log(config);
return [
200,newItem(1)
]
})
apiPath is just a function that returns a full apiUrl, whether it be dev, staging or production. any help would be appreciated
I'm trying to mock a GET request without specifying the params that it should match on.
According to the example docs:
// Mock any GET request to /users
// arguments for reply are (status, data, headers)
mock.onGet('/users').reply(200, {
users: [
{ id: 1, name: 'John Smith' }
]
});
You should be able to mock just a URL without specifying params and this should match on all GET requests with whatever query params they send in. This however is not the case. If a request sends in query params then this mock doesn't match. Is this the same problem you're having @r3wt ?
I also face this behavior. However, I believe the current behavior has to be matched to any query string passed in the URL.
Example:
mockApi.onGet('/tools').reply(200, []);
It should match /tools, /tools?filter1=value1, /tools?filter1=value2 or /tools?filter1=value1&filter2=value2 ... and so on.
Down here, you see how you're behaving these days, which, as I said above, I find incorrect.
it('should test', () => {
mockApi.onGet('/tools').reply(200, []);
fireEvent.click(getByTestId('tags'));
fireEvent.input(getByTestId('query'), {
target: { value: 'foobar' },
});
// ...
});
At this point, the application makes two queries in the API:
- GET
/tools- (All tools) - GET
/tools?q={query}- (Filtered tools)
The second time it fails, returning status 404. It did not match the URL with the filter parameters.
Although I think it is wrong, I solved my problem with the following code:
it('should test', () => {
mockApi.onGet(/^\/tools.*/).reply(200, []);
fireEvent.click(getByTestId('tags'));
fireEvent.input(getByTestId('query'), {
target: { value: 'foobar' },
});
// ...
});
What does @ctimmerm think? Could we wait for a PR on this, or do not you think it's valid?
I'm also facing the same issue. Please prioritize.
Now, it is still not working! Please make it works?
The solution for this problem is to use regex @r3wt in your case that would look like this:
mock.onGet( apiPath(/\/Facility/) ).reply(config=>{
return [
200,newItem(1)
]
})
This would match request to /Facility and also /Facility?x=1&y=2 etc