Build: replace CRLF with LF during minify
[jquery.git] / test / unit / deprecated.js
blobbddd40105f833e8a61618439ef94d50546077bf1
1 QUnit.module( "deprecated", { afterEach: moduleTeardown } );
3 if ( includesModule( "deprecated" ) ) {
5 QUnit.test( "bind/unbind", function( assert ) {
6 assert.expect( 4 );
8 var markup = jQuery(
9 "<div><p><span><b>b</b></span></p></div>"
12 markup
13 .find( "b" )
14 .bind( "click", { bindData: 19 }, function( e, trig ) {
15 assert.equal( e.type, "click", "correct event type" );
16 assert.equal( e.data.bindData, 19, "correct trigger data" );
17 assert.equal( trig, 42, "correct bind data" );
18 assert.equal( e.target.nodeName.toLowerCase(), "b", "correct element" );
19 } )
20 .trigger( "click", [ 42 ] )
21 .unbind( "click" )
22 .trigger( "click" )
23 .remove();
24 } );
26 QUnit.test( "delegate/undelegate", function( assert ) {
27 assert.expect( 2 );
29 var markup = jQuery(
30 "<div><p><span><b>b</b></span></p></div>"
33 markup
34 .delegate( "b", "click", function( e ) {
35 assert.equal( e.type, "click", "correct event type" );
36 assert.equal( e.target.nodeName.toLowerCase(), "b", "correct element" );
37 } )
38 .find( "b" )
39 .trigger( "click" )
40 .end()
41 .undelegate( "b", "click" )
42 .remove();
43 } );
45 QUnit.test( "hover() mouseenter mouseleave", function( assert ) {
46 assert.expect( 1 );
48 var times = 0,
49 handler1 = function() { ++times; },
50 handler2 = function() { ++times; };
52 jQuery( "#firstp" )
53 .hover( handler1, handler2 )
54 .mouseenter().mouseleave()
55 .off( "mouseenter", handler1 )
56 .off( "mouseleave", handler2 )
57 .hover( handler1 )
58 .mouseenter().mouseleave()
59 .off( "mouseenter mouseleave", handler1 )
60 .mouseenter().mouseleave();
62 assert.equal( times, 4, "hover handlers fired" );
63 } );
65 QUnit.test( "trigger() shortcuts", function( assert ) {
66 assert.expect( 5 );
68 var counter, clickCounter,
69 elem = jQuery( "<li><a href='#'>Change location</a></li>" ).prependTo( "#firstUL" );
70 elem.find( "a" ).on( "click", function() {
71 var close = jQuery( "spanx", this ); // same with jQuery(this).find("span");
72 assert.equal( close.length, 0, "Context element does not exist, length must be zero" );
73 assert.ok( !close[ 0 ], "Context element does not exist, direct access to element must return undefined" );
74 return false;
75 } ).click();
77 // manually clean up detached elements
78 elem.remove();
80 jQuery( "#check1" ).click( function() {
81 assert.ok( true, "click event handler for checkbox gets fired twice, see trac-815" );
82 } ).click();
84 counter = 0;
85 jQuery( "#firstp" )[ 0 ].onclick = function() {
86 counter++;
88 jQuery( "#firstp" ).click();
89 assert.equal( counter, 1, "Check that click, triggers onclick event handler also" );
91 clickCounter = 0;
92 jQuery( "#simon1" )[ 0 ].onclick = function() {
93 clickCounter++;
95 jQuery( "#simon1" ).click();
96 assert.equal( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
97 } );
99 if ( includesModule( "ajax" ) ) {
100 ajaxTest( "Ajax events aliases (with context)", 12, function( assert ) {
101 var context = document.createElement( "div" );
103 function event( e ) {
104 assert.equal( this, context, e.type );
107 function callback( msg ) {
108 return function() {
109 assert.equal( this, context, "context is preserved on callback " + msg );
113 return {
114 setup: function() {
115 jQuery( context ).appendTo( "#foo" )
116 .ajaxSend( event )
117 .ajaxComplete( event )
118 .ajaxError( event )
119 .ajaxSuccess( event );
121 requests: [ {
122 url: url( "name.html" ),
123 context: context,
124 beforeSend: callback( "beforeSend" ),
125 success: callback( "success" ),
126 complete: callback( "complete" )
127 }, {
128 url: url( "404.txt" ),
129 context: context,
130 beforeSend: callback( "beforeSend" ),
131 error: callback( "error" ),
132 complete: callback( "complete" )
135 } );
138 QUnit.test( "Event aliases", function( assert ) {
140 // Explicitly skipping focus/blur events due to their flakiness
141 var $elem = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" ),
142 aliases = ( "resize scroll click dblclick mousedown mouseup " +
143 "mousemove mouseover mouseout mouseenter mouseleave change " +
144 "select submit keydown keypress keyup contextmenu" ).split( " " );
145 assert.expect( aliases.length );
147 jQuery.each( aliases, function( i, name ) {
149 // e.g. $(elem).click(...).click();
150 $elem[ name ]( function( event ) {
151 assert.equal( event.type, name, "triggered " + name );
152 } )[ name ]().off( name );
153 } );
154 } );
156 QUnit.test( "jQuery.proxy", function( assert ) {
157 assert.expect( 9 );
159 var test2, test3, test4, fn, cb,
160 test = function() {
161 assert.equal( this, thisObject, "Make sure that scope is set properly." );
163 thisObject = { foo: "bar", method: test };
165 // Make sure normal works
166 test.call( thisObject );
168 // Basic scoping
169 jQuery.proxy( test, thisObject )();
171 // Another take on it
172 jQuery.proxy( thisObject, "method" )();
174 // Make sure it doesn't freak out
175 assert.equal( jQuery.proxy( null, thisObject ), undefined, "Make sure no function was returned." );
177 // Partial application
178 test2 = function( a ) {
179 assert.equal( a, "pre-applied", "Ensure arguments can be pre-applied." );
181 jQuery.proxy( test2, null, "pre-applied" )();
183 // Partial application w/ normal arguments
184 test3 = function( a, b ) {
185 assert.equal( b, "normal", "Ensure arguments can be pre-applied and passed as usual." );
187 jQuery.proxy( test3, null, "pre-applied" )( "normal" );
189 // Test old syntax
190 test4 = { "meth": function( a ) {
191 assert.equal( a, "boom", "Ensure old syntax works." );
192 } };
193 jQuery.proxy( test4, "meth" )( "boom" );
195 // jQuery 1.9 improved currying with `this` object
196 fn = function() {
197 assert.equal( Array.prototype.join.call( arguments, "," ), "arg1,arg2,arg3", "args passed" );
198 assert.equal( this.foo, "bar", "this-object passed" );
200 cb = jQuery.proxy( fn, null, "arg1", "arg2" );
201 cb.call( thisObject, "arg3" );
202 } );