【问题标题】:simulate change not working with trim() enzyme模拟变化不适用于 trim() 酶
【发布时间】:2019-09-02 21:49:00
【问题描述】:

当我遇到错误时,我正在使用这个测试,所以我使用修剪功能来解决它​​,这些测试失败,尝试了不同的方式但没有找到解决方案

const generalWrapper = shallow(<AddVehiclesTable {...generalProps} />)
const generalInstance = generalWrapper.instance()

describe('onSearchChange', () => {
    test('should change the "search" state', () => {
        const theFilterValue = 'a new filter value'
        generalWrapper.find('.filter-input').simulate('change', { target: { value: theFilterValue } })

        const expectedState = Object.assign({}, generalInstance.state)
        expectedState.searchValue = { 'target': { 'value': theFilterValue } }
        expect(generalInstance.state).toEqual(expectedState)
        expect(generalInstance.state.userInteractedWithComponent).toBe(true)
    })
})

onSearchChange (searchValue) {
    const value = searchValue.trim()
    this.setState({ searchValue: value, userInteractedWithComponent: true })
}

错误信息

TypeError: searchValue.trim 不是函数

任何建议

【问题讨论】:

    标签: reactjs jestjs enzyme


    【解决方案1】:

    您的函数将对象作为参数。 公开你需要的字段

    我没有看到全貌,但可以猜到你需要类似的东西

    onSearchChange ({ target: { value: incomeValue } }) {
        const value = incomeValue.trim()
        this.setState({ searchValue: value, userInteractedWithComponent: true })
    }
    

    【讨论】:

      猜你喜欢
      • 2020-12-31
      • 2016-04-25
      • 2023-04-02
      • 2011-05-08
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多