jquery data: Closes #3539. Exposed jQuery.queue. Moved all the data and queue functio...
[jquery.git] / test / unit / fx.js
1 module("fx");
2
3 test("animate(Hash, Object, Function)", function() {
4         expect(1);
5         stop();
6         var hash = {opacity: 'show'};
7         var hashCopy = jQuery.extend({}, hash);
8         jQuery('#foo').animate(hash, 0, function() {
9                 equals( hash.opacity, hashCopy.opacity, 'Check if animate changed the hash parameter' );
10                 start();
11         });
12 });
13
14 test("animate option (queue === false)", function () {
15         expect(1);
16         stop();
17
18         var order = [];
19
20         var $foo = jQuery("#foo");
21         $foo.animate({width:'100px'}, 200, function () {
22                 // should finish after unqueued animation so second
23                 order.push(2);
24         });
25         $foo.animate({fontSize:'2em'}, {queue:false, duration:10, complete:function () {
26                 // short duration and out of queue so should finish first
27                 order.push(1);
28         }});
29         $foo.animate({height:'100px'}, 10, function() {
30                 // queued behind the first animation so should finish third 
31                 order.push(3);
32                 isSet( order, [ 1, 2, 3], "Animations finished in the correct order" );
33                 start();
34         });
35 });
36
37 test("stop()", function() {
38         expect(3);
39         stop();
40
41         var $foo = jQuery("#nothiddendiv");
42         var w = 0;
43         $foo.hide().width(200).width();
44
45         $foo.animate({ width:'show' }, 1000);
46         setTimeout(function(){
47                 var nw = $foo.width();
48                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
49                 $foo.stop();
50
51                 nw = $foo.width();
52                 ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px");
53                 setTimeout(function(){
54                         equals( nw, $foo.width(), "The animation didn't continue" );
55                         start();
56                 }, 100);
57         }, 100);
58 });
59
60 test("stop() - several in queue", function() {
61         expect(4);
62         stop();
63
64         var $foo = jQuery("#nothiddendiv");
65         var w = 0;
66         $foo.hide().width(200).width();
67
68         $foo.animate({ width:'show' }, 1000);
69         $foo.animate({ width:'hide' }, 1000);
70         $foo.animate({ width:'show' }, 1000);
71         setTimeout(function(){
72                 equals( $foo.queue().length, 3, "All 3 still in the queue" );
73                 var nw = $foo.width();
74                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
75                 $foo.stop();
76
77                 nw = $foo.width();
78                 ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px");
79                 equals( $foo.queue().length, 2, "The next animation continued" );
80                 $foo.stop(true);
81                 start();
82         }, 100);
83 });
84
85 test("stop(clearQueue)", function() {
86         expect(4);
87         stop();
88
89         var $foo = jQuery("#nothiddendiv");
90         var w = 0;
91         $foo.hide().width(200).width();
92
93         $foo.animate({ width:'show' }, 1000);
94         $foo.animate({ width:'hide' }, 1000);
95         $foo.animate({ width:'show' }, 1000);
96         setTimeout(function(){
97                 var nw = $foo.width();
98                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
99                 $foo.stop(true);
100
101                 nw = $foo.width();
102                 ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px");
103
104                 equals( $foo.queue().length, 0, "The animation queue was cleared" );
105                 setTimeout(function(){
106                         equals( nw, $foo.width(), "The animation didn't continue" );
107                         start();
108                 }, 100);
109         }, 100);
110 });
111
112 test("stop(clearQueue, gotoEnd)", function() {
113         expect(3);
114         stop();
115
116         var $foo = jQuery("#nothiddendiv");
117         var w = 0;
118         $foo.hide().width(200).width();
119
120         $foo.animate({ width:'show' }, 1000);
121         $foo.animate({ width:'hide' }, 1000);
122         $foo.animate({ width:'show' }, 1000);
123         $foo.animate({ width:'hide' }, 1000);
124         setTimeout(function(){
125                 var nw = $foo.width();
126                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
127                 $foo.stop(false, true);
128
129                 nw = $foo.width();
130                 equals( nw, 200, "Stop() reset the animation" );
131
132                 setTimeout(function(){
133                         equals( $foo.queue().length, 3, "The next animation continued" );
134                         $foo.stop(true);
135                         start();
136                 }, 100);
137         }, 100);
138 });
139
140 test("toggle()", function() {
141         expect(6);
142         var x = jQuery("#foo");
143         ok( x.is(":visible"), "is visible" );
144         x.toggle();
145         ok( x.is(":hidden"), "is hidden" );
146         x.toggle();
147         ok( x.is(":visible"), "is visible again" );
148         
149         x.toggle(true);
150         ok( x.is(":visible"), "is visible" );
151         x.toggle(false);
152         ok( x.is(":hidden"), "is hidden" );
153         x.toggle(true);
154         ok( x.is(":visible"), "is visible again" );
155 });
156
157 var visible = {
158         Normal: function(elem){},
159         "CSS Hidden": function(elem){
160                 jQuery(this).addClass("hidden");
161         },
162         "JS Hidden": function(elem){
163                 jQuery(this).hide();
164         }
165 };
166
167 var from = {
168         "CSS Auto": function(elem,prop){
169                 jQuery(elem).addClass("auto" + prop)
170                         .text("This is a long string of text.");
171                 return "";
172         },
173         "JS Auto": function(elem,prop){
174                 jQuery(elem).css(prop,"auto")
175                         .text("This is a long string of text.");
176                 return "";
177         },
178         "CSS 100": function(elem,prop){
179                 jQuery(elem).addClass("large" + prop);
180                 return "";
181         },
182         "JS 100": function(elem,prop){
183                 jQuery(elem).css(prop,prop == "opacity" ? 1 : "100px");
184                 return prop == "opacity" ? 1 : 100;
185         },
186         "CSS 50": function(elem,prop){
187                 jQuery(elem).addClass("med" + prop);
188                 return "";
189         },
190         "JS 50": function(elem,prop){
191                 jQuery(elem).css(prop,prop == "opacity" ? 0.50 : "50px");
192                 return prop == "opacity" ? 0.5 : 50;
193         },
194         "CSS 0": function(elem,prop){
195                 jQuery(elem).addClass("no" + prop);
196                 return "";
197         },
198         "JS 0": function(elem,prop){
199                 jQuery(elem).css(prop,prop == "opacity" ? 0 : "0px");
200                 return 0;
201         }
202 };
203
204 var to = {
205         "show": function(elem,prop){
206                 jQuery(elem).hide().addClass("wide"+prop);
207                 return "show";
208         },
209         "hide": function(elem,prop){
210                 jQuery(elem).addClass("wide"+prop);
211                 return "hide";
212         },
213         "100": function(elem,prop){
214                 jQuery(elem).addClass("wide"+prop);
215                 return prop == "opacity" ? 1 : 100;
216         },
217         "50": function(elem,prop){
218                 return prop == "opacity" ? 0.50 : 50;
219         },
220         "0": function(elem,prop){
221                 jQuery(elem).addClass("noback");
222                 return 0;
223         }
224 };
225
226 function checkOverflowDisplay(){
227         var o = jQuery.css( this, "overflow" );
228
229         equals(o, "visible", "Overflow should be visible: " + o);
230         equals(jQuery.css( this, "display" ), "inline", "Display shouldn't be tampered with.");
231
232         start();
233 }
234
235 test("JS Overflow and Display", function() {
236         expect(2);
237         stop();
238         makeTest( "JS Overflow and Display" )
239                 .addClass("widewidth")
240                 .css({ overflow: "visible", display: "inline" })
241                 .addClass("widewidth")
242                 .text("Some sample text.")
243                 .before("text before")
244                 .after("text after")
245                 .animate({ opacity: 0.5 }, "slow", checkOverflowDisplay);
246 });
247                 
248 test("CSS Overflow and Display", function() {
249         expect(2);
250         stop();
251         makeTest( "CSS Overflow and Display" )
252                 .addClass("overflow inline")
253                 .addClass("widewidth")
254                 .text("Some sample text.")
255                 .before("text before")
256                 .after("text after")
257                 .animate({ opacity: 0.5 }, "slow", checkOverflowDisplay);
258 });
259
260 jQuery.each( from, function(fn, f){
261         jQuery.each( to, function(tn, t){
262                 test(fn + " to " + tn, function() {
263                         var elem = makeTest( fn + " to " + tn );
264         
265                         var t_w = t( elem, "width" );
266                         var f_w = f( elem, "width" );
267                         var t_h = t( elem, "height" );
268                         var f_h = f( elem, "height" );
269                         var t_o = t( elem, "opacity" );
270                         var f_o = f( elem, "opacity" );
271                         
272                         var num = 0;
273                         
274                         if ( t_h == "show" ) num++;
275                         if ( t_w == "show" ) num++;
276                         if ( t_w == "hide"||t_w == "show" ) num++;
277                         if ( t_h == "hide"||t_h == "show" ) num++;
278                         if ( t_o == "hide"||t_o == "show" ) num++;
279                         if ( t_w == "hide" ) num++;
280                         if ( t_o.constructor == Number ) num += 2;
281                         if ( t_w.constructor == Number ) num += 2;
282                         if ( t_h.constructor == Number ) num +=2;
283                         
284                         expect(num);
285                         stop();
286         
287                         var anim = { width: t_w, height: t_h, opacity: t_o };
288         
289                         elem.animate(anim, 50, function(){
290                                 if ( t_w == "show" )
291                                         equals( this.style.display, "block", "Showing, display should block: " + this.style.display);
292                                         
293                                 if ( t_w == "hide"||t_w == "show" )
294                                         equals(this.style.width.indexOf(f_w), 0, "Width must be reset to " + f_w + ": " + this.style.width);
295                                         
296                                 if ( t_h == "hide"||t_h == "show" )
297                                         equals(this.style.height.indexOf(f_h), 0, "Height must be reset to " + f_h + ": " + this.style.height);
298                                         
299                                 var cur_o = jQuery.attr(this.style, "opacity");
300                                 if ( cur_o !== "" ) cur_o = parseFloat( cur_o );
301         
302                                 if ( t_o == "hide"||t_o == "show" )
303                                         equals(cur_o, f_o, "Opacity must be reset to " + f_o + ": " + cur_o);
304                                         
305                                 if ( t_w == "hide" )
306                                         equals(this.style.display, "none", "Hiding, display should be none: " + this.style.display);
307                                         
308                                 if ( t_o.constructor == Number ) {
309                                         equals(cur_o, t_o, "Final opacity should be " + t_o + ": " + cur_o);
310                                         
311                                         ok(jQuery.curCSS(this, "opacity") != "" || cur_o == t_o, "Opacity should be explicitly set to " + t_o + ", is instead: " + cur_o);
312                                 }
313                                         
314                                 if ( t_w.constructor == Number ) {
315                                         equals(this.style.width, t_w + "px", "Final width should be " + t_w + ": " + this.style.width);
316                                         
317                                         var cur_w = jQuery.css(this,"width");
318
319                                         ok(this.style.width != "" || cur_w == t_w, "Width should be explicitly set to " + t_w + ", is instead: " + cur_w);
320                                 }
321                                         
322                                 if ( t_h.constructor == Number ) {
323                                         equals(this.style.height, t_h + "px", "Final height should be " + t_h + ": " + this.style.height);
324                                         
325                                         var cur_h = jQuery.css(this,"height");
326
327                                         ok(this.style.height != "" || cur_h == t_h, "Height should be explicitly set to " + t_h + ", is instead: " + cur_w);
328                                 }
329                                 
330                                 if ( t_h == "show" ) {
331                                         var old_h = jQuery.curCSS(this, "height");
332                                         jQuery(elem).append("<br/>Some more text<br/>and some more...");
333                                         ok(old_h != jQuery.css(this, "height" ), "Make sure height is auto.");
334                                 }
335         
336                                 start();
337                         });
338                 });
339         });
340 });
341
342 var check = ['opacity','height','width','display','overflow'];
343
344 jQuery.fn.saveState = function(){
345         expect(check.length);
346         stop();
347         return this.each(function(){
348                 var self = this;
349                 self.save = {};
350                 jQuery.each(check, function(i,c){
351                         self.save[c] = jQuery.css(self,c);
352                 });
353         });
354 };
355
356 function checkState(){
357         var self = this;
358         jQuery.each(this.save, function(c,v){
359                 var cur = jQuery.css(self,c);
360                 equals( v, cur, "Make sure that " + c + " is reset (Old: " + v + " Cur: " + cur + ")");
361         });
362         start();
363 }
364
365 // Chaining Tests
366 test("Chain fadeOut fadeIn", function() {
367         jQuery('#fadein div').saveState().fadeOut('fast').fadeIn('fast',checkState);
368 });
369 test("Chain fadeIn fadeOut", function() {
370         jQuery('#fadeout div').saveState().fadeIn('fast').fadeOut('fast',checkState);
371 });
372
373 test("Chain hide show", function() {
374         jQuery('#show div').saveState().hide('fast').show('fast',checkState);
375 });
376 test("Chain show hide", function() {
377         jQuery('#hide div').saveState().show('fast').hide('fast',checkState);
378 });
379
380 test("Chain toggle in", function() {
381         jQuery('#togglein div').saveState().toggle('fast').toggle('fast',checkState);
382 });
383 test("Chain toggle out", function() {
384         jQuery('#toggleout div').saveState().toggle('fast').toggle('fast',checkState);
385 });
386
387 test("Chain slideDown slideUp", function() {
388         jQuery('#slidedown div').saveState().slideDown('fast').slideUp('fast',checkState);
389 });
390 test("Chain slideUp slideDown", function() {
391         jQuery('#slideup div').saveState().slideUp('fast').slideDown('fast',checkState);
392 });
393
394 test("Chain slideToggle in", function() {
395         jQuery('#slidetogglein div').saveState().slideToggle('fast').slideToggle('fast',checkState);
396 });
397 test("Chain slideToggle out", function() {
398         jQuery('#slidetoggleout div').saveState().slideToggle('fast').slideToggle('fast',checkState);
399 });
400
401 function makeTest( text ){
402         var elem = jQuery("<div></div>")
403                 .attr("id", "test" + makeTest.id++)
404                 .addClass("box");
405
406         jQuery("<h4></h4>")
407                 .text( text )
408                 .appendTo("#fx-tests")
409                 .click(function(){
410                         jQuery(this).next().toggle();
411                 })
412                 .after( elem );
413
414         return elem;
415 }
416
417 makeTest.id = 1;