为什么不输入componentDidUpdate()?

时间:2019-09-23 14:41:08

标签: reactjs jestjs enzyme

您好,我正在尝试测试在componentDidUpdate中已更改但没有引起迷惑的状态。

代码

componentDidUpdate (newProps) {
    const { dataSource } = newProps
    // set value for nextButtonDisabled in first results async load
    if (dataSource.length) {
        const newPaginationInfo = Object.assign({}, this.state.paginationInfo)
        newPaginationInfo.nextButtonDisabled = dataSource.length <= this.pageSize
        this.setState({ paginationInfo: newPaginationInfo }) /* eslint-disable-line react/no-did-update-set-state */
    }
}

状态

this.state = {
    paginationInfo: {
        currentPage: 0,
        nextButtonDisabled: true
    }
}

和测试

it('should set nextButtonDisabled to false when gets new props.datasource if datasource length <= 20', () => {
    const component = shallow(<VehicleHistoryTable {...makeProps()} />)
    component.setProps({ dataSource: createDataSourceMock(3) })
    expect(component.instance().state.paginationInfo.nextButtonDisabled).toEqual(true)
})

函数createDataSourceMock()创建一个数字数组,在这种情况下为3行。

有什么建议吗?

P:S我正在尝试迁移到React 17

0 个答案:

没有答案