Skip to content

Instantly share code, notes, and snippets.

@sandwichsudo
Last active July 27, 2017 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandwichsudo/1aeec152c6dea88efd0d5135c848452f to your computer and use it in GitHub Desktop.
Save sandwichsudo/1aeec152c6dea88efd0d5135c848452f to your computer and use it in GitHub Desktop.
Add test for action with error flow
// src/containers/RepoSearchPage/actions/RepoSearchActions.spec.js
import { fetchRepos } from './RepoSearchActions';
import searchService from '../../../services/search/searchService';
import { actionTypes } from '../RepoSearchConstants';
jest.mock('../../../services/search/searchService');
describe('repo search actions', () => {
describe('fetchRepos', () => {
it('should call searchService.repoSearch and dispatch actions', async () => {
searchService.__setMockReposSearch(Promise.resolve({
items: 'foo',
}));
const dispatch = jest.fn();
await fetchRepos()(dispatch);
expect(searchService.repoSearch).toHaveBeenCalled();
// add REQUEST_COMPLETE event with empty error
expect(dispatch.mock.calls).toEqual([
[{ type: actionTypes.UPDATE_RESULTS, items: 'foo' }],
[{ type: actionTypes.REQUEST_COMPLETE, error: '' }]
]);
});
it('should dispatch error action when service throws and exception', async () => {
// mock server error using rejected promise
// our xhr module will return an object with a message property in this instance
searchService.__setMockReposSearch(Promise.reject({
message: 'Something went wrong',
}));
const dispatch = jest.fn();
await fetchRepos()(dispatch);
expect(searchService.repoSearch).toHaveBeenCalled();
// add REQUEST_COMPLETE event with error
expect(dispatch.mock.calls).toEqual([
[{ type: actionTypes.REQUEST_COMPLETE, error: 'Something went wrong' }]
]);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment