For those who want to use axios-mock-adapter instead of the mockfetch example in redux documentation for asynchronous testing , I have successfully used the following
actions.test.js :
describe('SignInUser', () => { var history = { push: function(str) { expect(str).toEqual('/feed'); } } it('Dispatches authorization', () => { let mock = new MockAdapter(axios); mock.onPost(`${ROOT_URL}/auth/signin`, { email: ' test@test.com ', password: 'test' }).reply(200, {token: 'testToken' }); const expectedActions = [ { type: types.AUTH_USER } ]; const store = mockStore({ auth: [] }); return store.dispatch(actions.signInUser({ email: ' test@test.com ', password: 'test', }, history)).then(() => { expect(store.getActions()).toEqual(expectedActions); }); });
To check the successful case for signInUser in actions/index.js :
export const signInUser = ({ email, password }, history) => async dispatch => { const res = await axios.post(`${ROOT_URL}/auth/signin`, { email, password }) .catch(({ response: { data } }) => { ... }); if (res) { dispatch({ type: AUTH_USER }); // test verified this localStorage.setItem('token', res.data.token); // test mocked this history.push('/feed'); // test mocked this } }
Given that this is a joke, calling localstorage should have been a mockery. This was in src/setupTests.js :
const localStorageMock = { removeItem: jest.fn(), getItem: jest.fn(), setItem: jest.fn(), clear: jest.fn() }; global.localStorage = localStorageMock;
vapurrmaid
source share