dave
dave

Reputation: 1430

Sweetalert2 and Qunit.js

Could anyone give me a pointer on how I test the following code in 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();
    });
});

}

I have tried getting the sweetalert2 dialog select first option, but it doesn't appear in the dom until after the test has failed? I basically want to test that the swal is visible.

Upvotes: 0

Views: 62

Answers (1)

pieceOpiland
pieceOpiland

Reputation: 359

It looks like you're looking for QUnit's async api: https://api.qunitjs.com/assert/async

The ChooseBranch appears to be asynchronous. If you want to be able to test that function, you need to provide some sort of hook into that asynchronous call. It's fairly easy in this example, just return BranchPromise from the ChooseBranch function.

Having done that, you should be able to write a qunit test like:

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

Upvotes: 2

Related Questions