Added support for multiple-namespaced events (in bind, trigger, and unbind).
[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("unbind(event)", function() {
202         expect(8);
203         var el = jQuery("#firstp");
204         el.click(function() {
205                 ok( true, "Fake normal bind" );
206         });
207         el.click(function(event) {
208                 el.unbind(event);
209                 ok( true, "Fake onebind" );
210         });
211         el.click().click();
212         
213         el.click(function() { return; });
214         el.unbind('click');
215         ok( !el[0].onclick, "Handler is removed" ); // Bug #964
216
217         el.click(function() { return; });
218         el.unbind('change',function(){ return; });
219         for (var ret in jQuery.data(el[0], "events")['click']) break;
220         ok( ret, "Extra handlers weren't accidentally removed." );
221
222         el.unbind('click');
223         ok( !jQuery.data(el[0], "events"), "Removed the events expando after all handlers are unbound." );
224         
225         reset();
226         var clickCounter = (mouseoverCounter = 0);
227         var handler = function(event) {
228                 if (event.type == "click")
229                         clickCounter += 1;
230                 else if (event.type == "mouseover")
231                         mouseoverCounter += 1;
232         };
233         jQuery("#firstp").bind("click mouseover", handler).unbind("click mouseover", handler).trigger("click").trigger("mouseover");
234         equals( clickCounter, 0, "unbind() with multiple events at once" );
235         equals( mouseoverCounter, 0, "unbind() with multiple events at once" );
236 });
237
238 test("trigger(event, [data], [fn])", function() {
239         expect(67);
240
241         var handler = function(event, a, b, c) {
242                 equals( event.type, "click", "check passed data" );
243                 equals( a, 1, "check passed data" );
244                 equals( b, "2", "check passed data" );
245                 equals( c, "abc", "check passed data" );
246                 return "test";
247         };
248
249         var handler2 = function(a, b, c) {
250                 equals( a, 1, "check passed data" );
251                 equals( b, "2", "check passed data" );
252                 equals( c, "abc", "check passed data" );
253                 return false;
254         };
255
256         var handler3 = function(a, b, c, v) {
257                 equals( a, 1, "check passed data" );
258                 equals( b, "2", "check passed data" );
259                 equals( c, "abc", "check passed data" );
260                 equals( v, "test", "check current value" );
261                 return "newVal";
262         };
263
264         var handler4 = function(a, b, c, v) {
265                 equals( a, 1, "check passed data" );
266                 equals( b, "2", "check passed data" );
267                 equals( c, "abc", "check passed data" );
268                 equals( v, "test", "check current value" );
269         };
270
271         // Simulate a "native" click
272         jQuery("#firstp")[0].click = function(){
273                 ok( true, "Native call was triggered" );
274         };
275
276         // Triggers handlrs and native
277         // Trigger 5
278         jQuery("#firstp").bind("click", handler).trigger("click", [1, "2", "abc"]);
279
280         // Triggers handlers, native, and extra fn
281         // Triggers 9
282         jQuery("#firstp").trigger("click", [1, "2", "abc"], handler4);
283
284         // Simulate a "native" click
285         jQuery("#firstp")[0].click = function(){
286                 ok( false, "Native call was triggered" );
287         };
288
289         // Triggers handlers, native, and extra fn
290         // Triggers 7
291         jQuery("#firstp").trigger("click", [1, "2", "abc"], handler2);
292
293         // Trigger only the handlers (no native)
294         // Triggers 5
295         equals( jQuery("#firstp").triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
296
297         // Trigger only the handlers (no native) and extra fn
298         // Triggers 8
299         equals( jQuery("#firstp").triggerHandler("click", [1, "2", "abc"], handler2), false, "Verify handler response" );
300
301         // Build fake click event to pass in
302         var eventObj = jQuery.event.fix({ type: "foo", target: document.body });
303
304         // Trigger only the handlers (no native), with external event obj
305         // Triggers 5
306         equals( jQuery("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"]), "test", "Verify handler response" );
307
308         // Trigger only the handlers (no native) and extra fn, with external event obj
309         // Triggers 9
310         eventObj = jQuery.event.fix({ type: "foo", target: document.body });
311         equals( jQuery("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"], handler), "test", "Verify handler response" );
312         
313         var pass = true;
314         try {
315                 jQuery('#form input:first')
316                         .hide()
317                         .trigger('focus');
318         } catch(e) {
319                 pass = false;
320         }
321         ok( pass, "Trigger focus on hidden element" );
322
323         // have the extra handler override the return
324         // Triggers 9
325         equals( jQuery("#firstp").triggerHandler("click", [1, "2", "abc"], handler3), "newVal", "Verify triggerHandler return is overwritten by extra function" );
326
327         // have the extra handler leave the return value alone
328         // Triggers 9
329         equals( jQuery("#firstp").triggerHandler("click", [1, "2", "abc"], handler4), "test", "Verify triggerHandler return is not overwritten by extra function" );
330 });
331
332 test("toggle(Function, Function, ...)", function() {
333         expect(11);
334         
335         var count = 0,
336                 fn1 = function(e) { count++; },
337                 fn2 = function(e) { count--; },
338                 preventDefault = function(e) { e.preventDefault() },
339                 link = jQuery('#mark');
340         link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
341         equals( count, 1, "Check for toggle(fn, fn)" );
342
343         jQuery("#firstp").toggle(function () {
344                 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
345         }, function() {}).trigger("click", [ 1, 2, 3 ]);
346
347         var first = 0;
348         jQuery("#simon1").one("click", function() {
349                 ok( true, "Execute event only once" );
350                 jQuery(this).toggle(function() {
351                         equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
352                 }, function() {
353                         equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
354                 });
355                 return false;
356         }).click().click().click();
357         
358         var turn = 0;
359         var fns = [
360                 function(){
361                         turn = 1;
362                 },
363                 function(){
364                         turn = 2;
365                 },
366                 function(){
367                         turn = 3;
368                 }
369         ];
370         
371         var $div = jQuery("<div>&nbsp;</div>").toggle( fns[0], fns[1], fns[2] );
372         $div.click();
373         equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
374         $div.click();
375         equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
376         $div.click();
377         equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
378         $div.click();
379         equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
380         $div.click();
381         equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
382         
383         $div.unbind('click',fns[0]);
384         var data = jQuery.data( $div[0], 'events' );
385         ok( !data, "Unbinding one function from toggle unbinds them all");
386 });
387 /*
388 test("jQuery(function($) {})", function() {
389         stop();
390         jQuery(function($) {
391                 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");
392                 start();
393         });
394 });
395
396 test("event properties", function() {
397         stop();
398         jQuery("#simon1").click(function(event) {
399                 ok( event.timeStamp, "assert event.timeStamp is present" );
400                 start();
401         }).click();
402 });
403 */