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