我正在使用Jest和酶来测试React复选框组件。
这是我的考验:
it('triggers checkbox onChange event', () => {
const configs = {
default: true,
label: 'My Label',
element: 'myElement',
}
const checkbox = shallow(
<CheckBox
configs={configs}
/>
)
checkbox.find('input').simulate('click')
})但是,在运行测试时,我会得到这个错误:
TypeError: Cannot read property 'target' of undefined这是我的组件的输入:
<div className="toggle-btn sm">
<input
id={this.props.configs.element}
className="toggle-input round"
type="checkbox"
defaultChecked={ this.props.defaultChecked }
onClick={ e => this.onChange(e.target) }
>
</input>
</div>我认为simulate但是我不知道如何做到这一点。
谢谢
发布于 2017-05-11 16:40:33
simulate函数接受将传递给事件处理程序的其他参数。你可以嘲笑你的事件。例如:
const mockedEvent = { target: {} }
checkbox.find('input').simulate('click', mockedEvent)https://stackoverflow.com/questions/43921183
复制相似问题