Skip to content

Instantly share code, notes, and snippets.

@bmfteixeira
Last active September 18, 2017 10:05
Show Gist options
  • Save bmfteixeira/942a3159ed0f73b32eb9324b59cc7fe9 to your computer and use it in GitHub Desktop.
Save bmfteixeira/942a3159ed0f73b32eb9324b59cc7fe9 to your computer and use it in GitHub Desktop.
Vue action test with API call
// the action
[actionTypes.ACTION_SET_FAV_ELEM] (context) {
return APIAdapter.services.fetchFavElem()
.then((response) => {
context.commit(mutationTypes.MUTATION_SET_FAV_ELEM, response)
resolve(true)
}).catch((error) => {
context.commit(mutationTypes.MUTATION_SET_FAV_ELEM, undefined)
reject(error)
})
},
// the tests
it('should invoke mutation to set the favourite element if the API call is succcessful', done => {
const expectedPayload = 'AAAAA'
const fetchFavElemStub = sinon.stub(APIAdapter.services, 'fetchFavElem').resolves(expectedPayload)
testAction(actions[actionTypes.ACTION_SET_FAV_ELEM], null, {}, [
{ type: mutationTypes.MUTATION_SET_FAV_ELEM, payload: expectedPayload }
], () => {
fetchFavElemStub.restore()
done()
})
})
it('should invoke mutation to set the favourite element as undefined if the API is not succcessful', done => {
const fetchFavElemStub = sinon.stub(APIAdapter.services, 'fetchFavElem').rejects()
testAction(actions[actionTypes.ACTION_SET_FAV_ELEM], null, {}, [
{ type: mutationTypes.MUTATION_SET_FAV_ELEM, payload: undefined }
], () => {
fetchFavElemStub.restore()
done()
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment