【发布时间】:2016-07-09 05:12:03
【问题描述】:
我正在尝试模拟对 react 组件中的 dom 元素的单击,并断言基于此调用了 spy。这是测试的相关部分(使用 karma、mocha、chai、sinon,但我很确定这与这里无关):
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);
}
测试运行,但结果是测试失败,并显示消息“预期为假为真”。我看不出代码有什么问题——没有错误,我的间谍正确传递。 还有什么我需要做的吗?
【问题讨论】:
标签: unit-testing reactjs reactjs-testutils