我在React中有一个按钮,当用户单击它时会打开一个简单的确认窗口。在添加confirm方法之前,下面的测试是绿色的。添加确认后,测试变成了红色。我需要如何更改测试来与额外的确认一起工作?
React删除按钮:
const DeleteButton = (props) => {
const handleDelete = () => {
if(confirm("Are you sure?")) {
props.onDelete(props.id)
}
};
return (
<Button className="btn" onClick={handleDelete}>
<i className="fa fa-trash-o"></i>
</Button>
);
};
这里是测试代码(使用enzyme):
describe('<DeleteButton />', () => {
it("deletes the entry", () => {
const onDelete = sinon.spy();
const props = {id: 1, onDelete: onDelete};
const wrapper = shallow(<DeleteButton {...props} />);
const deleteButton = wrapper.find(Button);
deleteButton.simulate("click");
expect(onDelete.calledOnce).to.equal(true);
});
});
const confirmStub = sinon.stub(global, 'confirm');
,并验证confirmStub
是否被调用。 - Yury Tarabankonpm test
,没有涉及任何浏览器。const confirmStub = sinon.stub(global, 'confirm');
工作正常。非常感谢! - Linus