2016-03-22 25 views
0

我试图模拟点击反应组件中的dom元素并声明基于此调用了间谍。下面是测试的相关部分(利用人缘,摩卡,薛宝钗,兴农但我相当肯定这是无关紧要这里):反应测试实用程序 - 执行模拟点击时未调用间谍 -

const selectSpy = sinon.spy(); 
const component = TestUtils.renderIntoDocument(<SomeComponent onSelect={selectSpy} />); 
TestUtils.Simulate.click(TestUtils.scryRenderedDOMComponentsWithTag(component, 'li')); 
expect(selectSpy.called).to.be.ok; 

和这里的反应成分:

render() { 
    let { title , data, onSelect } = this.props; 
    console.log(this.props.onSelect); // This correctly logs out the spy 
    return (
     <div> 
     <ul>{data.map((row, i) => { 
      return <li onClick={this.handle.bind(this,row)} key={i}>{row.title}</li>; })} 
     </ul> 
     </div> 
    ) 
} 

handle(row) { 
    this.props.onSelect(row); 
} 

试运行但结果是一个失败的测试,并显示“预计虚假是真的”。我看不出有太多错误的代码 - 没有错误,我的间谍被正确传递。 还有什么我需要做的?

回答

1

TestUtils.Simulate.click需要一个DOM元素,但scryRenderedDOMComponentsWithTag会返回一个DOM元素数组。

尝试改变

TestUtils.Simulate.click(TestUtils.scryRenderedDOMComponentsWithTag(component, 'li')); 

TestUtils.Simulate.click(TestUtils.scryRenderedDOMComponentsWithTag(component, 'li')[0]); 
+0

就是这样。非常感谢 –

相关问题