Fix for #1823 bug in animate {queue:false} plus a unit test.
[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 = $.extend({}, hash);
8         $('#foo').animate(hash, 0, function() {
9                 ok( 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 = $("#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] );
33     start();
34   });
35 });
36
37 test("stop()", function() {
38         expect(3);
39         stop();
40         reset();
41
42         var foo = $("#foo")[0];
43         var h = foo.style.height;
44
45         $("#foo").slideUp(1000);
46         setTimeout(function(){
47                 var nh = foo.style.height;
48                 ok( nh != h, "An animation occurred " + nh + " " + h );
49                 $("#foo").stop();
50
51                 nh = foo.style.height;
52                 ok( nh != h, "Stop didn't reset the animation " + nh + " " + h );
53                 setTimeout(function(){
54                         equals( nh, foo.style.height, "The animation didn't continue" );
55                         start();
56                 }, 100);
57         }, 100);
58 });
59
60 test("toggle()", function() {
61         expect(3);
62         var x = $("#foo");
63         ok( x.is(":visible") );
64         x.toggle();
65         ok( x.is(":hidden") );
66         x.toggle();
67         ok( x.is(":visible") );
68 });
69
70 var visible = {
71         Normal: function(elem){},
72         "CSS Hidden": function(elem){
73                 $(this).addClass("hidden");
74         },
75         "JS Hidden": function(elem){
76                 $(this).hide();
77         }
78 };
79
80 var from = {
81         "CSS Auto": function(elem,prop){
82                 $(elem).addClass("auto" + prop)
83                         .text("This is a long string of text.");
84                 return "";
85         },
86         "JS Auto": function(elem,prop){
87                 $(elem).css(prop,"auto")
88                         .text("This is a long string of text.");
89                 return "";
90         },
91         "CSS 100": function(elem,prop){
92                 $(elem).addClass("large" + prop);
93                 return "";
94         },
95         "JS 100": function(elem,prop){
96                 $(elem).css(prop,prop == "opacity" ? 1 : "100px");
97                 return prop == "opacity" ? 1 : 100;
98         },
99         "CSS 50": function(elem,prop){
100                 $(elem).addClass("med" + prop);
101                 return "";
102         },
103         "JS 50": function(elem,prop){
104                 $(elem).css(prop,prop == "opacity" ? 0.50 : "50px");
105                 return prop == "opacity" ? 0.5 : 50;
106         },
107         "CSS 0": function(elem,prop){
108                 $(elem).addClass("no" + prop);
109                 return "";
110         },
111         "JS 0": function(elem,prop){
112                 $(elem).css(prop,prop == "opacity" ? 0 : "0px");
113                 return 0;
114         }
115 };
116
117 var to = {
118         "show": function(elem,prop){
119                 $(elem).hide().addClass("wide"+prop);
120                 return "show";
121         },
122         "hide": function(elem,prop){
123                 $(elem).addClass("wide"+prop);
124                 return "hide";
125         },
126         "100": function(elem,prop){
127                 $(elem).addClass("wide"+prop);
128                 return prop == "opacity" ? 1 : 100;
129         },
130         "50": function(elem,prop){
131                 return prop == "opacity" ? 0.50 : 50;
132         },
133         "0": function(elem,prop){
134                 $(elem).addClass("noback");
135                 return 0;
136         }
137 };
138
139 function checkOverflowDisplay(){
140         var o = jQuery.css( this, "overflow" );
141
142         ok(o == "visible", "Overflow should be visible: " + o);
143         ok(jQuery.css( this, "display" ) == "inline", "Display shouldn't be tampered with.");
144
145         start();
146 }
147
148 test("JS Overflow and Display", function() {
149         expect(2);
150         stop();
151         makeTest( "JS Overflow and Display" )
152                 .addClass("widewidth")
153                 .css({ overflow: "visible", display: "inline" })
154                 .addClass("widewidth")
155                 .text("Some sample text.")
156                 .before("text before")
157                 .after("text after")
158                 .animate({ opacity: 0.5 }, "slow", checkOverflowDisplay);
159 });
160                 
161 test("CSS Overflow and Display", function() {
162         expect(2);
163         stop();
164         makeTest( "CSS Overflow and Display" )
165                 .addClass("overflow inline")
166                 .addClass("widewidth")
167                 .text("Some sample text.")
168                 .before("text before")
169                 .after("text after")
170                 .animate({ opacity: 0.5 }, "slow", checkOverflowDisplay);
171 });
172
173 jQuery.each( from, function(fn, f){
174         jQuery.each( to, function(tn, t){
175                 test(fn + " to " + tn, function() {
176                         var elem = makeTest( fn + " to " + tn );
177         
178                         var t_w = t( elem, "width" );
179                         var f_w = f( elem, "width" );
180                         var t_h = t( elem, "height" );
181                         var f_h = f( elem, "height" );
182                         var t_o = t( elem, "opacity" );
183                         var f_o = f( elem, "opacity" );
184                         
185                         var num = 0;
186                         
187                         if ( t_h == "show" ) num++;
188                         if ( t_w == "show" ) num++;
189                         if ( t_w == "hide"||t_w == "show" ) num++;
190                         if ( t_h == "hide"||t_h == "show" ) num++;
191                         if ( t_o == "hide"||t_o == "show" ) num++;
192                         if ( t_w == "hide" ) num++;
193                         if ( t_o.constructor == Number ) num += 2;
194                         if ( t_w.constructor == Number ) num += 2;
195                         if ( t_h.constructor == Number ) num +=2;
196                         
197                         expect(num);
198                         stop();
199         
200                         var anim = { width: t_w, height: t_h, opacity: t_o };
201         
202                         elem.animate(anim, 50, function(){
203                                 if ( t_w == "show" )
204                                         ok( this.style.display == "block", "Showing, display should block: " + this.style.display);
205                                         
206                                 if ( t_w == "hide"||t_w == "show" )
207                                         ok(this.style.width.indexOf(f_w) == 0, "Width must be reset to " + f_w + ": " + this.style.width);
208                                         
209                                 if ( t_h == "hide"||t_h == "show" )
210                                         ok(this.style.height.indexOf(f_h) == 0, "Height must be reset to " + f_h + ": " + this.style.height);
211                                         
212                                 var cur_o = jQuery.attr(this.style, "opacity");
213                                 if ( cur_o !== "" ) cur_o = parseFloat( cur_o );
214         
215                                 if ( t_o == "hide"||t_o == "show" )
216                                         ok(cur_o == f_o, "Opacity must be reset to " + f_o + ": " + cur_o);
217                                         
218                                 if ( t_w == "hide" )
219                                         ok(this.style.display == "none", "Hiding, display should be none: " + this.style.display);
220                                         
221                                 if ( t_o.constructor == Number ) {
222                                         ok(cur_o == t_o, "Final opacity should be " + t_o + ": " + cur_o);
223                                         
224                                         ok(jQuery.curCSS(this, "opacity") != "" || cur_o == t_o, "Opacity should be explicitly set to " + t_o + ", is instead: " + cur_o);
225                                 }
226                                         
227                                 if ( t_w.constructor == Number ) {
228                                         ok(this.style.width == t_w + "px", "Final width should be " + t_w + ": " + this.style.width);
229                                         
230                                         var cur_w = jQuery.css(this,"width");
231
232                                         ok(this.style.width != "" || cur_w == t_w, "Width should be explicitly set to " + t_w + ", is instead: " + cur_w);
233                                 }
234                                         
235                                 if ( t_h.constructor == Number ) {
236                                         ok(this.style.height == t_h + "px", "Final height should be " + t_h + ": " + this.style.height);
237                                         
238                                         var cur_h = jQuery.css(this,"height");
239
240                                         ok(this.style.height != "" || cur_h == t_h, "Height should be explicitly set to " + t_h + ", is instead: " + cur_w);
241                                 }
242                                 
243                                 if ( t_h == "show" ) {
244                                         var old_h = jQuery.curCSS(this, "height");
245                                         $(elem).append("<br/>Some more text<br/>and some more...");
246                                         ok(old_h != jQuery.css(this, "height" ), "Make sure height is auto.");
247                                 }
248         
249                                 start();
250                         });
251                 });
252         });
253 });
254
255 var check = ['opacity','height','width','display','overflow'];
256
257 jQuery.fn.saveState = function(){
258         expect(check.length);
259         stop();
260         return this.each(function(){
261                 var self = this;
262                 self.save = {};
263                 jQuery.each(check, function(i,c){
264                         self.save[c] = jQuery.css(self,c);
265                 });
266         });
267 };
268
269 function checkState(){
270         var self = this;
271         jQuery.each(this.save, function(c,v){
272                 var cur = jQuery.css(self,c);
273                 ok( v == cur, "Make sure that " + c + " is reset (Old: " + v + " Cur: " + cur + ")");
274         });
275         start();
276 }
277
278 // Chaining Tests
279 test("Chain fadeOut fadeIn", function() {
280         $('#fadein div').saveState().fadeOut('fast').fadeIn('fast',checkState);
281 });
282 test("Chain fadeIn fadeOut", function() {
283         $('#fadeout div').saveState().fadeIn('fast').fadeOut('fast',checkState);
284 });
285
286 test("Chain hide show", function() {
287         $('#show div').saveState().hide('fast').show('fast',checkState);
288 });
289 test("Chain show hide", function() {
290         $('#hide div').saveState().show('fast').hide('fast',checkState);
291 });
292
293 test("Chain toggle in", function() {
294         $('#togglein div').saveState().toggle('fast').toggle('fast',checkState);
295 });
296 test("Chain toggle out", function() {
297         $('#toggleout div').saveState().toggle('fast').toggle('fast',checkState);
298 });
299
300 test("Chain slideDown slideUp", function() {
301         $('#slidedown div').saveState().slideDown('fast').slideUp('fast',checkState);
302 });
303 test("Chain slideUp slideDown", function() {
304         $('#slideup div').saveState().slideUp('fast').slideDown('fast',checkState);
305 });
306
307 test("Chain slideToggle in", function() {
308         $('#slidetogglein div').saveState().slideToggle('fast').slideToggle('fast',checkState);
309 });
310 test("Chain slideToggle out", function() {
311         $('#slidetoggleout div').saveState().slideToggle('fast').slideToggle('fast',checkState);
312 });
313
314 function makeTest( text ){
315         var elem = $("<div></div>")
316                 .attr("id", "test" + makeTest.id++)
317                 .addClass("box");
318
319         $("<h4></h4>")
320                 .text( text )
321                 .appendTo("#fx-tests")
322                 .click(function(){
323                         $(this).next().toggle();
324                 })
325                 .after( elem );
326
327         return elem;
328 }
329
330 makeTest.id = 1;