1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
(function( $ ) {
module( "spinner: options" );
// culture is tested after numberFormat, since it depends on numberFormat
test( "incremental, false", function() {
expect( 100 );
var i, diff,
prev = 0,
element = $( "#spin" ).val( prev ).spinner({
incremental: false,
spin: function( event, ui ) {
equal( ui.value - prev, 1 );
prev = ui.value;
}
});
for ( i = 0; i < 100; i++ ) {
element.simulate( "keydown", { keyCode: $.ui.keyCode.UP } );
}
element.simulate( "keyup", { keyCode: $.ui.keyCode.UP } );
});
test( "incremental, true", function() {
expect( 100 );
function fill( num, val ) {
return $.map( new Array( num ), function() {
return val;
});
}
var i, diff,
prev = 0,
expected = [].concat( fill( 18, 1 ), fill( 37, 2 ), fill( 14, 3 ),
fill( 9, 4 ), fill( 6, 5 ), fill( 5, 6 ), fill ( 5, 7 ),
fill( 4, 8 ), fill( 2, 9 ) ),
element = $( "#spin" ).val( prev ).spinner({
incremental: true,
spin: function( event, ui ) {
equal( ui.value - prev, expected[ i ] );
prev = ui.value;
}
});
for ( i = 0; i < 100; i++ ) {
element.simulate( "keydown", { keyCode: $.ui.keyCode.UP } );
}
element.simulate( "keyup", { keyCode: $.ui.keyCode.UP } );
});
test( "incremental, function", function() {
expect( 100 );
var i,
prev = 0,
element = $( "#spin" ).val( prev ).spinner({
incremental: function( i ) {
return i;
},
spin: function( event, ui ) {
equal( ui.value - prev, i + 1 );
prev = ui.value;
}
});
for ( i = 0; i < 100; i++ ) {
element.simulate( "keydown", { keyCode: $.ui.keyCode.UP } );
}
element.simulate( "keyup", { keyCode: $.ui.keyCode.UP } );
});
test( "numberFormat, number", function() {
expect( 2 );
var element = $( "#spin" ).val( 0 ).spinner({ numberFormat: "n" });
equal( element.val(), "0.00", "formatted on init" );
element.spinner( "stepUp" );
equal( element.val(), "1.00", "formatted after step" );
});
test( "numberFormat, number, simple", function() {
expect( 2 );
var element = $( "#spin" ).val( 0 ).spinner({ numberFormat: "n0" });
equal( element.val(), "0", "formatted on init" );
element.spinner( "stepUp" );
equal( element.val(), "1", "formatted after step" );
});
test( "numberFormat, currency", function() {
expect( 2 );
var element = $( "#spin" ).val( 0 ).spinner({ numberFormat: "C" });
equal( element.val(), "$0.00", "formatted on init" );
element.spinner( "stepUp" );
equal( element.val(), "$1.00", "formatted after step" );
});
test( "numberFormat, change", function() {
expect( 2 );
var element = $( "#spin" ).val( 5 ).spinner({ numberFormat: "n1" });
equal( element.val(), "5.0", "formatted on init" );
element.spinner( "option", "numberFormat", "c" );
equal( element.val(), "$5.00", "formatted after change" );
});
test( "culture, null", function() {
expect( 2 );
Globalize.culture( "ja-JP" );
var element = $( "#spin" ).val( 0 ).spinner({ numberFormat: "C" });
equal( element.val(), "¥0", "formatted on init" );
element.spinner( "stepUp" );
equal( element.val(), "¥1", "formatted after step" );
// reset culture
Globalize.culture( "default" );
});
test( "currency, ja-JP", function() {
expect( 2 );
var element = $( "#spin" ).val( 0 ).spinner({
numberFormat: "C",
culture: "ja-JP"
});
equal( element.val(), "¥0", "formatted on init" );
element.spinner( "stepUp" );
equal( element.val(), "¥1", "formatted after step" );
});
test( "currency, change", function() {
expect( 2 );
var element = $( "#spin" ).val( 5 ).spinner({
numberFormat: "C",
culture: "ja-JP"
});
equal( element.val(), "¥5", "formatted on init" );
element.spinner( "option", "culture", "en" );
equal( element.val(), "$5.00", "formatted after change" );
});
test( "max", function() {
expect( 3 );
var element = $( "#spin" ).val( 1000 ).spinner({ max: 100 });
equals( element.val(), 1000, "value not constrained on init" );
element.spinner( "value", 1000 );
equals( element.val(), 100, "max constrained if value method is greater" );
element.val( 1000 ).blur();
equals( element.val(), 1000, "max not constrained if manual entry" );
});
test( "min", function() {
expect( 3 );
var element = $( "#spin" ).val( -1000 ).spinner({ min: -100 });
equals( element.val(), -1000, "value not constrained on init" );
element.spinner( "value", -1000 );
equals( element.val(), -100, "min constrained if value method is greater" );
element.val( -1000 ).blur();
equals( element.val(), -1000, "min not constrained if manual entry" );
});
test( "step, 2", function() {
expect( 3 );
var element = $( "#spin" ).val( 0 ).spinner({ step: 2 });
element.spinner( "stepUp" );
equals( element.val(), "2", "stepUp" );
element.spinner( "value", "10.5" );
equals( element.val(), "10", "value reset to 10" );
element.val( "4.5" );
element.spinner( "stepUp" );
equals( element.val(), "6", "stepUp" );
});
test( "step, 0.7", function() {
expect( 1 );
var element = $("#spin").val( 0 ).spinner({
step: 0.7
});
element.spinner( "stepUp" );
equals( element.val(), "0.7", "stepUp" );
});
})( jQuery );
|