Added support for .unbind(".test") to unbind all namespaced events on an element.
[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                 ok( event.data.foo == "bar", "bind() with data, Check value of passed data" );
8         };
9         $("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler);
10
11         ok( !jQuery.data($("#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                 ok( event.data.foo == "bar", "Check value of passed data" );
19                 ok( data, "Check trigger data" );
20                 ok( data.bar == "foo", "Check value of trigger data" );
21         };
22         $("#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         $("#firstp").bind("click mouseover", handler).trigger("click").trigger("mouseover");
36         ok( clickCounter == 1, "bind() with multiple events at once" );
37         ok( 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         $("#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         // $(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         $("#form select").each(function(i){
68                 $(this).bind('change', i, selectOnChange);
69         }).trigger('change');
70 });
71
72 test("bind(), namespaced events, cloned events", function() {
73         expect(6);
74
75         $("#firstp").bind("custom.test",function(e){
76                 ok(true, "Custom event triggered");
77         });
78
79         $("#firstp").bind("click",function(e){
80                 ok(true, "Normal click triggered");
81         });
82
83         $("#firstp").bind("click.test",function(e){
84                 ok(true, "Namespaced click triggered");
85         });
86
87         // Trigger both bound fn (2)
88         $("#firstp").trigger("click");
89
90         // Trigger one bound fn (1)
91         $("#firstp").trigger("click.test");
92
93         // Remove only the one fn
94         $("#firstp").unbind("click.test");
95
96         // Trigger the remaining fn (1)
97         $("#firstp").trigger("click");
98
99         // Remove the remaining fn
100         $("#firstp").unbind(".test");
101
102         // Trigger the remaining fn (0)
103         $("#firstp").trigger("custom");
104
105         // using contents will get comments regular, text, and comment nodes
106         $("#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         $("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
112         ok( $("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
113 });
114
115 test("click()", function() {
116         expect(4);
117         $('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
118                 var close = $('spanx', this); // same with $(this).find('span');
119                 ok( close.length == 0, "Context element does not exist, length must be zero" );
120                 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
121                 return false;
122         }).click();
123         
124         $("#check1").click(function() {
125                 ok( true, "click event handler for checkbox gets fired twice, see #815" );
126         }).click();
127         
128         var counter = 0;
129         $('#firstp')[0].onclick = function(event) {
130                 counter++;
131         };
132         $('#firstp').click();
133         ok( counter == 1, "Check that click, triggers onclick event handler also" );
134 });
135
136 test("unbind(event)", function() {
137         expect(8);
138         var el = $("#firstp");
139         el.click(function() {
140                 ok( true, "Fake normal bind" );
141         });
142         el.click(function(event) {
143                 el.unbind(event);
144                 ok( true, "Fake onebind" );
145         });
146         el.click().click();
147         
148         el.click(function() { return; });
149         el.unbind('click');
150         ok( !el[0].onclick, "Handler is removed" ); // Bug #964
151
152         el.click(function() { return; });
153         el.unbind('change',function(){ return; });
154         for (var ret in jQuery.data(el[0], "events")['click']) break;
155         ok( ret, "Extra handlers weren't accidentally removed." );
156
157         el.unbind('click');
158         ok( !jQuery.data(el[0], "events"), "Removed the events expando after all handlers are unbound." );
159         
160         reset();
161         var clickCounter = mouseoverCounter = 0;
162         var handler = function(event) {
163                 if (event.type == "click")
164                         clickCounter += 1;
165                 else if (event.type == "mouseover")
166                         mouseoverCounter += 1;
167         };
168         $("#firstp").bind("click mouseover", handler).unbind("click mouseover", handler).trigger("click").trigger("mouseover");
169         ok( clickCounter == 0, "unbind() with multiple events at once" );
170         ok( mouseoverCounter == 0, "unbind() with multiple events at once" );
171 });
172
173 test("trigger(event, [data], [fn])", function() {
174         expect(67);
175
176         var handler = function(event, a, b, c) {
177                 equals( event.type, "click", "check passed data" );
178                 equals( a, 1, "check passed data" );
179                 equals( b, "2", "check passed data" );
180                 equals( c, "abc", "check passed data" );
181                 return "test";
182         };
183
184         var handler2 = function(a, b, c) {
185                 equals( a, 1, "check passed data" );
186                 equals( b, "2", "check passed data" );
187                 equals( c, "abc", "check passed data" );
188                 return false;
189         };
190
191         var handler3 = function(a, b, c, v) {
192                 equals( a, 1, "check passed data" );
193                 equals( b, "2", "check passed data" );
194                 equals( c, "abc", "check passed data" );
195                 equals( v, "test", "check current value" );
196                 return "newVal";
197         };
198
199         var handler4 = function(a, b, c, v) {
200                 equals( a, 1, "check passed data" );
201                 equals( b, "2", "check passed data" );
202                 equals( c, "abc", "check passed data" );
203                 equals( v, "test", "check current value" );
204         };
205
206         // Simulate a "native" click
207         $("#firstp")[0].click = function(){
208                 ok( true, "Native call was triggered" );
209         };
210
211         // Triggers handlrs and native
212         // Trigger 5
213         $("#firstp").bind("click", handler).trigger("click", [1, "2", "abc"]);
214
215         // Triggers handlers, native, and extra fn
216         // Triggers 9
217         $("#firstp").trigger("click", [1, "2", "abc"], handler4);
218
219         // Simulate a "native" click
220         $("#firstp")[0].click = function(){
221                 ok( false, "Native call was triggered" );
222         };
223
224         // Triggers handlers, native, and extra fn
225         // Triggers 7
226         $("#firstp").trigger("click", [1, "2", "abc"], handler2);
227
228         // Trigger only the handlers (no native)
229         // Triggers 5
230         equals( $("#firstp").triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
231
232         // Trigger only the handlers (no native) and extra fn
233         // Triggers 8
234         equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler2), false, "Verify handler response" );
235
236         // Build fake click event to pass in
237         var eventObj = jQuery.event.fix({ type: "foo", target: document.body });
238
239         // Trigger only the handlers (no native), with external event obj
240         // Triggers 5
241         equals( $("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"]), "test", "Verify handler response" );
242
243         // Trigger only the handlers (no native) and extra fn, with external event obj
244         // Triggers 9
245         eventObj = jQuery.event.fix({ type: "foo", target: document.body });
246         equals( $("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"], handler), "test", "Verify handler response" );
247         
248         var pass = true;
249         try {
250                 $('input:first')
251                         .hide()
252                         .trigger('focus');
253         } catch(e) {
254                 pass = false;
255         }
256         ok( pass, "Trigger focus on hidden element" );
257
258         // have the extra handler override the return
259         // Triggers 9
260         equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler3), "newVal", "Verify triggerHandler return is overwritten by extra function" );
261
262         // have the extra handler leave the return value alone
263         // Triggers 9
264         equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler4), "test", "Verify triggerHandler return is not overwritten by extra function" );
265 });
266
267 test("toggle(Function, Function)", function() {
268         expect(5);
269         var count = 0,
270                 fn1 = function(e) { count++; },
271                 fn2 = function(e) { count--; },
272                 preventDefault = function(e) { e.preventDefault() },
273                 link = $('#mark');
274         link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
275         ok( count == 1, "Check for toggle(fn, fn)" );
276
277         $("#firstp").toggle(function () {
278                 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
279         }, function() {}).trigger("click", [ 1, 2, 3 ]);
280
281         var first = 0;
282         $("#simon1").one("click", function() {
283                 ok( true, "Execute event only once" );
284                 $(this).toggle(function() {
285                         ok( first++ == 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
286                 }, function() {
287                         ok( first == 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
288                 });
289                 return false;
290         }).click().click().click();
291 });
292
293 test("jQuery(function($) {})", function() {
294         stop();
295         jQuery(function($) {
296                 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");
297                 start();
298         });
299 });