Add .width() and .height() unit test for empty sets. Fix .height() test that was...
[jquery.git] / test / unit / dimensions.js
1 module("dimensions");
2
3 function pass( val ) {
4         return val;
5 }
6
7 function fn( val ) {
8         return function(){ return val; };
9 }
10
11 function testWidth( val ) {
12         expect(8);
13
14         var $div = jQuery("#nothiddendiv");
15         $div.width( val(30) );
16         equals($div.width(), 30, "Test set to 30 correctly");
17         $div.hide();
18         equals($div.width(), 30, "Test hidden div");
19         $div.show();
20         $div.width( val(-1) ); // handle negative numbers by ignoring #1599
21         equals($div.width(), 30, "Test negative width ignored");
22         $div.css("padding", "20px");
23         equals($div.width(), 30, "Test padding specified with pixels");
24         $div.css("border", "2px solid #fff");
25         equals($div.width(), 30, "Test border specified with pixels");
26
27         $div.css({ display: "", border: "", padding: "" });
28
29         jQuery("#nothiddendivchild").css({ width: 20, padding: "3px", border: "2px solid #fff" });
30         equals(jQuery("#nothiddendivchild").width(), 20, "Test child width with border and padding");
31         jQuery("#nothiddendiv, #nothiddendivchild").css({ border: "", padding: "", width: "" });
32
33         var blah = jQuery("blah");
34         equals( blah.width( val(10) ), blah, "Make sure that setting a width on an empty set returns the set." );
35         equals( blah.width(), null, "Make sure 'null' is returned on an empty set");
36 }
37
38 test("width()", function() {
39         testWidth( pass );
40 });
41
42 test("width() with function", function() {
43         testWidth( fn );
44 });
45
46 test("width() with function args", function() {
47         expect( 2 );
48         
49         var $div = jQuery("#nothiddendiv");
50         $div.width( 30 ).width(function(i, width) {
51                 equals( width, 30, "Make sure previous value is corrrect." );
52                 return width + 1;
53         });
54         
55         equals( $div.width(), 31, "Make sure value was modified correctly." );
56 });
57
58 function testHeight( val ) {
59         expect(8);
60
61         var $div = jQuery("#nothiddendiv");
62         $div.height( val(30) );
63         equals($div.height(), 30, "Test set to 30 correctly");
64         $div.hide();
65         equals($div.height(), 30, "Test hidden div");
66         $div.show();
67         $div.height( val(-1) ); // handle negative numbers by ignoring #1599
68         equals($div.height(), 30, "Test negative height ignored");
69         $div.css("padding", "20px");
70         equals($div.height(), 30, "Test padding specified with pixels");
71         $div.css("border", "2px solid #fff");
72         equals($div.height(), 30, "Test border specified with pixels");
73
74         $div.css({ display: "", border: "", padding: "", height: "1px" });
75
76         jQuery("#nothiddendivchild").css({ height: 20, padding: "3px", border: "2px solid #fff" });
77         equals(jQuery("#nothiddendivchild").height(), 20, "Test child height with border and padding");
78         jQuery("#nothiddendiv, #nothiddendivchild").css({ border: "", padding: "", height: "" });
79
80         var blah = jQuery("blah");
81         equals( blah.height( val(10) ), blah, "Make sure that setting a height on an empty set returns the set." );
82         equals( blah.height(), null, "Make sure 'null' is returned on an empty set");
83 }
84
85 test("height()", function() {
86         testHeight( pass );
87 });
88
89 test("height() with function", function() {
90         testHeight( fn );
91 });
92
93 test("height() with function args", function() {
94         expect( 2 );
95         
96         var $div = jQuery("#nothiddendiv");
97         $div.height( 30 ).height(function(i, height) {
98                 equals( height, 30, "Make sure previous value is corrrect." );
99                 return height + 1;
100         });
101         
102         equals( $div.height(), 31, "Make sure value was modified correctly." );
103 });
104
105 test("innerWidth()", function() {
106         expect(3);
107
108         var $div = jQuery("#nothiddendiv");
109         // set styles
110         $div.css({
111                 margin: 10,
112                 border: "2px solid #fff",
113                 width: 30
114         });
115         
116         equals($div.innerWidth(), 30, "Test with margin and border");
117         $div.css("padding", "20px");
118         equals($div.innerWidth(), 70, "Test with margin, border and padding");
119         $div.hide();
120         equals($div.innerWidth(), 70, "Test hidden div");
121         
122         // reset styles
123         $div.css({ display: "", border: "", padding: "", width: "", height: "" });
124 });
125
126 test("innerHeight()", function() {
127         expect(3);
128         
129         var $div = jQuery("#nothiddendiv");
130         // set styles
131         $div.css({
132                 margin: 10,
133                 border: "2px solid #fff",
134                 height: 30
135         });
136         
137         equals($div.innerHeight(), 30, "Test with margin and border");
138         $div.css("padding", "20px");
139         equals($div.innerHeight(), 70, "Test with margin, border and padding");
140         $div.hide();
141         equals($div.innerHeight(), 70, "Test hidden div");
142         
143         // reset styles
144         $div.css({ display: "", border: "", padding: "", width: "", height: "" });
145 });
146
147 test("outerWidth()", function() {
148         expect(6);
149         
150         var $div = jQuery("#nothiddendiv");
151         $div.css("width", 30);
152         
153         equals($div.outerWidth(), 30, "Test with only width set");
154         $div.css("padding", "20px");
155         equals($div.outerWidth(), 70, "Test with padding");
156         $div.css("border", "2px solid #fff");
157         equals($div.outerWidth(), 74, "Test with padding and border");
158         $div.css("margin", "10px");
159         equals($div.outerWidth(), 74, "Test with padding, border and margin without margin option");
160         $div.css("position", "absolute");
161         equals($div.outerWidth(true), 94, "Test with padding, border and margin with margin option");
162         $div.hide();
163         equals($div.outerWidth(true), 94, "Test hidden div with padding, border and margin with margin option");
164         
165         // reset styles
166         $div.css({ position: "", display: "", border: "", padding: "", width: "", height: "" });
167 });
168
169 test("outerHeight()", function() {
170         expect(6);
171         
172         var $div = jQuery("#nothiddendiv");
173         $div.css("height", 30);
174         
175         equals($div.outerHeight(), 30, "Test with only width set");
176         $div.css("padding", "20px");
177         equals($div.outerHeight(), 70, "Test with padding");
178         $div.css("border", "2px solid #fff");
179         equals($div.outerHeight(), 74, "Test with padding and border");
180         $div.css("margin", "10px");
181         equals($div.outerHeight(), 74, "Test with padding, border and margin without margin option");
182         equals($div.outerHeight(true), 94, "Test with padding, border and margin with margin option");
183         $div.hide();
184         equals($div.outerHeight(true), 94, "Test hidden div with padding, border and margin with margin option");
185         
186         // reset styles
187         $div.css({ display: "", border: "", padding: "", width: "", height: "" });
188 });