模拟事件单击不会调用组件方法Jest/Enzyme



可能的重复

已经浪费了三天试图让这个测试通过。 找不到任何解决方案,似乎模拟点击没有 调用组件函数。

测试用例:

it('checking focus is called', () => {
const wrapper = shallow(<NexMultiselect {...mock_props} />);
wrapper.instance().c = {autosuggest: { input: {focus: ()=>{}} }};
wrapper.instance().focus = jest.fn();
wrapper.find('.values_container').simulate('click');
expect(wrapper.instance().focus).toHaveBeenCalled();
});

组件渲染功能:

return (
<span className="multiselect">
{ label && id &&
<div className="form__field-label"><label htmlFor={id}>{label}</label></div> }
<span onClick={this.focus} className="values_container">
{renderedValues}
<NexAutocomplete
{...other}
onUpdate={this.onSearchUpdate}
data={this.state.data}
filter={[{
searchOn: 'value',
display: 'display'
}]}
value={this.state.inputText}
preferValueFromProps={this.state.preferValueFromProps}
ref={c => this.c = c}
disabled={size && values.length >= size }
/>
</span>
</span>
);

通过将onClick={this.focus}更改为onClick={() => this.focus()}解决了此问题。

这阻止了class method被调用而不是mock function

最新更新