javascript - 如何在 redux saga 中测试功能选择器?

标签 javascript redux jestjs redux-saga ramda.js

目前可以像这样编写 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)

将正确的函数注入(inject) saga
// 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/

相关文章:

javascript - 渐变/不透明度解决方法

javascript - jQuery/javascript "rotate"更改事件图像交换效果

angular - 非平凡 Angular 组件中 RxJS 的最佳实践

javascript - 在 AngularJS 中使用单例模式是一种不好的做法吗?

javascript - 如何以编程方式从 KeyboardEvent.code 和修饰键确定 KeyboardEvent.key

javascript - React Redux 职责

javascript - 渲染前更新状态 : propType marked as required but undefined

reactjs - React Native 测试错误 - 无法在卸载的组件上找到节点

reactjs - Jest 模拟类型错误 : Cannot read property 'credential' of undefined on library react-native-firebase

javascript - 使用 Vue 和 Jest/Vue 测试工具模拟 API 调用