reactjs - onClick Jest / 酶:方法“模拟”意味着在 1 个节点上运行。找到了 0 个
问题描述
我无法测试以下代码的 onClick。我不断收到以下错误:
Method “simulate” is meant to be run on 1 node. 0 found instead.
我的组件代码如下所示:
constructor (props) {
super(props)
this.state = INITIAL_STATE;
}
handleQuickFilter = (type) => {
this.setState({
quickFilterObj: {...this.state.quickFilterObj, [type]: {...this.state.quickFilterObj[type], checked: !this.state.quickFilterObj[type].checked}}
}, () => {
let filter = this.buildFilter();
filter ?
linker.UniversalGrid('Counterparty.Loa.Enrollment', '', `{form_filter}=$filter=(${filter}),{grid_selectable}=1,{can_add}=1`)
:
linker.UniversalGrid('Counterparty.Loa.Enrollment', '', `{form_filter}='',{grid_selectable}=1,{can_add}=1`)
})
}
render () {
return(
<div className='enrollment-grid-wrapper'>
<div className='quick-filter-div'>
<button className={this.state.quickFilterObj['New'] && this.state.quickFilterObj['New'].checked ? 'checked quick-filter' : 'unchecked quick-filter'}
id = 'testnew'
onClick={() => {this.handleQuickFilter('New')}}
>
New {this.state.quickFilterObj['New'] && this.state.quickFilterObj['New'].count !== undefined ? `(${this.state.quickFilterObj['New'].count})` : null}
</button>
<button className={this.state.quickFilterObj['Sent'] && this.state.quickFilterObj['Sent'].checked ? 'checked quick-filter' : 'unchecked quick-filter'}
onClick={()=>{this.handleQuickFilter('Sent')}}
>
Sent {this.state.quickFilterObj['Sent'] && this.state.quickFilterObj['Sent'].count !== undefined ? `(${this.state.quickFilterObj['Sent'].count})` : null}
</button>
)
}
我使用 Jest / 酶尝试了以下方法:
it("should HandlequickFilter with button click", () => {
wrapper.setProps({});
wrapper.setState({quickFilterObj:"test"});
wrapper.find('.quick-filter-div').at(0).simulate("click");
expect(wrapper.state().quickFilterObj.New.checked).toEqual(true);
});
我怎样才能通过以下测试?
解决方案
必须在你的 jest.fn 函数中添加一个 return new Promise
//mocking functions
getSelectedLOAs = jest.fn( () => {
return new Promise((resolve, reject) => {
resolve()
})
});
it("should test click event NEW method", () => {
wrapper.setProps({
});
wrapper.find('COMPONENT').setState({
quickFilterObj:{
New: {
checked: true
},
Sent: {
checked: true
},
}, } );
wrapper.update();
wrapper.find('#New-testclick').simulate("click");
wrapper.find('#Sent-testclick').simulate("click");
推荐阅读
- javascript - 对遍历对象以访问字段感到困惑
- python - 确定 Pandas 数据框行是否具有多个特定值
- java - GraphQL 服务器与 Kafka 的集成
- python - 多个条件满足 numpy select
- javascript - 如何使我的组件的多个实例不共享@ViewChild 地图/区域
- google-sheets - Delete Rows older than 1 year in Google Sheets (Large Data Set)
- python - Gurobi python中的不兼容尺寸
- r - 标记堆积条形图
- junit - 为什么在没有 @RunWith(SpringRunner.class) 的情况下仅使用 @SpringBootTest 在 Spring Boot 应用程序中进行测试
- python - 根据字符之间的空格将字符串变量拆分为行