There are a couple of problems in your code:
First, in the action creator you are using an axios instance to make the ajax call, but in the test you are not providing that instance to the axios-mock-adapter. You should provide your axios instance in your test when you create the instance of MockAdapter.
Second, the params property you are providing to the axios mock in the onGet method does not match the parameters that are sent in the get operation in your action creator. You should match the parameters in the call with their values. Thus, you should provide query and page params.
Last, you are returning the expectedActions in the mock request, but that does not seem right. Looking at your code, it seems that you want to return an empty array.
Having all that into account, your code would look like:
import MockAdapter from 'axios-mock-adapter';
import thunk from 'redux-thunk';
import configureMockStore from 'redux-mock-store';
import axiosInstence from '../../apis/axiosInstence';
import { searchPhotos } from '../searchAction';
const mock = new MockAdapter(axiosInstence);
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
const term = 'cars';
const store = mockStore({});
describe('actions', () => {
beforeEach(() => {
mock.reset();
store.clearActions();
});
it('Should create an action to signIn with a fake user', async () => {
const expectedActions = [{
type: 'SEACH_REQUEST'
}, {
type: 'SEARCH_PHOTOS',
payload: []
}];
mock.onGet('/search/photos', {
params: {
query: 'cars',
page: 1
}
}).reply(200, []);
const data = await store.dispatch(searchPhotos(term));
const receivedActions = store.getActions();
expect(receivedActions).toEqual(expectedActions);
});
});
Answer from mgarcia on Stack Overflow
» npm install axios-mock-adapter
`onPost()` not mocking and returning actual Axios response
Newest 'axios-mock-adapter' Questions - Stack Overflow
unit testing - Mock axios with axios-mock-adapter get undefined resp - Stack Overflow
reactjs - Cant make axios-mock-adpter work properly - Stack Overflow
Two things to try here:
- Maybe you already have this elsewhere in your code, but be sure to set up mockAdaptor:
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
const mockAdapter = new MockAdapter(axios);
- I haven't found a way to get the mock adapter working when the function you are testing uses 'axios.create' to set up a new axios instance. Try something along the lines of this instead:
// api/index.js
const api = {
get(path) {
return axios.get('/api' + path)
.then((response) => {
return response.data;
});
}
}
export default api;
For anyone still struggling with this.
You need to make sure you iniatilise your MockAdapter outside a test body.
ie. Incorrect
it('should do a thing', () => {
const mockAdapter = new MockAdapter(axios);
})
Correct
const mockAdapter = new MockAdapter(axios);
it('should pass' () => {})
I tried:
axios-mock-adapter
Axios test fails when you use a proxy in your package.json.
Error: connect ECONNREFUSED 127.0.0.1:80
Other then that, it works great. But I'm unable to use my CI because it outputs the error above too.
Nock
Works great, but I can't seem to mock requests with headers sent (such as authorization). I find it hard to find information on how to solve the issue.
I'm wondering if there are other libraries I should try out (or bonus points if there is a solution to the problems above :)!
As library documentation explains, unmocked requests should be explicitly allowed:
// Mock specific requests, but let unmatched ones through
mock
.onGet('/foo').reply(200)
.onPut('/bar', { xyz: 'abc' }).reply(204)
.onAny().passThrough();
Adding to @Estus answer, we can forward other requests to server as below as well.
// Mock all requests to /foo with HTTP 200,
// but forward any others requests to server
var mock = new MockAdapter(axiosInstance, { onNoMatch: "passthrough" });
mock.onAny("/foo").reply(200);