Made sure that return false works in .live() along with the event object being passed...
[jquery.git] / test / unit / event.js
1 module("event");
2
3 test("bind(), with data", function() {
4         expect(3);
5         var handler = function(event) {
6                 ok( event.data, "bind() with data, check passed data exists" );
7                 equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
8         };
9         jQuery("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler);
10
11         ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
12 });
13
14 test("bind(), with data, trigger with data", function() {
15         expect(4);
16         var handler = function(event, data) {
17                 ok( event.data, "check passed data exists" );
18                 equals( event.data.foo, "bar", "Check value of passed data" );
19                 ok( data, "Check trigger data" );
20                 equals( data.bar, "foo", "Check value of trigger data" );
21         };
22         jQuery("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).unbind("click", handler);
23 });
24
25 test("bind(), multiple events at once", function() {
26         expect(2);
27         var clickCounter = 0,
28                 mouseoverCounter = 0;
29         var handler = function(event) {
30                 if (event.type == "click")
31                         clickCounter += 1;
32                 else if (event.type == "mouseover")
33                         mouseoverCounter += 1;
34         };
35         jQuery("#firstp").bind("click mouseover", handler).trigger("click").trigger("mouseover");
36         equals( clickCounter, 1, "bind() with multiple events at once" );
37         equals( mouseoverCounter, 1, "bind() with multiple events at once" );
38 });
39
40 test("bind(), no data", function() {
41         expect(1);
42         var handler = function(event) {
43                 ok ( !event.data, "Check that no data is added to the event object" );
44         };
45         jQuery("#firstp").bind("click", handler).trigger("click");
46 });
47
48 test("bind(), iframes", function() {
49         // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
50         // var doc = document.getElementById("iframe").contentDocument;
51         // 
52         // doc.body.innerHTML = "<input type='text'/>";
53         //
54         // var input = doc.getElementsByTagName("input")[0];
55         //
56         // jQuery(input).bind("click",function() {
57         //      ok( true, "Binding to element inside iframe" );
58         // }).click();
59 });
60
61 test("bind(), trigger change on select", function() {
62         expect(3);
63         var counter = 0;
64         function selectOnChange(event) {
65                 equals( event.data, counter++, "Event.data is not a global event object" );
66         };
67         jQuery("#form select").each(function(i){
68                 jQuery(this).bind('change', i, selectOnChange);
69         }).trigger('change');
70 });
71
72 test("bind(), namespaced events, cloned events", function() {
73         expect(6);
74
75         jQuery("#firstp").bind("custom.test",function(e){
76                 ok(true, "Custom event triggered");
77         });
78
79         jQuery("#firstp").bind("click",function(e){
80                 ok(true, "Normal click triggered");
81         });
82
83         jQuery("#firstp").bind("click.test",function(e){
84                 ok(true, "Namespaced click triggered");
85         });
86
87         // Trigger both bound fn (2)
88         jQuery("#firstp").trigger("click");
89
90         // Trigger one bound fn (1)
91         jQuery("#firstp").trigger("click.test");
92
93         // Remove only the one fn
94         jQuery("#firstp").unbind("click.test");
95
96         // Trigger the remaining fn (1)
97         jQuery("#firstp").trigger("click");
98
99         // Remove the remaining fn
100         jQuery("#firstp").unbind(".test");
101
102         // Trigger the remaining fn (0)
103         jQuery("#firstp").trigger("custom");
104
105         // using contents will get comments regular, text, and comment nodes
106         jQuery("#nonnodes").contents().bind("tester", function () {
107                 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
108         }).trigger("tester");
109
110         // Make sure events stick with appendTo'd elements (which are cloned) #2027
111         jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
112         ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
113 });
114
115 test("bind(), multi-namespaced events", function() {
116         expect(6);
117         
118         var order = [
119                 "click.test.abc",
120                 "click.test.abc",
121                 "click.test",
122                 "click.test.abc",
123                 "click.test",
124                 "custom.test2"
125         ];
126         
127         function check(name, msg){
128                 same(name, order.shift(), msg);
129         }
130
131         jQuery("#firstp").bind("custom.test",function(e){
132                 check("custom.test", "Custom event triggered");
133         });
134
135         jQuery("#firstp").bind("custom.test2",function(e){
136                 check("custom.test2", "Custom event triggered");
137         });
138
139         jQuery("#firstp").bind("click.test",function(e){
140                 check("click.test", "Normal click triggered");
141         });
142
143         jQuery("#firstp").bind("click.test.abc",function(e){
144                 check("click.test.abc", "Namespaced click triggered");
145         });
146
147         // Trigger both bound fn (1)
148         jQuery("#firstp").trigger("click.test.abc");
149
150         // Trigger one bound fn (1)
151         jQuery("#firstp").trigger("click.abc");
152
153         // Trigger two bound fn (2)
154         jQuery("#firstp").trigger("click.test");
155
156         // Remove only the one fn
157         jQuery("#firstp").unbind("click.abc");
158
159         // Trigger the remaining fn (1)
160         jQuery("#firstp").trigger("click");
161
162         // Remove the remaining fn
163         jQuery("#firstp").unbind(".test");
164
165         // Trigger the remaining fn (1)
166         jQuery("#firstp").trigger("custom");
167 });
168
169 test("trigger() shortcuts", function() {
170         expect(6);
171         jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
172                 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
173                 equals( close.length, 0, "Context element does not exist, length must be zero" );
174                 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
175                 return false;
176         }).click();
177         
178         jQuery("#check1").click(function() {
179                 ok( true, "click event handler for checkbox gets fired twice, see #815" );
180         }).click();
181         
182         var counter = 0;
183         jQuery('#firstp')[0].onclick = function(event) {
184                 counter++;
185         };
186         jQuery('#firstp').click();
187         equals( counter, 1, "Check that click, triggers onclick event handler also" );
188         
189         var clickCounter = 0;
190         jQuery('#simon1')[0].onclick = function(event) {
191                 clickCounter++;
192         };
193         jQuery('#simon1').click();
194         equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
195         
196         jQuery('<img />').load(function(){
197                 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
198         }).load();
199 });
200
201 test("trigger() bubbling", function() {
202         expect(14);
203
204         var doc = 0, html = 0, body = 0, main = 0, ap = 0;
205
206         jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
207         jQuery("html").bind("click", function(e){ html++; });
208         jQuery("body").bind("click", function(e){ body++; });
209         jQuery("#main").bind("click", function(e){ main++; });
210         jQuery("#ap").bind("click", function(){ ap++; return false; });
211
212         jQuery("html").trigger("click");
213         equals( doc, 1, "HTML bubble" );
214         equals( html, 1, "HTML bubble" );
215
216         jQuery("body").trigger("click");
217         equals( doc, 2, "Body bubble" );
218         equals( html, 2, "Body bubble" );
219         equals( body, 1, "Body bubble" );
220
221         jQuery("#main").trigger("click");
222         equals( doc, 3, "Main bubble" );
223         equals( html, 3, "Main bubble" );
224         equals( body, 2, "Main bubble" );
225         equals( main, 1, "Main bubble" );
226
227         jQuery("#ap").trigger("click");
228         equals( doc, 3, "ap bubble" );
229         equals( html, 3, "ap bubble" );
230         equals( body, 2, "ap bubble" );
231         equals( main, 1, "ap bubble" );
232         equals( ap, 1, "ap bubble" );
233 });
234
235 test("unbind(event)", function() {
236         expect(8);
237         var el = jQuery("#firstp");
238         el.click(function() {
239                 ok( true, "Fake normal bind" );
240         });
241         el.click(function(event) {
242                 el.unbind(event);
243                 ok( true, "Fake onebind" );
244         });
245         el.click().click();
246         
247         el.click(function() { return; });
248         el.unbind('click');
249         ok( !el[0].onclick, "Handler is removed" ); // Bug #964
250
251         el.click(function() { return; });
252         el.unbind('change',function(){ return; });
253         for (var ret in jQuery.data(el[0], "events")['click']) break;
254         ok( ret, "Extra handlers weren't accidentally removed." );
255
256         el.unbind('click');
257         ok( !jQuery.data(el[0], "events"), "Removed the events expando after all handlers are unbound." );
258         
259         reset();
260         var clickCounter = (mouseoverCounter = 0);
261         var handler = function(event) {
262                 if (event.type == "click")
263                         clickCounter += 1;
264                 else if (event.type == "mouseover")
265                         mouseoverCounter += 1;
266         };
267         jQuery("#firstp").bind("click mouseover", handler).unbind("click mouseover", handler).trigger("click").trigger("mouseover");
268         equals( clickCounter, 0, "unbind() with multiple events at once" );
269         equals( mouseoverCounter, 0, "unbind() with multiple events at once" );
270 });
271
272 test("trigger(event, [data], [fn])", function() {
273         expect(69);
274
275         var handler = function(event, a, b, c) {
276                 equals( event.type, "click", "check passed data" );
277                 equals( a, 1, "check passed data" );
278                 equals( b, "2", "check passed data" );
279                 equals( c, "abc", "check passed data" );
280                 return "test";
281         };
282
283         var handler2 = function(a, b, c) {
284                 equals( a, 1, "check passed data" );
285                 equals( b, "2", "check passed data" );
286                 equals( c, "abc", "check passed data" );
287                 return false;
288         };
289
290         var handler3 = function(a, b, c, v) {
291                 equals( a, 1, "check passed data" );
292                 equals( b, "2", "check passed data" );
293                 equals( c, "abc", "check passed data" );
294                 equals( v, "test", "check current value" );
295                 return "newVal";
296         };
297
298         var handler4 = function(a, b, c, v) {
299                 equals( a, 1, "check passed data" );
300                 equals( b, "2", "check passed data" );
301                 equals( c, "abc", "check passed data" );
302                 equals( v, "test", "check current value" );
303         };
304         
305         var $elem = jQuery("#firstp");
306
307         // Simulate a "native" click
308         $elem[0].click = function(){
309                 ok( true, "Native call was triggered" );
310         };
311
312         // Triggers handlrs and native
313         // Trigger 5
314         $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
315
316         // Triggers handlers, native, and extra fn
317         // Triggers 9
318         $elem.trigger("click", [1, "2", "abc"], handler4);
319
320         // Simulate a "native" click
321         $elem[0].click = function(){
322                 ok( false, "Native call was triggered" );
323         };
324
325         // Trigger only the handlers (no native)
326         // Triggers 5
327         equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
328
329         // Trigger only the handlers (no native) and extra fn
330         // Triggers 8
331         equals( $elem.triggerHandler("click", [1, "2", "abc"], handler2), false, "Verify handler response" );
332
333         var eventObj = jQuery.Event("noNew");
334         
335         ok( eventObj != window, "Instantiate jQuery.Event without the 'new' keyword" );
336         equals( eventObj.type, "noNew", "Verify its type" );
337         
338         // Build fake click event to pass in
339         eventObj = new jQuery.Event("click");
340
341         // Trigger only the handlers (no native), with external event obj
342         // Triggers 5
343         equals( $elem.triggerHandler(eventObj, [1, "2", "abc"]), "test", "Verify handler response" );
344
345         // Trigger only the handlers (no native) and extra fn, with external event obj
346         // Triggers 9
347         eventObj = new jQuery.Event("click");
348         equals( $elem.triggerHandler(eventObj, [1, "2", "abc"], handler2), false, "Verify handler response" );
349         
350         var pass = true;
351         try {
352                 jQuery('#form input:first').hide().trigger('focus');
353         } catch(e) {
354                 pass = false;
355         }
356         ok( pass, "Trigger focus on hidden element" );
357
358         // have the extra handler override the return
359         // Triggers 9
360         equals( $elem.triggerHandler("click", [1, "2", "abc"], handler3), "newVal", "Verify triggerHandler return is overwritten by extra function" );
361
362         // have the extra handler leave the return value alone
363         // Triggers 9
364         equals( $elem.triggerHandler("click", [1, "2", "abc"], handler4), "test", "Verify triggerHandler return is not overwritten by extra function" );
365         
366         $elem.unbind('click').bind('foo',function(e){
367                 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
368                 equals( e.target.id, 'simon1', 'Verify event.target when passed passing an event object' );
369                 equals( e.currentTarget.id, 'firstp', 'Verify event.target when passed passing an event object' );
370                 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
371         });
372         
373         eventObj = new jQuery.Event('foo');
374         eventObj.secret = 'boo!';
375         
376         // Test with event object and bubbling  
377         jQuery("#simon1").trigger( eventObj );
378         
379         // Try passing an object literal
380         jQuery("#simon1").trigger( {type:'foo', secret:'boo!'} );
381         
382         $elem.unbind('foo');
383 });
384
385 test("toggle(Function, Function, ...)", function() {
386         expect(11);
387         
388         var count = 0,
389                 fn1 = function(e) { count++; },
390                 fn2 = function(e) { count--; },
391                 preventDefault = function(e) { e.preventDefault() },
392                 link = jQuery('#mark');
393         link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
394         equals( count, 1, "Check for toggle(fn, fn)" );
395
396         jQuery("#firstp").toggle(function () {
397                 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
398         }, function() {}).trigger("click", [ 1, 2, 3 ]);
399
400         var first = 0;
401         jQuery("#simon1").one("click", function() {
402                 ok( true, "Execute event only once" );
403                 jQuery(this).toggle(function() {
404                         equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
405                 }, function() {
406                         equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
407                 });
408                 return false;
409         }).click().click().click();
410         
411         var turn = 0;
412         var fns = [
413                 function(){
414                         turn = 1;
415                 },
416                 function(){
417                         turn = 2;
418                 },
419                 function(){
420                         turn = 3;
421                 }
422         ];
423         
424         var $div = jQuery("<div>&nbsp;</div>").toggle( fns[0], fns[1], fns[2] );
425         $div.click();
426         equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
427         $div.click();
428         equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
429         $div.click();
430         equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
431         $div.click();
432         equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
433         $div.click();
434         equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
435         
436         $div.unbind('click',fns[0]);
437         var data = jQuery.data( $div[0], 'events' );
438         ok( !data, "Unbinding one function from toggle unbinds them all");
439 });
440
441 test(".live()/.die()", function() {
442         expect(30);
443
444         var submit = 0, div = 0, livea = 0, liveb = 0;
445
446         jQuery("div").live("submit", function(){ submit++; return false; });
447         jQuery("div").live("click", function(){ div++; });
448         jQuery("div#nothiddendiv").live("click", function(){ livea++; });
449         jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
450
451         // Nothing should trigger on the body
452         jQuery("body").trigger("click");
453         equals( submit, 0, "Click on body" );
454         equals( div, 0, "Click on body" );
455         equals( livea, 0, "Click on body" );
456         equals( liveb, 0, "Click on body" );
457
458         // This should trigger two events
459         jQuery("div#nothiddendiv").trigger("click");
460         equals( submit, 0, "Click on div" );
461         equals( div, 1, "Click on div" );
462         equals( livea, 1, "Click on div" );
463         equals( liveb, 0, "Click on div" );
464
465         // This should trigger three events (w/ bubbling)
466         jQuery("div#nothiddendivchild").trigger("click");
467         equals( submit, 0, "Click on inner div" );
468         equals( div, 2, "Click on inner div" );
469         equals( livea, 2, "Click on inner div" );
470         equals( liveb, 1, "Click on inner div" );
471
472         // This should trigger one submit
473         jQuery("div#nothiddendivchild").trigger("submit");
474         equals( submit, 1, "Submit on div" );
475         equals( div, 2, "Submit on div" );
476         equals( livea, 2, "Submit on div" );
477         equals( liveb, 1, "Submit on div" );
478
479         // Make sure no other events were removed in the process
480         jQuery("div#nothiddendivchild").trigger("click");
481         equals( submit, 1, "die Click on inner div" );
482         equals( div, 3, "die Click on inner div" );
483         equals( livea, 3, "die Click on inner div" );
484         equals( liveb, 2, "die Click on inner div" );
485
486         // Now make sure that the removal works
487         jQuery("div#nothiddendivchild").die("click");
488         jQuery("div#nothiddendivchild").trigger("click");
489         equals( submit, 1, "die Click on inner div" );
490         equals( div, 4, "die Click on inner div" );
491         equals( livea, 4, "die Click on inner div" );
492         equals( liveb, 2, "die Click on inner div" );
493
494         // Make sure that the click wasn't removed too early
495         jQuery("div#nothiddendiv").trigger("click");
496         equals( submit, 1, "die Click on inner div" );
497         equals( div, 5, "die Click on inner div" );
498         equals( livea, 5, "die Click on inner div" );
499         equals( liveb, 2, "die Click on inner div" );
500
501         jQuery("div#nothiddendiv").die("click");
502         jQuery("div").die("click");
503         jQuery("div").die("submit");
504
505         // Verify that return false prevents default action
506         jQuery("#anchor2").live("click", function(){ return false; });
507         var hash = window.location.hash;
508         jQuery("#anchor2").trigger("click");
509         equals( window.location.hash, hash, "return false worked" );
510         jQuery("#anchor2").die("click");
511
512         // Verify that .preventDefault() prevents default action
513         jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
514         var hash = window.location.hash;
515         jQuery("#anchor2").trigger("click");
516         equals( window.location.hash, hash, "e.preventDefault() worked" );
517         jQuery("#anchor2").die("click");
518 });
519
520 /*
521 test("jQuery(function($) {})", function() {
522         stop();
523         jQuery(function($) {
524                 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");
525                 start();
526         });
527 });
528
529 test("event properties", function() {
530         stop();
531         jQuery("#simon1").click(function(event) {
532                 ok( event.timeStamp, "assert event.timeStamp is present" );
533                 start();
534         }).click();
535 });
536 */