Trigger onclick handlers of links
[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(5);
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         var clickCounter = 0;
136         $('#simon1')[0].onclick = function(event) {
137                 clickCounter++;
138         };
139         $('#simon1').click();
140         ok( clickCounter == 1, "Check that click, triggers onclick event handler on an a tag also" );
141 });
142
143 test("unbind(event)", function() {
144         expect(8);
145         var el = $("#firstp");
146         el.click(function() {
147                 ok( true, "Fake normal bind" );
148         });
149         el.click(function(event) {
150                 el.unbind(event);
151                 ok( true, "Fake onebind" );
152         });
153         el.click().click();
154         
155         el.click(function() { return; });
156         el.unbind('click');
157         ok( !el[0].onclick, "Handler is removed" ); // Bug #964
158
159         el.click(function() { return; });
160         el.unbind('change',function(){ return; });
161         for (var ret in jQuery.data(el[0], "events")['click']) break;
162         ok( ret, "Extra handlers weren't accidentally removed." );
163
164         el.unbind('click');
165         ok( !jQuery.data(el[0], "events"), "Removed the events expando after all handlers are unbound." );
166         
167         reset();
168         var clickCounter = mouseoverCounter = 0;
169         var handler = function(event) {
170                 if (event.type == "click")
171                         clickCounter += 1;
172                 else if (event.type == "mouseover")
173                         mouseoverCounter += 1;
174         };
175         $("#firstp").bind("click mouseover", handler).unbind("click mouseover", handler).trigger("click").trigger("mouseover");
176         ok( clickCounter == 0, "unbind() with multiple events at once" );
177         ok( mouseoverCounter == 0, "unbind() with multiple events at once" );
178 });
179
180 test("trigger(event, [data], [fn])", function() {
181         expect(67);
182
183         var handler = function(event, a, b, c) {
184                 equals( event.type, "click", "check passed data" );
185                 equals( a, 1, "check passed data" );
186                 equals( b, "2", "check passed data" );
187                 equals( c, "abc", "check passed data" );
188                 return "test";
189         };
190
191         var handler2 = function(a, b, c) {
192                 equals( a, 1, "check passed data" );
193                 equals( b, "2", "check passed data" );
194                 equals( c, "abc", "check passed data" );
195                 return false;
196         };
197
198         var handler3 = function(a, b, c, v) {
199                 equals( a, 1, "check passed data" );
200                 equals( b, "2", "check passed data" );
201                 equals( c, "abc", "check passed data" );
202                 equals( v, "test", "check current value" );
203                 return "newVal";
204         };
205
206         var handler4 = function(a, b, c, v) {
207                 equals( a, 1, "check passed data" );
208                 equals( b, "2", "check passed data" );
209                 equals( c, "abc", "check passed data" );
210                 equals( v, "test", "check current value" );
211         };
212
213         // Simulate a "native" click
214         $("#firstp")[0].click = function(){
215                 ok( true, "Native call was triggered" );
216         };
217
218         // Triggers handlrs and native
219         // Trigger 5
220         $("#firstp").bind("click", handler).trigger("click", [1, "2", "abc"]);
221
222         // Triggers handlers, native, and extra fn
223         // Triggers 9
224         $("#firstp").trigger("click", [1, "2", "abc"], handler4);
225
226         // Simulate a "native" click
227         $("#firstp")[0].click = function(){
228                 ok( false, "Native call was triggered" );
229         };
230
231         // Triggers handlers, native, and extra fn
232         // Triggers 7
233         $("#firstp").trigger("click", [1, "2", "abc"], handler2);
234
235         // Trigger only the handlers (no native)
236         // Triggers 5
237         equals( $("#firstp").triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
238
239         // Trigger only the handlers (no native) and extra fn
240         // Triggers 8
241         equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler2), false, "Verify handler response" );
242
243         // Build fake click event to pass in
244         var eventObj = jQuery.event.fix({ type: "foo", target: document.body });
245
246         // Trigger only the handlers (no native), with external event obj
247         // Triggers 5
248         equals( $("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"]), "test", "Verify handler response" );
249
250         // Trigger only the handlers (no native) and extra fn, with external event obj
251         // Triggers 9
252         eventObj = jQuery.event.fix({ type: "foo", target: document.body });
253         equals( $("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"], handler), "test", "Verify handler response" );
254         
255         var pass = true;
256         try {
257                 $('input:first')
258                         .hide()
259                         .trigger('focus');
260         } catch(e) {
261                 pass = false;
262         }
263         ok( pass, "Trigger focus on hidden element" );
264
265         // have the extra handler override the return
266         // Triggers 9
267         equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler3), "newVal", "Verify triggerHandler return is overwritten by extra function" );
268
269         // have the extra handler leave the return value alone
270         // Triggers 9
271         equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler4), "test", "Verify triggerHandler return is not overwritten by extra function" );
272 });
273
274 test("toggle(Function, Function)", function() {
275         expect(5);
276         var count = 0,
277                 fn1 = function(e) { count++; },
278                 fn2 = function(e) { count--; },
279                 preventDefault = function(e) { e.preventDefault() },
280                 link = $('#mark');
281         link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
282         ok( count == 1, "Check for toggle(fn, fn)" );
283
284         $("#firstp").toggle(function () {
285                 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
286         }, function() {}).trigger("click", [ 1, 2, 3 ]);
287
288         var first = 0;
289         $("#simon1").one("click", function() {
290                 ok( true, "Execute event only once" );
291                 $(this).toggle(function() {
292                         ok( first++ == 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
293                 }, function() {
294                         ok( first == 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
295                 });
296                 return false;
297         }).click().click().click();
298 });
299
300 test("jQuery(function($) {})", function() {
301         stop();
302         jQuery(function($) {
303                 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");
304                 start();
305         });
306 });