From: Felix Nagel Date: Sun, 22 May 2016 19:12:10 +0000 (+0200) Subject: Calendar tests: Add dateEqual assertion, clean up helper methods X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=85fe55335cb09db4caff378e9cc4a433e4e6cd27;p=jquery-ui.git Calendar tests: Add dateEqual assertion, clean up helper methods --- diff --git a/tests/lib/qunit-assert-dateequal.js b/tests/lib/qunit-assert-dateequal.js new file mode 100644 index 000000000..7a997003b --- /dev/null +++ b/tests/lib/qunit-assert-dateequal.js @@ -0,0 +1,22 @@ +/* + * Assertion for comparing Date objects by day, month and year. + */ +define( [ + "qunit" +], function( QUnit ) { + +QUnit.assert.dateEqual = function( value, expected, message ) { + + if ( !value || !expected ) { + this.push( false, value, expected, + "dateEqual failed, missing date object, message was: " + message ); + return; + } + + var newValue = new Date( value.getFullYear(), value.getMonth(), value.getDate() ), + newExpected = new Date( expected.getFullYear(), expected.getMonth(), expected.getDate() ); + + this.push( ( newValue.toString() === newExpected.toString() ), value, expected, message ); +}; + +} ); diff --git a/tests/lib/qunit.js b/tests/lib/qunit.js index e2a6e85c7..fe5c1d746 100644 --- a/tests/lib/qunit.js +++ b/tests/lib/qunit.js @@ -4,6 +4,7 @@ define( [ "qunit-assert-classes", "qunit-assert-close", "lib/qunit-assert-domequal", + "lib/qunit-assert-dateequal", "phantom-bridge" ], function( QUnit, $ ) { diff --git a/tests/unit/calendar/core.js b/tests/unit/calendar/core.js index 65daf1bd4..7945d0891 100644 --- a/tests/unit/calendar/core.js +++ b/tests/unit/calendar/core.js @@ -137,7 +137,7 @@ test( "Localization", function() { testLocalization( "After init: " ); } ); -asyncTest( "keyboard handling", function() { +asyncTest( "keyboard handling", function( assert ) { expect( 10 ); var element = $( "#calendar" ); @@ -150,7 +150,7 @@ asyncTest( "keyboard handling", function() { .simulate( "keydown", { keyCode: $.ui.keyCode.LEFT } ); setTimeout( function() { $( document.activeElement ).simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } ); - testHelper.equalsDate( + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2013, 12 - 1, 31 ), "Keystroke left to switch to previous day" @@ -167,7 +167,7 @@ asyncTest( "keyboard handling", function() { .simulate( "keydown", { keyCode: $.ui.keyCode.RIGHT } ) .simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } ); - testHelper.equalsDate( + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2014, 1 - 1, 2 ), "Keystroke right to switch to next day" @@ -181,7 +181,7 @@ asyncTest( "keyboard handling", function() { testHelper.focusGrid( element ).simulate( "keydown", { keyCode: $.ui.keyCode.UP } ); setTimeout( function() { $( document.activeElement ).simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } ); - testHelper.equalsDate( + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2013, 12 - 1, 25 ), "Keystroke up to move to the previous week" @@ -197,7 +197,7 @@ asyncTest( "keyboard handling", function() { testHelper.focusGrid( element ).simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } ); setTimeout( function() { $( document.activeElement ).simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } ); - testHelper.equalsDate( + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2014, 1 - 1, 8 ), "Keystroke down to move to the next week" @@ -213,7 +213,7 @@ asyncTest( "keyboard handling", function() { testHelper.focusGrid( element ).simulate( "keydown", { keyCode: $.ui.keyCode.PAGE_UP } ); setTimeout( function() { $( document.activeElement ).simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } ); - testHelper.equalsDate( + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2013, 12 - 1, 1 ), "Keystroke Page Up moves date to previous month" @@ -230,7 +230,7 @@ asyncTest( "keyboard handling", function() { .simulate( "keydown", { keyCode: $.ui.keyCode.PAGE_UP, altKey: true } ); setTimeout( function() { $( document.activeElement ).simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } ); - testHelper.equalsDate( + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2013, 1 - 1, 1 ), "Keystroke Page Up + ALT moves date to previous year" @@ -246,7 +246,7 @@ asyncTest( "keyboard handling", function() { testHelper.focusGrid( element ).simulate( "keydown", { keyCode: $.ui.keyCode.PAGE_DOWN } ); setTimeout( function() { $( document.activeElement ).simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } ); - testHelper.equalsDate( + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2014, 2 - 1, 1 ), "Keystroke Page Down moves date to next month" @@ -263,7 +263,7 @@ asyncTest( "keyboard handling", function() { .simulate( "keydown", { keyCode: $.ui.keyCode.PAGE_DOWN, altKey: true } ); setTimeout( function() { $( document.activeElement ).simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } ); - testHelper.equalsDate( + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2015, 1 - 1, 1 ), "Keystroke Page Down + ALT moves date to next year" @@ -280,7 +280,7 @@ asyncTest( "keyboard handling", function() { testHelper.focusGrid( element ).simulate( "keydown", { keyCode: $.ui.keyCode.PAGE_UP } ); setTimeout( function() { $( document.activeElement ).simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } ); - testHelper.equalsDate( + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2014, 2 - 1, 28 ), "Keystroke Page Up and short months" @@ -296,7 +296,7 @@ asyncTest( "keyboard handling", function() { testHelper.focusGrid( element ).simulate( "keydown", { keyCode: $.ui.keyCode.PAGE_DOWN } ); setTimeout( function() { $( document.activeElement ).simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } ); - testHelper.equalsDate( + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2016, 2 - 1, 29 ), "Keystroke Page Down and leap years" @@ -309,7 +309,7 @@ asyncTest( "keyboard handling", function() { step1(); } ); -asyncTest( "mouse", function() { +asyncTest( "mouse", function( assert ) { expect( 6 ); var element = $( "#calendar" ).calendar(), @@ -318,7 +318,7 @@ asyncTest( "mouse", function() { function step1() { $( "tbody button:contains(10)", element ).simulate( "mousedown" ); date.setDate( 10 ); - testHelper.equalsDate( + assert.dateEqual( element.calendar( "valueAsDate" ), date, "Mouse click" @@ -326,7 +326,7 @@ asyncTest( "mouse", function() { element.calendar( "option", "value", new Date( 2008, 2 - 1, 4 ) ); $( ".ui-calendar-calendar tbody button:contains(12)", element ).simulate( "mousedown" ); - testHelper.equalsDate( + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2008, 2 - 1, 12 ), "Mouse click - preset" @@ -336,7 +336,7 @@ asyncTest( "mouse", function() { element.calendar( "option", "value", new Date( 2008, 2 - 1, 4 ) ); $( ".ui-calendar-prev", element ).simulate( "click" ); $( ".ui-calendar-calendar tbody button:contains(16)", element ).simulate( "mousedown" ); - testHelper.equalsDate( + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2008, 1 - 1, 16 ), "Mouse click - previous" @@ -345,7 +345,7 @@ asyncTest( "mouse", function() { element.calendar( "option", "value", new Date( 2008, 2 - 1, 4 ) ); $( ".ui-calendar-next", element ).simulate( "click" ); $( ".ui-calendar-calendar tbody button:contains(18)", element ).simulate( "mousedown" ); - testHelper.equalsDate( + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2008, 3 - 1, 18 ), "Mouse click - next" @@ -365,7 +365,7 @@ asyncTest( "mouse", function() { $( ".ui-calendar-prev", element ).simulate( "click" ); $( "tbody button:contains(16)", element ).simulate( "mousedown" ); - testHelper.equalsDate( + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2008, 2 - 1, 16 ), "Mouse click - previous + min/max" @@ -383,7 +383,7 @@ asyncTest( "mouse", function() { $( ".ui-calendar-next", element ).simulate( "click" ); $( "tbody button:contains(18)", element ).simulate( "mousedown" ); - testHelper.equalsDate( + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2008, 2 - 1, 18 ), "Mouse click - next + min/max" diff --git a/tests/unit/calendar/helper.js b/tests/unit/calendar/helper.js index a82e96705..b5feff5a6 100644 --- a/tests/unit/calendar/helper.js +++ b/tests/unit/calendar/helper.js @@ -4,21 +4,6 @@ define( [ ], function( $, helper ) { return $.extend( helper, { - addMonths: function( date, offset ) { - var maxDay = 32 - new Date( date.getFullYear(), date.getMonth() + offset, 32 ).getDate(); - date.setDate( Math.min( date.getDate(), maxDay ) ); - date.setMonth( date.getMonth() + offset ); - return date; - }, - equalsDate: function( d1, d2, message ) { - if ( !d1 || !d2 ) { - ok( false, message + " - missing date" ); - return; - } - d1 = new Date( d1.getFullYear(), d1.getMonth(), d1.getDate() ); - d2 = new Date( d2.getFullYear(), d2.getMonth(), d2.getDate() ); - equal( d1.toString(), d2.toString(), message ); - }, focusGrid: function( element ) { element.find( ":tabbable" ).last().simulate( "focus" ); $( document.activeElement ).simulate( "keydown", { keyCode: $.ui.keyCode.TAB } ); diff --git a/tests/unit/calendar/methods.js b/tests/unit/calendar/methods.js index 84e265710..d5a4ba4a2 100644 --- a/tests/unit/calendar/methods.js +++ b/tests/unit/calendar/methods.js @@ -1,8 +1,7 @@ define( [ "jquery", - "./helper", "ui/widgets/calendar" -], function( $, testHelper ) { +], function( $ ) { module( "calendar: methods" ); @@ -56,7 +55,7 @@ test( "value", function() { equal( element.calendar( "value" ), "1/1/14", "Setting invalid values should be ignored." ); } ); -test( "valueAsDate", function() { +test( "valueAsDate", function( assert ) { expect( 11 ); var minDate, maxDate, dateAndTimeToSet, dateAndTimeClone, @@ -69,7 +68,7 @@ test( "valueAsDate", function() { .hasClass( "ui-state-active" ), "First day marked as selected" ); - testHelper.equalsDate( element.calendar( "valueAsDate" ), new Date( 2014, 0, 1 ), "Getter" ); + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2014, 0, 1 ), "Getter" ); element.calendar( "destroy" ); @@ -77,7 +76,7 @@ test( "valueAsDate", function() { equal( element.calendar( "valueAsDate" ), null, "Set date - default" ); element.calendar( "valueAsDate", date1 ); - testHelper.equalsDate( element.calendar( "valueAsDate" ), date1, "Set date - 2008-06-04" ); + assert.dateEqual( element.calendar( "valueAsDate" ), date1, "Set date - 2008-06-04" ); // With minimum/maximum element = $( "#calendar" ).calendar(); @@ -89,13 +88,13 @@ test( "valueAsDate", function() { element .calendar( "option", { min: minDate } ) .calendar( "valueAsDate", date2 ); - testHelper.equalsDate( + assert.dateEqual( element.calendar( "valueAsDate" ), date2, "Set date min/max - value > min" ); element.calendar( "valueAsDate", date1 ); - testHelper.equalsDate( + assert.dateEqual( element.calendar( "valueAsDate" ), date2, "Set date min/max - value < min" @@ -104,14 +103,14 @@ test( "valueAsDate", function() { element .calendar( "option", { max: maxDate, min: null } ) .calendar( "valueAsDate", date1 ); - testHelper.equalsDate( + assert.dateEqual( element.calendar( "valueAsDate" ), date1, "Set date min/max - value < max" ); element.calendar( "valueAsDate", date2 ); - testHelper.equalsDate( + assert.dateEqual( element.calendar( "valueAsDate" ), date1, "Set date min/max - value > max" @@ -120,14 +119,14 @@ test( "valueAsDate", function() { element .calendar( "option", { min: minDate } ) .calendar( "valueAsDate", date1 ); - testHelper.equalsDate( + assert.dateEqual( element.calendar( "valueAsDate" ), date1, "Set date min/max - value < min" ); element.calendar( "valueAsDate", date2 ); - testHelper.equalsDate( + assert.dateEqual( element.calendar( "valueAsDate" ), date1, "Set date min/max - value > max" ); diff --git a/tests/unit/calendar/options.js b/tests/unit/calendar/options.js index c407f096a..2e383855b 100644 --- a/tests/unit/calendar/options.js +++ b/tests/unit/calendar/options.js @@ -1,8 +1,7 @@ define( [ "jquery", - "./helper", "ui/widgets/calendar" -], function( $, testHelper ) { +], function( $ ) { module( "calendar: options" ); @@ -188,7 +187,7 @@ test( "showWeek", function() { equal( container.find( "thead th" ).length, 8, "supports changing option after init" ); } ); -test( "min / max", function() { +test( "min / max", function( assert ) { expect( 17 ); // With existing date @@ -202,38 +201,38 @@ test( "min / max", function() { element .calendar( "option", { min: minDate } ) .calendar( "value", "6/4/08" ); - testHelper.equalsDate( element.calendar( "valueAsDate" ), new Date( 2008, 6 - 1, 4 ), "Min/max - value > min" ); + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2008, 6 - 1, 4 ), "Min/max - value > min" ); element .calendar( "option", { min: minDate } ) .calendar( "value", "1/4/08" ); - testHelper.equalsDate( element.calendar( "valueAsDate" ), new Date( 2008, 6 - 1, 4 ), "Min/max - value < min" ); + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2008, 6 - 1, 4 ), "Min/max - value < min" ); element .calendar( "option", { min: null } ) .calendar( "value", "6/4/08" ) .calendar( "option", { max: maxDate } ); - testHelper.equalsDate( element.calendar( "valueAsDate" ), new Date( 2008, 6 - 1, 4 ), "Min/max - value < max" ); + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2008, 6 - 1, 4 ), "Min/max - value < max" ); element .calendar( "option", { max: maxDate } ) .calendar( "value", "1/4/09" ); - testHelper.equalsDate( element.calendar( "valueAsDate" ), new Date( 2008, 6 - 1, 4 ), "Min/max - setDate > max" ); + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2008, 6 - 1, 4 ), "Min/max - setDate > max" ); element .calendar( "option", { min: minDate, max: maxDate } ) .calendar( "value", "1/4/08" ); - testHelper.equalsDate( element.calendar( "valueAsDate" ), new Date( 2008, 6 - 1, 4 ), "Min/max - value < min" ); + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2008, 6 - 1, 4 ), "Min/max - value < min" ); element .calendar( "option", { min: minDate, max: maxDate } ) .calendar( "value", "6/4/08" ); - testHelper.equalsDate( element.calendar( "valueAsDate" ), new Date( 2008, 6 - 1, 4 ), "Min/max - value > min, < max" ); + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2008, 6 - 1, 4 ), "Min/max - value > min, < max" ); element .calendar( "option", { min: minDate, max: maxDate } ) .calendar( "value", "1/4/09" ); - testHelper.equalsDate( element.calendar( "valueAsDate" ), new Date( 2008, 6 - 1, 4 ), "Min/max - value > max" ); + assert.dateEqual( element.calendar( "valueAsDate" ), new Date( 2008, 6 - 1, 4 ), "Min/max - value > max" ); element .calendar( "option", { min: minDate, max: maxDate } ) diff --git a/tests/unit/datepicker/core.js b/tests/unit/datepicker/core.js index 337032075..f884e9680 100644 --- a/tests/unit/datepicker/core.js +++ b/tests/unit/datepicker/core.js @@ -41,7 +41,7 @@ asyncTest( "base structure", function() { }, 50 ); } ); -asyncTest( "Keyboard handling: input", function() { +asyncTest( "Keyboard handling: input", function( assert ) { expect( 10 ); var picker, instance, input = $( "#datepicker" ).datepicker(); @@ -84,7 +84,7 @@ asyncTest( "Keyboard handling: input", function() { .datepicker( "refresh" ) .datepicker( "open" ) .simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } ); - testHelper.equalsDate( input.datepicker( "valueAsDate" ), new Date( 2014, 0, 1 ), + assert.dateEqual( input.datepicker( "valueAsDate" ), new Date( 2014, 0, 1 ), "Keystroke enter - preset" ); input @@ -99,7 +99,7 @@ asyncTest( "Keyboard handling: input", function() { .val( "1/1/14" ) .datepicker( "open" ) .simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } ); - testHelper.equalsDate( input.datepicker( "valueAsDate" ), new Date( 2014, 0, 1 ), + assert.dateEqual( input.datepicker( "valueAsDate" ), new Date( 2014, 0, 1 ), "Keystroke esc - preset" ); input @@ -107,13 +107,13 @@ asyncTest( "Keyboard handling: input", function() { .datepicker( "open" ) .simulate( "keydown", { ctrlKey: true, keyCode: $.ui.keyCode.PAGE_UP } ) .simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } ); - testHelper.equalsDate( input.datepicker( "valueAsDate" ), new Date( 2014, 0, 1 ), + assert.dateEqual( input.datepicker( "valueAsDate" ), new Date( 2014, 0, 1 ), "Keystroke esc - abandoned" ); input .val( "1/2/14" ) .simulate( "keyup" ); - testHelper.equalsDate( input.datepicker( "valueAsDate" ), new Date( 2014, 0, 2 ), + assert.dateEqual( input.datepicker( "valueAsDate" ), new Date( 2014, 0, 2 ), "Picker updated as user types into input" ); input.datepicker( "destroy" ); @@ -128,7 +128,7 @@ test( "ARIA", function() { expect( 0 ); } ); -asyncTest( "mouse", function() { +asyncTest( "mouse", function( assert ) { expect( 4 ); var input = testHelper.init( $( "#datepicker" ).val( "" ) ), @@ -139,7 +139,7 @@ asyncTest( "mouse", function() { setTimeout( function() { input.val( "4/4/08" ).datepicker( "refresh" ).datepicker( "open" ); $( ".ui-calendar-calendar tbody button:contains(12)", picker ).simulate( "mousedown", {} ); - testHelper.equalsDate( + assert.dateEqual( input.datepicker( "valueAsDate" ), new Date( 2008, 4 - 1, 12 ), "Mouse click - preset" @@ -151,7 +151,7 @@ asyncTest( "mouse", function() { input.val( "4/4/08" ).datepicker( "refresh" ).datepicker( "open" ); input.simulate( "click" ); - testHelper.equalsDate( + assert.dateEqual( input.datepicker( "valueAsDate" ), new Date( 2008, 4 - 1, 4 ), "Mouse click - close + preset" @@ -160,7 +160,7 @@ asyncTest( "mouse", function() { input.val( "4/4/08" ).datepicker( "refresh" ).datepicker( "open" ); picker.find( "a.ui-calendar-prev" ).simulate( "click" ); input.simulate( "click" ); - testHelper.equalsDate( + assert.dateEqual( input.datepicker( "valueAsDate" ), new Date( 2008, 4 - 1, 4 ), "Mouse click - abandoned" diff --git a/tests/unit/datepicker/helper.js b/tests/unit/datepicker/helper.js index 81fdd9d00..da75cf770 100644 --- a/tests/unit/datepicker/helper.js +++ b/tests/unit/datepicker/helper.js @@ -5,21 +5,6 @@ define( [ ], function( $, helper ) { return $.extend( helper, { - addMonths: function( date, offset ) { - var maxDay = 32 - new Date( date.getFullYear(), date.getMonth() + offset, 32 ).getDate(); - date.setDate( Math.min( date.getDate(), maxDay ) ); - date.setMonth( date.getMonth() + offset ); - return date; - }, - equalsDate: function( d1, d2, message ) { - if ( !d1 || !d2 ) { - ok( false, message + " - missing date" ); - return; - } - d1 = new Date( d1.getFullYear(), d1.getMonth(), d1.getDate() ); - d2 = new Date( d2.getFullYear(), d2.getMonth(), d2.getDate() ); - equal( d1.toString(), d2.toString(), message ); - }, init: function( id, options ) { options = $.extend( { show: false, hide: false }, options || {} ); return $( id ).datepicker( options ); diff --git a/tests/unit/datepicker/methods.js b/tests/unit/datepicker/methods.js index 2cf835698..a274de3f9 100644 --- a/tests/unit/datepicker/methods.js +++ b/tests/unit/datepicker/methods.js @@ -86,7 +86,7 @@ test( "value", function() { strictEqual( input.datepicker( "value" ), null, "Invalid values should return null." ); } ); -test( "valueAsDate", function() { +test( "valueAsDate", function( assert ) { expect( 6 ); var input = testHelper.init( "#datepicker" ), @@ -99,7 +99,7 @@ test( "valueAsDate", function() { picker.find( "button[data-timestamp]" ).eq( 0 ).hasClass( "ui-state-active" ), "First day marked as selected" ); - testHelper.equalsDate( input.datepicker( "valueAsDate" ), new Date( 2014, 0, 1 ), "Getter" ); + assert.dateEqual( input.datepicker( "valueAsDate" ), new Date( 2014, 0, 1 ), "Getter" ); input.val( "a/b/c" ); equal( input.datepicker( "valueAsDate" ), null, "Invalid dates return null" ); @@ -109,7 +109,7 @@ test( "valueAsDate", function() { strictEqual( input.datepicker( "valueAsDate" ), null, "Set date - default" ); input.datepicker( "valueAsDate", date1 ); - testHelper.equalsDate( input.datepicker( "valueAsDate" ), date1, "Set date - 2008-06-04" ); + assert.dateEqual( input.datepicker( "valueAsDate" ), date1, "Set date - 2008-06-04" ); } ); test( "isValid", function() {