jquery-ui/tests/unit/spinner/events.js

266 lines
7.7 KiB
JavaScript
Raw Permalink Normal View History

define( [
2016-04-06 13:26:05 +00:00
"qunit",
"jquery",
2015-04-07 14:55:52 +00:00
"./helper",
"ui/widgets/spinner"
2016-04-06 13:26:05 +00:00
], function( QUnit, $, testHelper ) {
2011-08-07 12:10:49 +00:00
var simulateKeyDownUp = testHelper.simulateKeyDownUp;
2016-04-06 13:26:05 +00:00
QUnit.module( "spinner: events" );
2011-08-07 12:10:49 +00:00
2016-04-06 13:26:05 +00:00
QUnit.test( "start", function( assert ) {
assert.expect( 10 );
var element = $( "#spin" ).spinner();
2011-08-07 12:10:49 +00:00
function shouldStart( expectation, msg ) {
element.spinner( "option", "start", function() {
2016-04-06 13:26:05 +00:00
assert.ok( expectation, msg );
2015-08-24 12:57:40 +00:00
} );
}
shouldStart( true, "key UP" );
simulateKeyDownUp( element, $.ui.keyCode.UP );
shouldStart( true, "key DOWN" );
simulateKeyDownUp( element, $.ui.keyCode.DOWN );
shouldStart( true, "key PAGE_UP" );
simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
shouldStart( true, "key PAGE_DOWN" );
simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
shouldStart( true, "button up" );
element.spinner( "widget" ).find( ".ui-spinner-up" ).trigger( "mousedown" ).trigger( "mouseup" );
shouldStart( true, "button down" );
element.spinner( "widget" ).find( ".ui-spinner-down" ).trigger( "mousedown" ).trigger( "mouseup" );
shouldStart( true, "stepUp" );
element.spinner( "stepUp" );
shouldStart( true, "stepDown" );
element.spinner( "stepDown" );
shouldStart( true, "pageUp" );
element.spinner( "pageUp" );
shouldStart( true, "pageDown" );
element.spinner( "pageDown" );
shouldStart( false, "value" );
element.spinner( "value", 999 );
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( "spin", function( assert ) {
assert.expect( 10 );
var element = $( "#spin" ).spinner();
2011-08-07 12:10:49 +00:00
function shouldSpin( expectation, msg ) {
element.spinner( "option", "spin", function() {
2016-04-06 13:26:05 +00:00
assert.ok( expectation, msg );
2015-08-24 12:57:40 +00:00
} );
}
shouldSpin( true, "key UP" );
simulateKeyDownUp( element, $.ui.keyCode.UP );
shouldSpin( true, "key DOWN" );
simulateKeyDownUp( element, $.ui.keyCode.DOWN );
shouldSpin( true, "key PAGE_UP" );
simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
shouldSpin( true, "key PAGE_DOWN" );
simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
shouldSpin( true, "button up" );
element.spinner( "widget" ).find( ".ui-spinner-up" ).trigger( "mousedown" ).trigger( "mouseup" );
shouldSpin( true, "button down" );
element.spinner( "widget" ).find( ".ui-spinner-down" ).trigger( "mousedown" ).trigger( "mouseup" );
shouldSpin( true, "stepUp" );
element.spinner( "stepUp" );
shouldSpin( true, "stepDown" );
element.spinner( "stepDown" );
shouldSpin( true, "pageUp" );
element.spinner( "pageUp" );
shouldSpin( true, "pageDown" );
element.spinner( "pageDown" );
shouldSpin( false, "value" );
element.spinner( "value", 999 );
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( "stop", function( assert ) {
assert.expect( 10 );
var element = $( "#spin" ).spinner();
2011-08-07 12:10:49 +00:00
function shouldStop( expectation, msg ) {
element.spinner( "option", "stop", function() {
2016-04-06 13:26:05 +00:00
assert.ok( expectation, msg );
2015-08-24 12:57:40 +00:00
} );
}
shouldStop( true, "key UP" );
simulateKeyDownUp( element, $.ui.keyCode.UP );
shouldStop( true, "key DOWN" );
simulateKeyDownUp( element, $.ui.keyCode.DOWN );
shouldStop( true, "key PAGE_UP" );
simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
shouldStop( true, "key PAGE_DOWN" );
simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
shouldStop( true, "button up" );
element.spinner( "widget" ).find( ".ui-spinner-up" ).trigger( "mousedown" ).trigger( "mouseup" );
shouldStop( true, "button down" );
element.spinner( "widget" ).find( ".ui-spinner-down" ).trigger( "mousedown" ).trigger( "mouseup" );
shouldStop( true, "stepUp" );
element.spinner( "stepUp" );
shouldStop( true, "stepDown" );
element.spinner( "stepDown" );
shouldStop( true, "pageUp" );
element.spinner( "pageUp" );
shouldStop( true, "pageDown" );
element.spinner( "pageDown" );
shouldStop( false, "value" );
element.spinner( "value", 999 );
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( "change", function( assert ) {
var ready = assert.async();
assert.expect( 12 );
var element = $( "#spin" ).spinner();
function shouldChange( expectation, msg ) {
element.spinner( "option", "change", function() {
2016-04-06 13:26:05 +00:00
assert.ok( expectation, msg );
2015-08-24 12:57:40 +00:00
} );
}
function focusWrap( fn, next ) {
2015-08-24 12:57:40 +00:00
element[ 0 ].focus();
setTimeout( function() {
fn();
2015-08-24 12:57:40 +00:00
setTimeout( function() {
element[ 0 ].blur();
setTimeout( next );
2015-08-24 12:57:40 +00:00
} );
} );
}
function step1() {
2015-08-24 12:57:40 +00:00
focusWrap( function() {
shouldChange( false, "key UP, before blur" );
simulateKeyDownUp( element, $.ui.keyCode.UP );
shouldChange( true, "blur after key UP" );
}, step2 );
}
2011-08-07 12:10:49 +00:00
function step2() {
2015-08-24 12:57:40 +00:00
focusWrap( function() {
shouldChange( false, "key DOWN, before blur" );
simulateKeyDownUp( element, $.ui.keyCode.DOWN );
shouldChange( true, "blur after key DOWN" );
}, step3 );
}
function step3() {
2015-08-24 12:57:40 +00:00
focusWrap( function() {
shouldChange( false, "key PAGE_UP, before blur" );
simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
shouldChange( true, "blur after key PAGE_UP" );
}, step4 );
}
function step4() {
2015-08-24 12:57:40 +00:00
focusWrap( function() {
shouldChange( false, "key PAGE_DOWN, before blur" );
simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
shouldChange( true, "blur after key PAGE_DOWN" );
}, step5 );
}
function step5() {
2015-08-24 12:57:40 +00:00
focusWrap( function() {
shouldChange( false, "many keys, before blur" );
simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
simulateKeyDownUp( element, $.ui.keyCode.UP );
simulateKeyDownUp( element, $.ui.keyCode.UP );
simulateKeyDownUp( element, $.ui.keyCode.UP );
simulateKeyDownUp( element, $.ui.keyCode.DOWN );
shouldChange( true, "blur after many keys" );
}, step6 );
}
function step6() {
2015-08-24 12:57:40 +00:00
focusWrap( function() {
shouldChange( false, "many keys, same final value, before blur" );
simulateKeyDownUp( element, $.ui.keyCode.UP );
simulateKeyDownUp( element, $.ui.keyCode.UP );
simulateKeyDownUp( element, $.ui.keyCode.DOWN );
simulateKeyDownUp( element, $.ui.keyCode.DOWN );
shouldChange( false, "blur after many keys, same final value" );
shouldChange( false, "button up, before blur" );
element.spinner( "widget" ).find( ".ui-spinner-up" ).trigger( "mousedown" ).trigger( "mouseup" );
shouldChange( true, "blur after button up" );
}, step7 );
}
function step7() {
2015-08-24 12:57:40 +00:00
focusWrap( function() {
shouldChange( false, "button down, before blur" );
element.spinner( "widget" ).find( ".ui-spinner-down" ).trigger( "mousedown" ).trigger( "mouseup" );
shouldChange( true, "blur after button down" );
}, step8 );
}
function step8() {
2015-08-24 12:57:40 +00:00
focusWrap( function() {
shouldChange( false, "many buttons, same final value, before blur" );
element.spinner( "widget" ).find( ".ui-spinner-up" ).trigger( "mousedown" ).trigger( "mouseup" );
element.spinner( "widget" ).find( ".ui-spinner-up" ).trigger( "mousedown" ).trigger( "mouseup" );
element.spinner( "widget" ).find( ".ui-spinner-down" ).trigger( "mousedown" ).trigger( "mouseup" );
element.spinner( "widget" ).find( ".ui-spinner-down" ).trigger( "mousedown" ).trigger( "mouseup" );
shouldChange( false, "blur after many buttons, same final value" );
}, step9 );
}
function step9() {
shouldChange( true, "stepUp" );
element.spinner( "stepUp" );
shouldChange( true, "stepDown" );
element.spinner( "stepDown" );
shouldChange( true, "pageUp" );
element.spinner( "pageUp" );
shouldChange( true, "pageDown" );
element.spinner( "pageDown" );
shouldChange( true, "value" );
element.spinner( "value", 999 );
shouldChange( false, "value, same value" );
element.spinner( "value", 999 );
shouldChange( false, "max, value not changed" );
element.spinner( "option", "max", 900 );
shouldChange( false, "max, value not changed" );
element.spinner( "option", "max", 1000 );
shouldChange( false, "min, value not changed" );
element.spinner( "option", "min", 950 );
shouldChange( false, "min, value not changed" );
element.spinner( "option", "min", 200 );
2016-04-06 13:26:05 +00:00
ready();
}
setTimeout( step1 );
2015-08-24 12:57:40 +00:00
} );
2011-08-07 12:10:49 +00:00
} );