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