angular - 我如何模拟 sweetalert 以及如何使其在 Angular Testing 中返回 true

标签 angular testing sweetalert2

我想在我的 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/

相关文章:

css - 根据条件更改 css 类属性( Angular 2 中的 *ngIf)

javascript - angular - 访问 ngOnInit() 之外的数组

android - 安装先决条件应用程序以在 firebase 测试实验室中测试我们的应用程序

reactjs - 使用 Sweet Alert 2 获取 this.props 在使用警告警报后未定义

node.js - 如何在nodejs中使用sweetalert?

javascript - sweetalert2 示例报告 : await is only valid in async functions and async generators

angular - Angular2 RC0 中的 TemplateRef api 更改

javascript - Angular:从标签中应用 ngif

testing - 在 Plone 5 的测试中自动安装默认内容类型

python - 如何使用 HTTP Basic Auth 注释测试 Flask 路由方法