2006-11-18 14:37:01 +01:00
module ( "event" ) ;
2008-01-14 10:33:08 +01:00
test ( "bind(), with data" , function ( ) {
expect ( 3 ) ;
2007-03-25 20:06:18 +02:00
var handler = function ( event ) {
ok ( event . data , "bind() with data, check passed data exists" ) ;
2008-05-06 20:56:02 +02:00
equals ( event . data . foo , "bar" , "bind() with data, Check value of passed data" ) ;
2007-04-24 23:48:52 +02:00
} ;
2008-05-29 01:18:25 +02:00
jQuery ( "#firstp" ) . bind ( "click" , { foo : "bar" } , handler ) . click ( ) . unbind ( "click" , handler ) ;
2007-09-09 01:31:23 +02:00
2008-05-29 01:18:25 +02:00
ok ( ! jQuery . data ( jQuery ( "#firstp" ) [ 0 ] , "events" ) , "Event handler unbound when using data." ) ;
2008-01-14 10:33:08 +01:00
} ) ;
test ( "bind(), with data, trigger with data" , function ( ) {
expect ( 4 ) ;
2007-03-25 20:06:18 +02:00
var handler = function ( event , data ) {
ok ( event . data , "check passed data exists" ) ;
2008-05-06 20:56:02 +02:00
equals ( event . data . foo , "bar" , "Check value of passed data" ) ;
2007-03-25 20:06:18 +02:00
ok ( data , "Check trigger data" ) ;
2008-05-06 20:56:02 +02:00
equals ( data . bar , "foo" , "Check value of trigger data" ) ;
2007-04-24 23:48:52 +02:00
} ;
2008-05-29 01:18:25 +02:00
jQuery ( "#firstp" ) . bind ( "click" , { foo : "bar" } , handler ) . trigger ( "click" , [ { bar : "foo" } ] ) . unbind ( "click" , handler ) ;
2008-01-14 10:33:08 +01:00
} ) ;
test ( "bind(), multiple events at once" , function ( ) {
expect ( 2 ) ;
var clickCounter = 0 ,
mouseoverCounter = 0 ;
2007-12-15 06:55:33 +01:00
var handler = function ( event ) {
if ( event . type == "click" )
clickCounter += 1 ;
else if ( event . type == "mouseover" )
mouseoverCounter += 1 ;
} ;
2008-05-29 01:18:25 +02:00
jQuery ( "#firstp" ) . bind ( "click mouseover" , handler ) . trigger ( "click" ) . trigger ( "mouseover" ) ;
2008-05-06 20:56:02 +02:00
equals ( clickCounter , 1 , "bind() with multiple events at once" ) ;
equals ( mouseoverCounter , 1 , "bind() with multiple events at once" ) ;
2008-01-14 10:33:08 +01:00
} ) ;
test ( "bind(), no data" , function ( ) {
expect ( 1 ) ;
2007-04-24 23:48:52 +02:00
var handler = function ( event ) {
ok ( ! event . data , "Check that no data is added to the event object" ) ;
} ;
2008-05-29 01:18:25 +02:00
jQuery ( "#firstp" ) . bind ( "click" , handler ) . trigger ( "click" ) ;
2008-01-14 10:33:08 +01:00
} ) ;
test ( "bind(), iframes" , function ( ) {
2007-07-20 23:53:37 +02:00
// events don't work with iframes, see #939 - this test fails in IE because of contentDocument
// var doc = document.getElementById("iframe").contentDocument;
//
// doc.body.innerHTML = "<input type='text'/>";
2007-12-03 22:41:10 +01:00
//
2007-07-20 23:53:37 +02:00
// var input = doc.getElementsByTagName("input")[0];
2007-12-03 22:41:10 +01:00
//
2008-05-29 01:18:25 +02:00
// jQuery(input).bind("click",function() {
2007-07-20 23:53:37 +02:00
// ok( true, "Binding to element inside iframe" );
// }).click();
2008-01-14 10:33:08 +01:00
} ) ;
test ( "bind(), trigger change on select" , function ( ) {
expect ( 3 ) ;
2007-03-25 20:06:18 +02:00
var counter = 0 ;
function selectOnChange ( event ) {
2007-04-22 05:16:53 +02:00
equals ( event . data , counter ++ , "Event.data is not a global event object" ) ;
2007-04-24 23:48:52 +02:00
} ;
2008-05-29 01:18:25 +02:00
jQuery ( "#form select" ) . each ( function ( i ) {
jQuery ( this ) . bind ( 'change' , i , selectOnChange ) ;
2007-03-25 20:06:18 +02:00
} ) . trigger ( 'change' ) ;
2008-01-14 10:33:08 +01:00
} ) ;
2007-09-03 16:53:09 +02:00
2008-01-14 10:33:08 +01:00
test ( "bind(), namespaced events, cloned events" , function ( ) {
expect ( 6 ) ;
2007-09-03 16:53:09 +02:00
2008-05-29 01:18:25 +02:00
jQuery ( "#firstp" ) . bind ( "custom.test" , function ( e ) {
2008-02-03 05:33:11 +01:00
ok ( true , "Custom event triggered" ) ;
} ) ;
2008-05-29 01:18:25 +02:00
jQuery ( "#firstp" ) . bind ( "click" , function ( e ) {
2007-09-03 16:53:09 +02:00
ok ( true , "Normal click triggered" ) ;
2007-12-03 22:41:10 +01:00
} ) ;
2007-09-03 16:53:09 +02:00
2008-05-29 01:18:25 +02:00
jQuery ( "#firstp" ) . bind ( "click.test" , function ( e ) {
2007-09-03 16:53:09 +02:00
ok ( true , "Namespaced click triggered" ) ;
2007-12-03 22:41:10 +01:00
} ) ;
2007-09-03 16:53:09 +02:00
// Trigger both bound fn (2)
2008-05-29 01:18:25 +02:00
jQuery ( "#firstp" ) . trigger ( "click" ) ;
2007-09-03 16:53:09 +02:00
// Trigger one bound fn (1)
2008-05-29 01:18:25 +02:00
jQuery ( "#firstp" ) . trigger ( "click.test" ) ;
2007-09-03 16:53:09 +02:00
// Remove only the one fn
2008-05-29 01:18:25 +02:00
jQuery ( "#firstp" ) . unbind ( "click.test" ) ;
2007-09-03 16:53:09 +02:00
// Trigger the remaining fn (1)
2008-05-29 01:18:25 +02:00
jQuery ( "#firstp" ) . trigger ( "click" ) ;
2007-12-07 02:52:21 +01:00
2008-02-03 05:33:11 +01:00
// Remove the remaining fn
2008-05-29 01:18:25 +02:00
jQuery ( "#firstp" ) . unbind ( ".test" ) ;
2008-02-03 05:33:11 +01:00
// Trigger the remaining fn (0)
2008-05-29 01:18:25 +02:00
jQuery ( "#firstp" ) . trigger ( "custom" ) ;
2008-02-03 05:33:11 +01:00
2007-12-07 02:52:21 +01:00
// using contents will get comments regular, text, and comment nodes
2008-05-29 01:18:25 +02:00
jQuery ( "#nonnodes" ) . contents ( ) . bind ( "tester" , function ( ) {
2007-12-07 02:52:21 +01:00
equals ( this . nodeType , 1 , "Check node,textnode,comment bind just does real nodes" ) ;
} ) . trigger ( "tester" ) ;
2007-12-20 14:36:56 +01:00
// Make sure events stick with appendTo'd elements (which are cloned) #2027
2008-05-29 01:18:25 +02:00
jQuery ( "<a href='#fail' class='test'>test</a>" ) . click ( function ( ) { return false ; } ) . appendTo ( "p" ) ;
ok ( jQuery ( "a.test:first" ) . triggerHandler ( "click" ) === false , "Handler is bound to appendTo'd elements" ) ;
2007-03-25 20:06:18 +02:00
} ) ;
2008-05-08 18:25:12 +02:00
test ( "trigger() shortcuts" , function ( ) {
expect ( 6 ) ;
2008-05-29 01:18:25 +02:00
jQuery ( '<li><a href="#">Change location</a></li>' ) . prependTo ( '#firstUL' ) . find ( 'a' ) . bind ( 'click' , function ( ) {
var close = jQuery ( 'spanx' , this ) ; // same with jQuery(this).find('span');
2008-05-06 20:56:02 +02:00
equals ( close . length , 0 , "Context element does not exist, length must be zero" ) ;
2007-12-03 22:41:10 +01:00
ok ( ! close [ 0 ] , "Context element does not exist, direct access to element must return undefined" ) ;
return false ;
2007-03-25 20:06:18 +02:00
} ) . click ( ) ;
2008-05-29 01:18:25 +02:00
jQuery ( "#check1" ) . click ( function ( ) {
2007-03-25 20:06:18 +02:00
ok ( true , "click event handler for checkbox gets fired twice, see #815" ) ;
2007-04-22 05:16:53 +02:00
} ) . click ( ) ;
2007-07-21 03:04:59 +02:00
var counter = 0 ;
2008-05-29 01:18:25 +02:00
jQuery ( '#firstp' ) [ 0 ] . onclick = function ( event ) {
2007-07-21 03:04:59 +02:00
counter ++ ;
} ;
2008-05-29 01:18:25 +02:00
jQuery ( '#firstp' ) . click ( ) ;
2008-05-06 20:56:02 +02:00
equals ( counter , 1 , "Check that click, triggers onclick event handler also" ) ;
2008-04-21 22:39:17 +02:00
var clickCounter = 0 ;
2008-05-29 01:18:25 +02:00
jQuery ( '#simon1' ) [ 0 ] . onclick = function ( event ) {
2008-04-21 22:39:17 +02:00
clickCounter ++ ;
} ;
2008-05-29 01:18:25 +02:00
jQuery ( '#simon1' ) . click ( ) ;
2008-05-06 20:56:02 +02:00
equals ( clickCounter , 1 , "Check that click, triggers onclick event handler on an a tag also" ) ;
2008-05-08 18:25:12 +02:00
2008-05-29 01:18:25 +02:00
jQuery ( '<img />' ) . load ( function ( ) {
2008-05-08 18:25:12 +02:00
ok ( true , "Trigger the load event, using the shortcut .load() (#2819)" ) ;
} ) . load ( ) ;
2006-12-28 12:37:07 +01:00
} ) ;
test ( "unbind(event)" , function ( ) {
2007-12-15 06:55:33 +01:00
expect ( 8 ) ;
2008-05-29 01:18:25 +02:00
var el = jQuery ( "#firstp" ) ;
2006-12-28 12:37:07 +01:00
el . click ( function ( ) {
ok ( true , "Fake normal bind" ) ;
} ) ;
el . click ( function ( event ) {
el . unbind ( event ) ;
ok ( true , "Fake onebind" ) ;
} ) ;
el . click ( ) . click ( ) ;
2007-02-25 20:36:29 +01:00
el . click ( function ( ) { return ; } ) ;
el . unbind ( 'click' ) ;
ok ( ! el [ 0 ] . onclick , "Handler is removed" ) ; // Bug #964
2007-03-01 05:54:51 +01:00
el . click ( function ( ) { return ; } ) ;
el . unbind ( 'change' , function ( ) { return ; } ) ;
2007-09-09 01:31:23 +02:00
for ( var ret in jQuery . data ( el [ 0 ] , "events" ) [ 'click' ] ) break ;
2007-04-22 05:16:53 +02:00
ok ( ret , "Extra handlers weren't accidentally removed." ) ;
2007-03-01 05:54:51 +01:00
el . unbind ( 'click' ) ;
2007-09-09 01:31:23 +02:00
ok ( ! jQuery . data ( el [ 0 ] , "events" ) , "Removed the events expando after all handlers are unbound." ) ;
2007-12-15 06:55:33 +01:00
reset ( ) ;
2008-04-22 23:59:40 +02:00
var clickCounter = ( mouseoverCounter = 0 ) ;
2007-12-15 06:55:33 +01:00
var handler = function ( event ) {
if ( event . type == "click" )
clickCounter += 1 ;
else if ( event . type == "mouseover" )
mouseoverCounter += 1 ;
} ;
2008-05-29 01:18:25 +02:00
jQuery ( "#firstp" ) . bind ( "click mouseover" , handler ) . unbind ( "click mouseover" , handler ) . trigger ( "click" ) . trigger ( "mouseover" ) ;
2008-05-06 20:56:02 +02:00
equals ( clickCounter , 0 , "unbind() with multiple events at once" ) ;
equals ( mouseoverCounter , 0 , "unbind() with multiple events at once" ) ;
2006-12-28 12:37:07 +01:00
} ) ;
2007-08-30 18:34:34 +02:00
test ( "trigger(event, [data], [fn])" , function ( ) {
2007-12-08 03:54:09 +01:00
expect ( 67 ) ;
2007-08-30 07:51:11 +02:00
2006-12-28 12:37:07 +01:00
var handler = function ( event , a , b , c ) {
2007-08-30 07:51:11 +02:00
equals ( event . type , "click" , "check passed data" ) ;
equals ( a , 1 , "check passed data" ) ;
equals ( b , "2" , "check passed data" ) ;
equals ( c , "abc" , "check passed data" ) ;
return "test" ;
} ;
2007-08-30 18:34:34 +02:00
var handler2 = function ( a , b , c ) {
equals ( a , 1 , "check passed data" ) ;
equals ( b , "2" , "check passed data" ) ;
equals ( c , "abc" , "check passed data" ) ;
2007-12-03 22:41:10 +01:00
return false ;
} ;
var handler3 = function ( a , b , c , v ) {
equals ( a , 1 , "check passed data" ) ;
equals ( b , "2" , "check passed data" ) ;
equals ( c , "abc" , "check passed data" ) ;
equals ( v , "test" , "check current value" ) ;
return "newVal" ;
} ;
var handler4 = function ( a , b , c , v ) {
equals ( a , 1 , "check passed data" ) ;
equals ( b , "2" , "check passed data" ) ;
equals ( c , "abc" , "check passed data" ) ;
equals ( v , "test" , "check current value" ) ;
2007-08-30 18:34:34 +02:00
} ;
2007-08-30 07:51:11 +02:00
// Simulate a "native" click
2008-05-29 01:18:25 +02:00
jQuery ( "#firstp" ) [ 0 ] . click = function ( ) {
2007-08-30 07:51:11 +02:00
ok ( true , "Native call was triggered" ) ;
2007-04-24 23:48:52 +02:00
} ;
2007-08-30 07:51:11 +02:00
// Triggers handlrs and native
// Trigger 5
2008-05-29 01:18:25 +02:00
jQuery ( "#firstp" ) . bind ( "click" , handler ) . trigger ( "click" , [ 1 , "2" , "abc" ] ) ;
2007-08-30 07:51:11 +02:00
// Triggers handlers, native, and extra fn
2007-12-03 22:41:10 +01:00
// Triggers 9
2008-05-29 01:18:25 +02:00
jQuery ( "#firstp" ) . trigger ( "click" , [ 1 , "2" , "abc" ] , handler4 ) ;
2007-08-30 07:51:11 +02:00
// Simulate a "native" click
2008-05-29 01:18:25 +02:00
jQuery ( "#firstp" ) [ 0 ] . click = function ( ) {
2007-08-30 07:51:11 +02:00
ok ( false , "Native call was triggered" ) ;
} ;
2007-12-03 22:41:10 +01:00
// Triggers handlers, native, and extra fn
// Triggers 7
2008-05-29 01:18:25 +02:00
jQuery ( "#firstp" ) . trigger ( "click" , [ 1 , "2" , "abc" ] , handler2 ) ;
2007-12-03 22:41:10 +01:00
2007-08-30 07:51:11 +02:00
// Trigger only the handlers (no native)
2007-08-30 18:34:34 +02:00
// Triggers 5
2008-05-29 01:18:25 +02:00
equals ( jQuery ( "#firstp" ) . triggerHandler ( "click" , [ 1 , "2" , "abc" ] ) , "test" , "Verify handler response" ) ;
2007-08-30 07:51:11 +02:00
// Trigger only the handlers (no native) and extra fn
// Triggers 8
2008-05-29 01:18:25 +02:00
equals ( jQuery ( "#firstp" ) . triggerHandler ( "click" , [ 1 , "2" , "abc" ] , handler2 ) , false , "Verify handler response" ) ;
2007-08-30 18:34:34 +02:00
// Build fake click event to pass in
2007-09-15 04:23:08 +02:00
var eventObj = jQuery . event . fix ( { type : "foo" , target : document . body } ) ;
2007-08-30 18:34:34 +02:00
// Trigger only the handlers (no native), with external event obj
// Triggers 5
2008-05-29 01:18:25 +02:00
equals ( jQuery ( "#firstp" ) . triggerHandler ( "click" , [ eventObj , 1 , "2" , "abc" ] ) , "test" , "Verify handler response" ) ;
2007-08-30 18:34:34 +02:00
// Trigger only the handlers (no native) and extra fn, with external event obj
// Triggers 9
2007-12-20 08:39:35 +01:00
eventObj = jQuery . event . fix ( { type : "foo" , target : document . body } ) ;
2008-05-29 01:18:25 +02:00
equals ( jQuery ( "#firstp" ) . triggerHandler ( "click" , [ eventObj , 1 , "2" , "abc" ] , handler ) , "test" , "Verify handler response" ) ;
2007-12-08 03:54:09 +01:00
var pass = true ;
try {
2008-05-29 01:18:25 +02:00
jQuery ( 'input:first' )
2007-12-08 03:54:09 +01:00
. hide ( )
. trigger ( 'focus' ) ;
} catch ( e ) {
pass = false ;
}
ok ( pass , "Trigger focus on hidden element" ) ;
2007-12-03 22:41:10 +01:00
// have the extra handler override the return
// Triggers 9
2008-05-29 01:18:25 +02:00
equals ( jQuery ( "#firstp" ) . triggerHandler ( "click" , [ 1 , "2" , "abc" ] , handler3 ) , "newVal" , "Verify triggerHandler return is overwritten by extra function" ) ;
2007-12-03 22:41:10 +01:00
// have the extra handler leave the return value alone
// Triggers 9
2008-05-29 01:18:25 +02:00
equals ( jQuery ( "#firstp" ) . triggerHandler ( "click" , [ 1 , "2" , "abc" ] , handler4 ) , "test" , "Verify triggerHandler return is not overwritten by extra function" ) ;
2006-12-28 12:37:07 +01:00
} ) ;
2008-04-30 00:20:02 +02:00
test ( "toggle(Function, Function, ...)" , function ( ) {
expect ( 11 ) ;
2007-03-25 20:06:18 +02:00
var count = 0 ,
fn1 = function ( e ) { count ++ ; } ,
fn2 = function ( e ) { count -- ; } ,
preventDefault = function ( e ) { e . preventDefault ( ) } ,
2008-05-29 01:18:25 +02:00
link = jQuery ( '#mark' ) ;
2007-04-22 05:16:53 +02:00
link . click ( preventDefault ) . click ( ) . toggle ( fn1 , fn2 ) . click ( ) . click ( ) . click ( ) . click ( ) . click ( ) ;
2008-05-06 20:56:02 +02:00
equals ( count , 1 , "Check for toggle(fn, fn)" ) ;
2007-11-28 23:23:40 +01:00
2008-05-29 01:18:25 +02:00
jQuery ( "#firstp" ) . toggle ( function ( ) {
2007-11-28 23:23:40 +01:00
equals ( arguments . length , 4 , "toggle correctly passes through additional triggered arguments, see #1701" )
} , function ( ) { } ) . trigger ( "click" , [ 1 , 2 , 3 ] ) ;
2007-03-25 12:30:59 +02:00
var first = 0 ;
2008-05-29 01:18:25 +02:00
jQuery ( "#simon1" ) . one ( "click" , function ( ) {
2007-03-25 12:30:59 +02:00
ok ( true , "Execute event only once" ) ;
2008-05-29 01:18:25 +02:00
jQuery ( this ) . toggle ( function ( ) {
2008-05-06 20:56:02 +02:00
equals ( first ++ , 0 , "toggle(Function,Function) assigned from within one('xxx'), see #1054" ) ;
2007-03-25 12:30:59 +02:00
} , function ( ) {
2008-05-06 20:56:02 +02:00
equals ( first , 1 , "toggle(Function,Function) assigned from within one('xxx'), see #1054" ) ;
2007-03-25 12:30:59 +02:00
} ) ;
2007-03-25 12:34:03 +02:00
return false ;
} ) . click ( ) . click ( ) . click ( ) ;
2008-04-30 00:20:02 +02:00
var turn = 0 ;
var fns = [
function ( ) {
turn = 1 ;
} ,
function ( ) {
turn = 2 ;
} ,
function ( ) {
turn = 3 ;
}
] ;
2008-05-29 01:18:25 +02:00
var $div = jQuery ( "<div> </div>" ) . toggle ( fns [ 0 ] , fns [ 1 ] , fns [ 2 ] ) ;
2008-04-30 00:20:02 +02:00
$div . click ( ) ;
2008-05-06 20:56:02 +02:00
equals ( turn , 1 , "Trying toggle with 3 functions, attempt 1 yields 1" ) ;
2008-04-30 00:20:02 +02:00
$div . click ( ) ;
2008-05-06 20:56:02 +02:00
equals ( turn , 2 , "Trying toggle with 3 functions, attempt 2 yields 2" ) ;
2008-04-30 00:20:02 +02:00
$div . click ( ) ;
2008-05-06 20:56:02 +02:00
equals ( turn , 3 , "Trying toggle with 3 functions, attempt 3 yields 3" ) ;
2008-04-30 00:20:02 +02:00
$div . click ( ) ;
2008-05-06 20:56:02 +02:00
equals ( turn , 1 , "Trying toggle with 3 functions, attempt 4 yields 1" ) ;
2008-04-30 00:20:02 +02:00
$div . click ( ) ;
2008-05-06 20:56:02 +02:00
equals ( turn , 2 , "Trying toggle with 3 functions, attempt 5 yields 2" ) ;
2008-04-30 00:20:02 +02:00
$div . unbind ( 'click' , fns [ 0 ] ) ;
2008-05-29 01:18:25 +02:00
var data = jQuery . data ( $div [ 0 ] , 'events' ) ;
2008-04-30 00:20:02 +02:00
ok ( ! data , "Unbinding one function from toggle unbinds them all" ) ;
2007-08-30 07:51:11 +02:00
} ) ;
2007-12-17 18:39:50 +01:00
test ( "jQuery(function($) {})" , function ( ) {
stop ( ) ;
jQuery ( function ( $ ) {
equals ( jQuery , $ , "ready doesn't provide an event object, instead it provides a reference to the jQuery function, see http://docs.jquery.com/Events/ready#fn" ) ;
start ( ) ;
} ) ;
2007-12-20 08:39:35 +01:00
} ) ;
2008-04-22 23:59:40 +02:00
test ( "event properties" , function ( ) {
stop ( ) ;
2008-05-29 01:18:25 +02:00
jQuery ( "#simon1" ) . click ( function ( event ) {
2008-04-22 23:59:40 +02:00
ok ( event . timeStamp , "assert event.timeStamp is present" ) ;
start ( ) ;
} ) . click ( ) ;
2008-05-08 18:25:12 +02:00
} ) ;