gpt4 book ai didi

javascript - 如何使用 jasmine 测试具有 setTimeout 的函数?

转载 作者:IT王子 更新时间:2023-10-29 03:06:57 26 4
gpt4 key购买 nike

我需要为内部调用了 setTimeout() 的函数编写测试,但我找不到我应该怎么做。

这是函数

// Disables all submit buttons after a submit button is pressed.
var block_all_submit_and_ajax = function( el ) {
// Clone the clicked button, we need to know what button has been clicked so that we can react accordingly
var $clone = $( el ).clone();
// Change the type to hidden
$clone.attr( 'type', 'hidden' );
// Put the hidden button in the DOM
$( el ).after( $clone );
// Disable all submit button. I use setTimeout otherwise this doesn't work in chrome.
setTimeout(function() {
$( '#facebook input[type=submit]' ).prop( 'disabled', true );
}, 10);
// unbind all click handler from ajax
$( '#facebook a.btn' ).unbind( "click" );
// Disable all AJAX buttons.
$( '#facebook a.btn' ).click( function( e ) {
e.preventDefault();
e.stopImmediatePropagation();
} );
};

这是我的测试

it( "Disable all submit buttons", function() {
// Get a button
var $button = $( '#ai1ec_subscribe_users' );
// Call the function
utility_functions.block_all_submit_and_ajax( $button.get(0) );
// check that all submit are disabled
$( '#facebook input[type=submit]' ).each( function( i, el ) {
console.log( 'f' );
expect( el ).toHaveProp( 'disabled', true );
} );
} );

我试过使用 jasmine.Clock.useMock();jasmine.Clock.tick(11); 但我无法正常工作,测试从未通过

最佳答案

整体方法因您的 Jasmine 版本而异。

Jasmine 1.3

你可以使用waitsFor:

it( "Disable all submit buttons", function() {
// Get a button
var $button = $( '#ai1ec_subscribe_users' );
// Call the function
utility_functions.block_all_submit_and_ajax( $button.get(0) );

// Wait 100ms for all elements to be disabled.
waitsFor('button to be disabled', function(){
var found = true;
// check that all submit are disabled
$( '#facebook input[type=submit]' ).each( function( i, el ) {
if (!el.prop('disabled')) found = false;
});
return found;
}, 100);
});

如果您确切知道需要多长时间,也可以使用 waits:

it( "Disable all submit buttons", function() {
// Get a button
var $button = $( '#ai1ec_subscribe_users' );
// Call the function
utility_functions.block_all_submit_and_ajax( $button.get(0) );

// Wait 20ms before running 'runs' section.
waits(20);

runs(function(){
// check that all submit are disabled
$( '#facebook input[type=submit]' ).each( function( i, el ) {
expect( el ).toHaveProp( 'disabled', true );
});
});
});

还有第三种方法,不需要waitswaitsForruns

it( "Disable all submit buttons", function() {
jasmine.Clock.useMock();

// Get a button
var $button = $( '#ai1ec_subscribe_users' );
// Call the function
utility_functions.block_all_submit_and_ajax( $button.get(0) );

jasmine.Clock.tick(10);

// check that all submit are disabled
$( '#facebook input[type=submit]' ).each( function( i, el ) {
expect( el ).toHaveProp( 'disabled', true );
});
});

Jasmine 2.0

您可以使用done,测试回调:

it( "Disable all submit buttons", function(done) {
// Get a button
var $button = $( '#ai1ec_subscribe_users' );

utility_functions.block_all_submit_and_ajax( $button.get(0) );

setTimeout(function(){
// check that all submit are disabled
$( '#facebook input[type=submit]' ).each( function( i, el ) {
expect( el ).toHaveProp( 'disabled', true );
});

// Let Jasmine know the test is done.
done();
}, 20);
});

您可以模拟计时器行为:

it( "Disable all submit buttons", function() {
jasmine.clock().install();

// Get a button
var $button = $( '#ai1ec_subscribe_users' );
// Call the function
utility_functions.block_all_submit_and_ajax( $button.get(0) );

jasmine.clock().tick(10);

// check that all submit are disabled
$( '#facebook input[type=submit]' ).each( function( i, el ) {
expect( el ).toHaveProp( 'disabled', true );
});

jasmine.clock().uninstall()
});

关于javascript - 如何使用 jasmine 测试具有 setTimeout 的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10955201/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com