Skip to content
Permalink
Browse files

Spinner: Removed value option. Don't change empty values to zero on i…

…nit. Don't enforce min/max on blur.
  • Loading branch information
scottgonzalez committed Aug 13, 2011
1 parent 257021b commit 1aea0ff4b83d3bc3242767d4c5a87983b4b47839
@@ -24,13 +24,17 @@

_parse: function( value ) {
if ( typeof value === "string" ) {
// already a timestamp
if ( Number( value ) == value ) {
return Number( value );
}
return +Globalize.parseDate( value );
}
return value;
},

_format: function() {
return Globalize.format( new Date(this.options.value), "t" );
_format: function( value ) {
return Globalize.format( new Date(value), "t" );
}
});

@@ -4,9 +4,8 @@ module( "spinner: core" );

test( "keydown UP on input, increases value not greater than max", function() {
expect( 5 );
var element = $( "#spin" ).spinner({
var element = $( "#spin" ).val( 70 ).spinner({
max: 100,
value: 70,
step: 10
});

@@ -24,9 +23,8 @@ test( "keydown UP on input, increases value not greater than max", function() {

test( "keydown DOWN on input, decreases value not less than min", function() {
expect( 5 );
var element = $( "#spin" ).spinner({
var element = $( "#spin" ).val( 50 ).spinner({
min: 20,
value: 50,
step: 10
});

@@ -44,9 +42,8 @@ test( "keydown DOWN on input, decreases value not less than min", function() {

test( "keydown PAGE_UP on input, increases value not greater than max", function() {
expect( 5 );
var element = $( "#spin" ).spinner({
var element = $( "#spin" ).val( 70 ).spinner({
max: 100,
value: 70,
page: 10
});

@@ -64,9 +61,8 @@ test( "keydown PAGE_UP on input, increases value not greater than max", function

test( "keydown PAGE_DOWN on input, decreases value not less than min", function() {
expect( 5 );
var element = $( "#spin" ).spinner({
var element = $( "#spin" ).val( 50 ).spinner({
min: 20,
value: 50,
page: 10
});

@@ -84,9 +80,8 @@ test( "keydown PAGE_DOWN on input, decreases value not less than min", function(

test( "mouse click on up button, increases value not greater than max", function() {
expect( 3 );
var element = $( "#spin" ).spinner({
max: 20,
value: 18
var element = $( "#spin" ).val( 18 ).spinner({
max: 20
}),
button = element.spinner( "widget" ).find( ".ui-spinner-up" );

@@ -100,9 +95,8 @@ test( "mouse click on up button, increases value not greater than max", function

test( "mouse click on up button, increases value not greater than max", function() {
expect( 3 );
var element = $( "#spin" ).spinner({
min: 0,
value: 2
var element = $( "#spin" ).val( 2 ).spinner({
min: 0
}),
button = element.spinner( "widget" ).find( ".ui-spinner-down" );

@@ -137,29 +131,26 @@ test( "mousewheel on input", function() {
});

test( "reading HTML5 attributes", function() {
expect( 8 );
expect( 6 );
var markup = "<input type='number' min='-100' max='100' value='5' step='2'>",
element = $( markup ).spinner();
equal( element.spinner( "option", "value" ), 5, "value from markup" );
equal( element.spinner( "option", "min" ), -100, "min from markup" );
equal( element.spinner( "option", "max" ), 100, "max from markup" );
equal( element.spinner( "option", "step" ), 2, "step from markup" );

element = $( markup ).spinner({
min: -200,
max: 200,
value: 20,
step: 5
});
equal( element.spinner( "option", "value" ), 20, "value from options" );
equal( element.spinner( "option", "min" ), -200, "min from options" );
equal( element.spinner( "option", "max" ), 200, "max from options" );
equal( element.spinner( "option", "step" ), 5, "stop from options" );
});

test( "ARIA attributes", function() {
expect( 7 );
var element = $( "#spin" ).spinner({ min: -5, max: 5, value: 2 });
var element = $( "#spin" ).val( 2 ).spinner({ min: -5, max: 5 });

equal( element.attr( "role" ), "spinbutton", "role" );
equal( element.attr( "aria-valuemin" ), -5, "aria-valuemin" );
@@ -185,17 +176,22 @@ test( "focus text field when pressing button", function() {
ok( element[ 0 ] === document.activeElement, "focused after" );
});

test( "don't clear invalid value on blur", function() {
expect( 1 );
var element = $( "#spin" ).spinner();
element.focus().val( "a" ).blur();
equal( element.val(), "a" );
});

test( "precision", function() {
expect( 2 );
var element = $( "#spin" ).spinner({
value: .05,
var element = $( "#spin" ).val( .05 ).spinner({
step: .0001
});
element.spinner( "stepUp" );
equal( element.val(), "0.0501", "precision from step" );

element.spinner( "option", {
value: 1.05,
element.val( 1.05 ).spinner( "option", {
step: 1
});
element.spinner( "stepDown" );
@@ -7,7 +7,6 @@ commonWidgetTests( "spinner", {
numberFormat: null,
page: 10,
step: 1,
value: 0,

// callbacks
change: null,
@@ -129,24 +129,28 @@ test( "change", function() {
});
}

element.focus();
shouldChange( false, "key UP, before blur" );
spinner_simulateKeyDownUp( element, $.ui.keyCode.UP );
shouldChange( true, "blur after key UP" );
element.blur();

element.focus();
shouldChange( false, "key DOWN, before blur" );
spinner_simulateKeyDownUp( element, $.ui.keyCode.DOWN );
shouldChange( true, "blur after key DOWN" );
element.blur();

element.focus();
shouldChange( false, "key PAGE_UP, before blur" );
spinner_simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
shouldChange( true, "blur after key PAGE_UP" );
element.blur();

element.focus();
shouldChange( false, "key PAGE_DOWN, before blur" );
spinner_simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
shouldChange( true, "key PAGE_DOWN" );
shouldChange( true, "blur after key PAGE_DOWN" );
element.blur();

shouldChange( false, "many keys, before blur" );
@@ -11,7 +11,7 @@ test( "destroy", function() {

test( "disable", function() {
expect( 14 );
var element = $( "#spin" ).spinner({ value: 2 }),
var element = $( "#spin" ).val( 2 ).spinner(),
wrapper = $( "#spin" ).spinner( "widget" );

ok( !wrapper.hasClass( "ui-spinner-disabled" ), "before: wrapper does not have ui-spinner-disabled class" );
@@ -71,9 +71,8 @@ test( "enable", function() {

test( "pageDown", function() {
expect( 4 );
var element = $( "#spin" ).spinner({
var element = $( "#spin" ).val( -12 ).spinner({
page: 20,
value: -12,
min: -100
});

@@ -92,9 +91,8 @@ test( "pageDown", function() {

test( "pageUp", function() {
expect( 4 );
var element = $( "#spin" ).spinner({
var element = $( "#spin" ).val( 12 ).spinner({
page: 20,
value: 12,
max: 100
});

@@ -113,9 +111,8 @@ test( "pageUp", function() {

test( "stepDown", function() {
expect( 4 );
var element = $( "#spin" ).spinner({
var element = $( "#spin" ).val( 0 ).spinner({
step: 2,
value: 0,
min: -15
});

@@ -134,9 +131,8 @@ test( "stepDown", function() {

test( "stepUp", function() {
expect( 4 );
var element = $( "#spin" ).spinner({
var element = $( "#spin" ).val( 0 ).spinner({
step: 2,
value: 0,
max: 15
});

@@ -155,7 +151,7 @@ test( "stepUp", function() {

test( "value", function() {
expect( 2 );
var element = $( "#spin" ).spinner({ value: 0 });
var element = $( "#spin" ).val( 0 ).spinner();

element.spinner( "value", 10 );
equals( element.val(), 10, "change value via value method" );
@@ -3,30 +3,27 @@
module( "spinner: options" );

test( "numberFormat, number", function() {
expect( 1 );
var element = $( "#spin" ).spinner({
value: "1",
numberFormat: "n"
});
equal( element.val(), "1.00", "n" );
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( 1 );
var element = $( "#spin" ).spinner({
value: "1",
numberFormat: "n0"
});
equal( element.val(), "1", "n0" );
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( 1 );
var element = $( "#spin" ).spinner({
value: "1",
numberFormat: "C"
});
equal( element.val(), "$1.00", "C" );
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" );
});

/* TODO figure out how to test this properly
@@ -69,32 +66,31 @@ test("incremental - true (default)", function() {

test( "max", function() {
expect( 3 );
var element = $( "#spin" ).spinner({ max: 100, value: 1000 });
equals( element.val(), 100, "max constrained if value option is greater" );
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(), 100, "max constrained if manual entry" );
equals( element.val(), 1000, "max not constrained if manual entry" );
});

test( "min", function() {
expect( 3 );
var element = $( "#spin" ).spinner({ min: -100, value: -1000 });
equals( element.val(), -100, "min constrained if value option is greater" );
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(), -100, "min constrained if manual entry" );
equals( element.val(), -1000, "min not constrained if manual entry" );
});

test( "step, 2", function() {
expect( 4 );
var element = $( "#spin" ).spinner({ step: 2 });
equals( element.val(), "0", "value initialized to 0" );
expect( 3 );
var element = $( "#spin" ).val( 0 ).spinner({ step: 2 });

element.spinner( "stepUp" );
equals( element.val(), "2", "stepUp" );
@@ -107,16 +103,13 @@ test( "step, 2", function() {
});

test( "step, 0.7", function() {
expect( 2 );
var element = $("#spin").spinner({
expect( 1 );
var element = $("#spin").val( 0 ).spinner({
step: 0.7
});
equals( element.val(), "0", "value initialized to 0" );

element.spinner( "stepUp" );
equals( element.val(), "0.7", "stepUp" );
});

// TODO: test value

})( jQuery );

0 comments on commit 1aea0ff

Please sign in to comment.
You can’t perform that action at this time.