Jasmine - 等待fadeOut()完成

时间:2016-02-08 15:31:32

标签: javascript jquery jasmine

我有一个简单的函数,它在一个元素中淡出:

CODE

checkLengthSelect: function(){
    if($(this).val() != ''){ // if not empty
        $('.c--error').fadeOut('fast', function(){
            $('#button').fadeIn();
        })
    } else { // if empty hide button and show error message
        $('#button').fadeOut('fast', function(){
            $('.c--error').html('foo').fadeIn(); // <-- I want to test this on fadeOut callback
        })
    }
},

我正在尝试为此编写一个测试:

TEST

it("it should have a value or hide continue button", function(){

    // check cover length on select
    $(document).on('change', '#select', function(){ qqw.validate.checkLengthSelect.call(this) } );

    // force a change event to fire with empty value.
    $('#select').val('').change();

    setTimeout(function(){
        expect($('.c--error').html()).toEqual('foo');   
        done(); // <-- test if done
    }, 800);

});

错误

这显示在控制台

'expect' was used when there was no current spec, this could be because an asynchronous test timed out

问题

如何在fadeOut完成时测试事件发生?

我正在使用Jasmine v2 +

2 个答案:

答案 0 :(得分:2)

Jasmine 1.3

您可以使用waitsFor等待条件或超时后失败:

Map<CompositeKey, String> map = new HashMap<>();
map.put(new CompositeKey(200, "SLT", 0), "Value 1");
String value = map.get(new CompositeKey(200, "SLT", 0));

Jasmine 2.0

对于Jasmine 2.0,您可以模拟相同的行为:

waitsFor(function() {
  return $('.c--error').html() == 'foo';
}, "Error was never set to 'foo'", 800);

您需要increase the async timeout才能使测试失败。调整describe("stackoverflow test", function() { var originalTimeout; beforeEach(function() { originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL; jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000; }); it("it should have a value or hide continue button", function(done) { // check cover length on select $(document).on('change', '#select', function(){ checkLengthSelect.call(this) } ); // force a change event to fire with empty value. $('#select').val('').change(); var POLL_TIME = 10; var endTime = new Date().getTime() + 5000; var checkCondition = function() { if (new Date().getTime() <= endTime && $('.c--error').html() != 'foo') { setTimeout(checkCondition, POLL_TIME); } else { expect($('.c--error').html()).toEqual('foo'); done(); } }; checkCondition(); }); afterEach(function() { jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout; }); }); 或添加到当前日期的金额以更改该行为。我还应该注意,你当前的代码不会到达POLL_TIME的正确分支,因此测试将失败,我在测试中翻转了该分支上的条件,以确保其中的Jasmine部分按预期工作。

答案 1 :(得分:1)

使用

close()

推进计时器。

http://jasmine.github.io/2.4/introduction.html

相关问题