首页 > 解决方案 > Redux 在测试高阶组件时传递/模拟调度操作

问题描述

当我尝试测试 Redux 连接的反应组件时,我无法提供。下面是我连接的反应组件的代码:

    MyComponent.jsx
    
    class MyComponent extends React.Component{
    
    componentWillMount (){
    
    }
    
    
    componentDidMount () {
    
    setTimeOut (()=>{
    changeValue('personName' , 'something',someVar)
    
    },2000);
    
    }
    
    }
export default MyComponent

    
    index.jsx
    
    import MyComponent from './MyComponent'
    const mapStateToProps = state => (
    {
    rootState : state 
    info : state.someInfo
    }
    )
    
    const mapDispatchProps = dispatch => bindActionCreators({
    changeValue : change,
    resetValue : reset
    
    },dispatch)
    
    export default compose (connect (mapStateToProps,mapDispatchProps),injector,theme.wizard({}))(
    connect(state => ({
    values : selector (state, 'vehicleName', 'address','personName')
    }))
    )(MyComponent)

MyComponent.Test.js

import configureStore from 'redux-mock-store'
import {shallow, mount} from 'enzyme'
import MyComponent from './MyComponent'
jest.useFakeTimers();
describe('test1', ()=>{
it('test for componentDidMount' , ()=>{
const middlewares = []
const mockStore = configureStore(middlewares)
const initialState = {changeValue : (x,y,z)=> console.log(x)};
  const store = mockStore(initialState);
var component = shallow (<Provider store = {store}><MyComponent /></Provider>);
component.update();
jest.runAllTimers();
})
}

当我运行这个我得到错误:changeValue is not a function,我如何在测试中通过/模拟changeValue函数来解决这个错误。

标签: reactjsreact-reduxjestjsredux-thunkreact-testing-library

解决方案


推荐阅读