2015-04-06 23:46:15 +00:00
|
|
|
define( [
|
2016-04-06 13:26:05 +00:00
|
|
|
"qunit",
|
2015-04-06 23:46:15 +00:00
|
|
|
"jquery",
|
2020-05-16 07:16:24 +00:00
|
|
|
"lib/helper",
|
2015-04-07 14:55:52 +00:00
|
|
|
"./helper",
|
2015-07-15 02:10:12 +00:00
|
|
|
"ui/widgets/spinner"
|
2020-05-16 07:16:24 +00:00
|
|
|
], function( QUnit, $, helper, testHelper ) {
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2015-04-07 14:30:07 +00:00
|
|
|
var simulateKeyDownUp = testHelper.simulateKeyDownUp;
|
2012-04-19 17:12:23 +00:00
|
|
|
|
2020-05-16 07:16:24 +00:00
|
|
|
QUnit.module( "spinner: core", { afterEach: helper.moduleAfterEach } );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2016-04-06 13:26:05 +00:00
|
|
|
QUnit.test( "markup structure", function( assert ) {
|
|
|
|
assert.expect( 6 );
|
2015-02-24 21:17:29 +00:00
|
|
|
var element = $( "#spin" ).spinner(),
|
|
|
|
spinner = element.spinner( "widget" ),
|
|
|
|
up = spinner.find( ".ui-spinner-up" ),
|
|
|
|
down = spinner.find( ".ui-spinner-down" );
|
|
|
|
|
|
|
|
assert.hasClasses( element, "ui-spinner-input" );
|
|
|
|
assert.hasClasses( spinner, "ui-spinner ui-widget ui-widget-content" );
|
|
|
|
assert.hasClasses( up, "ui-spinner-button ui-spinner-up ui-widget" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( up.length, 1, "Spinner contains exactly one up button" );
|
2015-02-24 21:17:29 +00:00
|
|
|
assert.hasClasses( down, "ui-spinner-button ui-spinner-down ui-widget" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( down.length, 1, "Spinner contains exactly one down button" );
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2015-02-24 21:17:29 +00:00
|
|
|
|
2016-04-06 13:26:05 +00:00
|
|
|
QUnit.test( "keydown UP on input, increases value not greater than max", function( assert ) {
|
|
|
|
assert.expect( 5 );
|
2015-08-24 12:57:40 +00:00
|
|
|
var element = $( "#spin" ).val( 70 ).spinner( {
|
2011-08-07 12:10:49 +00:00
|
|
|
max: 100,
|
|
|
|
step: 10
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.UP );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 80 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.UP );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 90 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.UP );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 100 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.UP );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 100 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.UP );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 100 );
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2016-04-06 13:26:05 +00:00
|
|
|
QUnit.test( "keydown DOWN on input, decreases value not less than min", function( assert ) {
|
|
|
|
assert.expect( 5 );
|
2015-08-24 12:57:40 +00:00
|
|
|
var element = $( "#spin" ).val( 50 ).spinner( {
|
2011-08-07 12:10:49 +00:00
|
|
|
min: 20,
|
|
|
|
step: 10
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.DOWN );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 40 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.DOWN );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 30 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.DOWN );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 20 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.DOWN );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 20 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.DOWN );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 20 );
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2016-04-06 13:26:05 +00:00
|
|
|
QUnit.test( "keydown PAGE_UP on input, increases value not greater than max", function( assert ) {
|
|
|
|
assert.expect( 5 );
|
2015-08-24 12:57:40 +00:00
|
|
|
var element = $( "#spin" ).val( 70 ).spinner( {
|
2011-08-07 12:10:49 +00:00
|
|
|
max: 100,
|
|
|
|
page: 10
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 80 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 90 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 100 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 100 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 100 );
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2016-04-06 13:26:05 +00:00
|
|
|
QUnit.test( "keydown PAGE_DOWN on input, decreases value not less than min", function( assert ) {
|
|
|
|
assert.expect( 5 );
|
2015-08-24 12:57:40 +00:00
|
|
|
var element = $( "#spin" ).val( 50 ).spinner( {
|
2011-08-07 12:10:49 +00:00
|
|
|
min: 20,
|
|
|
|
page: 10
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 40 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 30 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 20 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 20 );
|
2012-04-19 17:12:23 +00:00
|
|
|
simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 20 );
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2016-04-06 13:26:05 +00:00
|
|
|
QUnit.test( "blur input while spinning with UP", function( assert ) {
|
|
|
|
var ready = assert.async();
|
|
|
|
assert.expect( 3 );
|
2013-02-22 14:15:43 +00:00
|
|
|
var value,
|
|
|
|
element = $( "#spin" ).val( 10 ).spinner();
|
|
|
|
|
|
|
|
function step1() {
|
|
|
|
element.simulate( "keydown", { keyCode: $.ui.keyCode.UP } );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 11 );
|
2013-02-22 14:15:43 +00:00
|
|
|
setTimeout( step2, 750 );
|
|
|
|
}
|
|
|
|
|
|
|
|
function step2() {
|
|
|
|
value = element.val();
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.ok( value > 11, "repeating while key is down" );
|
2013-04-01 12:59:52 +00:00
|
|
|
|
2015-05-14 02:01:44 +00:00
|
|
|
element.on( "blur", function() {
|
2013-03-31 16:55:57 +00:00
|
|
|
value = element.val();
|
2013-04-01 12:59:52 +00:00
|
|
|
setTimeout( step3, 750 );
|
2015-08-24 12:57:40 +00:00
|
|
|
} )[ 0 ].blur();
|
2013-02-22 14:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function step3() {
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), value, "stopped repeating on blur" );
|
|
|
|
ready();
|
2013-02-22 14:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
element[ 0 ].focus();
|
|
|
|
setTimeout( step1 );
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2013-02-22 14:15:43 +00:00
|
|
|
|
2016-04-06 13:26:05 +00:00
|
|
|
QUnit.test( "mouse click on up button, increases value not greater than max", function( assert ) {
|
|
|
|
assert.expect( 3 );
|
2015-08-24 12:57:40 +00:00
|
|
|
var element = $( "#spin" ).val( 18 ).spinner( {
|
2011-08-13 23:05:04 +00:00
|
|
|
max: 20
|
2015-08-24 12:57:40 +00:00
|
|
|
} ),
|
2011-08-07 22:34:03 +00:00
|
|
|
button = element.spinner( "widget" ).find( ".ui-spinner-up" );
|
|
|
|
|
|
|
|
button.trigger( "mousedown" ).trigger( "mouseup" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 19 );
|
2011-08-07 22:34:03 +00:00
|
|
|
button.trigger( "mousedown" ).trigger( "mouseup" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 20 );
|
2011-08-07 22:34:03 +00:00
|
|
|
button.trigger( "mousedown" ).trigger( "mouseup" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 20 );
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2016-04-06 13:26:05 +00:00
|
|
|
QUnit.test( "mouse click on up button, increases value not greater than max", function( assert ) {
|
|
|
|
assert.expect( 3 );
|
2015-08-24 12:57:40 +00:00
|
|
|
var element = $( "#spin" ).val( 2 ).spinner( {
|
2011-08-13 23:05:04 +00:00
|
|
|
min: 0
|
2015-08-24 12:57:40 +00:00
|
|
|
} ),
|
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" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 1 );
|
2011-08-07 22:34:03 +00:00
|
|
|
button.trigger( "mousedown" ).trigger( "mouseup" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 0 );
|
2011-08-07 22:34:03 +00:00
|
|
|
button.trigger( "mousedown" ).trigger( "mouseup" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 0 );
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2016-04-06 13:26:05 +00:00
|
|
|
QUnit.test( "mousewheel on input", function( assert ) {
|
2017-02-19 15:52:27 +00:00
|
|
|
var ready = assert.async();
|
|
|
|
assert.expect( 5 );
|
2011-08-07 22:34:03 +00:00
|
|
|
|
2015-08-24 12:57:40 +00:00
|
|
|
var element = $( "#spin" ).val( 0 ).spinner( {
|
2011-08-07 22:34:03 +00:00
|
|
|
step: 2
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2017-02-19 15:52:27 +00:00
|
|
|
element.simulate( "focus" );
|
|
|
|
setTimeout( step1 );
|
2011-08-07 22:34:03 +00:00
|
|
|
|
2017-02-19 15:52:27 +00:00
|
|
|
function step1() {
|
|
|
|
element.trigger( "mousewheel" );
|
|
|
|
assert.equal( element.val(), 0, "mousewheel event without delta does not change value" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2017-02-19 15:52:27 +00:00
|
|
|
element.trigger( "mousewheel", 1 );
|
|
|
|
assert.equal( element.val(), 2, "delta 1" );
|
|
|
|
|
|
|
|
element.trigger( "mousewheel", -0.2 );
|
|
|
|
assert.equal( element.val(), 0, "delta -0.2" );
|
|
|
|
|
|
|
|
element.trigger( "mousewheel", -15 );
|
|
|
|
assert.equal( element.val(), -2, "delta -15" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2017-02-19 15:52:27 +00:00
|
|
|
element.simulate( "blur" );
|
|
|
|
setTimeout( step2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
function step2() {
|
|
|
|
element.trigger( "mousewheel", 1 );
|
|
|
|
assert.equal( element.val(), -2, "mousewheel when not focused" );
|
|
|
|
|
|
|
|
ready();
|
|
|
|
}
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2016-04-06 13:26:05 +00:00
|
|
|
QUnit.test( "reading HTML5 attributes", function( assert ) {
|
|
|
|
assert.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();
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.spinner( "option", "min" ), -100, "min from markup" );
|
|
|
|
assert.equal( element.spinner( "option", "max" ), 100, "max from markup" );
|
|
|
|
assert.equal( element.spinner( "option", "step" ), 2, "step from markup" );
|
2011-08-07 22:34:03 +00:00
|
|
|
|
2015-08-24 12:57:40 +00:00
|
|
|
element = $( markup ).spinner( {
|
2011-08-07 22:34:03 +00:00
|
|
|
min: -200,
|
|
|
|
max: 200,
|
|
|
|
step: 5
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.spinner( "option", "min" ), -200, "min from options" );
|
|
|
|
assert.equal( element.spinner( "option", "max" ), 200, "max from options" );
|
|
|
|
assert.equal( element.spinner( "option", "step" ), 5, "stop from options" );
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2016-04-06 13:26:05 +00:00
|
|
|
QUnit.test( "ARIA attributes", function( assert ) {
|
|
|
|
assert.expect( 9 );
|
2015-08-24 12:57:40 +00:00
|
|
|
var element = $( "#spin" ).val( 2 ).spinner( { min: -5, max: 5 } );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.attr( "role" ), "spinbutton", "role" );
|
|
|
|
assert.equal( element.attr( "aria-valuemin" ), "-5", "aria-valuemin" );
|
|
|
|
assert.equal( element.attr( "aria-valuemax" ), "5", "aria-valuemax" );
|
|
|
|
assert.equal( element.attr( "aria-valuenow" ), "2", "aria-valuenow" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "stepUp" );
|
|
|
|
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.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 } );
|
|
|
|
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.attr( "aria-valuemin" ), "-10", "min option changed aria-valuemin changes" );
|
|
|
|
assert.equal( element.attr( "aria-valuemax" ), "10", "max option changed aria-valuemax changes" );
|
2011-09-09 23:24:10 +00:00
|
|
|
|
|
|
|
element.spinner( "option", "min", null );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.attr( "aria-valuemin" ), undefined, "aria-valuemin not set when no min" );
|
2011-09-09 23:24:10 +00:00
|
|
|
|
|
|
|
element.spinner( "option", "max", null );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.attr( "aria-valuemax" ), undefined, "aria-valuemax not set when no max" );
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2016-04-06 13:26:05 +00:00
|
|
|
QUnit.test( "focus text field when pressing button", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2011-08-07 23:11:49 +00:00
|
|
|
var element = $( "#spin" ).spinner();
|
2015-05-14 02:01:44 +00:00
|
|
|
$( "body" ).trigger( "focus" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.ok( element[ 0 ] !== document.activeElement, "not focused before" );
|
2015-05-14 02:01:44 +00:00
|
|
|
element.spinner( "widget" ).find( ".ui-spinner-up" ).trigger( "mousedown" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.ok( element[ 0 ] === document.activeElement, "focused after" );
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-07 23:11:49 +00:00
|
|
|
|
2016-04-06 13:26:05 +00:00
|
|
|
QUnit.test( "don't clear invalid value on blur", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2011-08-13 23:05:04 +00:00
|
|
|
var element = $( "#spin" ).spinner();
|
2015-05-14 02:01:44 +00:00
|
|
|
element.trigger( "focus" ).val( "a" ).trigger( "blur" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), "a" );
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-13 23:05:04 +00:00
|
|
|
|
2016-04-06 13:26:05 +00:00
|
|
|
QUnit.test( "precision", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2015-08-24 12:57:40 +00:00
|
|
|
var element = $( "#spin" ).val( 0.05 ).spinner( {
|
2012-04-19 02:36:15 +00:00
|
|
|
step: 0.0001
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-13 03:11:35 +00:00
|
|
|
element.spinner( "stepUp" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), "0.0501", "precision from step" );
|
2011-08-13 03:11:35 +00:00
|
|
|
|
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
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-13 03:11:35 +00:00
|
|
|
element.spinner( "stepDown" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), "0.05", "precision from min" );
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-13 03:11:35 +00:00
|
|
|
|
2015-04-06 23:46:15 +00:00
|
|
|
} );
|