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

2,086 views
Skip to first unread message

Nicola Peluchetti

unread,
Jun 11, 2012, 7:31:49 AM6/11/12
to jasmi...@googlegroups.com

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 an error and 

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.

Alberto Leal

unread,
Jun 11, 2012, 8:24:56 AM6/11/12
to jasmi...@googlegroups.com
Try to insert your code inside runs block:

runs(function() {
...
});

Alberto.

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/jasmine-js/-/hrioNrjvG8QJ.
To post to this group, send email to jasmi...@googlegroups.com.
To unsubscribe from this group, send email to jasmine-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jasmine-js?hl=en.



--
Alberto Leal
@albertoleal

Nicola Peluchetti

unread,
Jun 11, 2012, 9:14:16 AM6/11/12
to jasmi...@googlegroups.com
I tried 

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

But it still doesn't work, the test fails. I also tried commenting the block_all_submit_and_ajax() but the test still fails. This is the function i'm testing. I also tried calling $('.ai1ec-facebook-refresh-multiselect:first').data('events') and i se that spyOnEvent attached his event handler correctly, but it never gets triggered.

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

To unsubscribe from this group, send email to jasmine-js+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/jasmine-js?hl=en.



--
Alberto Leal
@albertoleal

Nicola Peluchetti

unread,
Jun 11, 2012, 12:07:19 PM6/11/12
to jasmi...@googlegroups.com
The error was in the way i triggered the click event. If i use 

           $( '.ai1ec-facebook-refresh-multiselect:first' )[0].click();

it works

Alberto Leal

unread,
Jun 11, 2012, 5:28:25 PM6/11/12
to jasmi...@googlegroups.com
Thanks for sharing, Nicola..

To view this discussion on the web visit https://groups.google.com/d/msg/jasmine-js/-/-3RZQgJcFJAJ.

To post to this group, send email to jasmi...@googlegroups.com.
To unsubscribe from this group, send email to jasmine-js+...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/jasmine-js?hl=en.



--
Alberto Leal
@albertoleal

Reply all
Reply to author
Forward
0 new messages