Spinner tests: Cleanup.

This commit is contained in:
Scott González 2011-08-06 17:07:12 -04:00
parent 2b22bbdd6a
commit 160a301d96
6 changed files with 145 additions and 159 deletions

View File

@ -19,6 +19,7 @@
<script src="../../jquery.simulate.js"></script> <script src="../../jquery.simulate.js"></script>
<script src="../testsuite.js"></script> <script src="../testsuite.js"></script>
<script src="spinner_test_helpers.js"></script>
<script src="spinner_core.js"></script> <script src="spinner_core.js"></script>
<script src="spinner_defaults.js"></script> <script src="spinner_defaults.js"></script>
<script src="spinner_events.js"></script> <script src="spinner_events.js"></script>

View File

@ -1,171 +1,151 @@
/* (function( $ ) {
* spinner_core.js
*/
var simulateKeyDownUp = function(el, kCode, shift) { module( "spinner: core" );
el.simulate("keydown",{keyCode:kCode, shiftKey: shift || false })
.simulate("keyup",{keyCode:kCode, shiftKey: shift || false });
};
(function($) { // TODO: move to spinner_methods
test( "destroy", function() {
// Spinner Tests expect( 1 );
module("spinner: core"); // TODO: is this cheat still needed?
test("destroy", function() {
// cheat a bit to get IE6 to pass // cheat a bit to get IE6 to pass
$("#spin").val(0); $( "#spin" ).val( 0 );
var beforeHtml = $("#spin").parent().html(); domEqual( "#spin", function() {
var afterHtml = $("#spin").spinner().spinner("destroy").parent().html(); $( "#spin" ).spinner().spinner( "destroy" );
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
}); });
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() { test( "keydown UP on input, increases value not greater than max", function() {
var el = $("#spin").spinner({ expect( 5 );
min:-100, var element = $( "#spin" ).spinner({
value:50, max: 100,
step:10 value: 70,
});
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,
step: 10 step: 10
}); });
simulateKeyDownUp(el, $.ui.keyCode.PAGE_UP); spinner_simulateKeyDownUp( element, $.ui.keyCode.UP );
equal(el.val(), 100); equals( element.val(), 80 );
spinner_simulateKeyDownUp( element, $.ui.keyCode.UP );
for (i = 0; i<5; i++) { equals( element.val(), 90 );
simulateKeyDownUp(el, $.ui.keyCode.PAGE_UP); spinner_simulateKeyDownUp( element, $.ui.keyCode.UP );
} equals( element.val(), 100 );
equal(el.val(), 500); spinner_simulateKeyDownUp( element, $.ui.keyCode.UP );
equals( element.val(), 100 );
el.spinner("value", 0); spinner_simulateKeyDownUp( element, $.ui.keyCode.UP );
simulateKeyDownUp(el, $.ui.keyCode.PAGE_UP); equals( element.val(), 100 );
equals(el.val(), 100);
}); });
test("keydown PGDN on input, decreases value not less than min", function() { test( "keydown DOWN on input, decreases value not less than min", function() {
var el = $("#spin").spinner({ expect( 5 );
min:-500, var element = $( "#spin" ).spinner({
value:0, min: 20,
step:10 value: 50,
step: 10
}); });
simulateKeyDownUp(el, $.ui.keyCode.PAGE_DOWN); spinner_simulateKeyDownUp( element, $.ui.keyCode.DOWN );
equals(el.val(), -100); equals( element.val(), 40 );
spinner_simulateKeyDownUp( element, $.ui.keyCode.DOWN );
for (i = 0; i<5; i++) { equals( element.val(), 30 );
simulateKeyDownUp(el, $.ui.keyCode.PAGE_DOWN); spinner_simulateKeyDownUp( element, $.ui.keyCode.DOWN );
} equals( element.val(), 20 );
equals(el.val(), -500); spinner_simulateKeyDownUp( element, $.ui.keyCode.DOWN );
equals( element.val(), 20 );
el.spinner("value", 0); spinner_simulateKeyDownUp( element, $.ui.keyCode.DOWN );
simulateKeyDownUp(el, $.ui.keyCode.PAGE_DOWN); equals( element.val(), 20 );
equals(el.val(), -100);
}); });
test("mouse click on buttons", function() { test( "keydown PGUP on input, increases value not greater than max", function() {
var el = $("#spin").spinner(), 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; val = 0;
$(".ui-spinner-up").trigger("mousedown").trigger("mouseup"); $( ".ui-spinner-up" ).trigger( "mousedown" ).trigger( "mouseup" );
equals(el.val(), ++val, "mouse click to up"); equals( element.val(), ++val, "mouse click on up" );
$(".ui-spinner-down").trigger("mousedown").trigger("mouseup"); $( ".ui-spinner-down" ).trigger( "mousedown" ).trigger( "mouseup" );
equals(el.val(), --val, "mouse click to down"); equals( element.val(), --val, "mouse click on 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);
}); });
test("mouse wheel on input", function() { test( "mouse wheel on input", function() {
expect(4); expect( 4 );
stop();
var el = $("#spin").spinner(); var element = $( "#spin" ).spinner();
equal(el.val(), 0); equal( element.val(), 0 );
el.trigger("mousewheel", 1); element.trigger( "mousewheel", 1 );
equal(el.val(), 1); equal( element.val(), 1 );
// mousewheel handler uses a timeout, need to accomodate that element.trigger( "mousewheel", -1 );
setTimeout(function() { equal( element.val(), 0 );
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(), -1 );
}); });
test("reading HTML5 attributes", function() { test( "reading HTML5 attributes", function() {
var el = $('<input id="spinner" type="number" min="-100" max="100" value="5" step="2">').spinner(); expect( 4 );
equals(el.spinner('option', 'value'), 5, 'value'); var element = $( "<input id='spinner' type='number' min='-100' max='100' value='5' step='2'>" ).spinner();
equals(el.spinner('option', 'max'), 100, 'max'); equals( element.spinner( "option", "value" ), 5, "value" );
equals(el.spinner('option', 'min'), -100, 'min'); equals( element.spinner( "option", "max" ), 100, "max" );
equals(el.spinner('option', 'step'), 2, 'step'); equals( element.spinner( "option", "min" ), -100, "min" );
equals( element.spinner( "option", "step" ), 2, "step" );
}); });
test("ARIA attributes", function() { test( "ARIA attributes", function() {
var el = $('#spin').spinner({ min: -5, max: 5, value: 2 });; expect( 7 );
var element = $( "#spin" ).spinner({ min: -5, max: 5, value: 2 });
equals(el.attr('role'), 'spinbutton', 'role'); equals( element.attr( "role" ), "spinbutton", "role" );
equals(el.attr('aria-valuemin'), -5, 'aria-valuemin'); equals( element.attr( "aria-valuemin" ), -5, "aria-valuemin" );
equals(el.attr('aria-valuemax'), 5, 'aria-valuemax'); equals( element.attr( "aria-valuemax" ), 5, "aria-valuemax" );
equals(el.attr('aria-valuenow'), 2, 'aria-valuenow'); 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( element.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-valuemax" ), 10, "max option changed aria-valuemax changes" );
}); });
})(jQuery); })( jQuery );

