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