From a6c705420b7878dbf6b540be128485952ff26440 Mon Sep 17 00:00:00 2001 From: Keith Wood Date: Wed, 1 Apr 2009 10:15:29 +0000 Subject: [PATCH] Datepicker: Corrected unit tests for structural changes and end of month --- tests/unit/datepicker/datepicker_core.js | 61 +++++++++++++-------- tests/unit/datepicker/datepicker_options.js | 14 ++--- 2 files changed, 43 insertions(+), 32 deletions(-) diff --git a/tests/unit/datepicker/datepicker_core.js b/tests/unit/datepicker/datepicker_core.js index 87b7567b1..134480693 100644 --- a/tests/unit/datepicker/datepicker_core.js +++ b/tests/unit/datepicker/datepicker_core.js @@ -24,11 +24,16 @@ function equalsDateArray(a1, a2, message) { same(a1, a2, message); } +function addMonths(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; +} + function init(id, options) { $.datepicker.setDefaults($.datepicker.regional['']); - var inp = $(id); - inp.datepicker($.extend({duration: ''}, options || {})); - return inp; + return $(id).datepicker($.extend({duration: ''}, options || {})); } var PROP_NAME = 'datepicker'; @@ -38,9 +43,9 @@ var PROP_NAME = 'datepicker'; module("datepicker: core"); test('baseStructure', function() { - var dp = $('#ui-datepicker-div'); var inp = init('#inp'); inp.focus(); + var dp = $('#ui-datepicker-div'); var iframe = ($.browser.msie && parseInt($.browser.version) < 7); ok(dp.is(':visible'), 'Structure - datepicker visible'); ok(!dp.is('.ui-datepicker-rtl'), 'Structure - not right-to-left'); @@ -94,26 +99,32 @@ test('baseStructure', function() { inp = init('#inp', {numberOfMonths: 2}); inp.focus(); ok(dp.is('.ui-datepicker-multi'), 'Structure multi [2] - multi-month'); - equals(dp.children().length, 2 + (iframe ? 1 : 0), 'Structure multi [2] - child count'); - month = dp.children(':first'); - ok(month.is('div.ui-datepicker-group') && month.is('div.ui-datepicker-group-first'), 'Structure multi [2] - first month division'); - month = dp.children(':eq(1)'); - ok(month.is('div.ui-datepicker-group') && month.is('div.ui-datepicker-group-last'), 'Structure multi [2] - second month division'); + equals(dp.children().length, 3 + (iframe ? 1 : 0), 'Structure multi [2] - child count'); + var child = dp.children(':first'); + ok(child.is('div.ui-datepicker-group') && child.is('div.ui-datepicker-group-first'), 'Structure multi [2] - first month division'); + child = dp.children(':eq(1)'); + ok(child.is('div.ui-datepicker-group') && child.is('div.ui-datepicker-group-last'), 'Structure multi [2] - second month division'); + child = dp.children(':eq(2)'); + ok(child.is('div.ui-datepicker-row-break'), 'Structure multi [2] - row break'); inp.datepicker('hide').datepicker('destroy'); // Multi-month [2, 2] inp = init('#inp', {numberOfMonths: [2, 2]}); inp.focus(); ok(dp.is('.ui-datepicker-multi'), 'Structure multi - multi-month'); - equals(dp.children().length, 4 + (iframe ? 1 : 0), 'Structure multi [2,2] - child count'); - month = dp.children(':first'); - ok(month.is('div.ui-datepicker-group') && month.is('div.ui-datepicker-group-first'), 'Structure multi [2,2] - first month division'); - month = dp.children(':eq(1)'); - ok(month.is('div.ui-datepicker-group') && month.is('div.ui-datepicker-group-last'), 'Structure multi [2,2] - second month division'); - month = dp.children(':eq(2)'); - ok(month.is('div.ui-datepicker-group') && month.is('div.ui-datepicker-group-first'), 'Structure multi [2,2] - third month division'); - month = dp.children(':eq(3)'); - ok(month.is('div.ui-datepicker-group') && month.is('div.ui-datepicker-group-last'), 'Structure multi [2,2] - fourth month division'); + equals(dp.children().length, 6 + (iframe ? 1 : 0), 'Structure multi [2,2] - child count'); + child = dp.children(':first'); + ok(child.is('div.ui-datepicker-group') && child.is('div.ui-datepicker-group-first'), 'Structure multi [2,2] - first month division'); + child = dp.children(':eq(1)'); + ok(child.is('div.ui-datepicker-group') && child.is('div.ui-datepicker-group-last'), 'Structure multi [2,2] - second month division'); + child = dp.children(':eq(2)'); + ok(child.is('div.ui-datepicker-row-break'), 'Structure multi [2,2] - row break'); + child = dp.children(':eq(3)'); + ok(child.is('div.ui-datepicker-group') && child.is('div.ui-datepicker-group-first'), 'Structure multi [2,2] - third month division'); + child = dp.children(':eq(4)'); + ok(child.is('div.ui-datepicker-group') && child.is('div.ui-datepicker-group-last'), 'Structure multi [2,2] - fourth month division'); + child = dp.children(':eq(5)'); + ok(child.is('div.ui-datepicker-row-break'), 'Structure multi [2,2] - row break'); inp.datepicker('hide').datepicker('destroy'); // Inline @@ -126,7 +137,7 @@ test('baseStructure', function() { var header = dp.children(':first'); ok(header.is('div.ui-datepicker-header'), 'Structure inline - header division'); equals(header.children().length, 3, 'Structure inline - header child count'); - var table = month.children(':eq(1)'); + var table = dp.children(':eq(1)'); ok(table.is('table.ui-datepicker-calendar'), 'Structure inline - month table'); ok(table.children(':first').is('thead'), 'Structure inline - month table thead'); ok(table.children(':eq(1)').is('tbody'), 'Structure inline - month table body'); @@ -136,11 +147,13 @@ test('baseStructure', function() { inl = init('#inl', {numberOfMonths: 2}); dp = inl.children(); ok(dp.is('.ui-datepicker-inline') && dp.is('.ui-datepicker-multi'), 'Structure inline multi - main div'); - equals(dp.children().length, 2 + (iframe ? 1 : 0), 'Structure multi - child count'); - month = dp.children(':first'); - ok(month.is('div.ui-datepicker-group') && month.is('div.ui-datepicker-group-first'), 'Structure multi - first month division'); - month = dp.children(':eq(1)'); - ok(month.is('div.ui-datepicker-group') && month.is('div.ui-datepicker-group-last'), 'Structure multi - second month division'); + equals(dp.children().length, 3 + (iframe ? 1 : 0), 'Structure inline multi - child count'); + child = dp.children(':first'); + ok(child.is('div.ui-datepicker-group') && child.is('div.ui-datepicker-group-first'), 'Structure inline multi - first month division'); + child = dp.children(':eq(1)'); + ok(child.is('div.ui-datepicker-group') && child.is('div.ui-datepicker-group-last'), 'Structure inline multi - second month division'); + child = dp.children(':eq(2)'); + ok(child.is('div.ui-datepicker-row-break'), 'Structure inline multi - row break'); inl.datepicker('destroy'); }); diff --git a/tests/unit/datepicker/datepicker_options.js b/tests/unit/datepicker/datepicker_options.js index 412519a4b..882a72b22 100644 --- a/tests/unit/datepicker/datepicker_options.js +++ b/tests/unit/datepicker/datepicker_options.js @@ -187,13 +187,12 @@ test('defaultDate', function() { inp.datepicker('option', {defaultDate: ' -1 m '}). datepicker('hide').val('').datepicker('show'). simulate('keydown', {keyCode: $.simulate.VK_ENTER}); - date = new Date(); - date.setMonth(date.getMonth() - 1); + date = addMonths(new Date(), -1); equalsDate(inp.datepicker('getDate'), date, 'Default date -1 m'); inp.datepicker('option', {defaultDate: '+2M'}). datepicker('hide').val('').datepicker('show'). simulate('keydown', {keyCode: $.simulate.VK_ENTER}); - date.setMonth(date.getMonth() + 3); + date = addMonths(new Date(), 2); equalsDate(inp.datepicker('getDate'), date, 'Default date +2M'); inp.datepicker('option', {defaultDate: '-2y'}). datepicker('hide').val('').datepicker('show'). @@ -209,8 +208,7 @@ test('defaultDate', function() { inp.datepicker('option', {defaultDate: '+1M +10d'}). datepicker('hide').val('').datepicker('show'). simulate('keydown', {keyCode: $.simulate.VK_ENTER}); - date = new Date(); - date.setMonth(date.getMonth() + 1); + date = addMonths(new Date(), 1); date.setDate(date.getDate() + 10); equalsDate(inp.datepicker('getDate'), date, 'Default date +1M +10d'); date = new Date(2007, 1 - 1, 26); @@ -245,7 +243,8 @@ test('miscellaneous', function() { equals(dp.find('.ui-datepicker-prev').text(), 'Prev', 'Navigation prev - default'); equals(dp.find('.ui-datepicker-current').text(), 'Today', 'Navigation current - default'); equals(dp.find('.ui-datepicker-next').text(), 'Next', 'Navigation next - default'); - inp.datepicker('hide').datepicker('option', {navigationAsDateFormat: true, prevText: '< M', currentText: 'MM', nextText: 'M >'}).val('02/04/2008').datepicker('show'); + inp.datepicker('hide').datepicker('option', {navigationAsDateFormat: true, prevText: '< M', currentText: 'MM', nextText: 'M >'}). + val('02/04/2008').datepicker('show'); var longNames = $.datepicker.regional[''].monthNames; var shortNames = $.datepicker.regional[''].monthNamesShort; var date = new Date(); @@ -329,8 +328,7 @@ test('minMax', function() { simulate('keydown', {keyCode: $.simulate.VK_ENTER}); equalsDate(inp.datepicker('getDate'), date, 'Min/max - -1w, +1 M +10 D - ctrl+pgup'); - date = new Date(); - date.setMonth(date.getMonth() + 1); + date = addMonths(new Date(), 1); date.setDate(date.getDate() + 10); inp.val('').datepicker('show'); inp.simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_PGDN}).