如何編寫單元測試以確保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