From 160a301d96c09403b87ef8365b201617344e425d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Scott=20Gonz=C3=A1lez?= Date: Sat, 6 Aug 2011 17:07:12 -0400 Subject: [PATCH] Spinner tests: Cleanup. --- tests/unit/spinner/spinner.html | 1 + tests/unit/spinner/spinner_core.js | 268 ++++++++++----------- tests/unit/spinner/spinner_events.js | 8 +- tests/unit/spinner/spinner_methods.js | 8 +- tests/unit/spinner/spinner_options.js | 8 +- tests/unit/spinner/spinner_test_helpers.js | 5 + 6 files changed, 142 insertions(+), 156 deletions(-) create mode 100644 tests/unit/spinner/spinner_test_helpers.js diff --git a/tests/unit/spinner/spinner.html b/tests/unit/spinner/spinner.html index b59275e5f..59185a441 100644 --- a/tests/unit/spinner/spinner.html +++ b/tests/unit/spinner/spinner.html @@ -19,6 +19,7 @@ + diff --git a/tests/unit/spinner/spinner_core.js b/tests/unit/spinner/spinner_core.js index 19bdb3495..b05a6630f 100644 --- a/tests/unit/spinner/spinner_core.js +++ b/tests/unit/spinner/spinner_core.js @@ -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" ); + $( "#spin" ).val( 0 ); + domEqual( "#spin", function() { + $( "#spin" ).spinner().spinner( "destroy" ); + }); }); -test("keydown UP on input, increases value not greater than max", function() { - var el = $("#spin").spinner({ - max:100, - value:50, - step:10 +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.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); + + 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 DOWN on input, decreases value not less than min", function() { - var el = $("#spin").spinner({ - min:-100, - value:50, - 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.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); + 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("keydown PGUP on input, increases value not greater than max", function() { - var el = $("#spin").spinner({ - max: 500, - value: 0, - step: 10 +test( "keydown PGUP on input, increases value not greater than max", function() { + expect( 5 ); + var element = $( "#spin" ).spinner({ + max: 100, + value: 70, + page: 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.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() { - var el = $("#spin").spinner({ - min:-500, - value:0, - step:10 +test( "keydown PGDN on input, decreases value not less than min", function() { + expect( 5 ); + var element = $( "#spin" ).spinner({ + min: 20, + value: 50, + page: 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.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() { - var el = $("#spin").spinner(), +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-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-up" ).trigger( "mousedown" ).trigger( "mouseup" ); + equals( element.val(), ++val, "mouse click on up" ); + + $( ".ui-spinner-down" ).trigger( "mousedown" ).trigger( "mouseup" ); + equals( element.val(), --val, "mouse click on down"); }); -test("mouse wheel on input", function() { - expect(4); - stop(); - - var el = $("#spin").spinner(); - equal(el.val(), 0); - el.trigger("mousewheel", 1); - equal(el.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); - +test( "mouse wheel on input", function() { + expect( 4 ); + + var element = $( "#spin" ).spinner(); + equal( element.val(), 0 ); + element.trigger( "mousewheel", 1 ); + equal( element.val(), 1 ); + + element.trigger( "mousewheel", -1 ); + equal( element.val(), 0 ); + + element.trigger( "mousewheel", -1 ); + equal(element.val(), -1 ); }); -test("reading HTML5 attributes", function() { - var el = $('').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 = $( "" ).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 });; - - 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'); - - el.spinner('stepUp'); - - equals(el.attr('aria-valuenow'), 3, 'stepUp 1 step changes aria-valuenow'); - - el.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'); +test( "ARIA attributes", function() { + expect( 7 ); + var element = $( "#spin" ).spinner({ min: -5, max: 5, value: 2 }); + + 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" ); + + element.spinner( "stepUp" ); + + equals( element.attr( "aria-valuenow" ), 3, "stepUp 1 step changes aria-valuenow" ); + + element.spinner( "option", { min: -10, max: 10 } ); + + 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 ); diff --git a/tests/unit/spinner/spinner_events.js b/tests/unit/spinner/spinner_events.js index 0f64067d7..f70ac5076 100644 --- a/tests/unit/spinner/spinner_events.js +++ b/tests/unit/spinner/spinner_events.js @@ -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"); diff --git a/tests/unit/spinner/spinner_methods.js b/tests/unit/spinner/spinner_methods.js index fb58edbdf..b47f68a5c 100644 --- a/tests/unit/spinner/spinner_methods.js +++ b/tests/unit/spinner/spinner_methods.js @@ -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'); diff --git a/tests/unit/spinner/spinner_options.js b/tests/unit/spinner/spinner_options.js index 398780ad2..2cb90ef5d 100644 --- a/tests/unit/spinner/spinner_options.js +++ b/tests/unit/spinner/spinner_options.js @@ -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"); }); diff --git a/tests/unit/spinner/spinner_test_helpers.js b/tests/unit/spinner/spinner_test_helpers.js new file mode 100644 index 000000000..af555dbf2 --- /dev/null +++ b/tests/unit/spinner/spinner_test_helpers.js @@ -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 } ); +}; -- 2.39.5