import configureMockStore from 'redux-mock-store'; import nock from 'nock'; import thunk from 'redux-thunk'; import * as actions from './../action'; import * as types from './../types'; const middlewares = [ thunk ]; const mockStore = configureMockStore(middlewares); describe('#submitForm', () => { afterEach(() => { nock.cleanAll(); }); const mock = { name: "jose", email: "jose@jose.com", telefone: "24 9999 9999", msg: "Mensagem" }; const url = 'http://localhost:3004'; it('submitForm success', () => { const msg = 'Enviado com sucesso'; nock(url) .post("/contact", mock) .reply(200, {}) const expectedActions = [ { type: types.TRY_SUBMIT }, { type: types.SUBMIT_SUCCESS, msg } ]; const store = mockStore({}); return store.dispatch(actions.submitForm(mock)) .then(() => { expect(store.getActions()).toEqual(expectedActions); }); }); it('submitForm failure', () => { const msg = "Erro ao enviar"; nock(url) .post(`/42`, {}) .reply(500, { msg }); const expectedActions = [ { type: types.TRY_SUBMIT }, { type: types.SUBMIT_FAILURE, msg } ]; const store = mockStore({}); return store.dispatch(actions.submitForm(mock)) .then(() => { expect(store.getActions()).toEqual(expectedActions); }); }); });