Redux saga测试选择效果

时间:2017-04-20 11:23:28

标签: reactjs redux react-redux redux-saga saga

如何测试选择效果?我的传奇取决于选择的结果,但每次我从传奇中获得undefined

以下是我的尝试:

在我的传奇中:

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

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

在测试中:

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

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

});

但结果是foo is : undefined。如何将数据传递到默认状态?有没有更好的方法呢?

1 个答案:

答案 0 :(得分:2)

尝试这种方法

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

});