Cleaned up a lot of the test suite - reorganized and renamed tests. Added a new trigg...
[jquery.git] / src / event / eventTest.js
1 module("event");
2
3 test("bind()", function() {
4         expect(11);
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();
11         
12         reset();
13         var handler = function(event, data) {
14                 ok( event.data, "check passed data exists" );
15                 ok( event.data.foo == "bar", "Check value of passed data" );
16                 ok( data, "Check trigger data" );
17                 ok( data.bar == "foo", "Check value of trigger data" );
18         }
19         $("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]);
20         
21         // events don't work with iframes, see #939
22     var tmp = document.createElement('iframe');
23     document.body.appendChild( tmp );
24     var doc = tmp.contentDocument;
25     doc.open();
26     doc.write("<html><body><input type='text'/></body></html>");
27     doc.close();
28     
29     var input = doc.getElementsByTagName("input")[0];
30     
31     input.addEventListener('click', function() {
32         ok( true, "Event handling via DOM 2 methods" );
33     }, false);
34     
35     $(input).bind("click",function() {
36         ok( true, "Event handling via jQuery's handler" );
37     });
38     
39     triggerEvent( input, "click" );
40     
41     document.body.removeChild( tmp );
42
43         var counter = 0;
44         function selectOnChange(event) {
45                 equals( event.data, counter++, "Event.data is a global event object" );
46         }
47         $("select").each(function(i){
48                 $(this).bind('change', i, selectOnChange);
49         }).trigger('change');
50 });
51
52 test("click()", function() {
53         expect(3);\r
54         $('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {\r
55             var close = $('spanx', this); // same with $(this).find('span');\r
56             ok( close.length == 0, "Context element does not exist, length must be zero" );\r
57             ok( !close[0], "Context element does not exist, direct access to element must return undefined" );\r
58             return false;\r
59         }).click();
60         
61         $("#check1").click(function() {
62                 ok( true, "click event handler for checkbox gets fired twice, see #815" );
63         }).click();\r
64 });
65
66 test("unbind(event)", function() {
67         expect(6);
68         var el = $("#firstp");
69         el.click(function() {
70                 ok( true, "Fake normal bind" );
71         });
72         el.click(function(event) {
73                 el.unbind(event);
74                 ok( true, "Fake onebind" );
75         });
76         el.click().click();
77         
78         el.click(function() { return; });
79         el.unbind('click');
80         ok( !el[0].onclick, "Handler is removed" ); // Bug #964
81
82         el.click(function() { return; });
83         el.unbind('change',function(){ return; });
84         ok( el[0].onclick, "Extra handlers weren't accidentally removed." );
85
86         el.unbind('click');
87         ok( !el[0].$events, "Removed the events expando after all handlers are unbound." );
88 });
89
90 test("trigger(event, [data]", function() {
91         expect(3);
92         var handler = function(event, a, b, c) {
93                 ok( a == 1, "check passed data" );
94                 ok( b == "2", "check passed data" );
95                 ok( c == "abc", "check passed data" );
96         }
97         $("#firstp").bind("click", handler).trigger("click", [1, "2", "abc"]);
98 });
99
100 test("toggle(Function, Function)", function() {
101         expect(4);
102         var count = 0,
103                 fn1 = function(e) { count++; },
104                 fn2 = function(e) { count--; },
105                 preventDefault = function(e) { e.preventDefault() },
106                 link = $('#mark');
107         if ( $.browser.msie || $.browser.opera || /konquerer/i.test(navigator.userAgent) )
108                 ok( false, "click() on link gets executed in IE/Opera/Konquerer, not intended behaviour!" );
109         else
110                 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
111         ok( count == 1, "Check for toggle(fn, fn)" );
112         
113         var first = 0;
114         $("#simon1").one("click", function() {
115                 ok( true, "Execute event only once" );
116                 $(this).toggle(function() {
117                         ok( first++ == 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
118                 }, function() {
119                         ok( first == 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
120                 });
121                 return false;
122         }).click().click().click();
123 });