Created
June 20, 2016 09:50
-
-
Save ghengeveld/442a2ce9c763c279fe40ff04ec89f9d3 to your computer and use it in GitHub Desktop.
How to test an async action (by monkeypatching the dispatch function)
This file contains 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
/* async-action.js */ | |
import SomeService from './some-service'; | |
export function doAsync(arg) { | |
return dispatch => { | |
// SomeService.doSomething returns a promise which resolves to the passed argument | |
SomeService.doSomething(arg).then(result => { | |
const action = { type: 'ASYNC_SUCCESS', payload: result }; | |
dispatch(action); | |
}).catch(error => { | |
const action = { type: 'ASYNC_FAILED', payload: error, error: true }; | |
dispatch(action); | |
}); | |
}; | |
} | |
/* async-action.spec.js */ | |
const doAsync = proxyquire('./async-action.js', { | |
// Mock the EchoService: | |
'./some-service': { | |
doSomething(value) { return Promise.resolve(value); } | |
} | |
}); | |
describe('Async action', () => { | |
it('should dispatch ASYNC_SUCCESS on success', (done) => { | |
const dispatch = (action) => { | |
expect(action.type).to.be('ASYNC_SUCCESS'); | |
expect(action.payload).to.be('hello'); | |
done(); | |
}; | |
doAsync('hello')(dispatch); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment