【问题标题】:Sweetalert2 and Qunit.jsSweetalert2 和 Qunit.js
【发布时间】:2017-04-05 12:49:21
【问题描述】:

谁能告诉我如何在 QUnit 中测试以下代码?

function ChooseBranch()
{
var BranchPromise = getBranches();
BranchPromise.then(function (BranchData) {
    var BranchOptions = [];
    $.each(BranchData, function (i, d) {
        BranchOptions.push(d.BranchDescription)
    });

    swal({
        title: 'Delivery Branch',
        text: 'Please choose the branch below where the customer would like the order delivered',
        showCancelButton: false,
        input: 'select',
        inputOptions: BranchOptions
    }).then(function (result) {
        DeliveryType = "Other";
        DeliverToBranch = BranchData[result].BranchCode;
        ItemOrder();
    });
});

}

我尝试让 sweetalert2 对话框选择第一个选项,但直到测试失败后它才会出现在 dom 中?我基本上是想测试一下swal是否可见。

【问题讨论】:

    标签: javascript qunit sweetalert sweetalert2


    【解决方案1】:

    您似乎正在寻找 QUnit 的异步 api:https://api.qunitjs.com/assert/async

    ChooseBranch 似乎是异步的。如果您希望能够测试该功能,则需要在该异步调用中提供某种挂钩。在这个例子中很简单,只需从ChooseBranch 函数返回BranchPromise

    完成后,您应该能够编写如下 qunit 测试:

    Qunit.test('ChooseBranch', function(assert) {
        var done = assert.async();
        var promise = ChooseBranch();
        promise.then(function() {
            // Test that the sweetalert2 dialog was displayed
            done();
        });
    });
    

    【讨论】:

    • 谢谢,这让我很清楚!我已经将警报的显示移到了它自己的功能中,然后我可以测试它是否可见。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    • 1970-01-01
    • 2019-11-15
    相关资源
    最近更新 更多