mirror of
https://github.com/jquery/jquery-ui.git
synced 2025-01-07 20:34:24 +00:00
Spinner tests: Cleanup.
This commit is contained in:
parent
2b22bbdd6a
commit
160a301d96
@ -19,6 +19,7 @@
|
||||
<script src="../../jquery.simulate.js"></script>
|
||||
<script src="../testsuite.js"></script>
|
||||
|
||||
<script src="spinner_test_helpers.js"></script>
|
||||
<script src="spinner_core.js"></script>
|
||||
<script src="spinner_defaults.js"></script>
|
||||
<script src="spinner_events.js"></script>
|
||||
|
@ -1,171 +1,151 @@
|
||||
/*
|
||||
* spinner_core.js
|
||||
*/
|
||||
(function( $ ) {
|
||||
|
||||
var simulateKeyDownUp = function(el, kCode, shift) {
|
||||
el.simulate("keydown",{keyCode:kCode, shiftKey: shift || false })
|
||||
.simulate("keyup",{keyCode:kCode, shiftKey: shift || false });
|
||||
};
|
||||
module( "spinner: core" );
|
||||
|
||||
(function($) {
|
||||
|
||||
// Spinner Tests
|
||||
module("spinner: core");
|
||||
|
||||
test("destroy", function() {
|
||||
// TODO: move to spinner_methods
|
||||
test( "destroy", function() {
|
||||
expect( 1 );
|
||||
// TODO: is this cheat still needed?
|
||||
// cheat a bit to get IE6 to pass
|
||||
$("#spin").val(0);
|
||||
var beforeHtml = $("#spin").parent().html();
|
||||
var afterHtml = $("#spin").spinner().spinner("destroy").parent().html();
|
||||
equal( afterHtml, beforeHtml, "before/after html should be the same" );
|
||||
});
|
||||
|
||||
test("keydown UP on input, increases value not greater than max", function() {
|
||||
var el = $("#spin").spinner({
|
||||
max:100,
|
||||
value:50,
|
||||
step:10
|
||||
$( "#spin" ).val( 0 );
|
||||
domEqual( "#spin", function() {
|
||||
$( "#spin" ).spinner().spinner( "destroy" );
|
||||
});
|
||||
|
||||
simulateKeyDownUp(el, $.ui.keyCode.UP);
|
||||
equals(el.val(), 60);
|
||||
|
||||
for (i = 0; i<11; i++) {
|
||||
simulateKeyDownUp(el, $.ui.keyCode.UP);
|
||||
}
|
||||
equals(el.val(), 100);
|
||||
|
||||
el.spinner("value", 50);
|
||||
simulateKeyDownUp(el, $.ui.keyCode.UP);
|
||||
equals(el.val(), 60);
|
||||
});
|
||||
|
||||
test("keydown DOWN on input, decreases value not less than min", function() {
|
||||
var el = $("#spin").spinner({
|
||||
min:-100,
|
||||
value:50,
|
||||
step:10
|
||||
});
|
||||
|
||||
simulateKeyDownUp(el, $.ui.keyCode.DOWN);
|
||||
equals(el.val(), 40);
|
||||
|
||||
for (i = 0; i<21; i++) {
|
||||
simulateKeyDownUp(el, $.ui.keyCode.DOWN);
|
||||
}
|
||||
equals(el.val(), -100);
|
||||
|
||||
el.spinner("value", 50);
|
||||
simulateKeyDownUp(el, $.ui.keyCode.DOWN);
|
||||
equals(el.val(), 40);
|
||||
});
|
||||
|
||||
test("keydown PGUP on input, increases value not greater than max", function() {
|
||||
var el = $("#spin").spinner({
|
||||
max: 500,
|
||||
value: 0,
|
||||
test( "keydown UP on input, increases value not greater than max", function() {
|
||||
expect( 5 );
|
||||
var element = $( "#spin" ).spinner({
|
||||
max: 100,
|
||||
value: 70,
|
||||
step: 10
|
||||
});
|
||||
|
||||
simulateKeyDownUp(el, $.ui.keyCode.PAGE_UP);
|
||||
equal(el.val(), 100);
|
||||
|
||||
for (i = 0; i<5; i++) {
|
||||
simulateKeyDownUp(el, $.ui.keyCode.PAGE_UP);
|
||||
}
|
||||
equal(el.val(), 500);
|
||||
|
||||
el.spinner("value", 0);
|
||||
simulateKeyDownUp(el, $.ui.keyCode.PAGE_UP);
|
||||
equals(el.val(), 100);
|
||||
spinner_simulateKeyDownUp( element, $.ui.keyCode.UP );
|
||||
equals( element.val(), 80 );
|
||||
spinner_simulateKeyDownUp( element, $.ui.keyCode.UP );
|
||||
equals( element.val(), 90 );
|
||||
spinner_simulateKeyDownUp( element, $.ui.keyCode.UP );
|
||||
equals( element.val(), 100 );
|
||||
spinner_simulateKeyDownUp( element, $.ui.keyCode.UP );
|
||||
equals( element.val(), 100 );
|
||||
spinner_simulateKeyDownUp( element, $.ui.keyCode.UP );
|
||||
equals( element.val(), 100 );
|
||||
});
|
||||
|
||||
test("keydown PGDN on input, decreases value not less than min", function() {
|
||||
var el = $("#spin").spinner({
|
||||
min:-500,
|
||||
value:0,
|
||||
step:10
|
||||
test( "keydown DOWN on input, decreases value not less than min", function() {
|
||||
expect( 5 );
|
||||
var element = $( "#spin" ).spinner({
|
||||
min: 20,
|
||||
value: 50,
|
||||
step: 10
|
||||
});
|
||||
|
||||
simulateKeyDownUp(el, $.ui.keyCode.PAGE_DOWN);
|
||||
equals(el.val(), -100);
|
||||
|
||||
for (i = 0; i<5; i++) {
|
||||
simulateKeyDownUp(el, $.ui.keyCode.PAGE_DOWN);
|
||||
}
|
||||
equals(el.val(), -500);
|
||||
|
||||
el.spinner("value", 0);
|
||||
simulateKeyDownUp(el, $.ui.keyCode.PAGE_DOWN);
|
||||
equals(el.val(), -100);
|
||||
spinner_simulateKeyDownUp( element, $.ui.keyCode.DOWN );
|
||||
equals( element.val(), 40 );
|
||||
spinner_simulateKeyDownUp( element, $.ui.keyCode.DOWN );
|
||||
equals( element.val(), 30 );
|
||||
spinner_simulateKeyDownUp( element, $.ui.keyCode.DOWN );
|
||||
equals( element.val(), 20 );
|
||||
spinner_simulateKeyDownUp( element, $.ui.keyCode.DOWN );
|
||||
equals( element.val(), 20 );
|
||||
spinner_simulateKeyDownUp( element, $.ui.keyCode.DOWN );
|
||||
equals( element.val(), 20 );
|
||||
});
|
||||
|
||||
test("mouse click on buttons", function() {
|
||||
var el = $("#spin").spinner(),
|
||||
test( "keydown PGUP on input, increases value not greater than max", function() {
|
||||
expect( 5 );
|
||||
var element = $( "#spin" ).spinner({
|
||||
max: 100,
|
||||
value: 70,
|
||||
page: 10
|
||||
});
|
||||
|
||||
spinner_simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
|
||||
equals( element.val(), 80 );
|
||||
spinner_simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
|
||||
equals( element.val(), 90 );
|
||||
spinner_simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
|
||||
equals( element.val(), 100 );
|
||||
spinner_simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
|
||||
equals( element.val(), 100 );
|
||||
spinner_simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
|
||||
equals( element.val(), 100 );
|
||||
});
|
||||
|
||||
test( "keydown PGDN on input, decreases value not less than min", function() {
|
||||
expect( 5 );
|
||||
var element = $( "#spin" ).spinner({
|
||||
min: 20,
|
||||
value: 50,
|
||||
page: 10
|
||||
});
|
||||
|
||||
spinner_simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
|
||||
equals( element.val(), 40 );
|
||||
spinner_simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
|
||||
equals( element.val(), 30 );
|
||||
spinner_simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
|
||||
equals( element.val(), 20 );
|
||||
spinner_simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
|
||||
equals( element.val(), 20 );
|
||||
spinner_simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
|
||||
equals( element.val(), 20 );
|
||||
});
|
||||
|
||||
test( "mouse click on buttons", function() {
|
||||
expect( 2 );
|
||||
var element = $( "#spin" ).spinner(),
|
||||
val = 0;
|
||||
|
||||
$(".ui-spinner-up").trigger("mousedown").trigger("mouseup");
|
||||
equals(el.val(), ++val, "mouse click to up");
|
||||
$( ".ui-spinner-up" ).trigger( "mousedown" ).trigger( "mouseup" );
|
||||
equals( element.val(), ++val, "mouse click on up" );
|
||||
|
||||
$(".ui-spinner-down").trigger("mousedown").trigger("mouseup");
|
||||
equals(el.val(), --val, "mouse click to down");
|
||||
|
||||
el.spinner("value", 50);
|
||||
$(".ui-spinner-up").trigger("mousedown").trigger("mouseup");
|
||||
equals(el.val(), 51);
|
||||
|
||||
el.spinner("value", 50);
|
||||
$(".ui-spinner-down").trigger("mousedown").trigger("mouseup");
|
||||
equals(el.val(), 49);
|
||||
$( ".ui-spinner-down" ).trigger( "mousedown" ).trigger( "mouseup" );
|
||||
equals( element.val(), --val, "mouse click on down");
|
||||
});
|
||||
|
||||
test("mouse wheel on input", function() {
|
||||
expect(4);
|
||||
stop();
|
||||
test( "mouse wheel on input", function() {
|
||||
expect( 4 );
|
||||
|
||||
var el = $("#spin").spinner();
|
||||
equal(el.val(), 0);
|
||||
el.trigger("mousewheel", 1);
|
||||
equal(el.val(), 1);
|
||||
var element = $( "#spin" ).spinner();
|
||||
equal( element.val(), 0 );
|
||||
element.trigger( "mousewheel", 1 );
|
||||
equal( element.val(), 1 );
|
||||
|
||||
// mousewheel handler uses a timeout, need to accomodate that
|
||||
setTimeout(function() {
|
||||
el.trigger("mousewheel", -1);
|
||||
equal(el.val(), 0);
|
||||
|
||||
setTimeout(function() {
|
||||
el.trigger("mousewheel", -1);
|
||||
equal(el.val(), -1);
|
||||
start();
|
||||
}, 100);
|
||||
}, 100);
|
||||
element.trigger( "mousewheel", -1 );
|
||||
equal( element.val(), 0 );
|
||||
|
||||
element.trigger( "mousewheel", -1 );
|
||||
equal(element.val(), -1 );
|
||||
});
|
||||
|
||||
test("reading HTML5 attributes", function() {
|
||||
var el = $('<input id="spinner" type="number" min="-100" max="100" value="5" step="2">').spinner();
|
||||
equals(el.spinner('option', 'value'), 5, 'value');
|
||||
equals(el.spinner('option', 'max'), 100, 'max');
|
||||
equals(el.spinner('option', 'min'), -100, 'min');
|
||||
equals(el.spinner('option', 'step'), 2, 'step');
|
||||
test( "reading HTML5 attributes", function() {
|
||||
expect( 4 );
|
||||
var element = $( "<input id='spinner' type='number' min='-100' max='100' value='5' step='2'>" ).spinner();
|
||||
equals( element.spinner( "option", "value" ), 5, "value" );
|
||||
equals( element.spinner( "option", "max" ), 100, "max" );
|
||||
equals( element.spinner( "option", "min" ), -100, "min" );
|
||||
equals( element.spinner( "option", "step" ), 2, "step" );
|
||||
});
|
||||
|
||||
test("ARIA attributes", function() {
|
||||
var el = $('#spin').spinner({ min: -5, max: 5, value: 2 });;
|
||||
test( "ARIA attributes", function() {
|
||||
expect( 7 );
|
||||
var element = $( "#spin" ).spinner({ min: -5, max: 5, value: 2 });
|
||||
|
||||
equals(el.attr('role'), 'spinbutton', 'role');
|
||||
equals(el.attr('aria-valuemin'), -5, 'aria-valuemin');
|
||||
equals(el.attr('aria-valuemax'), 5, 'aria-valuemax');
|
||||
equals(el.attr('aria-valuenow'), 2, 'aria-valuenow');
|
||||
equals( element.attr( "role" ), "spinbutton", "role" );
|
||||
equals( element.attr( "aria-valuemin" ), -5, "aria-valuemin" );
|
||||
equals( element.attr( "aria-valuemax" ), 5, "aria-valuemax" );
|
||||
equals( element.attr( "aria-valuenow" ), 2, "aria-valuenow" );
|
||||
|
||||
el.spinner('stepUp');
|
||||
element.spinner( "stepUp" );
|
||||
|
||||
equals(el.attr('aria-valuenow'), 3, 'stepUp 1 step changes aria-valuenow');
|
||||
equals( element.attr( "aria-valuenow" ), 3, "stepUp 1 step changes aria-valuenow" );
|
||||
|
||||
el.spinner('option', { min: -10, max: 10 });
|
||||
element.spinner( "option", { min: -10, max: 10 } );
|
||||
|
||||
equals(el.attr('aria-valuemin'), -10, 'min option changed aria-valuemin changes');
|
||||
equals(el.attr('aria-valuemax'), 10, 'max option changed aria-valuemax changes');
|
||||
equals( element.attr( "aria-valuemin" ), -10, "min option changed aria-valuemin changes" );
|
||||
equals( element.attr( "aria-valuemax" ), 10, "max option changed aria-valuemax changes" );
|
||||
});
|
||||
|
||||
})(jQuery);
|
||||
})( jQuery );
|
||||
|
@ -14,7 +14,7 @@ test("start", function() {
|
||||
}
|
||||
});
|
||||
|
||||
simulateKeyDownUp(el, $.ui.keyCode.UP);
|
||||
spinner_simulateKeyDownUp(el, $.ui.keyCode.UP);
|
||||
|
||||
equals(start, 1, "Start triggered");
|
||||
});
|
||||
@ -28,7 +28,7 @@ test("spin", function() {
|
||||
}
|
||||
});
|
||||
|
||||
simulateKeyDownUp(el, $.ui.keyCode.UP);
|
||||
spinner_simulateKeyDownUp(el, $.ui.keyCode.UP);
|
||||
|
||||
equals(spin, 1, "Spin triggered");
|
||||
});
|
||||
@ -42,7 +42,7 @@ test("stop", function() {
|
||||
}
|
||||
});
|
||||
|
||||
simulateKeyDownUp(el, $.ui.keyCode.DOWN);
|
||||
spinner_simulateKeyDownUp(el, $.ui.keyCode.DOWN);
|
||||
|
||||
equals(stop, 1, "Stop triggered");
|
||||
});
|
||||
@ -56,7 +56,7 @@ test("change", function() {
|
||||
}
|
||||
});
|
||||
|
||||
simulateKeyDownUp(el, $.ui.keyCode.UP);
|
||||
spinner_simulateKeyDownUp(el, $.ui.keyCode.UP);
|
||||
el.blur();
|
||||
|
||||
equals(change, 1, "Change triggered");
|
||||
|
@ -17,16 +17,16 @@ test("disable", function() {
|
||||
ok(wrapper.hasClass("ui-spinner-disabled"), "after: wrapper has ui-spinner-disabled class");
|
||||
ok(el.is(':disabled'), "after: input has disabled attribute");
|
||||
|
||||
simulateKeyDownUp(el, $.ui.keyCode.UP);
|
||||
spinner_simulateKeyDownUp(el, $.ui.keyCode.UP);
|
||||
equals(val, el.val(), "keyboard - value does not change on key UP");
|
||||
|
||||
simulateKeyDownUp(el, $.ui.keyCode.DOWN);
|
||||
spinner_simulateKeyDownUp(el, $.ui.keyCode.DOWN);
|
||||
equals(val, el.val(), "keyboard - value does not change on key DOWN");
|
||||
|
||||
simulateKeyDownUp(el, $.ui.keyCode.PAGE_UP);
|
||||
spinner_simulateKeyDownUp(el, $.ui.keyCode.PAGE_UP);
|
||||
equals(val, el.val(), "keyboard - value does not change on key PGUP");
|
||||
|
||||
simulateKeyDownUp(el, $.ui.keyCode.PAGE_DOWN);
|
||||
spinner_simulateKeyDownUp(el, $.ui.keyCode.PAGE_DOWN);
|
||||
equals(val, el.val(), "keyboard - value does not change on key PGDN");
|
||||
|
||||
wrapper.find(":ui-button").first().trigger('mousedown').trigger('mouseup');
|
||||
|
@ -94,7 +94,7 @@ test("step, 2", function() {
|
||||
equals(el.val(), "0", "value initialized to");
|
||||
|
||||
for ( var i = 0 ; i < 5 ; i++ ) {
|
||||
simulateKeyDownUp(el, $.ui.keyCode.UP);
|
||||
spinner_simulateKeyDownUp(el, $.ui.keyCode.UP);
|
||||
}
|
||||
equals(el.val(), "10", "UP 5 times with step: 2");
|
||||
|
||||
@ -103,7 +103,7 @@ test("step, 2", function() {
|
||||
|
||||
el.spinner('option', 'step', 2);
|
||||
for ( var i = 0 ; i < 5 ; i++ ) {
|
||||
simulateKeyDownUp(el, $.ui.keyCode.UP);
|
||||
spinner_simulateKeyDownUp(el, $.ui.keyCode.UP);
|
||||
}
|
||||
equals(el.val(), "20.5", "UP 5 times with step: 2");
|
||||
});
|
||||
@ -115,11 +115,11 @@ test("step, 0.7", function() {
|
||||
});
|
||||
equals(el.val(), "0.0", "value initialized to");
|
||||
|
||||
simulateKeyDownUp(el, $.ui.keyCode.DOWN);
|
||||
spinner_simulateKeyDownUp(el, $.ui.keyCode.DOWN);
|
||||
equals(el.val(), "-0.7", "DOWN 1 time with step: 0.7");
|
||||
|
||||
for ( var i = 0 ; i < 11 ; i++ ) {
|
||||
simulateKeyDownUp(el, $.ui.keyCode.UP);
|
||||
spinner_simulateKeyDownUp(el, $.ui.keyCode.UP);
|
||||
}
|
||||
equals(el.val(), "7.0", "UP 11 times with step: 0.7");
|
||||
});
|
||||
|
5
tests/unit/spinner/spinner_test_helpers.js
Normal file
5
tests/unit/spinner/spinner_test_helpers.js
Normal file
@ -0,0 +1,5 @@
|
||||
var spinner_simulateKeyDownUp = function( element, keyCode, shift ) {
|
||||
element
|
||||
.simulate( "keydown", { keyCode: keyCode, shiftKey: shift || false } )
|
||||
.simulate( "keyup", { keyCode: keyCode, shiftKey: shift || false } );
|
||||
};
|
Loading…
Reference in New Issue
Block a user