B
B
BugsCreator2018-06-19 15:48:48
React
BugsCreator, 2018-06-19 15:48:48

How to test it?

Started learning testing. How to test calling showGroup method in mapDispatchToProps ?

import React, { Component } from 'react'
import { withStyles } from '@material-ui/core/styles'
import { connect } from 'react-redux'
import DrawerContent from '../components/DrawerContent'
import RandomComponent from '../components/RandomComponent'
import { showGroup } from '../actions/interfaceActions'

export class FilterPanel extends Component {
  render() {
    const { classes, theme, showGroup } = this.props
    return (
      <div>
        <DrawerContent showGroup={showGroup} />
        <RandomComponent />
      </div>
    )
  }
}

const mapStateToProps = (store) => ({
  ...
})

const mapDispatchToProps = (dispatch) => ({
  showGroup: (group) => {dispatch(showGroup(group))}
})

export default withStyles(
  styles
)(connect(
  mapStateToProps, 
  mapDispatchToProps
)(FilterPanel))

The test itself:
Enzyme.configure({ adapter: new Adapter() })

describe('FilterPanel', () => {
  const initialState = {
    showDrawer: false,
    filterPanel: {},
    language: 'rus',
    dictonary: {}
  }
  const mockStore = configureStore()
  let store, container
  
  beforeEach(() => {
    store = mockStore(initialState)
    container = shallow(<ConnectedFilterPanel store={store} />)
  })
  
  test('Check HANDLE_SHOW_GROUP action on dispatching', () => {
    store.dispatch(showGroup("value"))
    const action = store.getActions()
    expect(action[0].type).toEqual("HANDLE_SHOW_GROUP")
  })
})

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question