axios-mock-adapter icon indicating copy to clipboard operation
axios-mock-adapter copied to clipboard

how to ignore queryString (params) when doing a mock

Open r3wt opened this issue 7 years ago • 5 comments

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

r3wt avatar May 23 '18 19:05 r3wt

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 ?

BrendanBall avatar Jul 09 '18 09:07 BrendanBall

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:

  1. GET /tools - (All tools)
  2. 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?

italoiz avatar Jul 15 '19 17:07 italoiz

I'm also facing the same issue. Please prioritize.

pavankmv avatar Feb 11 '21 13:02 pavankmv

Now, it is still not working! Please make it works?

vunb avatar Mar 10 '21 06:03 vunb

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

AregShahbazian avatar Jan 20 '22 18:01 AregShahbazian