Spinner tests: Cleanup.

This commit is contained in:
Scott González 2011-08-07 08:10:11 -04:00
parent e9cdd576f4
commit 380c2e6fbd
3 changed files with 195 additions and 238 deletions

View File

@ -1,65 +1,50 @@
/* (function( $ ) {
* spinner_events.js
*/
(function($) {
module("spinner: events"); module( "spinner: events" );
test("start", function() { test( "start", function() {
var start = 0; expect( 1 );
var element = $( "#spin" ).spinner({
var el = $("#spin").spinner({ start: function() {
start: function(){ ok( true, "start" );
start++;
} }
}); });
spinner_simulateKeyDownUp(el, $.ui.keyCode.UP); spinner_simulateKeyDownUp( element, $.ui.keyCode.UP );
equals(start, 1, "Start triggered");
}); });
test("spin", function() { test( "spin", function() {
var spin = 0; expect( 1 );
var element = $( "#spin" ).spinner({
var el = $("#spin").spinner({
spin: function(){ spin: function(){
spin++; ok( true, "spin" );
} }
}); });
spinner_simulateKeyDownUp(el, $.ui.keyCode.UP); spinner_simulateKeyDownUp( element, $.ui.keyCode.UP );
equals(spin, 1, "Spin triggered");
}); });
test("stop", function() { test( "stop", function() {
var stop = 0; expect( 1 );
var element = $( "#spin" ).spinner({
var el = $("#spin").spinner({
stop: function(){ stop: function(){
stop++; ok( true, "stop" );
} }
}); });
spinner_simulateKeyDownUp(el, $.ui.keyCode.DOWN); spinner_simulateKeyDownUp( element, $.ui.keyCode.DOWN );
equals(stop, 1, "Stop triggered");
}); });
test("change", function() { test( "change", function() {
var change = 0; expect( 1 );
var element = $( "#spin" ).spinner({
var el = $("#spin").spinner({
change: function(){ change: function(){
change++; ok( true, "change" );
} }
}); });
spinner_simulateKeyDownUp(el, $.ui.keyCode.UP); spinner_simulateKeyDownUp( element, $.ui.keyCode.UP );
el.blur(); element.blur();
equals(change, 1, "Change triggered");
}); });
})(jQuery); })( jQuery );

View File

