2017-04-05 1 views
0

誰でも私がどのようにQUnitで次のコードをテストするかについてのポインタを教えてもらえますか?Sweetalert2とQunit.js

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に表示されませんか?私は基本的にはスワールが見えることをテストしたい。

答えて

2

あなたはQUnitの非同期APIを探しているように見えます:https://api.qunitjs.com/assert/async

ChooseBranchは、非同期のように見えます。その関数をテストできるようにするには、その非同期呼び出しにある種のフックを用意する必要があります。この例ではかなり簡単ですが、BranchPromiseChooseBranch関数から返してください。あなたのようqunitテストを書くことができるはず、ということ

をやった:

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

私の頭の中で明確にそれを入れていること、ありがとうございます!私はアラートの表示をそれ自身の機能に移しました。それが可視かどうかをテストすることができます。 – dave

関連する問題