Want to test API request failures with Redux Saga? To make yield get an arbitrary value, pass it to next(). To make it “receive” an error, pass it to throw().
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | it('should return a LOGIN_FAIL action', () => { const action = { payload: { name: 'toto', password: '123456' } }; const generator = login(action); // Check that Saga asks to call the API expect( generator.next().value ).to.be.eql( call(api.login, action) ); // Note that *no actual request was made*! // We are just checking that the sequence of effects matches our expectations. // Check that Saga reacts correctly to the failure expect( generator.throw({ error: 'user not found' }).value ).to.be.eql( put({ type: 'LOGIN_FAIL', payload: { error: 'user not found' } }) ); }); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.