View File

@ -14,7 +14,7 @@ test("start", function() {
} }
}); });
simulateKeyDownUp(el, $.ui.keyCode.UP); spinner_simulateKeyDownUp(el, $.ui.keyCode.UP);
equals(start, 1, "Start triggered"); 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"); 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"); 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(); el.blur();
equals(change, 1, "Change triggered"); equals(change, 1, "Change triggered");

View File

@ -17,16 +17,16 @@ test("disable", function() {
ok(wrapper.hasClass("ui-spinner-disabled"), "after: wrapper has ui-spinner-disabled class"); ok(wrapper.hasClass("ui-spinner-disabled"), "after: wrapper has ui-spinner-disabled class");
ok(el.is(':disabled'), "after: input has disabled attribute"); 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"); 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"); 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"); 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"); equals(val, el.val(), "keyboard - value does not change on key PGDN");
wrapper.find(":ui-button").first().trigger('mousedown').trigger('mouseup'); wrapper.find(":ui-button").first().trigger('mousedown').trigger('mouseup');

View File

@ -94,7 +94,7 @@ test("step, 2", function() {
equals(el.val(), "0", "value initialized to"); equals(el.val(), "0", "value initialized to");
for ( var i = 0 ; i < 5 ; i++ ) { 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"); equals(el.val(), "10", "UP 5 times with step: 2");
@ -103,7 +103,7 @@ test("step, 2", function() {
el.spinner('option', 'step', 2); el.spinner('option', 'step', 2);
for ( var i = 0 ; i < 5 ; i++ ) { 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"); 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"); 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"); equals(el.val(), "-0.7", "DOWN 1 time with step: 0.7");
for ( var i = 0 ; i < 11 ; i++ ) { 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"); equals(el.val(), "7.0", "UP 11 times with step: 0.7");
}); });

View 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 } );
};