Sweetalert2 和 Qunit.js

Sweetalert2 and Qunit.js

谁能告诉我如何在 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 对话框 select 第一个选项,但直到测试失败后它才出现在 dom 中?我基本上想测试swal是否可见。

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

ChooseBranch 似乎是异步的。如果您希望能够测试该函数,则需要为该异步调用提供某种挂钩。在这个例子中相当简单,只是 ChooseBranch 函数中的 return 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();
    });
});