How to use SpyOnEvent to spy on events with jquery-jasmine

Nicola Peluchetti picture Nicola Peluchetti · Jun 9, 2012 · Viewed 9.4k times · Source

I'm using the jQuery-Jasmine extension to spy on events but i can't get the correct syntax.

// 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
var first_multi = $( '.ai1ec-facebook-refresh-multiselect:first' );
spyOnEvent( '.ai1ec-facebook-refresh-multiselect:first', 'click' );
first_multi.click();
expect( 'click' ).toHaveBeenTriggeredOn( '.ai1ec-facebook-refresh-multiselect:first' );

This gives me back

Expected event click to have been triggered on .ai1ec-facebook-refresh-multiselect:first

but i clicked it on the row before the check so i must be doing something wrong.

Answer

Nicola Peluchetti picture Nicola Peluchetti · Jun 11, 2012

The problem was that I wasn't triggering the event in the right way.

This worked:

it( "Prevent all ajax functionality", 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
        spyOnEvent( $( '.ai1ec-facebook-refresh-multiselect:first' ), 'click' );
        $( '.ai1ec-facebook-refresh-multiselect:first' )[0].click();
        expect( 'click' ).toHaveBeenTriggeredOn( $( '.ai1ec-facebook-refresh-multiselect:first' ) );
} );