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