@ -1,155 +1,159 @@
/* (function( $ ) {
* spinner_methods.js
*/
(function($) {
module("spinner: methods"); module( "spinner: methods" );
test("disable", function() { test( "disable", function() {
var el = $("#spin").spinner({ disabled: false }), expect( 14 );
val = el.val(), var element = $( "#spin" ).spinner({ value: 2 }),
wrapper = $("#spin").spinner("widget"); wrapper = $( "#spin" ).spinner( "widget" );
ok(!wrapper.hasClass("ui-spinner-disabled"), "before: wrapper does not have ui-spinner-disabled class"); ok( !wrapper.hasClass( "ui-spinner-disabled" ), "before: wrapper does not have ui-spinner-disabled class" );
ok(!el.is(':disabled'), "before: input does not have disabled attribute"); ok( !element.is( ":disabled" ), "before: input does not have disabled attribute" );
el.spinner("disable"); element.spinner( "disable" );
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( element.is( ":disabled"), "after: input has disabled attribute" );
spinner_simulateKeyDownUp(el, $.ui.keyCode.UP); spinner_simulateKeyDownUp( element, $.ui.keyCode.UP );
equals(val, el.val(), "keyboard - value does not change on key UP"); equals( 2, element.val(), "keyboard - value does not change on key UP" );
spinner_simulateKeyDownUp(el, $.ui.keyCode.DOWN); spinner_simulateKeyDownUp( element, $.ui.keyCode.DOWN );
equals(val, el.val(), "keyboard - value does not change on key DOWN"); equals( 2, element.val(), "keyboard - value does not change on key DOWN" );
spinner_simulateKeyDownUp(el, $.ui.keyCode.PAGE_UP); spinner_simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
equals(val, el.val(), "keyboard - value does not change on key PGUP"); equals( 2, element.val(), "keyboard - value does not change on key PGUP" );
spinner_simulateKeyDownUp(el, $.ui.keyCode.PAGE_DOWN); spinner_simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
equals(val, el.val(), "keyboard - value does not change on key PGDN"); equals( 2, element.val(), "keyboard - value does not change on key PGDN" );
wrapper.find(":ui-button").first().trigger('mousedown').trigger('mouseup'); wrapper.find( ".ui-spinner-up" ).trigger( "mousedown" ).trigger( "mouseup" );
equals(val, el.val(), "mouse - value does not change on clicking up button"); equals( 2, element.val(), "mouse - value does not change on clicking up button" );
wrapper.find(":ui-button").last().trigger('mousedown').trigger('mouseup'); wrapper.find( ".ui-spinner-down" ).trigger( "mousedown" ).trigger( "mouseup" );
equals(val, el.val(), "mouse - value does not change on clicking down button"); equals( 2, element.val(), "mouse - value does not change on clicking down button" );
el.spinner('stepUp', 6); element.spinner( "stepUp", 6 );
equals(6, el.val(), "script - stepUp 6 steps changes value"); equals( 8, element.val(), "script - stepUp 6 steps changes value");
el.spinner('stepDown'); element.spinner( "stepDown" );
equals(5, el.val(), "script - stepDown 1 step changes value"); equals( 7, element.val(), "script - stepDown 1 step changes value" );
el.spinner('pageUp'); element.spinner( "pageUp" );
equals(15, el.val(), "script - pageUp 1 page changes value"); equals( 17, element.val(), "script - pageUp 1 page changes value" );
el.spinner('pageDown');
equals(5, el.val(), "script - pageDown 1 page changes value");
element.spinner( "pageDown" );
equals( 7, element.val(), "script - pageDown 1 page changes value" );
}); });
test("enable", function() { test( "enable", function() {
var el = $("#spin").spinner({ disabled: true }) expect( 5 );
val = el.val(), var element = $( "#spin" ).spinner({ disabled: true })
wrapper = el.spinner("widget"); wrapper = element.spinner( "widget" );
ok(wrapper.hasClass("ui-spinner-disabled"), "before: wrapper has ui-spinner-disabled class"); ok( wrapper.hasClass( "ui-spinner-disabled" ), "before: wrapper has ui-spinner-disabled class" );
ok(el.is(':disabled'), "before: input has disabled attribute"); ok( element.is( ":disabled" ), "before: input has disabled attribute" );
el.spinner("enable"); element.spinner( "enable" );
ok(!wrapper.hasClass(".ui-spinner-disabled"), "after: wrapper does not have ui-spinner-disabled class"); ok( !wrapper.hasClass( ".ui-spinner-disabled" ), "after: wrapper does not have ui-spinner-disabled class" );
ok(!el.is(':disabled'), "after: input does not have disabled attribute"); ok( !element.is( ":disabled" ), "after: input does not have disabled attribute" );
spinner_simulateKeyDownUp( element, $.ui.keyCode.UP );
equals( 1, element.val(), "keyboard - value does not change on key UP" );
}); });
test("pageDown", function() { test( "pageDown", function() {
var el = $('#spin').spinner({ expect( 4 );
step: 2, var element = $( "#spin" ).spinner({
value: 0, page: 20,
value: -12,
min: -100 min: -100
}); });
el.spinner('pageDown'); element.spinner( "pageDown" );
equals(el.val(), -20, "pageDown 1 page"); equals( element.val(), -32, "pageDown 1 page" );
el.spinner('pageDown', 3); element.spinner( "pageDown", 3 );
equals(el.val(), -80, "pageDown 3 pages"); equals( element.val(), -92, "pageDown 3 pages" );
el.val(-91).spinner('pageDown'); element.spinner( "pageDown" );
equals(el.val(), -100, "value close to min and pageDown 1 page"); equals( element.val(), -100, "value close to min and pageDown 1 page" );
el.spinner('pageDown', 10); element.spinner( "pageDown", 10 );
equals(el.val(), -100, "value at min and pageDown 10 pages"); equals( element.val(), -100, "value at min and pageDown 10 pages" );
}); });
test("pageUp", function() { test( "pageUp", function() {
var el = $('#spin').spinner({ expect( 4 );
step: 2, var element = $( "#spin" ).spinner({
value: 0, page: 20,
value: 12,
max: 100 max: 100
}); });
el.spinner('pageUp'); element.spinner( "pageUp" );
equals(el.val(), 20, "pageUp 1 page"); equals( element.val(), 32, "pageUp 1 page" );
el.spinner('pageUp', 3); element.spinner( "pageUp", 3 );
equals(el.val(), 80, "pageUp 3 pages"); equals( element.val(), 92, "pageUp 3 pages" );
el.val(91).spinner('pageUp'); element.spinner( "pageUp" );
equals(el.val(), 100, "value close to max and pageUp 1 page"); equals( element.val(), 100, "value close to max and pageUp 1 page" );
el.spinner('pageUp', 10);
equals(el.val(), 100, "value at max and pageUp 10 pages");
element.spinner( "pageUp", 10 );
equals( element.val(), 100, "value at max and pageUp 10 pages" );
}); });
test("stepDown", function() { test( "stepDown", function() {
expect(4); expect( 4 );
var element = $( "#spin" ).spinner({
step: 2,
value: 0,
min: -15
});
el = $('#spin').spinner({ step: 2, page: 5, value: 0, min: -15 }); element.spinner( "stepDown" );
equals( element.val(), -2, "stepDown 1 step" );
el.spinner('stepDown') element.spinner( "stepDown", 5 );
equals(el.val(), -2, "stepDown 1 step"); equals( element.val(), -12, "stepDown 5 steps" );
el.spinner('stepDown', 5) element.spinner( "stepDown", 4 );
equals(el.val(), -12, "stepDown 5 steps"); equals( element.val(), -15, "close to min and stepDown 4 steps" );
el.spinner('stepDown', 3); element.spinner( "stepDown" );
equals(el.val(), -15, "close to min and stepDown 3 steps"); equals( element.val(), -15, "at min and stepDown 1 step" );
el.spinner('stepDown');
equals(el.val(), -15, "at min and stepDown 1 step");
}); });
test("stepUp", function() { test( "stepUp", function() {
expect(4); expect( 4 );
var element = $( "#spin" ).spinner({
step: 2,
value: 0,
max: 15
});
el = $('#spin').spinner({ step: 2, page: 5, value: 0, max: 15 }); element.spinner( "stepUp" );
equals( element.val(), 2, "stepUp 1 step" );
el.spinner('stepUp') element.spinner( "stepUp", 5 );
equals(el.val(), 2, "stepUp 1 steps"); equals( element.val(), 12, "stepUp 5 steps" );
el.spinner('stepUp', 5) element.spinner( "stepUp", 4 );
equals(el.val(), 12, "stepUp 5 steps"); equals( element.val(), 15, "close to min and stepUp 4 steps" );
el.spinner('stepUp', 3);
equals(el.val(), 15, "close to min and stepUp 3 steps");
el.spinner('stepUp');
equals(el.val(), 15, "at min and stepUp 1 step");
element.spinner( "stepUp" );
equals( element.val(), 15, "at max and stepUp 1 step" );
}); });
test("value", function() { test( "value", function() {
expect(2); expect( 2 );
var element = $( "#spin" ).spinner({ value: 0 });
el = $('#spin').spinner({ value: 0 }); element.spinner( "value", 10 );
equals( element.val(), 10, "change value via value method" );
el.spinner('value', 10); equals( element.spinner( "value" ), 10, "get value via value method" );
equals(el.val(), 10, "change value via value method");
equals(10, el.spinner('value'), "get value via value method");
}); });
})(jQuery); })( jQuery );

