在测试环境中触发 this.props.dispatch
Triggering this.props.dispatch in a test environment
你好,我有一个像这样的笑话/酶测试:
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')
})
还有一个看起来像这样的组件:
<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>
单击 input
会调用此方法:
this.props.dispatch(update(data))
它在我的浏览器中工作正常,但是在测试 onClick
事件时我得到这个错误:
TypeError: this.props.dispatch is not a function
如何让 this.props.dispatch
在我的测试中工作?
谢谢
您需要在测试中通过 dispatch
作为道具。
修改后的代码:
it('triggers checkbox onChange event', () => {
const configs = {
default: true,
label: 'My Label',
element: 'myElement',
}
const dispatch = jest.fn();
const props = {
configs,
dispatch
}
const checkbox = shallow(
<CheckBox
{...props}
/>
)
checkbox.find('input').simulate('click')
expect(dispatch).toHaveBeenCalledWith(update(data))
})
你好,我有一个像这样的笑话/酶测试:
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')
})
还有一个看起来像这样的组件:
<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>
单击 input
会调用此方法:
this.props.dispatch(update(data))
它在我的浏览器中工作正常,但是在测试 onClick
事件时我得到这个错误:
TypeError: this.props.dispatch is not a function
如何让 this.props.dispatch
在我的测试中工作?
谢谢
您需要在测试中通过 dispatch
作为道具。
修改后的代码:
it('triggers checkbox onChange event', () => {
const configs = {
default: true,
label: 'My Label',
element: 'myElement',
}
const dispatch = jest.fn();
const props = {
configs,
dispatch
}
const checkbox = shallow(
<CheckBox
{...props}
/>
)
checkbox.find('input').simulate('click')
expect(dispatch).toHaveBeenCalledWith(update(data))
})