Skip to content

Instantly share code, notes, and snippets.

@IsTheJack
Last active February 8, 2018 18:48
Show Gist options
  • Save IsTheJack/76c6e8dbee7f9067df109be32c571e5b to your computer and use it in GitHub Desktop.
Save IsTheJack/76c6e8dbee7f9067df109be32c571e5b to your computer and use it in GitHub Desktop.
// actions.test.js
// Alguns imports importantes:
// Importando o arquivo onde terá a função acoplada à função testada
import * as punkapi from './path/to/file';
// Importando o aquivo onde terá a função testada
import * as actions from './actions';
// Arquivo com as constantes dos types usados nas actions
import * as types from './types';
describe('App actions tests', () => {
beforeEach(() => {
jest.resetAllMocks();
});
describe('fetchBeers', () => {
// Testando caso de sucesso
it('must to return success action on success', () => {
jest
.spyOn(punkapi, 'getBeers')
.mockImplementation(() => Promise.resolve({ data: { mock: 'mock' } }));
const dispatchMock = jest.fn();
const promise = actions.fetchBeers()(dispatchMock);
return promise.then(() => {
const argOfFirstCall = dispatchMock.mock.calls[0][0];
const argOfSecondCall = dispatchMock.mock.calls[1][0];
expect(argOfFirstCall).toEqual({ type: types.FETCH_BEERS });
expect(argOfSecondCall).toEqual({
type: types.FETCH_BEERS_SUCCESS,
payload: { mock: 'mock' },
});
});
});
// Testando caso de falha
it('must to return failure action on failure', () => {
jest
.spyOn(punkapi, 'getBeers')
// Mockando com Promise.reject
.mockImplementation(() => Promise.reject({ error: 'Error' }));
const dispatchMock = jest.fn();
const promise = actions.fetchBeers()(dispatchMock);
return promise.then(() => {
const argOfFirstCall = dispatchMock.mock.calls[0][0];
const argOfSecondCall = dispatchMock.mock.calls[1][0];
expect(argOfFirstCall).toEqual({ type: types.FETCH_BEERS });
expect(argOfSecondCall).toEqual({
type: types.FETCH_BEERS_FAILURE,
error: { error: 'Error' },
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment