I decided it another way: introducing axioms in dependence on action. I prefer this approach with respect to rewriting dependencies.
So, I used the same approach to test the components related to reduction. When I export actions, I export two versions: one with (for use for components) and one without (for testing) binding bindings.
Here's what the actions.js file looks like:
import axios from 'axios' export const loadDataRequest = () => { return { type: 'LOAD_DATA_REQUEST' } } export const loadDataError = () => { return { type: 'LOAD_DATA_ERROR' } } export const loadDataSuccess = (data) =>{ return { type: 'LOAD_DATA_SUCCESS', data } } export const loadData = (axios) => { return dispatch => { dispatch(loadDataRequest()) axios .get('http://httpbin.org/ip') .then(({data})=> dispatch(loadDataSuccess(data))) .catch(()=> dispatch(loadDataError())) } } export default { loadData: loadData.bind(null, axios) }
Then testing with jest (actions.test.js):
import { loadData } from './actions' describe('testing loadData', ()=>{ test('loadData with success', (done)=>{ const get = jest.fn() const data = { mydata: { test: 1 } } get.mockReturnValue(Promise.resolve({data})) let callNumber = 0 const dispatch = jest.fn(params =>{ if (callNumber===0){ expect(params).toEqual({ type: 'LOAD_DATA_REQUEST' }) } if (callNumber===1){ expect(params).toEqual({ type: 'LOAD_DATA_SUCCESS', data: data }) done() } callNumber++ }) const axiosMock = { get } loadData(axiosMock)(dispatch) }) })
When using actions inside a component, I import everything:
import Actions from './actions'
And send:
Actions.loadData() // this is the version with axios binded.
source share