javascript - Jasmine - wait for fadeOut() to finish -
i have simple function fades in element:
code
checklengthselect: function(){ if($(this).val() != ''){ // if not empty $('.c--error').fadeout('fast', function(){ $('#button').fadein(); }) } else { // if empty hide button , show error message $('#button').fadeout('fast', function(){ $('.c--error').html('foo').fadein(); // <-- want test on fadeout callback }) } },
i'm trying write test this:
test
it("it should have value or hide continue button", function(){ // check cover length on select $(document).on('change', '#select', function(){ qqw.validate.checklengthselect.call(this) } ); // force change event fire empty value. $('#select').val('').change(); settimeout(function(){ expect($('.c--error').html()).toequal('foo'); done(); // <-- test if done }, 800); });
error
this shows in console
'expect' used when there no current spec, because asynchronous test timed out
question
how can test event happens when fadeout
completes?
i'm using jasmine v2+
jasmine 1.3
you can use waitsfor
wait condition or fail after timeout:
waitsfor(function() { return $('.c--error').html() == 'foo'; }, "error never set 'foo'", 800);
jasmine 2.0
for jasmine 2.0, can simulate same behavior:
describe("stackoverflow test", function() { var originaltimeout; beforeeach(function() { originaltimeout = jasmine.default_timeout_interval; jasmine.default_timeout_interval = 10000; }); it("it should have value or hide continue button", function(done) { // check cover length on select $(document).on('change', '#select', function(){ checklengthselect.call(this) } ); // force change event fire 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; }); });
you need increase async timeout keep test failing. adjust poll_time
or amount added current date change behavior. should note current code not hit correct branch of checklengthselect
test fail, flipped condition on branch in testing make sure jasmine part of works expected.
Comments
Post a Comment