added tests for show/hide with falsy, non-zero values
[jquery.git] / test / unit / effects.js
1 module("effects");
2
3 test("show()", function() {
4         expect(23);
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         var speeds = {
12           "null speed": null,
13           "undefined speed": undefined,
14           "empty string speed": "",
15           "false speed": false
16         };
17
18         jQuery.each(speeds, function(name, speed) {
19     pass = true;
20         div.hide().show(speed).each(function() {
21                 if ( this.style.display == "none" ) pass = false;
22         });
23         ok( pass, "Show with " + name);
24   });
25
26
27         jQuery.each(speeds, function(name, speed) {
28     pass = true;
29         div.hide().show(speed, function() {
30                         pass = false;
31                 });
32                 ok( pass, "Show with " + name + " does not call animate callback");
33         });
34         
35
36         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>');
37
38         var old = jQuery("#show-tests table").show().css("display") !== "table";
39
40         var test = {
41                 "div"      : "block",
42                 "p"        : "block",
43                 "a"        : "inline",
44                 "code"     : "inline",
45                 "pre"      : "block",
46                 "span"     : "inline",
47                 "table"    : old ? "block" : "table",
48                 "thead"    : old ? "block" : "table-header-group",
49                 "tbody"    : old ? "block" : "table-row-group",
50                 "tr"       : old ? "block" : "table-row",
51                 "th"       : old ? "block" : "table-cell",
52                 "td"       : old ? "block" : "table-cell",
53                 "ul"       : "block",
54                 "li"       : old ? "block" : "list-item"
55         };
56
57         jQuery.each(test, function(selector, expected) {
58                 var elem = jQuery(selector, "#show-tests").show();
59                 equals( elem.css("display"), expected, "Show using correct display type for " + selector );
60         });
61 });
62
63 test("show(Number) - other displays", function() {
64         expect(15);
65         reset();
66         stop();
67
68         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>');
69
70         var old = jQuery("#show-tests table").show().css("display") !== "table",
71                 num = 0;
72
73         var test = {
74                 "div"      : "block",
75                 "p"        : "block",
76                 "a"        : "inline",
77                 "code"     : "inline",
78                 "pre"      : "block",
79                 "span"     : "inline",
80                 "table"    : old ? "block" : "table",
81                 "thead"    : old ? "block" : "table-header-group",
82                 "tbody"    : old ? "block" : "table-row-group",
83                 "tr"       : old ? "block" : "table-row",
84                 "th"       : old ? "block" : "table-cell",
85                 "td"       : old ? "block" : "table-cell",
86                 "ul"       : "block",
87                 "li"       : old ? "block" : "list-item"
88         };
89
90         jQuery.each(test, function(selector, expected) {
91                 var elem = jQuery(selector, "#show-tests").show(1, function() {
92                         equals( elem.css("display"), expected, "Show using correct display type for " + selector );
93                         if ( ++num === 15 ) {
94                                 start();
95                         }
96                 });
97         });
98 });
99
100 test("animate(Hash, Object, Function)", function() {
101         expect(1);
102         stop();
103         var hash = {opacity: 'show'};
104         var hashCopy = jQuery.extend({}, hash);
105         jQuery('#foo').animate(hash, 0, function() {
106                 equals( hash.opacity, hashCopy.opacity, 'Check if animate changed the hash parameter' );
107                 start();
108         });
109 });
110
111 test("animate negative height", function() {
112         expect(1);
113         stop();
114         jQuery("#foo").animate({ height: -100 }, 100, function() {
115                 equals( this.offsetHeight, 0, "Verify height." );
116                 start();
117         });
118 });
119
120 /* // This test ends up being flaky depending upon the CPU load
121 test("animate option (queue === false)", function () {
122         expect(1);
123         stop();
124
125         var order = [];
126
127         var $foo = jQuery("#foo");
128         $foo.animate({width:'100px'}, 3000, function () {
129                 // should finish after unqueued animation so second
130                 order.push(2);
131                 same( order, [ 1, 2 ], "Animations finished in the correct order" );
132                 start();
133         });
134         $foo.animate({fontSize:'2em'}, {queue:false, duration:10, complete:function () {
135                 // short duration and out of queue so should finish first
136                 order.push(1);
137         }});
138 });
139 */
140
141 test("animate with no properties", function() {
142         expect(2);
143         
144         var divs = jQuery("div"), count = 0;
145
146         divs.animate({}, function(){
147                 count++;
148         });
149
150         equals( divs.length, count, "Make sure that callback is called for each element in the set." );
151
152         stop();
153
154         var foo = jQuery("#foo");
155
156         foo.animate({});
157         foo.animate({top: 10}, 100, function(){
158                 ok( true, "Animation was properly dequeued." );
159                 start();
160         });
161 });
162
163 test("animate duration 0", function() {
164         expect(11);
165         
166         stop();
167         
168         var $elems = jQuery([{ a:0 },{ a:0 }]), counter = 0;
169         
170         equals( jQuery.timers.length, 0, "Make sure no animation was running from another test" );
171                 
172         $elems.eq(0).animate( {a:1}, 0, function(){
173                 ok( true, "Animate a simple property." );
174                 counter++;
175         });
176         
177         // Failed until [6115]
178         equals( jQuery.timers.length, 0, "Make sure synchronic animations are not left on jQuery.timers" );
179         
180         equals( counter, 1, "One synchronic animations" );
181         
182         $elems.animate( { a:2 }, 0, function(){
183                 ok( true, "Animate a second simple property." );
184                 counter++;
185         });
186         
187         equals( counter, 3, "Multiple synchronic animations" );
188         
189         $elems.eq(0).animate( {a:3}, 0, function(){
190                 ok( true, "Animate a third simple property." );
191                 counter++;
192         });
193         $elems.eq(1).animate( {a:3}, 200, function(){
194                 counter++;
195                 // Failed until [6115]
196                 equals( counter, 5, "One synchronic and one asynchronic" );
197                 start();
198         });
199         
200         var $elem = jQuery("<div />");
201         $elem.show(0, function(){ 
202                 ok(true, "Show callback with no duration");
203         });
204         $elem.hide(0, function(){ 
205                 ok(true, "Hide callback with no duration");
206         });
207 });
208
209 test("animate hyphenated properties", function(){
210         expect(1);
211         stop();
212
213         jQuery("#nothiddendiv")
214                 .css("font-size", 10)
215                 .animate({"font-size": 20}, 200, function(){
216                         equals( this.style.fontSize, "20px", "The font-size property was animated." );
217                         start();
218                 });
219 });
220
221 test("animate non-element", function(){
222         expect(1);
223         stop();
224
225         var obj = { test: 0 };
226
227         jQuery(obj).animate({test: 200}, 200, function(){
228                 equals( obj.test, 200, "The custom property should be modified." );
229                 start();
230         });
231 });
232
233 test("stop()", function() {
234         expect(3);
235         stop();
236
237         var $foo = jQuery("#nothiddendiv");
238         var w = 0;
239         $foo.hide().width(200).width();
240
241         $foo.animate({ width:'show' }, 1000);
242         setTimeout(function(){
243                 var nw = $foo.width();
244                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
245                 $foo.stop();
246
247                 nw = $foo.width();
248                 ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px");
249                 setTimeout(function(){
250                         equals( nw, $foo.width(), "The animation didn't continue" );
251                         start();
252                 }, 100);
253         }, 100);
254 });
255
256 test("stop() - several in queue", function() {
257         expect(3);
258         stop();
259
260         var $foo = jQuery("#nothiddendivchild");
261         var w = 0;
262         $foo.hide().width(200).width();
263
264         $foo.animate({ width:'show' }, 1000);
265         $foo.animate({ width:'hide' }, 1000);
266         $foo.animate({ width:'show' }, 1000);
267         setTimeout(function(){
268                 equals( $foo.queue().length, 3, "All 3 still in the queue" );
269                 var nw = $foo.width();
270                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
271                 $foo.stop();
272
273                 nw = $foo.width();
274                 ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px");
275                 // Disabled, being flaky
276                 //equals( $foo.queue().length, 1, "The next animation continued" );
277                 $foo.stop(true);
278                 start();
279         }, 100);
280 });
281
282 test("stop(clearQueue)", function() {
283         expect(4);
284         stop();
285
286         var $foo = jQuery("#nothiddendiv");
287         var w = 0;
288         $foo.hide().width(200).width();
289
290         $foo.animate({ width:'show' }, 1000);
291         $foo.animate({ width:'hide' }, 1000);
292         $foo.animate({ width:'show' }, 1000);
293         setTimeout(function(){
294                 var nw = $foo.width();
295                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
296                 $foo.stop(true);
297
298                 nw = $foo.width();
299                 ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px");
300
301                 equals( $foo.queue().length, 0, "The animation queue was cleared" );
302                 setTimeout(function(){
303                         equals( nw, $foo.width(), "The animation didn't continue" );
304                         start();
305                 }, 100);
306         }, 100);
307 });
308
309 test("stop(clearQueue, gotoEnd)", function() {
310         expect(1);
311         stop();
312
313         var $foo = jQuery("#nothiddendivchild");
314         var w = 0;
315         $foo.hide().width(200).width();
316
317         $foo.animate({ width:'show' }, 1000);
318         $foo.animate({ width:'hide' }, 1000);
319         $foo.animate({ width:'show' }, 1000);
320         $foo.animate({ width:'hide' }, 1000);
321         setTimeout(function(){
322                 var nw = $foo.width();
323                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
324                 $foo.stop(false, true);
325
326                 nw = $foo.width();
327                 // Disabled, being flaky
328                 //equals( nw, 1, "Stop() reset the animation" );
329
330                 setTimeout(function(){
331                         // Disabled, being flaky
332                         //equals( $foo.queue().length, 2, "The next animation continued" );
333                         $foo.stop(true);
334                         start();
335                 }, 100);
336         }, 100);
337 });
338
339 test("toggle()", function() {
340         expect(6);
341         var x = jQuery("#nothiddendiv");
342         ok( x.is(":visible"), "is visible" );
343         x.toggle();
344         ok( x.is(":hidden"), "is hidden" );
345         x.toggle();
346         ok( x.is(":visible"), "is visible again" );
347         
348         x.toggle(true);
349         ok( x.is(":visible"), "is visible" );
350         x.toggle(false);
351         ok( x.is(":hidden"), "is hidden" );
352         x.toggle(true);
353         ok( x.is(":visible"), "is visible again" );
354 });
355
356 jQuery.checkOverflowDisplay = function(){
357         var o = jQuery.css( this, "overflow" );
358
359         equals(o, "visible", "Overflow should be visible: " + o);
360         equals(jQuery.css( this, "display" ), "inline", "Display shouldn't be tampered with.");
361
362         start();
363 }
364
365 test("JS Overflow and Display", function() {
366         expect(2);
367         stop();
368         jQuery.makeTest( "JS Overflow and Display" )
369                 .addClass("widewidth")
370                 .css({ overflow: "visible", display: "inline" })
371                 .addClass("widewidth")
372                 .text("Some sample text.")
373                 .before("text before")
374                 .after("text after")
375                 .animate({ opacity: 0.5 }, "slow", jQuery.checkOverflowDisplay);
376 });
377                 
378 test("CSS Overflow and Display", function() {
379         expect(2);
380         stop();
381         jQuery.makeTest( "CSS Overflow and Display" )
382                 .addClass("overflow inline")
383                 .addClass("widewidth")
384                 .text("Some sample text.")
385                 .before("text before")
386                 .after("text after")
387                 .animate({ opacity: 0.5 }, "slow", jQuery.checkOverflowDisplay);
388 });
389
390 jQuery.each( {
391         "CSS Auto": function(elem,prop){
392                 jQuery(elem).addClass("auto" + prop)
393                         .text("This is a long string of text.");
394                 return "";
395         },
396         "JS Auto": function(elem,prop){
397                 jQuery(elem).css(prop,"auto")
398                         .text("This is a long string of text.");
399                 return "";
400         },
401         "CSS 100": function(elem,prop){
402                 jQuery(elem).addClass("large" + prop);
403                 return "";
404         },
405         "JS 100": function(elem,prop){
406                 jQuery(elem).css(prop,prop == "opacity" ? 1 : "100px");
407                 return prop == "opacity" ? 1 : 100;
408         },
409         "CSS 50": function(elem,prop){
410                 jQuery(elem).addClass("med" + prop);
411                 return "";
412         },
413         "JS 50": function(elem,prop){
414                 jQuery(elem).css(prop,prop == "opacity" ? 0.50 : "50px");
415                 return prop == "opacity" ? 0.5 : 50;
416         },
417         "CSS 0": function(elem,prop){
418                 jQuery(elem).addClass("no" + prop);
419                 return "";
420         },
421         "JS 0": function(elem,prop){
422                 jQuery(elem).css(prop,prop == "opacity" ? 0 : "0px");
423                 return 0;
424         }
425 }, function(fn, f){
426         jQuery.each( {
427                 "show": function(elem,prop){
428                         jQuery(elem).hide().addClass("wide"+prop);
429                         return "show";
430                 },
431                 "hide": function(elem,prop){
432                         jQuery(elem).addClass("wide"+prop);
433                         return "hide";
434                 },
435                 "100": function(elem,prop){
436                         jQuery(elem).addClass("wide"+prop);
437                         return prop == "opacity" ? 1 : 100;
438                 },
439                 "50": function(elem,prop){
440                         return prop == "opacity" ? 0.50 : 50;
441                 },
442                 "0": function(elem,prop){
443                         jQuery(elem).addClass("noback");
444                         return 0;
445                 }
446         }, function(tn, t){
447                 test(fn + " to " + tn, function() {
448                         var elem = jQuery.makeTest( fn + " to " + tn );
449         
450                         var t_w = t( elem, "width" );
451                         var f_w = f( elem, "width" );
452                         var t_h = t( elem, "height" );
453                         var f_h = f( elem, "height" );
454                         var t_o = t( elem, "opacity" );
455                         var f_o = f( elem, "opacity" );
456                         
457                         var num = 0;
458                         
459                         if ( t_h == "show" ) num++;
460                         if ( t_w == "show" ) num++;
461                         if ( t_w == "hide"||t_w == "show" ) num++;
462                         if ( t_h == "hide"||t_h == "show" ) num++;
463                         if ( t_o == "hide"||t_o == "show" ) num++;
464                         if ( t_w == "hide" ) num++;
465                         if ( t_o.constructor == Number ) num += 2;
466                         if ( t_w.constructor == Number ) num += 2;
467                         if ( t_h.constructor == Number ) num +=2;
468                         
469                         expect(num);
470                         stop();
471         
472                         var anim = { width: t_w, height: t_h, opacity: t_o };
473         
474                         elem.animate(anim, 50, function(){
475                                 if ( t_w == "show" )
476                                         equals( this.style.display, "block", "Showing, display should block: " + this.style.display);
477                                         
478                                 if ( t_w == "hide"||t_w == "show" )
479                                         equals(this.style.width.indexOf(f_w), 0, "Width must be reset to " + f_w + ": " + this.style.width);
480                                         
481                                 if ( t_h == "hide"||t_h == "show" )
482                                         equals(this.style.height.indexOf(f_h), 0, "Height must be reset to " + f_h + ": " + this.style.height);
483                                         
484                                 var cur_o = jQuery.style(this, "opacity");
485                                 if ( cur_o !== "" ) cur_o = parseFloat( cur_o );
486         
487                                 if ( t_o == "hide"||t_o == "show" )
488                                         equals(cur_o, f_o, "Opacity must be reset to " + f_o + ": " + cur_o);
489                                         
490                                 if ( t_w == "hide" )
491                                         equals(this.style.display, "none", "Hiding, display should be none: " + this.style.display);
492                                         
493                                 if ( t_o.constructor == Number ) {
494                                         equals(cur_o, t_o, "Final opacity should be " + t_o + ": " + cur_o);
495                                         
496                                         ok(jQuery.curCSS(this, "opacity") != "" || cur_o == t_o, "Opacity should be explicitly set to " + t_o + ", is instead: " + cur_o);
497                                 }
498                                         
499                                 if ( t_w.constructor == Number ) {
500                                         equals(this.style.width, t_w + "px", "Final width should be " + t_w + ": " + this.style.width);
501                                         
502                                         var cur_w = jQuery.css(this,"width");
503
504                                         ok(this.style.width != "" || cur_w == t_w, "Width should be explicitly set to " + t_w + ", is instead: " + cur_w);
505                                 }
506                                         
507                                 if ( t_h.constructor == Number ) {
508                                         equals(this.style.height, t_h + "px", "Final height should be " + t_h + ": " + this.style.height);
509                                         
510                                         var cur_h = jQuery.css(this,"height");
511
512                                         ok(this.style.height != "" || cur_h == t_h, "Height should be explicitly set to " + t_h + ", is instead: " + cur_w);
513                                 }
514                                 
515                                 if ( t_h == "show" ) {
516                                         var old_h = jQuery.curCSS(this, "height");
517                                         jQuery(elem).append("<br/>Some more text<br/>and some more...");
518                                         ok(old_h != jQuery.css(this, "height" ), "Make sure height is auto.");
519                                 }
520         
521                                 start();
522                         });
523                 });
524         });
525 });
526
527 jQuery.fn.saveState = function(){
528         var check = ['opacity','height','width','display','overflow'];  
529         expect(check.length);
530         
531         stop();
532         return this.each(function(){
533                 var self = this;
534                 self.save = {};
535                 jQuery.each(check, function(i,c){
536                         self.save[c] = jQuery.css(self,c);
537                 });
538         });
539 };
540
541 jQuery.checkState = function(){
542         var self = this;
543         jQuery.each(this.save, function(c,v){
544                 var cur = jQuery.css(self,c);
545                 equals( v, cur, "Make sure that " + c + " is reset (Old: " + v + " Cur: " + cur + ")");
546         });
547         start();
548 }
549
550 // Chaining Tests
551 test("Chain fadeOut fadeIn", function() {
552         jQuery('#fadein div').saveState().fadeOut('fast').fadeIn('fast',jQuery.checkState);
553 });
554 test("Chain fadeIn fadeOut", function() {
555         jQuery('#fadeout div').saveState().fadeIn('fast').fadeOut('fast',jQuery.checkState);
556 });
557
558 test("Chain hide show", function() {
559         jQuery('#show div').saveState().hide('fast').show('fast',jQuery.checkState);
560 });
561 test("Chain show hide", function() {
562         jQuery('#hide div').saveState().show('fast').hide('fast',jQuery.checkState);
563 });
564
565 test("Chain toggle in", function() {
566         jQuery('#togglein div').saveState().toggle('fast').toggle('fast',jQuery.checkState);
567 });
568 test("Chain toggle out", function() {
569         jQuery('#toggleout div').saveState().toggle('fast').toggle('fast',jQuery.checkState);
570 });
571
572 test("Chain slideDown slideUp", function() {
573         jQuery('#slidedown div').saveState().slideDown('fast').slideUp('fast',jQuery.checkState);
574 });
575 test("Chain slideUp slideDown", function() {
576         jQuery('#slideup div').saveState().slideUp('fast').slideDown('fast',jQuery.checkState);
577 });
578
579 test("Chain slideToggle in", function() {
580         jQuery('#slidetogglein div').saveState().slideToggle('fast').slideToggle('fast',jQuery.checkState);
581 });
582 test("Chain slideToggle out", function() {
583         jQuery('#slidetoggleout div').saveState().slideToggle('fast').slideToggle('fast',jQuery.checkState);
584 });
585
586 jQuery.makeTest = function( text ){
587         var elem = jQuery("<div></div>")
588                 .attr("id", "test" + jQuery.makeTest.id++)
589                 .addClass("box");
590
591         jQuery("<h4></h4>")
592                 .text( text )
593                 .appendTo("#fx-tests")
594                 .click(function(){
595                         jQuery(this).next().toggle();
596                 })
597                 .after( elem );
598
599         return elem;
600 }
601
602 jQuery.makeTest.id = 1;
603
604 test("jQuery.show('fast') doesn't clear radio buttons (bug #1095)", function () {
605         expect(4);
606   stop();
607
608         var $checkedtest = jQuery("#checkedtest");
609         // IE6 was clearing "checked" in jQuery(elem).show("fast");
610         $checkedtest.hide().show("fast", function() {
611         ok( !! jQuery(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." );
612         ok( ! jQuery(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." );
613         ok( !! jQuery(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." );
614         ok( ! jQuery(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." );
615         start();
616         });
617 });
618
619 test("animate with per-property easing", function(){
620         
621         expect(3);
622         stop();
623         
624         var _test1_called = false;
625         var _test2_called = false;
626         var _default_test_called = false;
627         
628         jQuery.easing['_test1'] = function() {
629                 _test1_called = true;
630         };
631         
632         jQuery.easing['_test2'] = function() {
633                 _test2_called = true;
634         };
635         
636         jQuery.easing['_default_test'] = function() {
637                 _default_test_called = true;
638         };
639         
640         jQuery({a:0,b:0,c:0}).animate({
641                 a: [100, '_test1'],
642                 b: [100, '_test2'],
643                 c: 100
644         }, 400, '_default_test', function(){
645                 start();
646                 ok(_test1_called, "Easing function (1) called");
647                 ok(_test2_called, "Easing function (2) called");
648                 ok(_default_test_called, "Easing function (_default) called");
649         });
650         
651 });