2011-08-07 12:10:49 +00:00
|
|
|
(function( $ ) {
|
|
|
|
|
2012-04-19 17:12:23 +00:00
|
|
|
var simulateKeyDownUp = TestHelpers.spinner.simulateKeyDownUp;
|
|
|
|
|
2011-08-07 12:10:49 +00:00
|
|
|
module( "spinner: core" );
|
|
|
|
|
|
|
|
test( "keydown UP on input, increases value not greater than max", function() {
|
|
|
|
expect( 5 );
|
2011-08-13 23:05:04 +00:00
|
|
|
var element = $( "#spin" ).val( 70 ).spinner({
|
2011-08-07 12:10:49 +00:00
|
|
|
max: 100,
|
|
|
|
step: 10
|
|
|
|
});
|
|
|
|
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.UP );
|
2011-08-07 22:34:03 +00:00
|
|
|
equal( element.val(), 80 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.UP );
|
2011-08-07 22:34:03 +00:00
|
|
|
equal( element.val(), 90 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.UP );
|
2011-08-07 22:34:03 +00:00
|
|
|
equal( element.val(), 100 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.UP );
|
2011-08-07 22:34:03 +00:00
|
|
|
equal( element.val(), 100 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.UP );
|
2011-08-07 22:34:03 +00:00
|
|
|
equal( element.val(), 100 );
|
2011-08-07 12:10:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test( "keydown DOWN on input, decreases value not less than min", function() {
|
|
|
|
expect( 5 );
|
2011-08-13 23:05:04 +00:00
|
|
|
var element = $( "#spin" ).val( 50 ).spinner({
|
2011-08-07 12:10:49 +00:00
|
|
|
min: 20,
|
|
|
|
step: 10
|
|
|
|
});
|
|
|
|
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.DOWN );
|
2011-08-07 22:34:03 +00:00
|
|
|
equal( element.val(), 40 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.DOWN );
|
2011-08-07 22:34:03 +00:00
|
|
|
equal( element.val(), 30 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.DOWN );
|
2011-08-07 22:34:03 +00:00
|
|
|
equal( element.val(), 20 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.DOWN );
|
2011-08-07 22:34:03 +00:00
|
|
|
equal( element.val(), 20 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.DOWN );
|
2011-08-07 22:34:03 +00:00
|
|
|
equal( element.val(), 20 );
|
2011-08-07 12:10:49 +00:00
|
|
|
});
|
|
|
|
|
2011-08-07 22:34:03 +00:00
|
|
|
test( "keydown PAGE_UP on input, increases value not greater than max", function() {
|
2011-08-07 12:10:49 +00:00
|
|
|
expect( 5 );
|
2011-08-13 23:05:04 +00:00
|
|
|
var element = $( "#spin" ).val( 70 ).spinner({
|
2011-08-07 12:10:49 +00:00
|
|
|
max: 100,
|
|
|
|
page: 10
|
|
|
|
});
|
|
|
|
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
|
2011-08-07 22:34:03 +00:00
|
|
|
equal( element.val(), 80 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
|
2011-08-07 22:34:03 +00:00
|
|
|
equal( element.val(), 90 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
|
2011-08-07 22:34:03 +00:00
|
|
|
equal( element.val(), 100 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
|
2011-08-07 22:34:03 +00:00
|
|
|
equal( element.val(), 100 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
|
2011-08-07 22:34:03 +00:00
|
|
|
equal( element.val(), 100 );
|
2011-08-07 12:10:49 +00:00
|
|
|
});
|
|
|
|
|
2011-08-07 22:34:03 +00:00
|
|
|
test( "keydown PAGE_DOWN on input, decreases value not less than min", function() {
|
2011-08-07 12:10:49 +00:00
|
|
|
expect( 5 );
|
2011-08-13 23:05:04 +00:00
|
|
|
var element = $( "#spin" ).val( 50 ).spinner({
|
2011-08-07 12:10:49 +00:00
|
|
|
min: 20,
|
|
|
|
page: 10
|
|
|
|
});
|
|
|
|
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
|
2011-08-07 22:34:03 +00:00
|
|
|
equal( element.val(), 40 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
|
2011-08-07 22:34:03 +00:00
|
|
|
equal( element.val(), 30 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
|
2011-08-07 22:34:03 +00:00
|
|
|
equal( element.val(), 20 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
|
2011-08-07 22:34:03 +00:00
|
|
|
equal( element.val(), 20 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
|
2011-08-07 22:34:03 +00:00
|
|
|
equal( element.val(), 20 );
|
2011-08-07 12:10:49 +00:00
|
|
|
});
|
|
|
|
|
2011-08-07 22:34:03 +00:00
|
|
|
test( "mouse click on up button, increases value not greater than max", function() {
|
|
|
|
expect( 3 );
|
2011-08-13 23:05:04 +00:00
|
|
|
var element = $( "#spin" ).val( 18 ).spinner({
|
|
|
|
max: 20
|
2011-08-07 22:34:03 +00:00
|
|
|
}),
|
|
|
|
button = element.spinner( "widget" ).find( ".ui-spinner-up" );
|
|
|
|
|
|
|
|
button.trigger( "mousedown" ).trigger( "mouseup" );
|
|
|
|
equal( element.val(), 19 );
|
|
|
|
button.trigger( "mousedown" ).trigger( "mouseup" );
|
|
|
|
equal( element.val(), 20 );
|
|
|
|
button.trigger( "mousedown" ).trigger( "mouseup" );
|
|
|
|
equal( element.val(), 20 );
|
|
|
|
});
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2011-08-07 22:34:03 +00:00
|
|
|
test( "mouse click on up button, increases value not greater than max", function() {
|
|
|
|
expect( 3 );
|
2011-08-13 23:05:04 +00:00
|
|
|
var element = $( "#spin" ).val( 2 ).spinner({
|
|
|
|
min: 0
|
2011-08-07 22:34:03 +00:00
|
|
|
}),
|
|
|
|
button = element.spinner( "widget" ).find( ".ui-spinner-down" );
|
2012-04-19 02:36:15 +00:00
|
|
|
|
2011-08-07 22:34:03 +00:00
|
|
|
button.trigger( "mousedown" ).trigger( "mouseup" );
|
|
|
|
equal( element.val(), 1 );
|
|
|
|
button.trigger( "mousedown" ).trigger( "mouseup" );
|
|
|
|
equal( element.val(), 0 );
|
|
|
|
button.trigger( "mousedown" ).trigger( "mouseup" );
|
|
|
|
equal( element.val(), 0 );
|
2011-08-07 12:10:49 +00:00
|
|
|
});
|
|
|
|
|
2011-08-07 22:34:03 +00:00
|
|
|
test( "mousewheel on input", function() {
|
2011-09-14 12:54:29 +00:00
|
|
|
expect( 4 );
|
2011-08-07 22:34:03 +00:00
|
|
|
|
2011-09-14 12:54:29 +00:00
|
|
|
var element = $( "#spin" ).val( 0 ).spinner({
|
2011-08-07 22:34:03 +00:00
|
|
|
step: 2
|
|
|
|
});
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2011-08-07 22:34:03 +00:00
|
|
|
element.trigger( "mousewheel" );
|
|
|
|
equal( element.val(), 0, "mousewheel event without delta does not change value" );
|
|
|
|
|
2011-08-07 12:10:49 +00:00
|
|
|
element.trigger( "mousewheel", 1 );
|
2011-08-07 22:34:03 +00:00
|
|
|
equal( element.val(), 2 );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2011-08-07 22:34:03 +00:00
|
|
|
element.trigger( "mousewheel", -0.2 );
|
2011-08-07 12:10:49 +00:00
|
|
|
equal( element.val(), 0 );
|
|
|
|
|
2011-08-07 22:34:03 +00:00
|
|
|
element.trigger( "mousewheel", -15 );
|
|
|
|
equal(element.val(), -2 );
|
2011-08-07 12:10:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test( "reading HTML5 attributes", function() {
|
2011-08-13 23:05:04 +00:00
|
|
|
expect( 6 );
|
2011-08-07 22:34:03 +00:00
|
|
|
var markup = "<input type='number' min='-100' max='100' value='5' step='2'>",
|
|
|
|
element = $( markup ).spinner();
|
|
|
|
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,
|
|
|
|
step: 5
|
|
|
|
});
|
|
|
|
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" );
|
2011-08-07 12:10:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test( "ARIA attributes", function() {
|
2011-09-09 23:24:10 +00:00
|
|
|
expect( 9 );
|
2011-08-13 23:05:04 +00:00
|
|
|
var element = $( "#spin" ).val( 2 ).spinner({ min: -5, max: 5 });
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2011-08-07 22:34:03 +00:00
|
|
|
equal( element.attr( "role" ), "spinbutton", "role" );
|
2011-09-09 23:24:10 +00:00
|
|
|
equal( element.attr( "aria-valuemin" ), "-5", "aria-valuemin" );
|
|
|
|
equal( element.attr( "aria-valuemax" ), "5", "aria-valuemax" );
|
|
|
|
equal( element.attr( "aria-valuenow" ), "2", "aria-valuenow" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "stepUp" );
|
|
|
|
|
2011-09-09 23:24:10 +00:00
|
|
|
equal( element.attr( "aria-valuenow" ), "3", "stepUp 1 step changes aria-valuenow" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "option", { min: -10, max: 10 } );
|
|
|
|
|
2011-09-09 23:24:10 +00:00
|
|
|
equal( element.attr( "aria-valuemin" ), "-10", "min option changed aria-valuemin changes" );
|
|
|
|
equal( element.attr( "aria-valuemax" ), "10", "max option changed aria-valuemax changes" );
|
|
|
|
|
|
|
|
element.spinner( "option", "min", null );
|
|
|
|
equal( element.attr( "aria-valuemin" ), undefined, "aria-valuemin not set when no min" );
|
|
|
|
|
|
|
|
element.spinner( "option", "max", null );
|
|
|
|
equal( element.attr( "aria-valuemax" ), undefined, "aria-valuemax not set when no max" );
|
2011-08-07 12:10:49 +00:00
|
|
|
});
|
|
|
|
|
2011-08-07 23:11:49 +00:00
|
|
|
test( "focus text field when pressing button", function() {
|
|
|
|
expect( 2 );
|
|
|
|
var element = $( "#spin" ).spinner();
|
|
|
|
$( "body" ).focus();
|
|
|
|
ok( element[ 0 ] !== document.activeElement, "not focused before" );
|
|
|
|
element.spinner( "widget" ).find( ".ui-spinner-up" ).mousedown();
|
|
|
|
ok( element[ 0 ] === document.activeElement, "focused after" );
|
|
|
|
});
|
|
|
|
|
2011-08-13 23:05:04 +00:00
|
|
|
test( "don't clear invalid value on blur", function() {
|
|
|
|
expect( 1 );
|
|
|
|
var element = $( "#spin" ).spinner();
|
|
|
|
element.focus().val( "a" ).blur();
|
|
|
|
equal( element.val(), "a" );
|
|
|
|
});
|
|
|
|
|
2011-08-13 03:11:35 +00:00
|
|
|
test( "precision", function() {
|
|
|
|
expect( 2 );
|
2012-04-19 02:36:15 +00:00
|
|
|
var element = $( "#spin" ).val( 0.05 ).spinner({
|
|
|
|
step: 0.0001
|
2011-08-13 03:11:35 +00:00
|
|
|
});
|
|
|
|
element.spinner( "stepUp" );
|
|
|
|
equal( element.val(), "0.0501", "precision from step" );
|
|
|
|
|
2011-08-13 23:05:04 +00:00
|
|
|
element.val( 1.05 ).spinner( "option", {
|
2011-09-14 12:54:29 +00:00
|
|
|
step: 1,
|
|
|
|
min: -9.95
|
2011-08-13 03:11:35 +00:00
|
|
|
});
|
|
|
|
element.spinner( "stepDown" );
|
2011-09-14 12:54:29 +00:00
|
|
|
equal( element.val(), "0.05", "precision from min" );
|
2011-08-13 03:11:35 +00:00
|
|
|
});
|
|
|
|
|
2011-08-07 12:10:49 +00:00
|
|
|
})( jQuery );
|