Datepicker: Corrected unit tests for structural changes and end of month

This commit is contained in:
Keith Wood 2009-04-01 10:15:29 +00:00
parent 4d5f8d4ae0
commit a6c705420b
2 changed files with 43 additions and 32 deletions

View File

@ -24,11 +24,16 @@ function equalsDateArray(a1, a2, message) {
same(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) { function init(id, options) {
$.datepicker.setDefaults($.datepicker.regional['']); $.datepicker.setDefaults($.datepicker.regional['']);
var inp = $(id); return $(id).datepicker($.extend({duration: ''}, options || {}));
inp.datepicker($.extend({duration: ''}, options || {}));
return inp;
} }
var PROP_NAME = 'datepicker'; var PROP_NAME = 'datepicker';
@ -38,9 +43,9 @@ var PROP_NAME = 'datepicker';
module("datepicker: core"); module("datepicker: core");
test('baseStructure', function() { test('baseStructure', function() {
var dp = $('#ui-datepicker-div');
var inp = init('#inp'); var inp = init('#inp');
inp.focus(); inp.focus();
var dp = $('#ui-datepicker-div');
var iframe = ($.browser.msie && parseInt($.browser.version) < 7); var iframe = ($.browser.msie && parseInt($.browser.version) < 7);
ok(dp.is(':visible'), 'Structure - datepicker visible'); ok(dp.is(':visible'), 'Structure - datepicker visible');
ok(!dp.is('.ui-datepicker-rtl'), 'Structure - not right-to-left'); ok(!dp.is('.ui-datepicker-rtl'), 'Structure - not right-to-left');
@ -94,26 +99,32 @@ test('baseStructure', function() {
inp = init('#inp', {numberOfMonths: 2}); inp = init('#inp', {numberOfMonths: 2});
inp.focus(); inp.focus();
ok(dp.is('.ui-datepicker-multi'), 'Structure multi [2] - multi-month'); ok(dp.is('.ui-datepicker-multi'), 'Structure multi [2] - multi-month');
equals(dp.children().length, 2 + (iframe ? 1 : 0), 'Structure multi [2] - child count'); equals(dp.children().length, 3 + (iframe ? 1 : 0), 'Structure multi [2] - child count');
month = dp.children(':first'); var child = dp.children(':first');
ok(month.is('div.ui-datepicker-group') && month.is('div.ui-datepicker-group-first'), 'Structure multi [2] - first month division'); ok(child.is('div.ui-datepicker-group') && child.is('div.ui-datepicker-group-first'), 'Structure multi [2] - first month division');
month = dp.children(':eq(1)'); child = dp.children(':eq(1)');
ok(month.is('div.ui-datepicker-group') && month.is('div.ui-datepicker-group-last'), 'Structure multi [2] - second month division'); 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'); inp.datepicker('hide').datepicker('destroy');
// Multi-month [2, 2] // Multi-month [2, 2]
inp = init('#inp', {numberOfMonths: [2, 2]}); inp = init('#inp', {numberOfMonths: [2, 2]});
inp.focus(); inp.focus();
ok(dp.is('.ui-datepicker-multi'), 'Structure multi - multi-month'); ok(dp.is('.ui-datepicker-multi'), 'Structure multi - multi-month');
equals(dp.children().length, 4 + (iframe ? 1 : 0), 'Structure multi [2,2] - child count'); equals(dp.children().length, 6 + (iframe ? 1 : 0), 'Structure multi [2,2] - child count');
month = dp.children(':first'); child = dp.children(':first');
ok(month.is('div.ui-datepicker-group') && month.is('div.ui-datepicker-group-first'), 'Structure multi [2,2] - first month division'); ok(child.is('div.ui-datepicker-group') && child.is('div.ui-datepicker-group-first'), 'Structure multi [2,2] - first month division');
month = dp.children(':eq(1)'); child = 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'); ok(child.is('div.ui-datepicker-group') && child.is('div.ui-datepicker-group-last'), 'Structure multi [2,2] - second month division');
month = dp.children(':eq(2)'); child = 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'); ok(child.is('div.ui-datepicker-row-break'), 'Structure multi [2,2] - row break');
month = dp.children(':eq(3)'); child = 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'); 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'); inp.datepicker('hide').datepicker('destroy');
// Inline // Inline
@ -126,7 +137,7 @@ test('baseStructure', function() {
var header = dp.children(':first'); var header = dp.children(':first');
ok(header.is('div.ui-datepicker-header'), 'Structure inline - header division'); ok(header.is('div.ui-datepicker-header'), 'Structure inline - header division');
equals(header.children().length, 3, 'Structure inline - header child count'); 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.is('table.ui-datepicker-calendar'), 'Structure inline - month table');
ok(table.children(':first').is('thead'), 'Structure inline - month table thead'); ok(table.children(':first').is('thead'), 'Structure inline - month table thead');
ok(table.children(':eq(1)').is('tbody'), 'Structure inline - month table body'); ok(table.children(':eq(1)').is('tbody'), 'Structure inline - month table body');
@ -136,11 +147,13 @@ test('baseStructure', function() {
inl = init('#inl', {numberOfMonths: 2}); inl = init('#inl', {numberOfMonths: 2});
dp = inl.children(); dp = inl.children();
ok(dp.is('.ui-datepicker-inline') && dp.is('.ui-datepicker-multi'), 'Structure inline multi - main div'); 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'); equals(dp.children().length, 3 + (iframe ? 1 : 0), 'Structure inline multi - child count');
month = dp.children(':first'); child = dp.children(':first');
ok(month.is('div.ui-datepicker-group') && month.is('div.ui-datepicker-group-first'), 'Structure multi - first month division'); ok(child.is('div.ui-datepicker-group') && child.is('div.ui-datepicker-group-first'), 'Structure inline multi - first month division');
month = dp.children(':eq(1)'); child = dp.children(':eq(1)');
ok(month.is('div.ui-datepicker-group') && month.is('div.ui-datepicker-group-last'), 'Structure multi - second month division'); 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'); inl.datepicker('destroy');
}); });

View File

@ -187,13 +187,12 @@ test('defaultDate', function() {
inp.datepicker('option', {defaultDate: ' -1 m '}). inp.datepicker('option', {defaultDate: ' -1 m '}).
datepicker('hide').val('').datepicker('show'). datepicker('hide').val('').datepicker('show').
simulate('keydown', {keyCode: $.simulate.VK_ENTER}); simulate('keydown', {keyCode: $.simulate.VK_ENTER});
date = new Date(); date = addMonths(new Date(), -1);
date.setMonth(date.getMonth() - 1);
equalsDate(inp.datepicker('getDate'), date, 'Default date -1 m'); equalsDate(inp.datepicker('getDate'), date, 'Default date -1 m');
inp.datepicker('option', {defaultDate: '+2M'}). inp.datepicker('option', {defaultDate: '+2M'}).
datepicker('hide').val('').datepicker('show'). datepicker('hide').val('').datepicker('show').
simulate('keydown', {keyCode: $.simulate.VK_ENTER}); simulate('keydown', {keyCode: $.simulate.VK_ENTER});
date.setMonth(date.getMonth() + 3); date = addMonths(new Date(), 2);
equalsDate(inp.datepicker('getDate'), date, 'Default date +2M'); equalsDate(inp.datepicker('getDate'), date, 'Default date +2M');
inp.datepicker('option', {defaultDate: '-2y'}). inp.datepicker('option', {defaultDate: '-2y'}).
datepicker('hide').val('').datepicker('show'). datepicker('hide').val('').datepicker('show').
@ -209,8 +208,7 @@ test('defaultDate', function() {
inp.datepicker('option', {defaultDate: '+1M +10d'}). inp.datepicker('option', {defaultDate: '+1M +10d'}).
datepicker('hide').val('').datepicker('show'). datepicker('hide').val('').datepicker('show').
simulate('keydown', {keyCode: $.simulate.VK_ENTER}); simulate('keydown', {keyCode: $.simulate.VK_ENTER});
date = new Date(); date = addMonths(new Date(), 1);
date.setMonth(date.getMonth() + 1);
date.setDate(date.getDate() + 10); date.setDate(date.getDate() + 10);
equalsDate(inp.datepicker('getDate'), date, 'Default date +1M +10d'); equalsDate(inp.datepicker('getDate'), date, 'Default date +1M +10d');
date = new Date(2007, 1 - 1, 26); 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-prev').text(), 'Prev', 'Navigation prev - default');
equals(dp.find('.ui-datepicker-current').text(), 'Today', 'Navigation current - default'); equals(dp.find('.ui-datepicker-current').text(), 'Today', 'Navigation current - default');
equals(dp.find('.ui-datepicker-next').text(), 'Next', 'Navigation next - 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 longNames = $.datepicker.regional[''].monthNames;
var shortNames = $.datepicker.regional[''].monthNamesShort; var shortNames = $.datepicker.regional[''].monthNamesShort;
var date = new Date(); var date = new Date();
@ -329,8 +328,7 @@ test('minMax', function() {
simulate('keydown', {keyCode: $.simulate.VK_ENTER}); simulate('keydown', {keyCode: $.simulate.VK_ENTER});
equalsDate(inp.datepicker('getDate'), date, equalsDate(inp.datepicker('getDate'), date,
'Min/max - -1w, +1 M +10 D - ctrl+pgup'); 'Min/max - -1w, +1 M +10 D - ctrl+pgup');
date = new Date(); date = addMonths(new Date(), 1);
date.setMonth(date.getMonth() + 1);
date.setDate(date.getDate() + 10); date.setDate(date.getDate() + 10);
inp.val('').datepicker('show'); inp.val('').datepicker('show');
inp.simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_PGDN}). inp.simulate('keydown', {ctrlKey: true, keyCode: $.simulate.VK_PGDN}).