Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8055123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:17:53+00:00 2026-06-05T08:17:53+00:00

I need to write a test for a function that has a setTimeout() call

  • 0

I need to write a test for a function that has a setTimeout() call inside, but i can’t find how i should do.

This is the function

// 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();
    } );
};

And this is my test

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 );
    } );
} );

I’ve tried using jasmine.Clock.useMock(); and jasmine.Clock.tick(11); but i couldn’t get things to work, the test never pass

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:17:54+00:00Added an answer on June 5, 2026 at 8:17 am

    The overall approach varies based on your Jasmine version.

    Jasmine 1.3

    You can use 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);
    });
    

    You could also use waits if you know exactly how long it will take:

    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 );
            });
        });
    });
    

    There is also a third way of doing this, without the need for waits, waitsFor, and runs.

    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

    You can use done, the test callback:

    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);
    });
    

    you can mock out the timer behavior:

    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()
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a unit test for a function that returns a dictionary.
I have a Delphi DLL that I did not write, but need to call
I need to test if a user can write to a folder before actually
I need to write, in JavaScript (I am using jQuery), a function that is
I need to write a function that does exactly opposite of preg_quote function. Simply
I am trying to write some front end Javascript tests that can test drag
I need to write a jUnit test for a rather complex application which runs
I need to write a use cases to test the asp.net mvc applicatoin. How
I need to write a small console app (patch) that turns off the print
I need to write an app that reads a config file with info on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.