【发布时间】:2021-09-24 03:40:36
【问题描述】:
我想模拟单击按钮并期望调用 onclick。
当前结果失败 //expect(wrapper.instance().onclick().called).to.be.equal(true) FAILED jsx
const DeselectAll = (props: Props) => (
<div className="deselectall">
<Button
className="deselectall--button"
icon="minus"
onClick={props.clearItems}
text={translate("deselectall")}
/>
</div>
);
测试
function MySpy() {
this.calls = 0;
}
MySpy.prototype.fn = function () {
return () => this.calls++;
}
When(/^Deselect all button is clicked$/, function () {
const mySpy = new MySpy();
const mockCallBack = mySpy.fn();
const button = const mySpy = new MySpy();
const mockCallBack = mySpy.fn();
const button = React.createElement(componentName, { onClick:
mountCallBack});
const childBtn = button.find('.whiteboardarea__deselectall--button');
console.log("deselectall button found ??? ==== " + childBtn.exists());
button.props.onClick();
console.log("if it works ==== " + expect(mySpy.calls).to.be.equal(1));
return true;
});
【问题讨论】:
-
应该
wrapper.instance().onclick().called是wrapper.instance().onClick().called? -
@DrewReese 我试过但这是结果。错误:单击取消全选按钮时:错误:wrapper.instance(...).onClick 不是函数
-
@DrewReese 我想这不是检查的方法。我不知道如何检查。
-
您可以尝试模拟
clearItems回调函数,模拟单击按钮,并断言回调被调用。您的测试基本上是在测试Button是否已将onClick属性正确连接到底层 DOMNode,但您正在尝试测试DeselectAll组件代码/逻辑。 -
@DrewReese 你能用代码显示吗
标签: javascript reactjs enzyme spy