View File

@ -1,32 +1,32 @@
/* (function( $ ) {
* spinner_options.js
*/
(function($) {
module("spinner: options"); module( "spinner: options" );
test("numberFormat, number", function() { test( "numberFormat, number", function() {
var el = $("#spin").spinner({ expect( 1 );
var element = $( "#spin" ).spinner({
value: "1", value: "1",
numberFormat: "n" numberFormat: "n"
}); });
equal(el.val(), "1.00"); equal( element.val(), "1.00", "n" );
}); });
test("numberFormat, number, simple", function() { test( "numberFormat, number, simple", function() {
var el = $("#spin").spinner({ expect( 1 );
var element = $( "#spin" ).spinner({
value: "1", value: "1",
numberFormat: "n0" numberFormat: "n0"
}); });
equal(el.val(), "1"); equal( element.val(), "1", "n0" );
}); });
test("numberFormat, currency", function() { test( "numberFormat, currency", function() {
var el = $("#spin").spinner({ expect( 1 );
var element = $( "#spin" ).spinner({
value: "1", value: "1",
numberFormat: "C" numberFormat: "C"
}); });
equal(el.val(), "$1.00"); equal( element.val(), "$1.00", "C" );
}); });
/* TODO figure out how to test this properly /* TODO figure out how to test this properly
@ -67,88 +67,56 @@ test("incremental - true (default)", function() {
}); });
*/ */
test("max", function() { test( "max", function() {
var el = $("#spin").spinner({ max: 100, value: 1000 }); expect( 3 );
equals(el.val(), 100, "max constrained if value option is greater"); var element = $( "#spin" ).spinner({ max: 100, value: 1000 });
equals( element.val(), 100, "max constrained if value option is greater" );
el.spinner('value', 1000); element.spinner( "value", 1000 );
equals(el.val(), 100, "max constrained if value method is greater"); equals( element.val(), 100, "max constrained if value method is greater" );
el.val(1000).blur(); element.val( 1000 ).blur();
equals(el.val(), 100, "max constrained if manual entry"); equals( element.val(), 100, "max constrained if manual entry" );
}); });
test("min", function() { test( "min", function() {
var el = $("#spin").spinner({ min: -100, value: -1000 }); expect( 3 );
equals(el.val(), -100, "min constrained if value option is greater"); var element = $( "#spin" ).spinner({ min: -100, value: -1000 });
equals( element.val(), -100, "min constrained if value option is greater" );
el.spinner('value', -1000); element.spinner( "value", -1000 );
equals(el.val(), -100, "min constrained if value method is greater"); equals( element.val(), -100, "min constrained if value method is greater" );
el.val(-1000).blur(); element.val( -1000 ).blur();
equals(el.val(), -100, "min constrained if manual entry"); equals( element.val(), -100, "min constrained if manual entry" );
}); });
test("step, 2", function() { test( "step, 2", function() {
var el = $("#spin").spinner({ step: 2 }); expect( 4 );
equals(el.val(), "0", "value initialized to"); var element = $( "#spin" ).spinner({ step: 2 });
equals( element.val(), "0", "value initialized to 0" );
for ( var i = 0 ; i < 5 ; i++ ) { element.spinner( "stepUp" );
spinner_simulateKeyDownUp(el, $.ui.keyCode.UP); equals( element.val(), "2", "stepUp" );
}
equals(el.val(), "10", "UP 5 times with step: 2");
el.spinner('value', '10.5'); element.spinner( "value", "10.5" );
equals(el.val(), "10.5", "value reset to"); equals( element.val(), "10.5", "value reset to 10.5" );
el.spinner('option', 'step', 2); element.spinner( "stepUp" );
for ( var i = 0 ; i < 5 ; i++ ) { equals( element.val(), "12.5", "stepUp" );
spinner_simulateKeyDownUp(el, $.ui.keyCode.UP);
}
equals(el.val(), "20.5", "UP 5 times with step: 2");
}); });
test("step, 0.7", function() { test( "step, 0.7", function() {
var el = $("#spin").spinner({ expect( 2 );
step: 0.7, var element = $("#spin").spinner({
numberFormat: "n1" step: 0.7
}); });
equals(el.val(), "0.0", "value initialized to"); equals( element.val(), "0", "value initialized to 0" );
spinner_simulateKeyDownUp(el, $.ui.keyCode.DOWN); element.spinner( "stepUp" );
equals(el.val(), "-0.7", "DOWN 1 time with step: 0.7"); equals( element.val(), "0.7", "stepUp" );
for ( var i = 0 ; i < 11 ; i++ ) {
spinner_simulateKeyDownUp(el, $.ui.keyCode.UP);
}
equals(el.val(), "7.0", "UP 11 times with step: 0.7");
}); });
test("value, default, specified in markup", function() { // TODO: test value
var el = $('#spin2').spinner();
equals(el.val(), 2, "starting value");
});
test("value, default, nothing specified", function() { })( jQuery );
var el = $('#spin').spinner();
equals(el.val(), 0, "starting value");
});
test("value, override", function() {
var el = $('#spin').spinner({ value: 100 });
equals(el.val(), 100, "starting value");
});
test("value, override markup", function() {
var el = $('#spin2').spinner({ value: 100 });
equals(el.val(), 100, "starting value");
});
test("value, override later", function() {
var el = $('#spin').spinner();
equals(el.val(), 0, "starting value");
el.spinner('option', 'value', 1000);
equals(el.val(), 1000, "value option changed and set as current value");
});
})(jQuery);