如何编写单元测试以确保mapDispatchToProps正确返回包装在调度功能中的动作创建者?我应该如何在Redux中测试mapDispatchToProps?
我目前使用摩卡和酵素进行测试。
这是我的容器组件。
import { Component } from 'react'
import { connect } from 'react-redux'
import Sidebar from '/components/Sidebar'
import Map from '/components/Map'
import * as LayerActions from '../actions/index'
// Use named export for unconnected component (for tests)
export const App = ({layers, actions}) => (
<div>
<Sidebar LayerActions={actions} />
<Map />
</div>
)
export const mapStateToProps = state => ({
layers: state.layers
})
export const mapDispatchToProps = dispatch => ({
actions: bindActionCreators(LayerActions, dispatch)
})
// Use default export for the connected component (for app)
export default connect(mapStateToProps, mapDispatchToProps)(App)
很好。非常感谢。如果我将对象字面量作为第二个参数传入,那么如何将由Connect注入的注入操作传递到边栏子组件? – therewillbecode
您通常需要将动作相关的道具放在一起,然后传递给他们。这可能是显式的('const {firstAction} = props'),或者更像是一个全面的('const {layers,... allOtherProps} = props')。无论哪种方式,提取它们,然后使用这些道具渲染侧边栏。 – markerikson