Mark’s answer is correct. Middleware executes those instructions. But this makes your life easier: in the test, you can provide whatever you want as the argument to next()
, and the generator function will receive it as a result of yield
. This is exactly what saga middleware does (except that it actually fires up a request instead of giving you a fake response).
To make yield
get an arbitrary value, pass it to next()
. To make it “receive” an error, pass it to throw()
. In your example:
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' }
})
);
});