onClick笑话/酶:方法“模拟”旨在在1个节点上运行。改为找到0个

时间:2019-02-21 01:33:09

标签: reactjs unit-testing onclick jestjs enzyme

我无法为以下代码测试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);
});

我如何使以下测试通过?

2 个答案:

答案 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");