Fixed [1993] although it actually wasn't a bug in the core but rather a misunderstand...
[jquery.git] / test / unit / event.js
1 module("event");
2
3 test("bind()", function() {
4         expect(15);
5
6         var handler = function(event) {
7                 ok( event.data, "bind() with data, check passed data exists" );
8                 ok( event.data.foo == "bar", "bind() with data, Check value of passed data" );
9         };
10         $("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler);
11
12         ok( !jQuery.data($("#firstp")[0], "events"), "Event handler unbound when using data." );
13         
14         reset();
15         var handler = function(event, data) {
16                 ok( event.data, "check passed data exists" );
17                 ok( event.data.foo == "bar", "Check value of passed data" );
18                 ok( data, "Check trigger data" );
19                 ok( data.bar == "foo", "Check value of trigger data" );
20         };
21         $("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).unbind(handler);
22         
23         reset();
24         var handler = function(event) {
25                 ok ( !event.data, "Check that no data is added to the event object" );
26         };
27         $("#firstp").bind("click", handler).trigger("click");
28         
29         
30         // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
31         // var doc = document.getElementById("iframe").contentDocument;
32         // 
33         // doc.body.innerHTML = "<input type='text'/>";
34         //
35         // var input = doc.getElementsByTagName("input")[0];
36         //
37         // $(input).bind("click",function() {
38         //      ok( true, "Binding to element inside iframe" );
39         // }).click();
40         
41         var counter = 0;
42         function selectOnChange(event) {
43                 equals( event.data, counter++, "Event.data is not a global event object" );
44         };
45         $("#form select").each(function(i){
46                 $(this).bind('change', i, selectOnChange);
47         }).trigger('change');
48
49         reset();
50
51         $("#firstp").bind("click",function(e){
52                 ok(true, "Normal click triggered");
53         });
54
55         $("#firstp").bind("click.test",function(e){
56                 ok(true, "Namespaced click triggered");
57         });
58
59         // Trigger both bound fn (2)
60         $("#firstp").trigger("click");
61
62         // Trigger one bound fn (1)
63         $("#firstp").trigger("click.test");
64
65         // Remove only the one fn
66         $("#firstp").unbind("click.test");
67
68         // Trigger the remaining fn (1)
69         $("#firstp").trigger("click");
70 });
71
72 test("click()", function() {
73         expect(4);
74         $('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
75                 var close = $('spanx', this); // same with $(this).find('span');
76                 ok( close.length == 0, "Context element does not exist, length must be zero" );
77                 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
78                 return false;
79         }).click();
80         
81         $("#check1").click(function() {
82                 ok( true, "click event handler for checkbox gets fired twice, see #815" );
83         }).click();
84         
85         var counter = 0;
86         $('#firstp')[0].onclick = function(event) {
87                 counter++;
88         };
89         $('#firstp').click();
90         ok( counter == 1, "Check that click, triggers onclick event handler also" );
91 });
92
93 test("unbind(event)", function() {
94         expect(6);
95         var el = $("#firstp");
96         el.click(function() {
97                 ok( true, "Fake normal bind" );
98         });
99         el.click(function(event) {
100                 el.unbind(event);
101                 ok( true, "Fake onebind" );
102         });
103         el.click().click();
104         
105         el.click(function() { return; });
106         el.unbind('click');
107         ok( !el[0].onclick, "Handler is removed" ); // Bug #964
108
109         el.click(function() { return; });
110         el.unbind('change',function(){ return; });
111         for (var ret in jQuery.data(el[0], "events")['click']) break;
112         ok( ret, "Extra handlers weren't accidentally removed." );
113
114         el.unbind('click');
115         ok( !jQuery.data(el[0], "events"), "Removed the events expando after all handlers are unbound." );
116 });
117
118 test("trigger(event, [data], [fn])", function() {
119         expect(66);
120
121         var handler = function(event, a, b, c) {
122                 equals( event.type, "click", "check passed data" );
123                 equals( a, 1, "check passed data" );
124                 equals( b, "2", "check passed data" );
125                 equals( c, "abc", "check passed data" );
126                 return "test";
127         };
128
129         var handler2 = function(a, b, c) {
130                 equals( a, 1, "check passed data" );
131                 equals( b, "2", "check passed data" );
132                 equals( c, "abc", "check passed data" );
133                 return false;
134         };
135
136         var handler3 = function(a, b, c, v) {
137                 equals( a, 1, "check passed data" );
138                 equals( b, "2", "check passed data" );
139                 equals( c, "abc", "check passed data" );
140                 equals( v, "test", "check current value" );
141                 return "newVal";
142         };
143
144         var handler4 = function(a, b, c, v) {
145                 equals( a, 1, "check passed data" );
146                 equals( b, "2", "check passed data" );
147                 equals( c, "abc", "check passed data" );
148                 equals( v, "test", "check current value" );
149         };
150
151         // Simulate a "native" click
152         $("#firstp")[0].click = function(){
153                 ok( true, "Native call was triggered" );
154         };
155
156         // Triggers handlrs and native
157         // Trigger 5
158         $("#firstp").bind("click", handler).trigger("click", [1, "2", "abc"]);
159
160         // Triggers handlers, native, and extra fn
161         // Triggers 9
162         $("#firstp").trigger("click", [1, "2", "abc"], handler4);
163
164         // Simulate a "native" click
165         $("#firstp")[0].click = function(){
166                 ok( false, "Native call was triggered" );
167         };
168
169         // Triggers handlers, native, and extra fn
170         // Triggers 7
171         $("#firstp").trigger("click", [1, "2", "abc"], handler2);
172
173         // Trigger only the handlers (no native)
174         // Triggers 5
175         equals( $("#firstp").triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
176
177         // Trigger only the handlers (no native) and extra fn
178         // Triggers 8
179         equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler2), false, "Verify handler response" );
180
181         // Build fake click event to pass in
182         var eventObj = jQuery.event.fix({ type: "foo", target: document.body });
183
184         // Trigger only the handlers (no native), with external event obj
185         // Triggers 5
186         equals( $("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"]), "test", "Verify handler response" );
187
188         // Trigger only the handlers (no native) and extra fn, with external event obj
189         // Triggers 9
190         equals( $("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"], handler), "test", "Verify handler response" );
191
192         // have the extra handler override the return
193         // Triggers 9
194         equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler3), "newVal", "Verify triggerHandler return is overwritten by extra function" );
195
196         // have the extra handler leave the return value alone
197         // Triggers 9
198         equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler4), "test", "Verify triggerHandler return is not overwritten by extra function" );
199 });
200
201 test("toggle(Function, Function)", function() {
202         expect(5);
203         var count = 0,
204                 fn1 = function(e) { count++; },
205                 fn2 = function(e) { count--; },
206                 preventDefault = function(e) { e.preventDefault() },
207                 link = $('#mark');
208         link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
209         ok( count == 1, "Check for toggle(fn, fn)" );
210
211         $("#firstp").toggle(function () {
212                 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
213         }, function() {}).trigger("click", [ 1, 2, 3 ]);
214
215         var first = 0;
216         $("#simon1").one("click", function() {
217                 ok( true, "Execute event only once" );
218                 $(this).toggle(function() {
219                         ok( first++ == 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
220                 }, function() {
221                         ok( first == 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
222                 });
223                 return false;
224         }).click().click().click();
225 });