Last active
July 27, 2017 14:31
-
-
Save sandwichsudo/1aeec152c6dea88efd0d5135c848452f to your computer and use it in GitHub Desktop.
Add test for action with error flow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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