酶测试问题返回未定义

Enzyme test issues returns undefined

本文关键字:未定义 返回 问题 测试      更新时间:2023-09-26

我试图做单元测试我的React类组件使用{mount}方法。当我尝试访问类变量(使用此关键字)时,运行测试会给出未定义的错误。下面这个例子。调用mount(<GridComponent recordArr=[1,2,3] />

后,数据源求值为undefined<>之前导出类GridComponent扩展React。组件{构造函数(道具){超级(道具)这一点。状态= {pageRecords: [],totalSize: 0}这一点。选项= {页:1、sizePerPage: 10}这一点。DataSource =[]//所有记录将存储在这里}componentDidMount () {这一点。DataSource = this.props.recordArrthis.parseProps ()}componentWillReceiveProps (nextProps) {这一点。DataSource = nextProps.recordArrthis.parseProps ()}parseProps = () => {let pageRecords = []if (! __isempty (this. datasource)) {//this. datasource。DataSource ==> undefinedlet startIndex = (this.options.)page - 1) * this.options.sizePerPage让count = this.options。page - 1) * this.options.sizePerPage + this.options.sizePerPagepageRecords = .slice数据源,startIndex, count)}this.setState ({……this.state,pageRecords: pageRecords,totalSize: this.DataSource.length})}呈现(){…//呈现来自this.state.pageRecords的记录}}

this.DataSource更改为组件状态,然后在测试用例中使用,Enzyme util setState函数。

wrapper.setState({ DataSource: 'somesource' });

例如,你的组件的componentWillReceiveProps方法应该是这样的:

componentWillReceiveProps(nextProps) {
  this.setState({
    DataSource: nextProps.recordArr,
  });
  this.parseProps()
}