reactjs - 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函数来解决这个错误。
解决方案
推荐阅读
- maven - Maven 和 SonarTsPlugin 的 SonarQube 扫描仪无法运行节点
- java - 为什么@Transactional + spring data jdbc + Sybase 不回滚?
- url - URL 命名和解析
- entity-framework - 使用输出参数执行存储过程后,实体框架 6 ObjectParameter 为空
- php - 访问嵌套的 php 对象数据
- python - 集合(列表)中的元素但不是列表中的元素如何?
- javascript - JQuery 最后一个选择器没有按预期工作
- java-8 - Drools - 查找最低 Java 8 本地日期
- excel - Excel 日期值函数
- html - 如何将“背景图片放在另一个 div”上?