Reputation: 871
When i the run test it gives me an error TypeError: Cannot read property 'then' of undefined, I've tried to look for some fix on the Docs but i didn't find one to fix this issue, any idea on how to fix this problem will be much appreciated thanks.
contact-actions.js
export function getContacts() {
return function(dispatch) {
dispatch({type: 'GET_CONTACTS_PENDING'})
axios.get('http://127.0.0.1:8000/api/contacts', {
})
.then((response) => {
dispatch({type: 'GET_CONTACTS', payload: response.data})
dispatch(hideLoading())
})
.catch((error) => {
dispatch({type: 'CONTACT_ERROR', payload: error})
})
}
}
app.spec.js
import React from 'react';
import { shallow, mount } from 'enzyme';
import renderer from 'react-test-renderer';
import MockAdapter from 'axios-mock-adapter';
import configureMockStore from 'redux-mock-store';
import nock from 'nock';
import axios from 'axios';
import moxios from 'moxios';
import expect from 'expect';
import thunk from 'redux-thunk';
import { Provider } from 'react-redux';
import * as actions from '../src/actions/contact-actions';
import * as types from '../src/constants/action-types';
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
describe('async actions', () => {
afterEach(() => {
nock.cleanAll();
});
it('creates GET_CONTACTS when fetching contacts has been done', () => {
const store = mockStore({});
const contacts = {};
nock('http://127.0.0.1:8000')
.get('/api/contacts')
.reply(200, { body: { contacts: [{ first_name: 'shadow', last_name: 'madow', phone: 5566, email: '[email protected]' }] } });
const expectedActions = [
{ type: types.GET_CONTACTS, body: { contacts: [{ first_name: 'shadow', last_name: 'madow', phone: 5566, email: '[email protected]' }] }}
];
return store.dispatch(actions.getContacts()).then(() => {
// return of async actions
expect(store.getActions()).toEqual(expectedActions);
});
});
});
Upvotes: 3
Views: 8339
Reputation: 7057
Be sure to return the promise from the anonymous function inside getContacts():
export function getContacts() {
return function(dispatch) {
dispatch({type: 'GET_CONTACTS_PENDING'})
axios.get('http://127.0.0.1:8000/api/contacts', { // THIS LINE
})
...
Change this:
axios.get('http://127.0.0.1:8000/api/contacts', {
to this:
return axios.get('http://127.0.0.1:8000/api/contacts', {
Right now it lacks a return statement and so returns undefined
.
Upvotes: 4