我想在我的 typescript 上编写 Angular Testing 。我的 typescript 代码就是这样
swal({
title: "ONAY",
text: `Delete this row?`,
type: "warning",
showConfirmButton: true,
showCancelButton: true,
confirmButtonText: "Delete",
cancelButtonText: "Not Delete",
confirmButtonColor: "#ef5350"
}).then(isConfirm => {
if (isConfirm.value != null && isConfirm.value) {
//DO SOMETHING
}
});
我想对//DO SOMETHING 表达式编写测试,但由于sweetalert 确认窗口,我没有到达此块。
我如何模拟 sweetalert 以及如何让它返回 true?
最佳答案
我能够在我的测试套件中模拟 Swal。
您需要模拟并解决 sweetalert fire 函数
jest.mock("sweetalert2", () => ({
fire: jest.fn().mockResolvedValue({ isConfirmed: true }),
}));
然后刷新测试中的 promise 。expect(Swal.fire).toBeCalled();
await flushPromises();
expect(something).toBeTruthy();
希望这会有所帮助。
关于angular - 我如何模拟 sweetalert 以及如何使其在 Angular Testing 中返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59782337/