目前可以像这样编写 reducer 和测试 sagas:
// selector
const selectThingById = (state, id) => state.things[id]
// in saga
const thing = yield select(selectThingById, id)
// and test
expect(gen.next().value)
.toEqual(select(selectThingById, id))
但是我想使用更函数式的方法来编写我的 reducer 并将数据(状态)放在参数的最后:
// selector
const selectThingById = R.curry((id, state) => state.things[id])
// in saga
const thing = yield select(selectThingById(id))
// test: this actually fails
expect(gen.next().value)
.toEqual(select(selectThingById(id)))
测试失败,因为 selectThingById(id)
每次都会创建新函数。
这可以通过将参数添加到 select
而不是附加的选项来解决。是否有可能或有什么方法可以测试此类选择器?
最佳答案
您需要使用 call
效果调用选择器工厂,以便您可以使用 gen.next(val)
// in saga
const selector = yield call(selectThingById, id)
const thing = yield select(selector)
// test
const selector = selectThingById(id)
gen.next(selector)
.toEqual(call(selectThingById, id))
expect(gen.next().value)
.toEqual(select(selector))
关于javascript - 如何在 redux saga 中测试功能选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45751764/