How do I unit test mapDispatchToProps in Redux?

How to write unit test so that mapDispatchToProps correctly returns action creators that were wrapped in a dispatch function?

I am currently using Mocha and Enzyme for testing.

Here is my container component.

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)
+4
source share
1 answer

, . dispatch , , connect. , , connect, bindActionCreators . , export default connect(mapState, LayerActions)(App).

, actions. , , "", Redux (.. this.props.actions.someActionCreator() this.props.someActionCreator()).

Idiomatic Redux: ?, .

+5

Source: https://habr.com/ru/post/1663769/


All Articles