我无法为以下代码测试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);
});
我如何使以下测试通过?
答案 0 :(得分:0)
由于使用的是CSS模块,因此实际呈现的DOM将不包含类为div
的{{1}}。相反,该类将类似于quick-filter-div
。因此,quick-filter-div__012xyz
将返回0个节点。
您可以通过在测试中的wrapper.find('.quick-filter-div')
行之后放置以下行来验证这一点:
setState
但是,您似乎要测试的是单击expect(wrapper.find('.quick-filter-div').exists()).toEqual(false)
内部的button
。那不正确吗?毕竟,div.quick-filter-div
处理程序是onClick
而不是button
的道具。
而且,由于您的div
被赋予了ID,因此您可以执行以下操作:
button
如果您没有为按钮提供ID,但您知道要测试的按钮单击是第一个按钮(而不是第二个),则可以尝试:
it("should HandlequickFilter with button click", () => {
wrapper.setProps({});
wrapper.setState({quickFilterObj:"test"});
const button = wrapper.find('button#testnew') // find button based on id selector
expect(button.exists()).toEqual(true) // make sure button is found
button.simulate("click"); // simulate click
expect(wrapper.state().quickFilterObj.New.checked).toEqual(true);
});
答案 1 :(得分:0)
必须在jest.fn函数中添加一个返回新的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");