Skip to content

Instantly share code, notes, and snippets.

@DanCassiano
Last active April 30, 2017 00:15
Show Gist options
  • Save DanCassiano/ed3a8285f7a92f9d283560fbf1d54f01 to your computer and use it in GitHub Desktop.
Save DanCassiano/ed3a8285f7a92f9d283560fbf1d54f01 to your computer and use it in GitHub Desktop.
Actions tests
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: "[email protected]",
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);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment