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