Closed denchen closed 6 years ago
Hi, @denchen.
You can test this a couple ways, depending on if you're using testSaga
or expectSaga
. With testSaga
, you can use the inspect
method to get the take
effect. Then, you can unwrap it to get your selectAction
function and make sure it passes with an expected action. You can do something similar with the provide
method of expectSaga
. I added the return for an example of additional logic, but you do whatever. Notice that I used next(42)
in testSaga
after the inspect
call and that I return 42
in the provider for expectSaga
.
import { asEffect } from 'redux-saga/utils';
import { take } from 'redux-saga/effects';
import { testSaga, expectSaga } from '../src';
function* test() {
const value = yield take(action =>
action.type === 'FOO' && action.key === 'mykey'
);
return value;
}
it('testSaga test', () => {
const action = { type: 'FOO', key: 'mykey' };
testSaga(test)
.next()
.inspect((effect) => {
const selectAction = asEffect.take(effect).pattern;
expect(selectAction(action)).toBe(true);
})
.next(42)
.returns(42);
});
it('expectSaga test', () => {
const action = { type: 'FOO', key: 'mykey' };
return expectSaga(test)
.provide({
take(takeEffect) {
const selectAction = takeEffect.pattern;
expect(selectAction(action)).toBe(true);
return 42;
},
})
.returns(42)
.run();
});
Closing this for now. Let me know if you have any other questions @denchen.
Just had the same question and this example helped me a lot – might be a good addition to the Dynamic Providers documentation :)
If I have this relatively simple saga:
How would I go about unit-testing this? The examples I've seen with
take()
all assume a string, not a function.