Michael
Michael

Reputation: 405

How do I test react Request parameters with jest?

I have a get request in react:

  axiosInstance.get(`${apiUrl}`, {
    params: {
      ...callParameters,
    },
  })

Which I can then mock with:

      mockAxios.get.mockImplementationOnce(() =>
        Promise.resolve({
          data: { records: mockResponse }
        })
      );

But how can I check what params the API was called with?

Upvotes: 0

Views: 1946

Answers (1)

kidney
kidney

Reputation: 3083

You should be able to get the mocked function calls using:

axiosInstance.get.mock.calls[0][1]

where the first index in calls is the number of the call and the second index is the argument index (0 would be the URL, 1 should be the config object).

See https://jestjs.io/docs/en/mock-function-api#mockfnmockcalls

Upvotes: 2

Related Questions