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 ) {
|
2021-06-06 22:58:12 +00:00
|
|
|
"use strict";
|
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: methods", { afterEach: helper.moduleAfterEach } );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2016-04-06 13:26:05 +00:00
|
|
|
QUnit.test( "destroy", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2015-04-06 23:46:15 +00:00
|
|
|
assert.domEqual( "#spin", function() {
|
2011-08-07 17:05:04 +00:00
|
|
|
$( "#spin" ).spinner().spinner( "destroy" );
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
|
|
|
} );
|
2011-08-07 17:05:04 +00:00
|
|
|
|
2016-04-06 13:26:05 +00:00
|
|
|
QUnit.test( "disable", function( assert ) {
|
|
|
|
assert.expect( 15 );
|
2011-08-13 23:05:04 +00:00
|
|
|
var element = $( "#spin" ).val( 2 ).spinner(),
|
2011-08-07 12:10:49 +00:00
|
|
|
wrapper = $( "#spin" ).spinner( "widget" );
|
|
|
|
|
2015-01-30 15:59:50 +00:00
|
|
|
assert.lacksClasses( wrapper, "ui-spinner-disabled" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.ok( !element.is( ":disabled" ), "before: input does not have disabled attribute" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "disable" );
|
2015-01-30 15:59:50 +00:00
|
|
|
assert.hasClasses( wrapper, "ui-state-disabled ui-spinner-disabled" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.ok( !wrapper.attr( "aria-disabled" ), "after: wrapper does not have aria-disabled attr" );
|
|
|
|
assert.ok( element.is( ":disabled" ), "after: input has disabled attribute" );
|
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( 2, element.val(), "keyboard - value does not change on key UP" );
|
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( 2, element.val(), "keyboard - value does not change on key DOWN" );
|
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( 2, element.val(), "keyboard - value does not change on key PGUP" );
|
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( 2, element.val(), "keyboard - value does not change on key PGDN" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
wrapper.find( ".ui-spinner-up" ).trigger( "mousedown" ).trigger( "mouseup" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( 2, element.val(), "mouse - value does not change on clicking up button" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
wrapper.find( ".ui-spinner-down" ).trigger( "mousedown" ).trigger( "mouseup" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( 2, element.val(), "mouse - value does not change on clicking down button" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "stepUp", 6 );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( 8, element.val(), "script - stepUp 6 steps changes value" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "stepDown" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( 7, element.val(), "script - stepDown 1 step changes value" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "pageUp" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( 17, element.val(), "script - pageUp 1 page changes value" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "pageDown" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( 7, element.val(), "script - pageDown 1 page changes value" );
|
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( "enable", function( assert ) {
|
|
|
|
assert.expect( 5 );
|
2015-08-24 12:57:40 +00:00
|
|
|
var element = $( "#spin" ).val( 1 ).spinner( { disabled: true } ),
|
2011-08-07 12:10:49 +00:00
|
|
|
wrapper = element.spinner( "widget" );
|
|
|
|
|
2015-01-30 15:59:50 +00:00
|
|
|
assert.hasClasses( wrapper, "ui-spinner-disabled" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.ok( element.is( ":disabled" ), "before: input has disabled attribute" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "enable" );
|
|
|
|
|
2015-01-30 15:59:50 +00:00
|
|
|
assert.lacksClasses( wrapper, "ui-spinner-disabled" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.ok( !element.is( ":disabled" ), "after: input does not have disabled attribute" );
|
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( 2, element.val(), "keyboard - value changes on key UP" );
|
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( "isValid", function( assert ) {
|
|
|
|
assert.expect( 8 );
|
2015-08-24 12:57:40 +00:00
|
|
|
var element = $( "#spin" ).spinner( {
|
2013-09-11 20:00:27 +00:00
|
|
|
min: 0,
|
|
|
|
max: 10,
|
|
|
|
step: 2
|
2015-08-24 12:57:40 +00:00
|
|
|
} ),
|
2013-09-11 20:00:27 +00:00
|
|
|
spinner = element.spinner( "instance" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.ok( !spinner.isValid(), "initial state is invalid" );
|
2013-09-11 20:00:27 +00:00
|
|
|
|
|
|
|
element.val( "this is not a number" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.ok( !spinner.isValid(), "text string is not valid" );
|
2013-09-11 20:00:27 +00:00
|
|
|
|
|
|
|
element.val( "0" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.ok( spinner.isValid(), "min value is valid" );
|
2013-09-11 20:00:27 +00:00
|
|
|
|
|
|
|
element.val( "10" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.ok( spinner.isValid(), "max value is valid" );
|
2013-09-11 20:00:27 +00:00
|
|
|
|
|
|
|
element.val( "4" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.ok( spinner.isValid(), "inbetween step is valid" );
|
2013-09-11 20:00:27 +00:00
|
|
|
|
|
|
|
element.val( "-1" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.ok( !spinner.isValid(), "below min is invalid" );
|
2013-09-11 20:00:27 +00:00
|
|
|
|
|
|
|
element.val( "11" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.ok( !spinner.isValid(), "above max is invalid" );
|
2013-09-11 20:00:27 +00:00
|
|
|
|
|
|
|
element.val( "1" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.ok( !spinner.isValid(), "step mismatch is invalid" );
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2013-09-11 20:00:27 +00:00
|
|
|
|
2016-04-06 13:26:05 +00:00
|
|
|
QUnit.test( "pageDown", function( assert ) {
|
|
|
|
assert.expect( 4 );
|
2015-08-24 12:57:40 +00:00
|
|
|
var element = $( "#spin" ).val( -12 ).spinner( {
|
2011-08-07 12:10:49 +00:00
|
|
|
page: 20,
|
|
|
|
min: -100
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "pageDown" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), -32, "pageDown 1 page" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "pageDown", 3 );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), -92, "pageDown 3 pages" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "pageDown" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), -100, "value close to min and pageDown 1 page" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "pageDown", 10 );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), -100, "value at min and pageDown 10 pages" );
|
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( "pageUp", function( assert ) {
|
|
|
|
assert.expect( 4 );
|
2015-08-24 12:57:40 +00:00
|
|
|
var element = $( "#spin" ).val( 12 ).spinner( {
|
2011-08-07 12:10:49 +00:00
|
|
|
page: 20,
|
|
|
|
max: 100
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "pageUp" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 32, "pageUp 1 page" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "pageUp", 3 );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 92, "pageUp 3 pages" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "pageUp" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 100, "value close to max and pageUp 1 page" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "pageUp", 10 );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 100, "value at max and pageUp 10 pages" );
|
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( "stepDown", function( assert ) {
|
|
|
|
assert.expect( 4 );
|
2015-08-24 12:57:40 +00:00
|
|
|
var element = $( "#spin" ).val( 0 ).spinner( {
|
2011-08-07 12:10:49 +00:00
|
|
|
step: 2,
|
|
|
|
min: -15
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "stepDown" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), "-1", "stepDown 1 step" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "stepDown", 5 );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), "-11", "stepDown 5 steps" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "stepDown", 4 );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), "-15", "close to min and stepDown 4 steps" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "stepDown" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), "-15", "at min and stepDown 1 step" );
|
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( "stepUp", function( assert ) {
|
|
|
|
assert.expect( 4 );
|
2015-08-24 12:57:40 +00:00
|
|
|
var element = $( "#spin" ).val( 0 ).spinner( {
|
2011-08-07 12:10:49 +00:00
|
|
|
step: 2,
|
2011-09-14 12:54:29 +00:00
|
|
|
max: 16
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "stepUp" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 2, "stepUp 1 step" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "stepUp", 5 );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 12, "stepUp 5 steps" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "stepUp", 4 );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 16, "close to max and stepUp 4 steps" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "stepUp" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 16, "at max and stepUp 1 step" );
|
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( "value", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2015-08-24 12:57:40 +00:00
|
|
|
var element = $( "#spin" ).val( 0 ).spinner( {
|
2011-09-14 12:54:29 +00:00
|
|
|
step: 3
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
|
|
|
element.spinner( "value", 10 );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.val(), 9, "change value via value method" );
|
2011-08-07 12:10:49 +00:00
|
|
|
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( element.spinner( "value" ), 9, "get value via value method" );
|
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( "widget", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2012-08-27 00:19:35 +00:00
|
|
|
var element = $( "#spin" ).spinner(),
|
|
|
|
widgetElement = element.spinner( "widget" );
|
2016-04-06 13:26:05 +00:00
|
|
|
assert.equal( widgetElement.length, 1, "one element" );
|
|
|
|
assert.strictEqual( widgetElement[ 0 ], element.parent()[ 0 ], "parent element" );
|
2015-08-24 12:57:40 +00:00
|
|
|
} );
|
2012-08-27 00:19:35 +00:00
|
|
|
|
2015-04-06 23:46:15 +00:00
|
|
|
} );
|