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" ) ;
ok ( event . data . foo == "bar" , "bind() with data, Check value of passed data" ) ;
2007-04-24 23:48:52 +02:00
} ;
2007-04-25 00:35:04 +02:00
$ ( "#firstp" ) . bind ( "click" , { foo : "bar" } , handler ) . click ( ) . unbind ( "click" , handler ) ;
2007-09-09 01:31:23 +02:00
ok ( ! jQuery . data ( $ ( "#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" ) ;
ok ( event . data . foo == "bar" , "Check value of passed data" ) ;
ok ( data , "Check trigger data" ) ;
ok ( data . bar == "foo" , "Check value of trigger data" ) ;
2007-04-24 23:48:52 +02:00
} ;
2007-12-15 06:55:33 +01:00
$ ( "#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 ;
} ;
$ ( "#firstp" ) . bind ( "click mouseover" , handler ) . trigger ( "click" ) . trigger ( "mouseover" ) ;
ok ( clickCounter == 1 , "bind() with multiple events at once" ) ;
ok ( 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" ) ;
} ;
2007-04-25 00:35:04 +02:00
$ ( "#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
//
2007-07-20 23:53:37 +02:00
// $(input).bind("click",function() {
// 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
} ;
2007-09-05 19:06:05 +02:00
$ ( "#form select" ) . each ( function ( i ) {
2007-03-25 20:06:18 +02:00
$ ( this ) . bind ( 'change' , i , selectOnChange ) ;
} ) . 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-02-03 05:33:11 +01:00
$ ( "#firstp" ) . bind ( "custom.test" , function ( e ) {
ok ( true , "Custom event triggered" ) ;
} ) ;
2007-12-03 22:41:10 +01:00
$ ( "#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
2007-12-03 22:41:10 +01:00
$ ( "#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)
2007-12-03 22:41:10 +01:00
$ ( "#firstp" ) . trigger ( "click" ) ;
2007-09-03 16:53:09 +02:00
// Trigger one bound fn (1)
2007-12-03 22:41:10 +01:00
$ ( "#firstp" ) . trigger ( "click.test" ) ;
2007-09-03 16:53:09 +02:00
// Remove only the one fn
2007-12-03 22:41:10 +01:00
$ ( "#firstp" ) . unbind ( "click.test" ) ;
2007-09-03 16:53:09 +02:00
// Trigger the remaining fn (1)
2007-12-03 22:41:10 +01:00
$ ( "#firstp" ) . trigger ( "click" ) ;
2007-12-07 02:52:21 +01:00
2008-02-03 05:33:11 +01:00
// Remove the remaining fn
$ ( "#firstp" ) . unbind ( ".test" ) ;
// Trigger the remaining fn (0)
$ ( "#firstp" ) . trigger ( "custom" ) ;
2007-12-07 02:52:21 +01:00
// using contents will get comments regular, text, and comment nodes
$ ( "#nonnodes" ) . contents ( ) . bind ( "tester" , function ( ) {
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
$ ( "<a href='#fail' class='test'>test</a>" ) . click ( function ( ) { return false ; } ) . appendTo ( "p" ) ;
ok ( $ ( "a.test:first" ) . triggerHandler ( "click" ) === false , "Handler is bound to appendTo'd elements" ) ;
2007-03-25 20:06:18 +02:00
} ) ;
test ( "click()" , function ( ) {
2008-04-21 22:39:17 +02:00
expect ( 5 ) ;
2007-04-22 05:16:53 +02:00
$ ( '<li><a href="#">Change location</a></li>' ) . prependTo ( '#firstUL' ) . find ( 'a' ) . bind ( 'click' , function ( ) {
2007-12-03 22:41:10 +01:00
var close = $ ( 'spanx' , this ) ; // same with $(this).find('span');
ok ( close . length == 0 , "Context element does not exist, length must be zero" ) ;
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 ( ) ;
$ ( "#check1" ) . click ( function ( ) {
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 ;
$ ( '#firstp' ) [ 0 ] . onclick = function ( event ) {
counter ++ ;
} ;
$ ( '#firstp' ) . click ( ) ;
ok ( counter == 1 , "Check that click, triggers onclick event handler also" ) ;
2008-04-21 22:39:17 +02:00
var clickCounter = 0 ;
$ ( '#simon1' ) [ 0 ] . onclick = function ( event ) {
clickCounter ++ ;
} ;
$ ( '#simon1' ) . click ( ) ;
ok ( clickCounter == 1 , "Check that click, triggers onclick event handler on an a tag also" ) ;
2006-12-28 12:37:07 +01:00
} ) ;
test ( "unbind(event)" , function ( ) {
2007-12-15 06:55:33 +01:00
expect ( 8 ) ;
2006-12-28 12:37:07 +01:00
var el = $ ( "#firstp" ) ;
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 ( ) ;
var clickCounter = mouseoverCounter = 0 ;
var handler = function ( event ) {
if ( event . type == "click" )
clickCounter += 1 ;
else if ( event . type == "mouseover" )
mouseoverCounter += 1 ;
} ;
$ ( "#firstp" ) . bind ( "click mouseover" , handler ) . unbind ( "click mouseover" , handler ) . trigger ( "click" ) . trigger ( "mouseover" ) ;
ok ( clickCounter == 0 , "unbind() with multiple events at once" ) ;
ok ( 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
$ ( "#firstp" ) [ 0 ] . click = function ( ) {
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
2006-12-28 12:37:07 +01:00
$ ( "#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
$ ( "#firstp" ) . trigger ( "click" , [ 1 , "2" , "abc" ] , handler4 ) ;
2007-08-30 07:51:11 +02:00
// Simulate a "native" click
$ ( "#firstp" ) [ 0 ] . click = function ( ) {
ok ( false , "Native call was triggered" ) ;
} ;
2007-12-03 22:41:10 +01:00
// Triggers handlers, native, and extra fn
// Triggers 7
$ ( "#firstp" ) . trigger ( "click" , [ 1 , "2" , "abc" ] , handler2 ) ;
2007-08-30 07:51:11 +02:00
// Trigger only the handlers (no native)
2007-08-30 18:34:34 +02:00
// Triggers 5
2007-08-30 07:51:11 +02:00
equals ( $ ( "#firstp" ) . triggerHandler ( "click" , [ 1 , "2" , "abc" ] ) , "test" , "Verify handler response" ) ;
// Trigger only the handlers (no native) and extra fn
// Triggers 8
2007-12-03 22:41:10 +01:00
equals ( $ ( "#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
2007-09-15 04:23:08 +02:00
equals ( $ ( "#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 } ) ;
2007-09-15 04:23:08 +02:00
equals ( $ ( "#firstp" ) . triggerHandler ( "click" , [ eventObj , 1 , "2" , "abc" ] , handler ) , "test" , "Verify handler response" ) ;
2007-12-08 03:54:09 +01:00
var pass = true ;
try {
$ ( 'input:first' )
. 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
equals ( $ ( "#firstp" ) . triggerHandler ( "click" , [ 1 , "2" , "abc" ] , handler3 ) , "newVal" , "Verify triggerHandler return is overwritten by extra function" ) ;
// have the extra handler leave the return value alone
// Triggers 9
equals ( $ ( "#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
} ) ;
2007-03-25 20:06:18 +02:00
test ( "toggle(Function, Function)" , function ( ) {
2007-11-28 23:23:40 +01:00
expect ( 5 ) ;
2007-03-25 20:06:18 +02:00
var count = 0 ,
fn1 = function ( e ) { count ++ ; } ,
fn2 = function ( e ) { count -- ; } ,
preventDefault = function ( e ) { e . preventDefault ( ) } ,
link = $ ( '#mark' ) ;
2007-04-22 05:16:53 +02:00
link . click ( preventDefault ) . click ( ) . toggle ( fn1 , fn2 ) . click ( ) . click ( ) . click ( ) . click ( ) . click ( ) ;
2007-03-25 20:06:18 +02:00
ok ( count == 1 , "Check for toggle(fn, fn)" ) ;
2007-11-28 23:23:40 +01:00
$ ( "#firstp" ) . toggle ( function ( ) {
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 ;
2007-03-25 12:34:03 +02:00
$ ( "#simon1" ) . one ( "click" , function ( ) {
2007-03-25 12:30:59 +02:00
ok ( true , "Execute event only once" ) ;
$ ( this ) . toggle ( function ( ) {
2007-03-25 20:06:18 +02:00
ok ( first ++ == 0 , "toggle(Function,Function) assigned from within one('xxx'), see #1054" ) ;
2007-03-25 12:30:59 +02:00
} , function ( ) {
2007-03-25 20:06:18 +02:00
ok ( 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 ( ) ;
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
} ) ;