Search code examples
reactjsreduxreact-reduxredux-sagasaga

Redux saga test select effect


How do I test select effect? My saga depends on the result of the select but each time I am just getting undefined from the saga.

Here is what I tried:

in my saga:

function* loadData() {
    const foo = yield select((state) => {
        return state.foo.value;
    });

    console.log('foo is :', foo);
}

in the test:

test.only('Data saga', t => {

    const generator = loadData();
    console.log(generator.next({foo: 'blah'}).value);
    console.log(generator.next());

});

but the result is foo is : undefined. How do I pass data to the default state please? Is there a better way of doing this?


Solution

  • Try this approach

    test.only('Data saga', t => {
    
        const iterator = loadData();
    
        const selectFooValue = iterator.next(); // <-- this is effect descriptor
    
        // here you assert about selectFooValue
    
        const nextStep = iterator.next('bar'); // <-- this how you "running" select effect  
    
        // here you assert about nextStep
    
    });