second commit

This commit is contained in:
2024-12-27 22:31:23 +09:00
parent 2353324570
commit 10a0f110ca
8819 changed files with 1307198 additions and 28 deletions

View File

@ -0,0 +1,635 @@
describe('options - beforeShow hooks', function () {
let clock;
let input;
beforeEach(function () {
clock = sinon.useFakeTimers({now: new Date(2020, 1, 14)});
input = document.createElement('input');
testContainer.appendChild(input);
});
afterEach(function () {
testContainer.removeChild(input);
clock.restore();
});
describe('beforeShowDay', function () {
it('disables the date when it returns false', function () {
const beforeShowDay = date => !!(date.getDate() % 10);
const {dp, picker} = createDP(input, {beforeShowDay});
let cells = getCells(picker);
let disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', [cells[4], cells[15], cells[25]]);
expect(disabledCells.map(el => el.textContent), 'to equal', ['30', '10', '20']);
picker.querySelector('.next-btn').click();
cells = getCells(picker);
disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', [cells[9], cells[19], cells[29], cells[40]]);
expect(disabledCells.map(el => el.textContent), 'to equal', ['10', '20', '30', '10']);
dp.destroy();
});
it('adds classes to the date when it returns spece separated classes', function () {
const beforeShowDay = date => date.getDate() % 10 ? undefined : 'foo bar';
const {dp, picker} = createDP(input, {beforeShowDay});
let cells = getCells(picker);
let fooCells = filterCells(cells, '.foo');
let barCells = filterCells(cells, '.bar');
expect(fooCells, 'to equal', [cells[4], cells[15], cells[25]]);
expect(barCells, 'to equal', fooCells);
expect(fooCells.map(el => el.textContent), 'to equal', ['30', '10', '20']);
picker.querySelector('.next-btn').click();
cells = getCells(picker);
fooCells = filterCells(cells, '.foo');
barCells = filterCells(cells, '.bar');
expect(fooCells, 'to equal', [cells[9], cells[19], cells[29], cells[40]]);
expect(barCells, 'to equal', fooCells);
expect(fooCells.map(el => el.textContent), 'to equal', ['10', '20', '30', '10']);
dp.destroy();
});
it('disables the date when the return contains enabled: false', function () {
const beforeShowDay = date => ({enabled: !!(date.getDate() % 10)});
const {dp, picker} = createDP(input, {beforeShowDay});
let cells = getCells(picker);
let disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', [cells[4], cells[15], cells[25]]);
picker.querySelector('.next-btn').click();
cells = getCells(picker);
disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', [cells[9], cells[19], cells[29], cells[40]]);
dp.destroy();
});
it('adds classes to the date when the return contains space separated classes in the classes property', function () {
const beforeShowDay = date => date.getDate() % 10 ? undefined : {classes: 'foo bar'};
const {dp, picker} = createDP(input, {beforeShowDay});
let cells = getCells(picker);
let fooCells = filterCells(cells, '.foo');
let barCells = filterCells(cells, '.bar');
expect(fooCells, 'to equal', [cells[4], cells[15], cells[25]]);
expect(barCells, 'to equal', fooCells);
picker.querySelector('.next-btn').click();
cells = getCells(picker);
fooCells = filterCells(cells, '.foo');
barCells = filterCells(cells, '.bar');
expect(fooCells, 'to equal', [cells[9], cells[19], cells[29], cells[40]]);
expect(barCells, 'to equal', fooCells);
dp.destroy();
});
it('uses custom content to the date cell when the return contains text/html in the content property', function () {
const beforeShowDay = (date) => date.getDate() % 10 === 4 ? {content: '<em>❤️</em>'} : undefined;
const {dp, picker} = createDP(input, {beforeShowDay});
let cells = getCells(picker);
let ccCells = cells.filter(el => el.children.length > 0);
expect(ccCells, 'to equal', [cells[9], cells[19], cells[29], cells[38]]);
ccCells.forEach((cell) => {
expect(cell.innerHTML, 'to be', '<em>❤️</em>');
});
picker.querySelector('.next-btn').click();
cells = getCells(picker);
ccCells = cells.filter(el => el.children.length > 0);
expect(ccCells, 'to equal', [cells[3], cells[13], cells[23], cells[34]]);
ccCells.forEach((cell) => {
expect(cell.innerHTML, 'to be', '<em>❤️</em>');
});
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
dp.setOptions({beforeShowDay: date => !!(date.getDate() % 10)});
dp.show();
let cells = getCells(picker);
let disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', [cells[4], cells[15], cells[25]]);
dp.hide();
dp.setOptions({beforeShowDay: null});
dp.show();
cells = getCells(picker);
disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', []);
dp.destroy();
});
});
describe('beforeShowMonth', function () {
it('disables the month on months view when it returns false', function () {
const beforeShowMonth = date => !!(date.getMonth() % 5);
const {dp, picker} = createDP(input, {beforeShowMonth});
const viewSwitch = getViewSwitch(picker);
dp.show();
viewSwitch.click();
let cells = getCells(picker);
let disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', [cells[0], cells[5], cells[10]]);
expect(disabledCells.map(el => el.textContent), 'to equal', ['Jan', 'Jun', 'Nov']);
picker.querySelector('.next-btn').click();
cells = getCells(picker);
disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', [cells[0], cells[5], cells[10]]);
expect(disabledCells.map(el => el.textContent), 'to equal', ['Jan', 'Jun', 'Nov']);
dp.destroy();
});
it('adds classes to the month on months view when it returns spece separated classes', function () {
const beforeShowMonth = date => date.getMonth() % 5 ? undefined : 'foo bar';
const {dp, picker} = createDP(input, {beforeShowMonth});
const viewSwitch = getViewSwitch(picker);
dp.show();
viewSwitch.click();
let cells = getCells(picker);
let fooCells = filterCells(cells, '.foo');
let barCells = filterCells(cells, '.bar');
expect(fooCells, 'to equal', [cells[0], cells[5], cells[10]]);
expect(barCells, 'to equal', fooCells);
expect(fooCells.map(el => el.textContent), 'to equal', ['Jan', 'Jun', 'Nov']);
picker.querySelector('.next-btn').click();
cells = getCells(picker);
fooCells = filterCells(cells, '.foo');
barCells = filterCells(cells, '.bar');
expect(fooCells, 'to equal', [cells[0], cells[5], cells[10]]);
expect(barCells, 'to equal', fooCells);
expect(fooCells.map(el => el.textContent), 'to equal', ['Jan', 'Jun', 'Nov']);
dp.destroy();
});
it('disables the month on months view when the return contains enabled: false', function () {
const beforeShowMonth = date => ({enabled: !!(date.getMonth() % 5)});
const {dp, picker} = createDP(input, {beforeShowMonth});
const viewSwitch = getViewSwitch(picker);
dp.show();
viewSwitch.click();
let cells = getCells(picker);
let disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', [cells[0], cells[5], cells[10]]);
picker.querySelector('.next-btn').click();
cells = getCells(picker);
disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', [cells[0], cells[5], cells[10]]);
dp.destroy();
});
it('adds classes to the month on months view when the return contains space separated classes in the classes property', function () {
const beforeShowMonth = date => date.getMonth() % 5 ? undefined : {classes: 'foo bar'};
const {dp, picker} = createDP(input, {beforeShowMonth});
const viewSwitch = getViewSwitch(picker);
dp.show();
viewSwitch.click();
let cells = getCells(picker);
let fooCells = filterCells(cells, '.foo');
let barCells = filterCells(cells, '.bar');
expect(fooCells, 'to equal', [cells[0], cells[5], cells[10]]);
expect(barCells, 'to equal', fooCells);
picker.querySelector('.next-btn').click();
cells = getCells(picker);
fooCells = filterCells(cells, '.foo');
barCells = filterCells(cells, '.bar');
expect(fooCells, 'to equal', [cells[0], cells[5], cells[10]]);
expect(barCells, 'to equal', fooCells);
dp.destroy();
});
it('uses custom content to the month cell when the return contains text/html in the content property', function () {
const beforeShowMonth = (date) => (date.getMonth() + date.getFullYear() % 10) % 10 ? undefined : {content: '🍀'};
const {dp, picker} = createDP(input, {beforeShowMonth});
const viewSwitch = getViewSwitch(picker);
dp.show();
viewSwitch.click();
let cells = getCells(picker);
let ccCells = cells.filter(el => el.textContent.length < 3);
expect(ccCells, 'to equal', [cells[0], cells[10]]);
ccCells.forEach((cell) => {
expect(cell.textContent, 'to be', '🍀');
});
picker.querySelector('.next-btn').click();
cells = getCells(picker);
ccCells = cells.filter(el => el.textContent.length < 3);
expect(ccCells, 'to equal', [cells[9]]);
ccCells.forEach((cell) => {
expect(cell.textContent, 'to be', '🍀');
});
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
const viewSwitch = getViewSwitch(picker);
dp.setOptions({beforeShowMonth: date => !!(date.getMonth() % 5)});
dp.show();
viewSwitch.click();
let cells = getCells(picker);
let disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', [cells[0], cells[5], cells[10]]);
dp.hide();
dp.setOptions({beforeShowMonth: null});
dp.show();
viewSwitch.click();
cells = getCells(picker);
disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', []);
dp.destroy();
});
});
describe('beforeShowYear', function () {
it('disables the year on years view when it returns false', function () {
const beforeShowYear = date => !!(date.getFullYear() % 4);
const {dp, picker} = createDP(input, {beforeShowYear});
const viewSwitch = getViewSwitch(picker);
dp.show();
viewSwitch.click();
viewSwitch.click();
let cells = getCells(picker);
let disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', [cells[1], cells[5], cells[9]]);
expect(disabledCells.map(el => el.textContent), 'to equal', ['2020', '2024', '2028',]);
picker.querySelector('.next-btn').click();
cells = getCells(picker);
disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', [cells[3], cells[7], cells[11]]);
expect(disabledCells.map(el => el.textContent), 'to equal', ['2032', '2036', '2040']);
dp.destroy();
});
it('adds classes to the year on years view when it returns spece separated classes', function () {
const beforeShowYear = date => date.getFullYear() % 4 ? undefined : 'foo bar';
const {dp, picker} = createDP(input, {beforeShowYear});
const viewSwitch = getViewSwitch(picker);
dp.show();
viewSwitch.click();
viewSwitch.click();
let cells = getCells(picker);
let fooCells = filterCells(cells, '.foo');
let barCells = filterCells(cells, '.bar');
expect(fooCells, 'to equal', [cells[1], cells[5], cells[9]]);
expect(barCells, 'to equal', fooCells);
expect(fooCells.map(el => el.textContent), 'to equal', ['2020', '2024', '2028']);
picker.querySelector('.next-btn').click();
cells = getCells(picker);
fooCells = filterCells(cells, '.foo');
barCells = filterCells(cells, '.bar');
expect(fooCells, 'to equal', [cells[3], cells[7], cells[11]]);
expect(barCells, 'to equal', fooCells);
expect(fooCells.map(el => el.textContent), 'to equal', ['2032', '2036', '2040']);
dp.destroy();
});
it('disables the year on years view when the return contains enabled: false', function () {
const beforeShowYear = date => ({enabled: !!(date.getFullYear() % 4)});
const {dp, picker} = createDP(input, {beforeShowYear});
const viewSwitch = getViewSwitch(picker);
dp.show();
viewSwitch.click();
viewSwitch.click();
let cells = getCells(picker);
let disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', [cells[1], cells[5], cells[9]]);
picker.querySelector('.next-btn').click();
cells = getCells(picker);
disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', [cells[3], cells[7], cells[11]]);
dp.destroy();
});
it('adds classes to the year on years view when the return contains space separated classes in the classes property', function () {
const beforeShowYear = date => date.getFullYear() % 4 ? undefined : {classes: 'foo bar'};
const {dp, picker} = createDP(input, {beforeShowYear});
const viewSwitch = getViewSwitch(picker);
dp.show();
viewSwitch.click();
viewSwitch.click();
let cells = getCells(picker);
let fooCells = filterCells(cells, '.foo');
let barCells = filterCells(cells, '.bar');
expect(fooCells, 'to equal', [cells[1], cells[5], cells[9]]);
expect(barCells, 'to equal', fooCells);
picker.querySelector('.next-btn').click();
cells = getCells(picker);
fooCells = filterCells(cells, '.foo');
barCells = filterCells(cells, '.bar');
expect(fooCells, 'to equal', [cells[3], cells[7], cells[11]]);
expect(barCells, 'to equal', fooCells);
dp.destroy();
});
it('uses custom content to the year cell when the return contains text/html in the content property', function () {
const beforeShowYear = (date) => {
const year = date.getFullYear();
return (year + Math.floor(year / 10) % 10) % 4 ? undefined : {content: '<i class="icn-x"></i>'};
};
const {dp, picker} = createDP(input, {beforeShowYear});
const viewSwitch = getViewSwitch(picker);
dp.show();
viewSwitch.click();
viewSwitch.click();
let cells = getCells(picker);
let ccCells = cells.filter(el => el.children.length > 0);
expect(ccCells, 'to equal', [cells[1], cells[5], cells[9]]);
ccCells.forEach((cell) => {
expect(cell.innerHTML, 'to be', '<i class="icn-x"></i>');
});
picker.querySelector('.next-btn').click();
cells = getCells(picker);
ccCells = cells.filter(el => el.children.length > 0);
expect(ccCells, 'to equal', [cells[2], cells[6], cells[10]]);
ccCells.forEach((cell) => {
expect(cell.innerHTML, 'to be', '<i class="icn-x"></i>');
});
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
const viewSwitch = getViewSwitch(picker);
dp.setOptions({beforeShowYear: date => !!(date.getFullYear() % 4)});
dp.show();
viewSwitch.click();
viewSwitch.click();
let cells = getCells(picker);
let disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', [cells[1], cells[5], cells[9]]);
dp.hide();
dp.setOptions({beforeShowYear: null});
dp.show();
viewSwitch.click();
viewSwitch.click();
cells = getCells(picker);
disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', []);
dp.destroy();
});
});
describe('beforeShowDecade', function () {
it('disables the decade on decades view when it returns false', function () {
const beforeShowDecade = date => !!((date.getFullYear() / 10) % 4);
const {dp, picker} = createDP(input, {beforeShowDecade});
const viewSwitch = getViewSwitch(picker);
dp.show();
viewSwitch.click();
viewSwitch.click();
viewSwitch.click();
let cells = getCells(picker);
let disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', [cells[1], cells[5], cells[9]]);
expect(disabledCells.map(el => el.textContent), 'to equal', ['2000', '2040', '2080',]);
picker.querySelector('.next-btn').click();
cells = getCells(picker);
disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', [cells[3], cells[7], cells[11]]);
expect(disabledCells.map(el => el.textContent), 'to equal', ['2120', '2160', '2200']);
dp.destroy();
});
it('adds classes to the decade on decades view when it returns spece separated classes', function () {
const beforeShowDecade = date => (date.getFullYear() / 10) % 4 ? undefined : 'foo bar';
const {dp, picker} = createDP(input, {beforeShowDecade});
const viewSwitch = getViewSwitch(picker);
dp.show();
viewSwitch.click();
viewSwitch.click();
viewSwitch.click();
let cells = getCells(picker);
let fooCells = filterCells(cells, '.foo');
let barCells = filterCells(cells, '.bar');
expect(fooCells, 'to equal', [cells[1], cells[5], cells[9]]);
expect(barCells, 'to equal', fooCells);
expect(fooCells.map(el => el.textContent), 'to equal', ['2000', '2040', '2080']);
picker.querySelector('.next-btn').click();
cells = getCells(picker);
fooCells = filterCells(cells, '.foo');
barCells = filterCells(cells, '.bar');
expect(fooCells, 'to equal', [cells[3], cells[7], cells[11]]);
expect(barCells, 'to equal', fooCells);
expect(fooCells.map(el => el.textContent), 'to equal', ['2120', '2160', '2200']);
dp.destroy();
});
it('disables the decade on decades view when the return contains enabled: false', function () {
const beforeShowDecade = date => ({enabled: !!((date.getFullYear() / 10) % 4)});
const {dp, picker} = createDP(input, {beforeShowDecade});
const viewSwitch = getViewSwitch(picker);
dp.show();
viewSwitch.click();
viewSwitch.click();
viewSwitch.click();
let cells = getCells(picker);
let disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', [cells[1], cells[5], cells[9]]);
picker.querySelector('.next-btn').click();
cells = getCells(picker);
disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', [cells[3], cells[7], cells[11]]);
dp.destroy();
});
it('adds classes to the decade on decades view when the return contains space separated classes in the classes property', function () {
const beforeShowDecade = date => (date.getFullYear() / 10) % 4 ? undefined : {classes: 'foo bar'};
const {dp, picker} = createDP(input, {beforeShowDecade});
const viewSwitch = getViewSwitch(picker);
dp.show();
viewSwitch.click();
viewSwitch.click();
viewSwitch.click();
let cells = getCells(picker);
let fooCells = filterCells(cells, '.foo');
let barCells = filterCells(cells, '.bar');
expect(fooCells, 'to equal', [cells[1], cells[5], cells[9]]);
expect(barCells, 'to equal', fooCells);
picker.querySelector('.next-btn').click();
cells = getCells(picker);
fooCells = filterCells(cells, '.foo');
barCells = filterCells(cells, '.bar');
expect(fooCells, 'to equal', [cells[3], cells[7], cells[11]]);
expect(barCells, 'to equal', fooCells);
dp.destroy();
});
it('uses custom content to the decade cell when the return contains text/html in the content property', function () {
const beforeShowDecade = (date) => {
const dec = date.getFullYear() / 10;
return (dec + Math.floor(dec / 10) % 10) % 5 ? undefined : {content: '<strong>X</strong>'};
};
const {dp, picker} = createDP(input, {beforeShowDecade});
const viewSwitch = getViewSwitch(picker);
dp.show();
viewSwitch.click();
viewSwitch.click();
viewSwitch.click();
let cells = getCells(picker);
let ccCells = cells.filter(el => el.children.length > 0);
expect(ccCells, 'to equal', [cells[1], cells[6]]);
ccCells.forEach((cell) => {
expect(cell.innerHTML, 'to be', '<strong>X</strong>');
});
picker.querySelector('.next-btn').click();
cells = getCells(picker);
ccCells = cells.filter(el => el.children.length > 0);
expect(ccCells, 'to equal', [cells[5], cells[10]]);
ccCells.forEach((cell) => {
expect(cell.innerHTML, 'to be', '<strong>X</strong>');
});
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
const viewSwitch = getViewSwitch(picker);
dp.setOptions({beforeShowDecade: date => !!((date.getFullYear() / 10) % 4)});
dp.show();
viewSwitch.click();
viewSwitch.click();
viewSwitch.click();
let cells = getCells(picker);
let disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', [cells[1], cells[5], cells[9]]);
dp.hide();
dp.setOptions({beforeShowDecade: null});
dp.show();
viewSwitch.click();
viewSwitch.click();
viewSwitch.click();
cells = getCells(picker);
disabledCells = filterCells(cells, '.disabled');
expect(disabledCells, 'to equal', []);
dp.destroy();
});
});
});

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,383 @@
describe('options - buttons', function () {
let clock;
let input;
beforeEach(function () {
clock = sinon.useFakeTimers({now: new Date(2020, 1, 14)});
input = document.createElement('input');
testContainer.appendChild(input);
});
afterEach(function () {
testContainer.removeChild(input);
clock.restore();
});
describe('clearBtn', function () {
it('displays a button to clear the selection when true', function () {
const {dp, picker} = createDP(input, {clearBtn: true});
const [viewSwitch, clearBtn] = getParts(picker, ['.view-switch', '.clear-btn']);
dp.show();
expect(isVisible(clearBtn), 'to be true');
expect(clearBtn.textContent, 'to be', 'Clear');
// months view
viewSwitch.click();
expect(isVisible(clearBtn), 'to be true');
// years view
viewSwitch.click();
expect(isVisible(clearBtn), 'to be true');
// decades view
viewSwitch.click();
expect(isVisible(clearBtn), 'to be true');
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
dp.setOptions({clearBtn: true});
dp.show();
const clearBtn = picker.querySelector('.clear-btn');
expect(isVisible(clearBtn), 'to be true');
dp.setOptions({clearBtn: false});
expect(isVisible(clearBtn), 'to be false');
dp.destroy();
});
describe('clear button', function () {
let dp;
let picker;
let clearBtn;
beforeEach(function () {
({dp, picker} = createDP(input, {clearBtn: true}));
clearBtn = picker.querySelector('.clear-btn');
dp.show();
});
afterEach(function () {
dp.destroy();
});
it('clears the selection', function () {
dp.setDate('2/14/2020');
clearBtn.click();
expect(dp.dates, 'to equal', []);
expect(input.value, 'to be', '');
const viewSwitch = getViewSwitch(picker);
// months view
dp.setDate('2/14/2020');
viewSwitch.click();
clearBtn.click();
expect(dp.dates, 'to equal', []);
// years view
dp.setDate('2/14/2020');
viewSwitch.click();
clearBtn.click();
expect(dp.dates, 'to equal', []);
// decades view
dp.setDate('2/14/2020');
viewSwitch.click();
clearBtn.click();
expect(dp.dates, 'to equal', []);
});
it('hides the picker as well when autohide = true', function () {
dp.setDate('2/14/2020');
dp.setOptions({autohide: true});
clearBtn.click();
expect(isVisible(picker), 'to be false');
});
});
});
describe('todayBtn', function () {
it('displays a button to change the view date to current date when true', function () {
const {dp, picker} = createDP(input, {todayBtn: true});
const [viewSwitch, todayBtn] = getParts(picker, ['.view-switch', '.today-btn']);
dp.show();
expect(isVisible(todayBtn), 'to be true');
expect(todayBtn.textContent, 'to be', 'Today');
// months view
viewSwitch.click();
expect(isVisible(todayBtn), 'to be true');
// years view
viewSwitch.click();
expect(isVisible(todayBtn), 'to be true');
// decades view
viewSwitch.click();
expect(isVisible(todayBtn), 'to be true');
dp.destroy();
});
it('today will be disabled if the current date is out of the range of minDate/maxDate', function () {
const {dp, picker} = createDP(input, {todayBtn: true});
const todayBtn = picker.querySelector('.today-btn');
dp.show();
expect(todayBtn.disabled, 'to be false');
dp.setOptions({minDate: '2/20/2020'});
expect(todayBtn.disabled, 'to be true');
dp.setOptions({minDate: null, maxDate: '2/10/2020'});
expect(todayBtn.disabled, 'to be true');
dp.setOptions({minDate: '2/1/2020', maxDate: '2/29/2020'});
expect(todayBtn.disabled, 'to be false');
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
dp.setOptions({todayBtn: true});
dp.show();
const todayBtn = picker.querySelector('.today-btn');
expect(isVisible(todayBtn), 'to be true');
dp.setOptions({todayBtn: false});
expect(isVisible(todayBtn), 'to be false');
dp.setOptions({todayBtn: 'true'});
expect(isVisible(todayBtn), 'to be true');
dp.destroy();
});
});
describe('todayBtnMode', function () {
let dp;
let picker;
let viewSwitch;
let todayBtn;
let cells;
beforeEach(function () {
({dp, picker} = createDP(input, {todayBtn: true}));
[viewSwitch, todayBtn] = getParts(picker, ['.view-switch', '.today-btn']);
dp.show();
});
afterEach(function () {
dp.destroy();
});
it('specifies the behavior of the today buton', function () {
const date = dateValue(2020, 1, 11);
// defualt to 0: focus-on (change view date)
dp.setDate(date);
todayBtn.click();
cells = getCells(picker);
expect(cells[19].textContent, 'to be', '14');
expect(cells[19].classList.contains('focused'), 'to be true');
expect(cells[16].classList.contains('selected'), 'to be true');
expect(dp.dates, 'to equal', [date]);
dp.destroy();
// 1: select (change the selection)
({dp, picker} = createDP(input, {todayBtn: true, todayBtnMode: 1}));
todayBtn = picker.querySelector('.today-btn');
dp.setDate(date);
dp.show();
todayBtn.click();
cells = getCells(picker);
expect(cells[19].textContent, 'to be', '14');
expect(cells[19].classList.contains('focused'), 'to be true');
expect(cells[19].classList.contains('selected'), 'to be true');
expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
});
it('can be updated with setOptions()', function () {
const date = dateValue(2020, 1, 11);
dp.setDate(date);
dp.setOptions({todayBtnMode: 1});
todayBtn.click();
cells = getCells(picker);
expect(cells[19].textContent, 'to be', '14');
expect(cells[19].classList.contains('focused'), 'to be true');
expect(cells[19].classList.contains('selected'), 'to be true');
expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
dp.setDate(date);
dp.setOptions({todayBtnMode: 0});
todayBtn.click();
expect(cells[19].textContent, 'to be', '14');
expect(cells[19].classList.contains('focused'), 'to be true');
expect(cells[16].classList.contains('selected'), 'to be true');
expect(dp.dates, 'to equal', [date]);
});
describe('today button', function () {
it('changes the view date to the current date when todayBtnMode = 0', function () {
dp.setDate('4/22/2020');
todayBtn.click();
expect(viewSwitch.textContent, 'to be', 'February 2020');
cells = getCells(picker);
expect(cells[19].textContent, 'to be', '14');
expect(cells[19].classList.contains('focused'), 'to be true');
expect(cells.find(el => el.classList.contains('selected')), 'to be undefined');
expect(dp.dates, 'to equal', [dateValue(2020, 3, 22)]);
expect(input.value, 'to be', '04/22/2020');
dp.setDate({clear: true});
});
it('also changes the view to days view when todayBtnMode = 0', function () {
// months view
dp.setDate('4/22/2020');
viewSwitch.click();
todayBtn.click();
expect(viewSwitch.textContent, 'to be', 'February 2020');
// years view
dp.setDate({clear: true});
dp.setDate('4/22/2020');
viewSwitch.click();
viewSwitch.click();
todayBtn.click();
expect(viewSwitch.textContent, 'to be', 'February 2020');
// decades view
dp.setDate({clear: true});
dp.setDate('4/22/2020');
viewSwitch.click();
viewSwitch.click();
viewSwitch.click();
todayBtn.click();
expect(viewSwitch.textContent, 'to be', 'February 2020');
dp.setDate({clear: true});
});
it('changes the selection to the current date when todayBtnMode = 1', function () {
dp.setOptions({todayBtnMode: 1});
dp.setDate('4/22/2020');
todayBtn.click();
expect(viewSwitch.textContent, 'to be', 'February 2020');
cells = getCells(picker);
expect(cells[19].textContent, 'to be', '14');
expect(cells[19].classList.contains('focused'), 'to be true');
expect(cells[19].classList.contains('selected'), 'to be true');
expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
expect(input.value, 'to be', '02/14/2020');
dp.setDate({clear: true});
dp.setDate('4/22/2020');
viewSwitch.click();
viewSwitch.click();
todayBtn.click();
expect(viewSwitch.textContent, 'to be', 'February 2020');
expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
dp.setDate({clear: true});
});
it('also hides the picker when todayBtnMode = 1 and autohide = true', function () {
dp.setOptions({todayBtnMode: 1, autohide: true});
dp.setDate('4/22/2020');
todayBtn.click();
expect(isVisible(picker), 'to be false');
dp.setDate({clear: true});
});
it('always changes the view to current date\'s days view when todayBtnMode = 1', function () {
const nextBtn = picker.querySelector('.next-btn');
dp.setOptions({todayBtnMode: 1});
// after moving other month or view while the current date is selected already
// (issue #11)
todayBtn.click();
nextBtn.click();
todayBtn.click();
cells = getCells(picker);
expect(viewSwitch.textContent, 'to be', 'February 2020');
expect(cells[19].classList.contains('focused'), 'to be true');
expect(cells[19].classList.contains('selected'), 'to be true');
expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
viewSwitch.click();
nextBtn.click();
todayBtn.click();
cells = getCells(picker);
expect(viewSwitch.textContent, 'to be', 'February 2020');
expect(cells[19].classList.contains('focused'), 'to be true');
expect(cells[19].classList.contains('selected'), 'to be true');
expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
viewSwitch.click();
viewSwitch.click();
nextBtn.click();
nextBtn.click();
todayBtn.click();
cells = getCells(picker);
expect(viewSwitch.textContent, 'to be', 'February 2020');
expect(cells[19].classList.contains('focused'), 'to be true');
expect(cells[19].classList.contains('selected'), 'to be true');
expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
// when current date is deslected by toggling in multi-date mode
dp.setOptions({maxNumberOfDates: 3});
nextBtn.click();
getCells(picker)[20].click();
todayBtn.click();
cells = getCells(picker);
expect(viewSwitch.textContent, 'to be', 'February 2020');
expect(cells[19].classList.contains('focused'), 'to be true');
expect(cells[19].classList.contains('selected'), 'to be false');
expect(dp.dates, 'to equal', [dateValue(2020, 2, 21)]);
nextBtn.click();
todayBtn.click();
cells = getCells(picker);
expect(viewSwitch.textContent, 'to be', 'February 2020');
expect(cells[19].classList.contains('selected'), 'to be true');
expect(dp.dates, 'to equal', [dateValue(2020, 2, 21), dateValue(2020, 1, 14)]);
viewSwitch.click();
nextBtn.click();
todayBtn.click();
cells = getCells(picker);
expect(viewSwitch.textContent, 'to be', 'February 2020');
expect(cells[19].classList.contains('focused'), 'to be true');
expect(cells[19].classList.contains('selected'), 'to be false');
expect(dp.dates, 'to equal', [dateValue(2020, 2, 21)]);
dp.setDate({clear: true});
});
});
});
});

View File

@ -0,0 +1,802 @@
describe('options - date restrictions', function () {
const getDisabled = cells => filterCells(cells, '.disabled').map(el => [el, el.textContent]);
let input;
beforeEach(function () {
input = document.createElement('input');
testContainer.appendChild(input);
});
afterEach(function () {
testContainer.removeChild(input);
});
describe('datesDisabled', function () {
let clock;
beforeEach(function () {
clock = sinon.useFakeTimers({now: new Date(2020, 1, 14)});
});
afterEach(function () {
clock.restore();
});
it('specifies unselectable dates', function () {
const dp = new Datepicker(input, {
datesDisabled: [new Date(2020, 1, 12), '2/13/2020', new Date(2020, 1, 13), '2/20/2020'],
});
const picker = document.querySelector('.datepicker');
dp.show();
expect(picker.querySelector('.prev-btn').disabled, 'to be false');
expect(picker.querySelector('.next-btn').disabled, 'to be false');
let cells = getCells(picker);
expect(getDisabled(cells), 'to equal', [
[cells[17], '12'],
[cells[18], '13'],
[cells[25], '20'],
]);
cells[17].click();
expect(dp.dates, 'to equal', []);
expect(input.value, 'to be', '');
expect(filterCells(cells, '.selected'), 'to equal', []);
dp.setDate(new Date(2020, 1, 12));
expect(dp.dates, 'to equal', []);
expect(input.value, 'to be', '');
expect(filterCells(cells, '.selected'), 'to equal', []);
input.value = '2/12/2020';
dp.update();
expect(dp.dates, 'to equal', []);
expect(input.value, 'to be', '2/12/2020');
expect(filterCells(cells, '.selected'), 'to equal', []);
cells[16].click();
expect(dp.dates, 'to equal', [dateValue(2020, 1, 11)]);
expect(input.value, 'to be', '02/11/2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[16]]);
dp.enterEditMode();
input.value = '2/12/2020';
simulant.fire(input, 'keydown', {key: 'Enter'});
expect(dp.dates, 'to equal', [dateValue(2020, 1, 11)]);
expect(input.value, 'to be', '2/12/2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[16]]);
dp.destroy();
});
it('makes the picker prevent those dates becoming view date', function () {
const dp = new Datepicker(input, {
datesDisabled: ['2/11/2020', '2/12/2020', '2/13/2020', '2/20/2020'],
});
const picker = document.querySelector('.datepicker');
const cells = getCells(picker);
dp.show();
simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
expect(filterCells(cells, '.focused'), 'to equal', [cells[15]]);
simulant.fire(input, 'keydown', {key: 'ArrowRight'});
expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
simulant.fire(input, 'keydown', {key: 'ArrowUp'});
simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
// on 5th
simulant.fire(input, 'keydown', {key: 'ArrowDown'});
expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
simulant.fire(input, 'keydown', {key: 'ArrowDown'});
simulant.fire(input, 'keydown', {key: 'ArrowDown'});
simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
// on 27th
simulant.fire(input, 'keydown', {key: 'ArrowUp'});
expect(filterCells(cells, '.focused'), 'to equal', [cells[24]]);
// on 19th
simulant.fire(input, 'keydown', {key: 'ArrowUp'});
expect(filterCells(cells, '.focused'), 'to equal', [cells[15]]);
simulant.fire(input, 'keydown', {key: 'ArrowUp'});
simulant.fire(input, 'keydown', {key: 'ArrowRight'});
// on 4th
simulant.fire(input, 'keydown', {key: 'ArrowDown'});
expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
dp.setOptions({datesDisabled: [new Date(2020, 1, 11), new Date(2020, 1, 26)]});
dp.show();
let cells = getCells(picker);
expect(getDisabled(cells), 'to equal', [
[cells[16], '11'],
[cells[31], '26'],
]);
dp.setOptions({datesDisabled: []});
cells = getCells(picker);
expect(getDisabled(cells), 'to equal', []);
dp.destroy();
});
});
describe('daysOfWeekDisabled', function () {
let clock;
beforeEach(function () {
clock = sinon.useFakeTimers({now: new Date(2020, 1, 14)});
});
afterEach(function () {
clock.restore();
});
it('specifies unselectable days of week', function () {
const {dp, picker} = createDP(input, {daysOfWeekDisabled: [0, 6]});
dp.show();
expect(picker.querySelector('.prev-btn').disabled, 'to be false');
expect(picker.querySelector('.next-btn').disabled, 'to be false');
let cells = getCells(picker);
expect(getDisabled(cells), 'to equal', [
[cells[0], '26'],
[cells[6], '1'],
[cells[7], '2'],
[cells[13], '8'],
[cells[14], '9'],
[cells[20], '15'],
[cells[21], '16'],
[cells[27], '22'],
[cells[28], '23'],
[cells[34], '29'],
[cells[35], '1'],
[cells[41], '7'],
]);
cells[14].click();
expect(dp.dates, 'to equal', []);
expect(input.value, 'to be', '');
expect(filterCells(cells, '.selected'), 'to equal', []);
dp.setDate(new Date(2020, 1, 9));
expect(dp.dates, 'to equal', []);
expect(input.value, 'to be', '');
expect(filterCells(cells, '.selected'), 'to equal', []);
input.value = '2/9/2020';
dp.update();
expect(dp.dates, 'to equal', []);
expect(input.value, 'to be', '2/9/2020');
expect(filterCells(cells, '.selected'), 'to equal', []);
cells[15].click();
expect(dp.dates, 'to equal', [dateValue(2020, 1, 10)]);
expect(input.value, 'to be', '02/10/2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[15]]);
dp.enterEditMode();
input.value = '2/9/2020';
simulant.fire(input, 'keydown', {key: 'Enter'});
expect(dp.dates, 'to equal', [dateValue(2020, 1, 10)]);
expect(input.value, 'to be', '2/9/2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[15]]);
dp.destroy();
});
it('makes the picker prevent those dates becoming view date', function () {
const {dp, picker} = createDP(input, {daysOfWeekDisabled: [0, 6]});
const cells = getCells(picker);
dp.show();
simulant.fire(input, 'keydown', {key: 'ArrowRight'});
expect(filterCells(cells, '.focused'), 'to equal', [cells[22]]);
simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
simulant.fire(input, 'keydown', {key: 'ArrowUp'});
// on 7th
simulant.fire(input, 'keydown', {key: 'ArrowRight'});
expect(filterCells(cells, '.focused'), 'to equal', [cells[15]]);
simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
expect(filterCells(cells, '.focused'), 'to equal', [cells[12]]);
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
dp.setOptions({daysOfWeekDisabled: [4]});
dp.show();
let cells = getCells(picker);
expect(getDisabled(cells), 'to equal', [
[cells[4], '30'],
[cells[11], '6'],
[cells[18], '13'],
[cells[25], '20'],
[cells[32], '27'],
[cells[39], '5'],
]);
dp.setOptions({daysOfWeekDisabled: []});
cells = getCells(picker);
expect(getDisabled(cells), 'to equal', []);
dp.destroy();
});
});
describe('maxDate', function () {
let clock;
beforeEach(function () {
clock = sinon.useFakeTimers({now: new Date(2020, 1, 14)});
});
afterEach(function () {
clock.restore();
});
it('specifies the maximum selectable date', function () {
const {dp, picker} = createDP(input, {maxDate: new Date(2020, 1, 25)});
const viewSwitch = getViewSwitch(picker);
dp.show();
let cells = getCells(picker);
expect(getDisabled(cells), 'to equal', [
[cells[31], '26'],
[cells[32], '27'],
[cells[33], '28'],
[cells[34], '29'],
[cells[35], '1'],
[cells[36], '2'],
[cells[37], '3'],
[cells[38], '4'],
[cells[39], '5'],
[cells[40], '6'],
[cells[41], '7'],
]);
viewSwitch.click();
cells = getCells(picker);
expect(getDisabled(cells), 'to equal', [
[cells[2], 'Mar'],
[cells[3], 'Apr'],
[cells[4], 'May'],
[cells[5], 'Jun'],
[cells[6], 'Jul'],
[cells[7], 'Aug'],
[cells[8], 'Sep'],
[cells[9], 'Oct'],
[cells[10], 'Nov'],
[cells[11], 'Dec'],
]);
viewSwitch.click();
cells = getCells(picker);
expect(getDisabled(cells), 'to equal', [
[cells[2], '2021'],
[cells[3], '2022'],
[cells[4], '2023'],
[cells[5], '2024'],
[cells[6], '2025'],
[cells[7], '2026'],
[cells[8], '2027'],
[cells[9], '2028'],
[cells[10], '2029'],
[cells[11], '2030'],
]);
viewSwitch.click();
cells = getCells(picker);
expect(getDisabled(cells), 'to equal', [
[cells[4], '2030'],
[cells[5], '2040'],
[cells[6], '2050'],
[cells[7], '2060'],
[cells[8], '2070'],
[cells[9], '2080'],
[cells[10], '2090'],
[cells[11], '2100'],
]);
dp.hide();
dp.show();
cells = getCells(picker);
cells[31].click();
expect(dp.dates, 'to equal', []);
expect(input.value, 'to be', '');
expect(filterCells(cells, '.selected'), 'to equal', []);
dp.setDate(new Date(2020, 1, 26));
expect(dp.dates, 'to equal', []);
expect(input.value, 'to be', '');
expect(filterCells(cells, '.selected'), 'to equal', []);
input.value = '2/26/2020';
dp.update();
expect(dp.dates, 'to equal', []);
expect(input.value, 'to be', '2/26/2020');
expect(filterCells(cells, '.selected'), 'to equal', []);
cells[30].click();
expect(dp.dates, 'to equal', [dateValue(2020, 1, 25)]);
expect(input.value, 'to be', '02/25/2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[30]]);
dp.enterEditMode();
input.value = '2/26/2020';
simulant.fire(input, 'keydown', {key: 'Enter'});
expect(dp.dates, 'to equal', [dateValue(2020, 1, 25)]);
expect(input.value, 'to be', '2/26/2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[30]]);
dp.destroy();
});
it('makes the picker disallow to navigate to after the max date', function () {
const {dp, picker} = createDP(input, {maxDate: '2/14/2020'});
const [viewSwitch, prevBtn, nextBtn] = getParts(picker, ['.view-switch', '.prev-btn', '.next-btn']);
dp.show();
expect(prevBtn.disabled, 'to be false');
expect(nextBtn.disabled, 'to be true');
simulant.fire(input, 'keydown', {key: 'ArrowRight'});
let cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
simulant.fire(input, 'keydown', {key: 'ArrowRight', ctrlKey: true});
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
simulant.fire(input, 'keydown', {key: 'ArrowUp'});
simulant.fire(input, 'keydown', {key: 'ArrowRight'});
simulant.fire(input, 'keydown', {key: 'ArrowDown'});
expect(filterCells(cells, '.focused'), 'to equal', [cells[13]]);
prevBtn.click();
expect(prevBtn.disabled, 'to be false');
expect(nextBtn.disabled, 'to be false');
// move to Jan 22nd
simulant.fire(input, 'keydown', {key: 'ArrowDown'});
simulant.fire(input, 'keydown', {key: 'ArrowDown'});
nextBtn.click();
// view date is limited to the max date
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
// the same goes for ctrl + ArrowRight key
prevBtn.click();
simulant.fire(input, 'keydown', {key: 'ArrowDown'});
simulant.fire(input, 'keydown', {key: 'ArrowRight', ctrlKey: true});
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
// months view
viewSwitch.click();
expect(prevBtn.disabled, 'to be false');
expect(nextBtn.disabled, 'to be true');
simulant.fire(input, 'keydown', {key: 'ArrowRight'});
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
simulant.fire(input, 'keydown', {key: 'ArrowUp'});
simulant.fire(input, 'keydown', {key: 'ArrowRight'});
// on prev year's Nov
simulant.fire(input, 'keydown', {key: 'ArrowDown'});
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[10]]);
prevBtn.click();
expect(prevBtn.disabled, 'to be false');
expect(nextBtn.disabled, 'to be false');
// move to Mar
simulant.fire(input, 'keydown', {key: 'ArrowDown'});
nextBtn.click();
// view date is limited to the Feb
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
// the same goes for ctrl + ArrowRight key
prevBtn.click();
simulant.fire(input, 'keydown', {key: 'ArrowDown'});
simulant.fire(input, 'keydown', {key: 'ArrowRight', ctrlKey: true});
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
// years view
viewSwitch.click();
expect(prevBtn.disabled, 'to be false');
expect(nextBtn.disabled, 'to be true');
simulant.fire(input, 'keydown', {key: 'ArrowRight'});
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
simulant.fire(input, 'keydown', {key: 'ArrowUp'});
simulant.fire(input, 'keydown', {key: 'ArrowRight'});
// on 2017
simulant.fire(input, 'keydown', {key: 'ArrowDown'});
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[8]]);
simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
simulant.fire(input, 'keydown', {key: 'ArrowDown'});
prevBtn.click();
expect(prevBtn.disabled, 'to be false');
expect(nextBtn.disabled, 'to be false');
// move to 2011
simulant.fire(input, 'keydown', {key: 'ArrowRight'});
nextBtn.click();
// view year is limited to 2020
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
// the same goes for ctrl + ArrowRight key
prevBtn.click();
simulant.fire(input, 'keydown', {key: 'ArrowDown'});
simulant.fire(input, 'keydown', {key: 'ArrowRight', ctrlKey: true});
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
// decades view
viewSwitch.click();
expect(prevBtn.disabled, 'to be false');
expect(nextBtn.disabled, 'to be true');
simulant.fire(input, 'keydown', {key: 'ArrowRight'});
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
simulant.fire(input, 'keydown', {key: 'ArrowUp'});
simulant.fire(input, 'keydown', {key: 'ArrowRight'});
// on 1990
simulant.fire(input, 'keydown', {key: 'ArrowDown'});
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[10]]);
simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
simulant.fire(input, 'keydown', {key: 'ArrowDown'});
prevBtn.click();
expect(prevBtn.disabled, 'to be false');
expect(nextBtn.disabled, 'to be false');
// move to 1930
simulant.fire(input, 'keydown', {key: 'ArrowRight'});
nextBtn.click();
// view decade is limited to 2020
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
// the same goes for ctrl + ArrowRight key
prevBtn.click();
simulant.fire(input, 'keydown', {key: 'ArrowDown'});
simulant.fire(input, 'keydown', {key: 'ArrowRight', ctrlKey: true});
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
dp.setOptions({maxDate: new Date(2020, 1, 28)});
dp.show();
let cells = getCells(picker);
expect(getDisabled(cells), 'to equal', [
[cells[34], '29'],
[cells[35], '1'],
[cells[36], '2'],
[cells[37], '3'],
[cells[38], '4'],
[cells[39], '5'],
[cells[40], '6'],
[cells[41], '7'],
]);
dp.setOptions({maxDate: null});
cells = getCells(picker);
expect(getDisabled(cells), 'to equal', []);
dp.destroy();
});
});
describe('minDate', function () {
let clock;
beforeEach(function () {
clock = sinon.useFakeTimers({now: new Date(2022, 6, 14)});
});
afterEach(function () {
clock.restore();
});
it('specifies the minimum selectable date', function () {
const {dp, picker} = createDP(input, {minDate: new Date(2022, 6, 4)});
const viewSwitch = getViewSwitch(picker);
dp.show();
let cells = getCells(picker);
expect(getDisabled(cells), 'to equal', [
[cells[0], '26'],
[cells[1], '27'],
[cells[2], '28'],
[cells[3], '29'],
[cells[4], '30'],
[cells[5], '1'],
[cells[6], '2'],
[cells[7], '3'],
]);
viewSwitch.click();
cells = getCells(picker);
expect(getDisabled(cells), 'to equal', [
[cells[0], 'Jan'],
[cells[1], 'Feb'],
[cells[2], 'Mar'],
[cells[3], 'Apr'],
[cells[4], 'May'],
[cells[5], 'Jun'],
]);
viewSwitch.click();
cells = getCells(picker);
expect(getDisabled(cells), 'to equal', [
[cells[0], '2019'],
[cells[1], '2020'],
[cells[2], '2021'],
]);
viewSwitch.click();
cells = getCells(picker);
expect(getDisabled(cells), 'to equal', [
[cells[0], '1990'],
[cells[1], '2000'],
[cells[2], '2010'],
]);
dp.hide();
dp.show();
cells = getCells(picker);
cells[7].click();
expect(dp.dates, 'to equal', []);
expect(input.value, 'to be', '');
expect(filterCells(cells, '.selected'), 'to equal', []);
dp.setDate(new Date(2022, 6, 3));
expect(dp.dates, 'to equal', []);
expect(input.value, 'to be', '');
expect(filterCells(cells, '.selected'), 'to equal', []);
input.value = '7/3/2022';
dp.update();
expect(dp.dates, 'to equal', []);
expect(input.value, 'to be', '7/3/2022');
expect(filterCells(cells, '.selected'), 'to equal', []);
cells[8].click();
expect(dp.dates, 'to equal', [dateValue(2022, 6, 4)]);
expect(input.value, 'to be', '07/04/2022');
expect(filterCells(cells, '.selected'), 'to equal', [cells[8]]);
dp.enterEditMode();
input.value = '7/3/2022';
simulant.fire(input, 'keydown', {key: 'Enter'});
expect(dp.dates, 'to equal', [dateValue(2022, 6, 4)]);
expect(input.value, 'to be', '7/3/2022');
expect(filterCells(cells, '.selected'), 'to equal', [cells[8]]);
dp.destroy();
});
it('makes the picker disallow to navigate to before the min date', function () {
const {dp, picker} = createDP(input, {minDate: '7/14/2022'});
const [viewSwitch, prevBtn, nextBtn] = getParts(picker, ['.view-switch', '.prev-btn', '.next-btn']);
dp.show();
expect(prevBtn.disabled, 'to be true');
expect(nextBtn.disabled, 'to be false');
simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
let cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[18]]);
simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[18]]);
simulant.fire(input, 'keydown', {key: 'ArrowDown'});
simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
simulant.fire(input, 'keydown', {key: 'ArrowUp'});
expect(filterCells(cells, '.focused'), 'to equal', [cells[24]]);
nextBtn.click();
expect(prevBtn.disabled, 'to be false');
expect(nextBtn.disabled, 'to be false');
// move to Aug 13th
simulant.fire(input, 'keydown', {key: 'ArrowUp'});
prevBtn.click();
// view date is limited to min date
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[18]]);
// the same goes for ctrl + ArrowLeft key
prevBtn.click();
simulant.fire(input, 'keydown', {key: 'ArrowUp'});
simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[18]]);
// months view
viewSwitch.click();
expect(prevBtn.disabled, 'to be true');
expect(nextBtn.disabled, 'to be false');
simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[6]]);
simulant.fire(input, 'keydown', {key: 'ArrowDown'});
simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
// on Oct
simulant.fire(input, 'keydown', {key: 'ArrowUp'});
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[9]]);
nextBtn.click();
expect(prevBtn.disabled, 'to be false');
expect(nextBtn.disabled, 'to be false');
// move to Jun
simulant.fire(input, 'keydown', {key: 'ArrowUp'});
prevBtn.click();
// view date is limited to Jul
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[6]]);
// the same goes for ctrl + ArrowLeft key
prevBtn.click();
simulant.fire(input, 'keydown', {key: 'ArrowUp'});
simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[6]]);
// years view
viewSwitch.click();
expect(prevBtn.disabled, 'to be true');
expect(nextBtn.disabled, 'to be false');
simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
simulant.fire(input, 'keydown', {key: 'ArrowDown'});
simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
// on 2025
simulant.fire(input, 'keydown', {key: 'ArrowUp'});
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[6]]);
simulant.fire(input, 'keydown', {key: 'ArrowRight'});
simulant.fire(input, 'keydown', {key: 'ArrowUp'});
nextBtn.click();
expect(prevBtn.disabled, 'to be false');
expect(nextBtn.disabled, 'to be false');
// move to 2031
simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
prevBtn.click();
// view year is limited to 2022
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
// the same goes for ctrl + ArrowLeft key
prevBtn.click();
simulant.fire(input, 'keydown', {key: 'ArrowUp'});
simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
// decades view
viewSwitch.click();
expect(prevBtn.disabled, 'to be true');
expect(nextBtn.disabled, 'to be false');
simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
simulant.fire(input, 'keydown', {key: 'ArrowDown'});
simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
// on 2050
simulant.fire(input, 'keydown', {key: 'ArrowUp'});
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[6]]);
simulant.fire(input, 'keydown', {key: 'ArrowRight'});
simulant.fire(input, 'keydown', {key: 'ArrowUp'});
nextBtn.click();
expect(prevBtn.disabled, 'to be false');
expect(nextBtn.disabled, 'to be false');
// move to 2110
simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
prevBtn.click();
// view decade is limited to 2020
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
// the same goes for ctrl + ArrowLeft key
prevBtn.click();
simulant.fire(input, 'keydown', {key: 'ArrowUp'});
simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
dp.setOptions({minDate: new Date(2022, 6, 2)});
dp.show();
let cells = getCells(picker);
expect(getDisabled(cells), 'to equal', [
[cells[0], '26'],
[cells[1], '27'],
[cells[2], '28'],
[cells[3], '29'],
[cells[4], '30'],
[cells[5], '1'],
]);
dp.setOptions({minDate: null});
cells = getCells(picker);
expect(getDisabled(cells), 'to equal', []);
dp.destroy();
});
});
});

281
node_modules/flowbite-datepicker/test/options/format.js generated vendored Normal file
View File

@ -0,0 +1,281 @@
describe('options - format & language', function () {
let clock;
let input;
beforeEach(function () {
clock = sinon.useFakeTimers({now: new Date(2020, 1, 14)});
input = document.createElement('input');
testContainer.appendChild(input);
});
afterEach(function () {
testContainer.removeChild(input);
clock.restore();
});
describe('format', function () {
it('specifies the date format used to parse/format the date string in input', function () {
const dp = new Datepicker(input, {format: 'yyyy-mm-dd'});
dp.setDate(new Date(2020, 1, 14));
expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
expect(input.value, 'to be', '2020-02-14');
input.value = '2020/4/22';
dp.update();
expect(dp.dates, 'to equal', [dateValue(2020, 3, 22)]);
expect(input.value, 'to be', '2020-04-22');
// when a date in a wrong format is given...
dp.setDate('2/14/2020');
expect(dp.dates, 'to equal', [dateValue(2, 13, 2020)]);
expect(input.value, 'to be', '0008-08-12');
input.value = '22/4/2020';
dp.update();
expect(dp.dates, 'to equal', [dateValue(22, 3, 2020)]);
expect(input.value, 'to be', '0027-10-11');
dp.destroy();
input.value = '';
});
it('custom parser/fomatter can be used by providing them as toValue/toDisplay of an object', function () {
const dp = new Datepicker(input, {
format: {
toDisplay(date) {
return new Date(date.getTime() - date.getTimezoneOffset() * 60000).toISOString()
.slice(0, 10)
.replace(/-/g, '');
},
toValue(date) {
const parts = [
parseInt(date.slice(0, 4), 10),
parseInt(date.slice(4, 6), 10) - 1,
parseInt(date.slice(6, 8), 10),
];
return dateValue(...parts);
},
},
});
dp.setDate(new Date(2020, 1, 14));
expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
expect(input.value, 'to be', '20200214');
input.value = '20200422';
dp.update();
expect(dp.dates, 'to equal', [dateValue(2020, 3, 22)]);
expect(input.value, 'to be', '20200422');
dp.destroy();
input.value = '';
});
it('can be updated with setOptions()', function () {
const dp = new Datepicker(input);
dp.setOptions({format: 'd M, \'yy'});
dp.setDate('14/2/2020');
expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
expect(input.value, 'to be', '14 Feb, \'20');
dp.setOptions({format: 'mm/dd/yyyy'});
expect(input.value, 'to be', '02/14/2020');
input.value = '4/22/2020';
dp.update();
expect(dp.dates, 'to equal', [dateValue(2020, 3, 22)]);
expect(input.value, 'to be', '04/22/2020');
dp.destroy();
input.value = '';
});
});
describe('language', function () {
const getDayNames = picker => Array.from(picker.querySelectorAll('.dow')).map(el => el.textContent);
it('specifies the language used for the month/day names, today/clear buttons and the default format/weekStart', function () {
const locale = Datepicker.locales['zh-CN'];
const {dp, picker} = createDP(input, {language: 'zh-CN', todayBtn: true, clearBtn: true});
const viewSwitch = getViewSwitch(picker);
dp.setDate(new Date(2020, 1, 14));
dp.show();
expect(viewSwitch.textContent, 'to be', '2020年02月');
expect(input.value, 'to be', '2020-02-14');
expect(picker.querySelector('.today-btn').textContent, 'to be', locale.today);
expect(picker.querySelector('.clear-btn').textContent, 'to be', locale.clear);
const dayNames = locale.daysMin.slice(1);
dayNames.push(locale.daysMin[0]);
expect(getDayNames(picker), 'to equal', dayNames);
let cells = getCells(picker);
expect(cells[0].textContent, 'to be', '27');
expect(cells[5].textContent, 'to be', '1');
expect(cells[33].textContent, 'to be', '29');
expect(cells[41].textContent, 'to be', '8');
expect(filterCells(cells, '.selected'), 'to equal', [cells[18]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[18]]);
expect(cells[18].textContent, 'to be', '14');
viewSwitch.click();
expect(viewSwitch.textContent, 'to be', '2020');
cells = getCells(picker);
expect(Array.from(cells).map(el => el.textContent), 'to equal', locale.monthsShort);
cells[1].click();
input.value = '2020-4-22';
dp.update();
expect(viewSwitch.textContent, 'to be', '2020年04月');
expect(input.value, 'to be', '2020-04-22');
cells = getCells(picker);
expect(filterCells(cells, '.selected'), 'to equal', [cells[23]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[23]]);
expect(cells[23].textContent, 'to be', '22');
dp.destroy();
input.value = '';
});
it('default format/weekStart in the locale are overriden by user-specified ones', function () {
const locale = Datepicker.locales['zh-CN'];
const {dp, picker} = createDP(input, {language: 'zh-CN', format: 'yyyy年mm月dd日', weekStart: 0});
const viewSwitch = getViewSwitch(picker);
dp.setDate(new Date(2020, 1, 14));
dp.show();
expect(viewSwitch.textContent, 'to be', '2020年02月');
expect(input.value, 'to be', '2020年02月14日');
expect(getDayNames(picker), 'to equal', locale.daysMin);
let cells = getCells(picker);
expect(filterCells(cells, '.selected'), 'to equal', [cells[19]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
expect(cells[19].textContent, 'to be', '14');
dp.destroy();
input.value = '';
});
it('language code + tag not installed falls back to the language code without tag', function () {
const locale = Datepicker.locales.fr;
const {dp, picker} = createDP(input, {language: 'fr-CA', todayBtn: true, clearBtn: true});
const viewSwitch = getViewSwitch(picker);
dp.setDate(new Date(2020, 1, 14));
dp.show();
expect(viewSwitch.textContent, 'to be', 'février 2020');
expect(input.value, 'to be', '14/02/2020');
expect(picker.querySelector('.today-btn').textContent, 'to be', locale.today);
expect(picker.querySelector('.clear-btn').textContent, 'to be', locale.clear);
const dayNames = locale.daysMin.slice(1);
dayNames.push(locale.daysMin[0]);
expect(getDayNames(picker), 'to equal', dayNames);
dp.destroy();
input.value = '';
});
it('language code not installed falls back to "en"', function () {
const locale = Datepicker.locales.en;
const {dp, picker} = createDP(input, {language: 'it', todayBtn: true, clearBtn: true});
const viewSwitch = getViewSwitch(picker);
dp.setDate(new Date(2020, 1, 14));
dp.show();
expect(viewSwitch.textContent, 'to be', 'February 2020');
expect(input.value, 'to be', '02/14/2020');
expect(picker.querySelector('.today-btn').textContent, 'to be', locale.today);
expect(picker.querySelector('.clear-btn').textContent, 'to be', locale.clear);
expect(getDayNames(picker), 'to equal', locale.daysMin);
dp.destroy();
input.value = '';
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input, {todayBtn: true, clearBtn: true});
const viewSwitch = getViewSwitch(picker);
let locale = Datepicker.locales['zh-CN'];
dp.setDate(new Date(2020, 1, 14));
dp.setOptions({language: 'zh-CN'});
dp.show();
expect(viewSwitch.textContent, 'to be', '2020年02月');
expect(input.value, 'to be', '2020-02-14');
expect(picker.querySelector('.today-btn').textContent, 'to be', locale.today);
expect(picker.querySelector('.clear-btn').textContent, 'to be', locale.clear);
let dayNames = locale.daysMin.slice(1);
dayNames.push(locale.daysMin[0]);
expect(getDayNames(picker), 'to equal', dayNames);
let cells = getCells(picker);
expect(filterCells(cells, '.selected'), 'to equal', [cells[18]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[18]]);
expect(cells[18].textContent, 'to be', '14');
locale = Datepicker.locales.fr;
dp.setOptions({language: 'fr'});
expect(viewSwitch.textContent, 'to be', 'février 2020');
expect(input.value, 'to be', '14/02/2020');
expect(picker.querySelector('.today-btn').textContent, 'to be', locale.today);
expect(picker.querySelector('.clear-btn').textContent, 'to be', locale.clear);
dayNames = locale.daysMin.slice(1);
dayNames.push(locale.daysMin[0]);
expect(getDayNames(picker), 'to equal', dayNames);
cells = getCells(picker);
expect(filterCells(cells, '.selected'), 'to equal', [cells[18]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[18]]);
expect(cells[18].textContent, 'to be', '14');
locale = Datepicker.locales.en;
dp.setOptions({language: 'en'});
expect(viewSwitch.textContent, 'to be', 'February 2020');
expect(input.value, 'to be', '02/14/2020');
expect(picker.querySelector('.today-btn').textContent, 'to be', locale.today);
expect(picker.querySelector('.clear-btn').textContent, 'to be', locale.clear);
expect(getDayNames(picker), 'to equal', locale.daysMin);
cells = getCells(picker);
expect(filterCells(cells, '.selected'), 'to equal', [cells[19]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
expect(cells[19].textContent, 'to be', '14');
dp.destroy();
input.value = '';
});
it('user-specified format/weekStart other than old language\'s default are kept on being updated dynamically', function () {
const {dp, picker} = createDP(input, {language: 'zh-CN', format: 'yyyy/mm/dd', weekStart: 0});
dp.setDate(new Date(2020, 1, 14));
dp.show();
let locale = Datepicker.locales.fr;
dp.setOptions({language: 'fr'});
expect(input.value, 'to be', '2020/02/14');
expect(getDayNames(picker), 'to equal', locale.daysMin);
let cells = getCells(picker);
expect(filterCells(cells, '.selected'), 'to equal', [cells[19]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
expect(cells[19].textContent, 'to be', '14');
dp.destroy();
input.value = '';
});
});
});

View File

@ -0,0 +1,380 @@
describe('options - multi date', function () {
let clock;
let input;
beforeEach(function () {
clock = sinon.useFakeTimers({now: new Date(2020, 1, 14)});
input = document.createElement('input');
testContainer.appendChild(input);
});
afterEach(function () {
testContainer.removeChild(input);
clock.restore();
});
describe('maxNumberOfDates', function () {
it('specifies the muximum number of dates the datepicker accepts for the selection', function () {
let {dp, picker} = createDP(input, {maxNumberOfDates: 2});
dp.setDate('2/14/2020', '4/22/2020');
expect(dp.dates, 'to equal', [dateValue(2020, 1, 14), dateValue(2020, 3, 22)]);
expect(input.value, 'to be', '02/14/2020,04/22/2020');
// the dates come later win
dp.setDate('1/4/2020', '2/22/2020', '3/21/2020');
expect(dp.dates, 'to equal', [dateValue(2020, 1, 22), dateValue(2020, 2, 21)]);
expect(input.value, 'to be', '02/22/2020,03/21/2020');
// repeated dates are eliminated
dp.setDate('4/22/2020', '7/14/2020', '5/5/2020', '7/14/2020');
expect(dp.dates, 'to equal', [dateValue(2020, 6, 14), dateValue(2020, 4, 5)]);
expect(input.value, 'to be', '07/14/2020,05/05/2020');
dp.destroy();
input.value = '';
({dp, picker} = createDP(input, {maxNumberOfDates: 3}));
dp.show();
let cells = getCells(picker);
cells[19].click();
cells[9].click();
expect(dp.dates, 'to equal', [dateValue(2020, 1, 14), dateValue(2020, 1, 4)]);
expect(input.value, 'to be', '02/14/2020,02/04/2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[19], cells[9]]);
// view date is changed co the last selected item
expect(filterCells(cells, '.focused'), 'to equal', [cells[9]]);
input.value = '2/3/2020,2/22/2020';
dp.update();
expect(dp.dates, 'to equal', [dateValue(2020, 1, 3), dateValue(2020, 1, 22)]);
expect(input.value, 'to be', '02/03/2020,02/22/2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[8], cells[27]]);
// view date is changed co the last item of the selection
expect(filterCells(cells, '.focused'), 'to equal', [cells[27]]);
dp.destroy();
input.value = '';
({dp, picker} = createDP(input, {maxNumberOfDates: 3}));
dp.setDate('2/14/2020', '4/22/2020', '3/21/2020');
expect(dp.dates, 'to equal', [
dateValue(2020, 1, 14),
dateValue(2020, 3, 22),
dateValue(2020, 2, 21),
]);
expect(input.value, 'to be', '02/14/2020,04/22/2020,03/21/2020');
dp.destroy();
input.value = '';
({dp, picker} = createDP(input, {maxNumberOfDates: 3}));
dp.show();
getCells(picker)[1].click();
getCells(picker)[40].click();
cells = getCells(picker);
cells[19].click();
expect(dp.dates, 'to equal', [
dateValue(2020, 0, 27),
dateValue(2020, 1, 7),
dateValue(2020, 1, 14),
]);
expect(input.value, 'to be', '01/27/2020,02/07/2020,02/14/2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[1], cells[12], cells[19]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
input.value = '2/3/2020,2/22/2020';
dp.update();
expect(dp.dates, 'to equal', [dateValue(2020, 1, 3), dateValue(2020, 1, 22)]);
expect(input.value, 'to be', '02/03/2020,02/22/2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[8], cells[27]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[27]]);
// setting initial dates does not cuase error
// (issue #51)
dp.destroy();
input.value = '02/14/2020,04/22/2020,03/21/2020';
({dp, picker} = createDP(input, {maxNumberOfDates: 2}));
expect(dp.dates, 'to equal', [
dateValue(2020, 3, 22),
dateValue(2020, 2, 21),
]);
expect(input.value, 'to be', '04/22/2020,03/21/2020');
dp.destroy();
input.value = '';
});
it('makes the picker deselect the date when a selected date is clicked if value != 1', function () {
const {dp, picker} = createDP(input, {maxNumberOfDates: 3});
dp.show();
let cells = getCells(picker);
cells[19].click();
cells[12].click();
cells[19].click();
expect(dp.dates, 'to equal', [dateValue(2020, 1, 7)]);
expect(input.value, 'to be', '02/07/2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[12]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[12]]);
cells[12].click();
expect(dp.dates, 'to equal', []);
expect(input.value, 'to be', '');
expect(filterCells(cells, '.selected'), 'to equal', []);
// view date is changed to the default view date
expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
dp.destroy();
input.value = '';
});
it('makes the picker deselect the date when a selected date is clicked if value != 1', function () {
const {dp, picker} = createDP(input, {maxNumberOfDates: 3});
const cells = getCells(picker);
dp.setDate('2/14/2020', '2/7/2020');
dp.show();
dp.setDate('2/14/2020');
expect(dp.dates, 'to equal', [dateValue(2020, 1, 7)]);
expect(input.value, 'to be', '02/07/2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[12]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[12]]);
dp.setDate('2/11/2020', '2/7/2020', '2/14/2020');
expect(dp.dates, 'to equal', [dateValue(2020, 1, 11), dateValue(2020, 1, 14)]);
expect(input.value, 'to be', '02/11/2020,02/14/2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[16], cells[19]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
dp.destroy();
input.value = '';
});
it('setDate() replaces the selection instead of deselect/merg-ing if clear: true option is passed', function () {
const {dp, picker} = createDP(input, {maxNumberOfDates: 3});
const cells = getCells(picker);
dp.setDate('2/14/2020', '2/7/2020');
dp.show();
dp.setDate('2/14/2020', {clear: true});
expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
expect(input.value, 'to be', '02/14/2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[19]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
dp.setDate('2/11/2020', '2/7/2020', '2/14/2020', {clear: true});
expect(dp.dates, 'to equal', [
dateValue(2020, 1, 11),
dateValue(2020, 1, 7),
dateValue(2020, 1, 14),
]);
expect(input.value, 'to be', '02/11/2020,02/07/2020,02/14/2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[16], cells[12], cells[19]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
dp.destroy();
input.value = '';
});
it('setDate() does nothing if no dates or all-invalid dates are passed', function () {
const dp = new Datepicker(input, {maxNumberOfDates: 3});
dp.setDate('2/14/2020', '2/7/2020');
dp.show();
const origDates = [...dp.dates];
dp.setDate([]);
expect(dp.dates, 'to equal', origDates);
expect(input.value, 'to be', '02/14/2020,02/07/2020');
dp.setDate();
expect(dp.dates, 'to equal', origDates);
expect(input.value, 'to be', '02/14/2020,02/07/2020');
dp.setDate([false, NaN], {clear: true});
expect(dp.dates, 'to equal', origDates);
expect(input.value, 'to be', '02/14/2020,02/07/2020');
dp.setDate('', null);
expect(dp.dates, 'to equal', origDates);
expect(input.value, 'to be', '02/14/2020,02/07/2020');
dp.destroy();
});
it('setDate() clears all selected dates if no dates + clear: true option are passed', function () {
const dp = new Datepicker(input, {maxNumberOfDates: 3});
dp.setDate('2/14/2020', '2/7/2020');
dp.show();
dp.setDate([], {clear: true});
expect(dp.dates, 'to equal', []);
expect(input.value, 'to be', '');
dp.setDate('2/14/2020', '2/7/2020');
dp.setDate({clear: true});
expect(dp.dates, 'to equal', []);
expect(input.value, 'to be', '');
dp.destroy();
});
it('setDate() does nothing if all-invalid dates + clear: true option are passed', function () {
const dp = new Datepicker(input, {maxNumberOfDates: 3});
dp.setDate('2/14/2020', '2/7/2020');
dp.show();
const origDates = [...dp.dates];
dp.setDate([false, NaN], {clear: true});
expect(dp.dates, 'to equal', origDates);
expect(input.value, 'to be', '02/14/2020,02/07/2020');
dp.setDate('', null, {clear: true});
expect(dp.dates, 'to equal', origDates);
expect(input.value, 'to be', '02/14/2020,02/07/2020');
dp.destroy();
});
it('does not apply deselecting behavior to update()', function () {
const {dp, picker} = createDP(input, {maxNumberOfDates: 3});
const cells = getCells(picker);
dp.setDate('2/14/2020', '2/7/2020');
dp.show();
input.value = '2/14/2020';
dp.update();
expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
expect(input.value, 'to be', '02/14/2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[19]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
input.value = '2/11/2020,2/7/2020,2/14/2020';
dp.update();
expect(dp.dates, 'to equal', [
dateValue(2020, 1, 11),
dateValue(2020, 1, 7),
dateValue(2020, 1, 14),
]);
expect(input.value, 'to be', '02/11/2020,02/07/2020,02/14/2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[16], cells[12], cells[19]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
dp.destroy();
input.value = '';
});
it('makes getDate() return array of dates if value != 1', function () {
const dp = new Datepicker(input, {maxNumberOfDates: 3});
expect(dp.getDate(), 'to equal', []);
expect(dp.getDate('yyyy-mm-dd'), 'to equal', []);
dp.setDate('2/11/2020', '2/7/2020', '2/14/2020');
expect(dp.getDate(), 'to equal', [new Date(2020, 1, 11), new Date(2020, 1, 7), new Date(2020, 1, 14)]);
expect(dp.getDate('yyyy-mm-dd'), 'to equal', ['2020-02-11', '2020-02-07', '2020-02-14']);
dp.setDate('2/7/2020', {clear: true});
expect(dp.getDate(), 'to equal', [new Date(2020, 1, 7)]);
expect(dp.getDate('d M, yyyy'), 'to equal', ['7 Feb, 2020']);
const changeDateListener = (e) => {
evt = e;
};
let evt;
input.addEventListener('changeDate', changeDateListener);
dp.setDate('7/4/2020', '7/14/2020');
expect(evt.detail.date, 'to equal', dp.getDate());
input.removeEventListener('changeDate', changeDateListener);
dp.destroy();
input.value = '';
});
it('value 0 is considered unlimited', function () {
if (window.navigator.userAgent.indexOf('Edge') > -1) {
this.timeout(5000);
}
const max = new Date(2100, 0, 1).getTime();
const generateDates = (dates, length, index = 0) => {
const date = dateUtils.stripTime(Math.floor(Math.random() * max));
if (dates.includes(date)) {
return generateDates(dates, length, index);
} else {
dates.push(date);
return index <= length
? generateDates(dates, length, index + 1)
: dates;
}
};
const dates = generateDates([], 3000);
const dp = new Datepicker(input, {maxNumberOfDates: 0});
dp.setDate(dates);
expect(dp.dates, 'to equal', dates);
dp.destroy();
input.value = '';
});
it('can be updated with setOptions()', function () {
const dp = new Datepicker(input);
dp.setOptions({maxNumberOfDates: 3});
dp.setDate('2/11/2020', '2/7/2020', '2/14/2020');
expect(dp.dates, 'to equal', [
dateValue(2020, 1, 11),
dateValue(2020, 1, 7),
dateValue(2020, 1, 14),
]);
dp.setOptions({maxNumberOfDates: 1});
dp.setDate('7/4/2020', '4/22/2020');
expect(dp.dates, 'to equal', [dateValue(2020, 3, 22)]);
expect(dp.getDate(), 'to be a date');
dp.destroy();
input.value = '';
});
});
describe('dateDelimiter', function () {
it('specifies the date delemiter for the input string', function () {
const dp = new Datepicker(input, {maxNumberOfDates: 3, dateDelimiter: '|'});
dp.setDate('2/14/2020', '4/22/2020');
expect(input.value, 'to be', '02/14/2020|04/22/2020');
input.value = '2/11/2020|2/7/2020|2/14/2020';
dp.update();
expect(dp.dates, 'to equal', [
dateValue(2020, 1, 11),
dateValue(2020, 1, 7),
dateValue(2020, 1, 14),
]);
dp.destroy();
input.value = '';
});
it('can be updated with setOptions()', function () {
const dp = new Datepicker(input, {maxNumberOfDates: 3});
dp.setOptions({dateDelimiter: '_'});
dp.setDate('2/11/2020', '2/7/2020', '2/14/2020');
dp.setOptions({dateDelimiter: ' - '});
expect(input.value, 'to be', '02/11/2020 - 02/07/2020 - 02/14/2020');
dp.setOptions({dateDelimiter: ','});
expect(input.value, 'to be', '02/11/2020,02/07/2020,02/14/2020');
dp.destroy();
input.value = '';
});
});
});

View File

@ -0,0 +1,727 @@
describe('options', function () {
let clock;
let input;
beforeEach(function () {
clock = sinon.useFakeTimers({now: new Date(2020, 1, 14)});
input = document.createElement('input');
testContainer.appendChild(input);
});
afterEach(function () {
testContainer.removeChild(input);
clock.restore();
});
describe('autohide', function () {
it('makes the picker hide automatically on selection when true', function () {
const {dp, picker} = createDP(input, {autohide: true});
dp.show();
// by satDate()
dp.setDate('2/4/2020');
expect(isVisible(picker), 'to be false');
dp.show();
// by click on day cell
getCells(picker)[25].click();
expect(isVisible(picker), 'to be false');
dp.show();
// by typing enter key in edit mode
dp.enterEditMode();
input.value = '2/14/2020';
simulant.fire(input, 'keydown', {key: 'Enter'});
expect(isVisible(picker), 'to be false');
// focus is kept on input field after auto-hidng by clicking day cell
// (issue #21)
const spyFocus = sinon.spy(input, 'focus');
dp.show();
getCells(picker)[25].click();
expect(spyFocus.called, 'to be true');
spyFocus.restore();
dp.destroy();
input.value = '';
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
dp.setOptions({autohide: true});
dp.show();
dp.setDate('2/4/2020');
expect(isVisible(picker), 'to be false');
dp.setOptions({autohide: false});
dp.show();
dp.setDate('2/14/2020');
expect(isVisible(picker), 'to be true');
dp.destroy();
input.value = '';
});
});
describe('buttonClass', function () {
it('specifies the main class used for the button elements', function () {
const {dp, picker} = createDP(input, {buttonClass: 'btn'});
const [viewSwitch, prevBtn, nextBtn, todayBtn, clearBtn] = getParts(picker, [
'.view-switch',
'.prev-btn',
'.next-btn',
'.today-btn',
'.clear-btn',
]);
expect(viewSwitch.className, 'to be', 'btn view-switch');
expect(prevBtn.className, 'to be', 'btn prev-btn');
expect(nextBtn.className, 'to be', 'btn next-btn');
expect(todayBtn.className, 'to be', 'btn today-btn');
expect(clearBtn.className, 'to be', 'btn clear-btn');
dp.destroy();
});
it('cannot be update with setOptions()', function () {
const {dp, picker} = createDP(input);
dp.setOptions({buttonClass: 'btn'});
const [viewSwitch, prevBtn, nextBtn, todayBtn, clearBtn] = getParts(picker, [
'.view-switch',
'.prev-btn',
'.next-btn',
'.today-btn',
'.clear-btn',
]);
expect(viewSwitch.className, 'to be', 'button view-switch');
expect(prevBtn.className, 'to be', 'button prev-btn');
expect(nextBtn.className, 'to be', 'button next-btn');
expect(todayBtn.className, 'to be', 'button today-btn');
expect(clearBtn.className, 'to be', 'button clear-btn');
dp.destroy();
});
});
describe('calendarWeeks', function () {
const getDisplayedWeeks = (picker) => {
const calendarWeeks = picker.querySelector('.calendar-weeks');
return Array.from(calendarWeeks.querySelectorAll('.week')).map(el => el.textContent);
};
it('enables display ISO weeks in days view when true', function () {
const {dp, picker} = createDP(input, {calendarWeeks: true});
const [viewSwitch, prevBtn] = getParts(picker, ['.view-switch', '.prev-btn']);
dp.show();
let calendarWeeks = picker.querySelector('.calendar-weeks');
expect(isVisible(calendarWeeks), 'to be true');
expect(getDisplayedWeeks(picker), 'to equal', ['5', '6', '7', '8', '9', '10']);
prevBtn.click();
expect(getDisplayedWeeks(picker), 'to equal', ['1', '2', '3', '4', '5', '6']);
prevBtn.click();
expect(getDisplayedWeeks(picker), 'to equal', ['48', '49', '50', '51', '52', '1']);
prevBtn.click();
expect(getDisplayedWeeks(picker), 'to equal', ['44', '45', '46', '47', '48', '49']);
dp.setDate('01/01/2021');
expect(getDisplayedWeeks(picker), 'to equal', ['53', '1', '2', '3', '4', '5']);
prevBtn.click();
expect(getDisplayedWeeks(picker), 'to equal', ['49', '50', '51', '52', '53', '1']);
// months view
viewSwitch.click();
expect(picker.querySelector('.calendar-weeks'), 'to be null');
// years view
viewSwitch.click();
expect(picker.querySelector('.calendar-weeks'), 'to be null');
// decades view
viewSwitch.click();
expect(picker.querySelector('.calendar-weeks'), 'to be null');
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
dp.setOptions({calendarWeeks: true});
dp.show();
expect(isVisible(picker.querySelector('.calendar-weeks')), 'to be true');
dp.setOptions({calendarWeeks: false});
expect(picker.querySelector('.calendar-weeks'), 'to be null');
dp.destroy();
});
});
describe('container', function () {
let foo;
beforeEach(function () {
foo = parseHTML('<div id="foo"><div>').firstChild;
testContainer.appendChild(foo);
});
afterEach(function () {
testContainer.removeChild(foo);
});
it('specifies the element to attach the picker', function () {
const dp = new Datepicker(input, {container: '#foo'});
expect(document.querySelector('.datepicker').parentElement, 'to be', foo);
dp.destroy();
});
it('cannot be update with setOptions()', function () {
const dp = new Datepicker(input);
dp.setOptions({container: '#foo'});
expect(document.querySelector('.datepicker').parentElement, 'to be', document.body);
dp.destroy();
});
});
describe('daysOfWeekHighlighted', function () {
const highlightedCellIndices = (picker) => {
const cells = getCells(picker);
return filterCells(cells, '.highlighted').map(el => cells.indexOf(el));
};
const highlighted1stWeekIndices = picker => highlightedCellIndices(picker).filter(val => val < 7);
it('specifies the days of week to highlight by dey numbers', function () {
const {dp, picker} = createDP(input, {daysOfWeekHighlighted: [1, 5]});
dp.show();
expect(highlightedCellIndices(picker), 'to equal', [1, 5, 8, 12, 15, 19, 22, 26, 29, 33, 36, 40]);
const viewSwitch = getViewSwitch(picker);
// months view
viewSwitch.click();
expect(highlightedCellIndices(picker), 'to equal', []);
// years view
viewSwitch.click();
expect(highlightedCellIndices(picker), 'to equal', []);
// decades view
viewSwitch.click();
expect(highlightedCellIndices(picker), 'to equal', []);
dp.destroy();
});
it('can contain values of 0 - 6 and max 6 items', function () {
const {dp, picker} = createDP(input, {daysOfWeekHighlighted: [0, -1, 1, 2, 3, 2, 4, 5, 6, 7]});
dp.show();
expect(highlighted1stWeekIndices(picker), 'to equal', [0, 1, 2, 3, 4, 5]);
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
dp.setOptions({daysOfWeekHighlighted: [6, 0, 3]});
dp.show();
expect(highlighted1stWeekIndices(picker), 'to equal', [0, 3, 6]);
dp.setOptions({daysOfWeekHighlighted: []});
expect(highlightedCellIndices(picker), 'to equal', []);
dp.destroy();
});
});
describe('defaultViewDate', function () {
it('specifies the start view date in the case no selection is made', function () {
const date = new Date(1984, 0, 24);
const {dp, picker} = createDP(input, {defaultViewDate: date});
dp.show();
expect(getViewSwitch(picker).textContent, 'to be', 'January 1984');
let cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[23]]);
expect(cells[23].textContent, 'to be', '24');
dp.setDate('7/4/2020');
dp.setDate({clear: true});
expect(getViewSwitch(picker).textContent, 'to be', 'January 1984');
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[23]]);
expect(cells[23].textContent, 'to be', '24');
picker.querySelector('.prev-btn').click();
dp.hide();
dp.show();
expect(getViewSwitch(picker).textContent, 'to be', 'January 1984');
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[23]]);
expect(cells[23].textContent, 'to be', '24');
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
dp.show();
dp.setOptions({defaultViewDate: new Date(1984, 0, 24)});
dp.hide();
dp.show();
expect(getViewSwitch(picker).textContent, 'to be', 'January 1984');
let cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[23]]);
expect(cells[23].textContent, 'to be', '24');
dp.setOptions({defaultViewDate: new Date(2007, 5, 29)});
dp.setDate('7/4/2020');
dp.setDate({clear: true});
expect(getViewSwitch(picker).textContent, 'to be', 'June 2007');
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[33]]);
expect(cells[33].textContent, 'to be', '29');
dp.destroy();
});
});
describe('disableTouchKeyboard', function () {
const ontouchstartSupported = 'ontouchstart' in document;
before(function () {
if (!ontouchstartSupported) {
document.ontouchstart = null;
}
});
after(function () {
if (!ontouchstartSupported) {
delete document.ontouchstart;
}
});
it('unfocuses the input after showing the picker', function () {
const dp = new Datepicker(input, {disableTouchKeyboard: true});
input.focus();
expect(document.activeElement, 'not to be', input);
dp.destroy();
});
it('prevents the input from getting focus after an eleent in the picker is clicked', function () {
const {dp, picker} = createDP(input, {disableTouchKeyboard: true});
const [viewSwitch, prevBtn] = getParts(picker, ['.view-switch', '.prev-btn']);
dp.show();
prevBtn.focus();
simulant.fire(prevBtn, 'click');
expect(document.activeElement, 'not to be', input);
simulant.fire(getCells(picker)[15], 'click');
expect(document.activeElement, 'not to be', input);
viewSwitch.focus();
simulant.fire(viewSwitch, 'click');
expect(document.activeElement, 'not to be', input);
simulant.fire(getCells(picker)[6], 'click');
expect(document.activeElement, 'not to be', input);
dp.destroy();
});
it('is ignored if the browser does not support document.ontouchstart', function () {
if (ontouchstartSupported) {
return;
}
delete document.ontouchstart;
const {dp, picker} = createDP(input, {disableTouchKeyboard: true});
const [viewSwitch, prevBtn] = getParts(picker, ['.view-switch', '.prev-btn']);
input.focus();
expect(document.activeElement, 'to be', input);
prevBtn.focus();
simulant.fire(prevBtn, 'click');
expect(document.activeElement, 'to be', input);
prevBtn.focus();
simulant.fire(getCells(picker)[15], 'click');
expect(document.activeElement, 'to be', input);
viewSwitch.focus();
simulant.fire(viewSwitch, 'click');
expect(document.activeElement, 'to be', input);
viewSwitch.focus();
simulant.fire(getCells(picker)[6], 'click');
expect(document.activeElement, 'to be', input);
dp.destroy();
document.ontouchstart = null;
});
it('can be updated with setOptions()', function () {
const dp = new Datepicker(input);
dp.setOptions({disableTouchKeyboard: true});
input.focus();
expect(document.activeElement, 'not to be', input);
dp.hide();
dp.setOptions({disableTouchKeyboard: false});
input.focus();
expect(document.activeElement, 'to be', input);
dp.destroy();
});
});
describe('nextArrow', function () {
it('specifies the label of the next button in HTML (or plain text)', function () {
const html = '<i class="icn icn-arrow-right"></i>';
const {dp, picker} = createDP(input, {nextArrow: html});
const nextBtn = picker.querySelector('.next-btn');
dp.show();
expect(nextBtn.innerHTML, 'to be', html);
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
const nextBtn = picker.querySelector('.next-btn');
dp.setOptions({nextArrow: 'N'});
dp.show();
expect(nextBtn.textContent, 'to be', 'N');
dp.setOptions({nextArrow: '>'});
expect(nextBtn.textContent, 'to be', '>');
dp.destroy();
});
});
describe('prevArrow', function () {
it('specifies the label of the next button in HTML (or plain text)', function () {
const html = '<i class="icn icn-arrow-left"></i>';
const {dp, picker} = createDP(input, {prevArrow: html});
const prevBtn = picker.querySelector('.prev-btn');
dp.show();
expect(prevBtn.innerHTML, 'to be', html);
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
const prevBtn = picker.querySelector('.prev-btn');
dp.setOptions({prevArrow: 'P'});
dp.show();
expect(prevBtn.textContent, 'to be', 'P');
dp.setOptions({prevArrow: '<'});
expect(prevBtn.textContent, 'to be', '<');
dp.destroy();
});
});
describe('showDaysOfWeek', function () {
it('hides day names of week when false', function () {
const {dp, picker} = createDP(input, {showDaysOfWeek: false});
dp.show();
expect(isVisible(picker.querySelector('.days-of-week')), 'to be false');
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
dp.setOptions({showDaysOfWeek: false});
dp.show();
expect(isVisible(picker.querySelector('.days-of-week')), 'to be false');
dp.setOptions({showDaysOfWeek: true});
expect(isVisible(picker.querySelector('.days-of-week')), 'to be true');
dp.destroy();
});
});
describe('showOnClick', function () {
it('disables the picker to auto-open on clicking input when false', function () {
const {dp, picker} = createDP(input, {showOnClick: false});
input.focus();
dp.hide();
simulant.fire(input, 'mousedown');
input.click();
expect(isVisible(picker), 'to be false');
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
dp.setOptions({showOnClick: false});
input.focus();
dp.hide();
simulant.fire(input, 'mousedown');
input.click();
expect(isVisible(picker), 'to be false');
dp.setOptions({showOnClick: true});
simulant.fire(input, 'mousedown');
input.click();
expect(isVisible(picker), 'to be true');
dp.destroy();
});
});
describe('showOnFocus', function () {
it('disables the picker to auto-open on focus when false', function () {
const {dp, picker} = createDP(input, {showOnFocus: false});
input.focus();
expect(isVisible(picker), 'to be false');
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
dp.setOptions({showOnFocus: false});
input.focus();
expect(isVisible(picker), 'to be false');
input.blur();
dp.setOptions({showOnFocus: true});
input.focus();
expect(isVisible(picker), 'to be true');
dp.destroy();
});
});
describe('title', function () {
it('specifies the title of the picker and shows it when not empty', function () {
const {dp, picker} = createDP(input, {title: 'Foo Bar'});
const title = picker.querySelector('.datepicker-title');
dp.show();
expect(title.textContent, 'to be', 'Foo Bar');
expect(isVisible(title), 'to be true');
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
const title = picker.querySelector('.datepicker-title');
dp.setOptions({title: 'My Datepicker'});
dp.show();
expect(title.textContent, 'to be', 'My Datepicker');
expect(isVisible(title), 'to be true');
dp.setOptions({title: ''});
expect(title.textContent, 'to be', '');
expect(isVisible(title), 'to be false');
dp.destroy();
});
});
describe('todayHighlight', function () {
it('highlights the current date in days view when true', function () {
const {dp, picker} = createDP(input, {todayHighlight: true});
const viewSwitch = getViewSwitch(picker);
dp.show();
let cells = getCells(picker);
expect(filterCells(cells, '.today'), 'to equal', [cells[19]]);
picker.querySelector('.prev-btn').click();
expect(filterCells(getCells(picker), '.today'), 'to equal', []);
picker.querySelector('.next-btn').click();
viewSwitch.click();
expect(filterCells(getCells(picker), '.today'), 'to equal', []);
viewSwitch.click();
expect(filterCells(getCells(picker), '.today'), 'to equal', []);
viewSwitch.click();
expect(filterCells(getCells(picker), '.today'), 'to equal', []);
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
dp.setOptions({todayHighlight: true});
dp.show();
let cells = getCells(picker);
expect(filterCells(cells, '.today'), 'to equal', [cells[19]]);
dp.setOptions({todayHighlight: false});
cells = getCells(picker);
expect(filterCells(cells, '.today'), 'to equal', []);
dp.destroy();
});
});
describe('updateOnBlur', function () {
it('discards unparsed input on losing focus when false', function () {
const outsider = document.createElement('p');
testContainer.appendChild(outsider);
const {dp, picker} = createDP(input, {updateOnBlur: false});
input.focus();
input.value = 'foo';
// on tab key press
simulant.fire(input, 'keydown', {key: 'Tab'});
expect(input.value, 'to be', '');
dp.setDate('04/22/2020');
dp.show();
dp.enterEditMode();
input.value = 'foo';
simulant.fire(input, 'keydown', {key: 'Tab'});
expect(input.value, 'to be', '04/22/2020');
// on click outside
dp.show();
input.value = 'foo';
simulant.fire(picker.querySelector('.dow'), 'mousedown');
expect(input.value, 'to be', 'foo');
simulant.fire(input, 'mousedown');
expect(input.value, 'to be', 'foo');
simulant.fire(outsider, 'mousedown');
expect(input.value, 'to be', '04/22/2020');
dp.setDate({clear: true});
input.value = 'foo';
simulant.fire(outsider, 'mousedown');
expect(input.value, 'to be', '');
dp.destroy();
testContainer.removeChild(outsider);
});
it('can be updated with setOptions()', function () {
const dp = new Datepicker(input);
dp.setOptions({updateOnBlur: false});
input.focus();
input.value = '04/22/2020';
simulant.fire(input, 'keydown', {key: 'Tab'});
expect(input.value, 'to be', '');
dp.setOptions({updateOnBlur: true});
input.focus();
input.value = '04/22/2020';
simulant.fire(input, 'keydown', {key: 'Tab'});
expect(input.value, 'to be', '04/22/2020');
dp.destroy();
});
});
describe('weekStart', function () {
const getDayNames = (picker) => {
const daysOfWeek = picker.querySelector('.days-of-week');
return Array.from(daysOfWeek.children).map(el => el.textContent);
};
const getDatesInColumn = (picker, colIndex) => {
const cells = getCells(picker);
return cells.reduce((dates, el, ix) => {
if (ix % 7 === colIndex) {
dates.push(el.textContent);
}
return dates;
}, []);
};
it('specifies the day of week to display in the first column', function () {
const {dp, picker} = createDP(input, {weekStart: 1});
dp.show();
expect(getDayNames(picker), 'to equal', ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su']);
expect(getDatesInColumn(picker, 0), 'to equal', ['27', '3', '10', '17', '24', '2']);
expect(getDatesInColumn(picker, 6), 'to equal', ['2', '9', '16', '23', '1', '8']);
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
dp.setOptions({weekStart: 4});
dp.show();
expect(getDayNames(picker), 'to equal', ['Th', 'Fr', 'Sa', 'Su', 'Mo', 'Tu', 'We']);
expect(getDatesInColumn(picker, 0), 'to equal', ['30', '6', '13', '20', '27', '5']);
expect(getDatesInColumn(picker, 6), 'to equal', ['5', '12', '19', '26', '4', '11']);
dp.setOptions({weekStart: 0});
expect(getDayNames(picker), 'to equal', ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa']);
expect(getDatesInColumn(picker, 0), 'to equal', ['26', '2', '9', '16', '23', '1']);
expect(getDatesInColumn(picker, 6), 'to equal', ['1', '8', '15', '22', '29', '7']);
dp.destroy();
});
});
});

View File

@ -0,0 +1,410 @@
describe('options - orientation', function () {
const getIntSize = px => Math.round(parseFloat(px));
const getTopPos = (el, wrap) => el.offsetTop + (wrap ? wrap.offsetTop : 0) + window.scrollY;
const getLeftPos = (el, wrap) => el.offsetLeft + (wrap ? wrap.offsetLeft : 0) + window.scrollX;
const getBottomPos = (el, wrap) => getTopPos(el, wrap) + el.offsetHeight;
const getRightPos = (el, wrap) => getLeftPos(el, wrap) + el.offsetWidth;
let wrapper;
let input;
beforeEach(function () {
wrapper = document.createElement('div');
Object.assign(wrapper.style, {
boxSizing: 'border-box',
position: 'fixed',
top: '50px',
left: '50px',
width: '300px',
paddingTop: '300px',
});
input = document.createElement('input');
wrapper.appendChild(input);
testContainer.appendChild(wrapper);
});
afterEach(function () {
domUtils.emptyChildNodes(testContainer);
});
it('"auto" makes the picker show on top left of the input by default', function () {
const {dp, picker} = createDP(input);
dp.show();
expect(getIntSize(picker.style.top), 'to be close to', getTopPos(input, wrapper) - picker.offsetHeight, 1);
expect(getIntSize(picker.style.left), 'to be close to', getLeftPos(input, wrapper), 1);
expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
dp.destroy();
});
it('"auto" makes the picker show on top right of the input if computed style of the input has direction: rrl', function () {
const {dp, picker} = createDP(input);
wrapper.setAttribute('dir', 'rtl');
dp.show();
expect(getIntSize(picker.style.top), 'to be close to', getTopPos(input, wrapper) - picker.offsetHeight, 1);
expect(getIntSize(picker.style.left), 'to be close to', getRightPos(input, wrapper) - picker.offsetWidth, 1);
expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
dp.destroy();
});
it('"auto" makes the picker show on bottom of the input if visible space above the input < picker height', function () {
const {dp, picker} = createDP(input);
wrapper.style.paddingTop = '0';
dp.show();
expect(getIntSize(picker.style.top), 'to be close to', getBottomPos(input, wrapper), 1);
expect(getIntSize(picker.style.left), 'to be close to', getLeftPos(input, wrapper), 1);
expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
dp.hide();
wrapper.setAttribute('dir', 'rtl');
dp.show();
expect(getIntSize(picker.style.top), 'to be close to', getBottomPos(input, wrapper), 1);
expect(getIntSize(picker.style.left), 'to be close to', getRightPos(input, wrapper) - picker.offsetWidth, 1);
expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
dp.destroy();
});
it('"auto" makes the picker move to 10px from document\'s left if picker\'s left < document\'s', function () {
const {dp, picker} = createDP(input);
wrapper.style.left = '-40px';
dp.show();
expect(getIntSize(picker.style.left), 'to be', 10);
expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
dp.destroy();
});
it('"auto" makes the picker show on right if picker\'s right edge > document\'s', function () {
const {dp, picker} = createDP(input);
Object.assign(wrapper.style, {left: 'auto', right: 0, width: '150px'});
dp.show();
expect(getIntSize(picker.style.left), 'to be close to', getRightPos(input, wrapper) - picker.offsetWidth, 1);
expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
dp.destroy();
});
it('"top" makes the picker show on top of the input regardless of the size of the space above', function () {
const {dp, picker} = createDP(input, {orientation: 'top'});
dp.show();
expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
dp.hide();
wrapper.style.paddingTop = '0';
dp.show();
expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
dp.hide();
wrapper.setAttribute('dir', 'rtl');
dp.show();
expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
dp.destroy();
});
it('"bottom" makes the picker show on bottom of the input regardless of the size of the space below', function () {
wrapper.style.paddingTop = '0';
const {dp, picker} = createDP(input, {orientation: 'bottom'});
dp.show();
expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
dp.hide();
Object.assign(wrapper.style, {top: 'auto', bottom: '0'});
dp.show();
expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
dp.hide();
wrapper.setAttribute('dir', 'rtl');
dp.show();
expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
dp.destroy();
});
it('"left" makes the picker show on left of the input regardless of the direction of the input', function () {
const {dp, picker} = createDP(input, {orientation: 'left'});
dp.show();
expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
dp.hide();
wrapper.setAttribute('dir', 'rtl');
dp.show();
expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
dp.destroy();
});
it('"right" makes the picker show on right of the input regardless of the direction of the input', function () {
const {dp, picker} = createDP(input, {orientation: 'right'});
dp.show();
expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
dp.hide();
wrapper.setAttribute('dir', 'rtl');
dp.show();
expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
dp.destroy();
});
it('"top left" makes the picker always show on top left of the input', function () {
wrapper.style.paddingTop = '0';
const {dp, picker} = createDP(input, {orientation: 'top left'});
dp.show();
expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
dp.hide();
Object.assign(wrapper.style, {top: 'auto', bottom: '0'});
dp.show();
expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
dp.hide();
wrapper.setAttribute('dir', 'rtl');
dp.show();
expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
dp.hide();
Object.assign(wrapper.style, {top: '0', bottom: ''});
dp.show();
expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
dp.destroy();
});
it('"top right" makes the picker always show on top right of the input', function () {
wrapper.style.paddingTop = '0';
const {dp, picker} = createDP(input, {orientation: 'top right'});
dp.show();
expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
dp.hide();
Object.assign(wrapper.style, {top: 'auto', bottom: '0'});
dp.show();
expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
dp.hide();
wrapper.setAttribute('dir', 'rtl');
dp.show();
expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
dp.hide();
Object.assign(wrapper.style, {top: '0', bottom: ''});
dp.show();
expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
dp.destroy();
});
it('"bottom left" makes the picker always show on bottom left of the input', function () {
wrapper.style.paddingTop = '0';
const {dp, picker} = createDP(input, {orientation: 'bottom left'});
dp.show();
expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
dp.hide();
Object.assign(wrapper.style, {top: 'auto', bottom: '0'});
dp.show();
expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
dp.hide();
wrapper.setAttribute('dir', 'rtl');
dp.show();
expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
dp.hide();
Object.assign(wrapper.style, {top: '0', bottom: ''});
dp.show();
expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
dp.destroy();
});
it('"bottom right" makes the picker always show on bottom right of the input', function () {
wrapper.style.paddingTop = '0';
const {dp, picker} = createDP(input, {orientation: 'bottom right'});
dp.show();
expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
dp.hide();
Object.assign(wrapper.style, {top: 'auto', bottom: '0'});
dp.show();
expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
dp.hide();
wrapper.setAttribute('dir', 'rtl');
dp.show();
expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
dp.hide();
Object.assign(wrapper.style, {top: '0', bottom: ''});
dp.show();
expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
dp.setOptions({orientation: 'right bottom'});
dp.show();
expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
dp.hide();
dp.setOptions({orientation: 'bottom auto'});
dp.show();
expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
dp.hide();
dp.setOptions({orientation: 'auto right'});
dp.show();
expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
dp.hide();
dp.setOptions({orientation: 'auto'});
dp.show();
expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
dp.destroy();
});
describe('with custom container', function () {
let foo;
beforeEach(function () {
foo = parseHTML('<div id="foo"></div>').firstChild;
Object.assign(foo.style, {
boxSizing: 'border-box',
position: 'fixed',
top: '20px',
left: '20px',
height: '360px',
overflow: 'auto',
});
Object.assign(wrapper.style, {position: '', top: '', left: ''});
testContainer.replaceChild(foo, wrapper);
foo.appendChild(wrapper);
});
it('makes the picker\'s position relative to the container', function () {
const {dp, picker} = createDP(input, {container: '#foo'});
dp.show();
expect(getIntSize(picker.style.top), 'to be', input.offsetTop - picker.offsetHeight);
expect(getIntSize(picker.style.left), 'to be', input.offsetLeft);
expect(picker.classList.contains('datepicker-orient-top'), 'to be true');
expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
dp.hide();
wrapper.style.paddingBottom = '200px';
foo.scrollTop = 100;
dp.show();
expect(getIntSize(picker.style.top), 'to be', input.offsetTop + input.offsetHeight);
expect(getIntSize(picker.style.left), 'to be', input.offsetLeft);
expect(picker.classList.contains('datepicker-orient-bottom'), 'to be true');
expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
foo.scrollTop = 0;
wrapper.style.paddingBottom = '';
dp.destroy();
});
it('"auto" makes the picker move to 10px from container\'s left if picker\'s left < container\'s', function () {
const {dp, picker} = createDP(input, {container: '#foo'});
wrapper.style.marginLeft = '-40px';
dp.show();
expect(getIntSize(picker.style.left), 'to be', 10);
expect(picker.classList.contains('datepicker-orient-left'), 'to be true');
dp.destroy();
});
it('"auto" makes the picker show on right if picker\'s right edge < container\'s', function () {
const {dp, picker} = createDP(input, {container: '#foo'});
wrapper.style.width = '150px';
dp.show();
expect(getIntSize(picker.style.left), 'to be', input.offsetLeft + input.offsetWidth - picker.offsetWidth);
expect(picker.classList.contains('datepicker-orient-right'), 'to be true');
dp.destroy();
});
});
});

View File

@ -0,0 +1,357 @@
describe('options - pick level & view', function () {
let clock;
let input;
beforeEach(function () {
clock = sinon.useFakeTimers({now: new Date(2020, 1, 14)});
input = document.createElement('input');
testContainer.appendChild(input);
});
afterEach(function () {
testContainer.removeChild(input);
clock.restore();
});
describe('pickLevel', function () {
it('limits the minimum of available views', function () {
const {dp, picker} = createDP(input, {pickLevel: 2});
const viewSwitch = getViewSwitch(picker);
const cells1 = getCells(picker)[1];
dp.show();
expect(viewSwitch.textContent, 'to be', '2020-2029');
expect(cells1.textContent, 'to be', '2020');
cells1.click();
expect(viewSwitch.textContent, 'to be', '2020-2029');
expect(getCells(picker)[1].textContent, 'to be', '2020');
dp.destroy();
});
it('changes the selection level to month when 1', function () {
input.value = '2/14/2020';
const {dp, picker} = createDP(input, {pickLevel: 1});
const [viewSwitch, nextBtn] = getParts(picker, ['.view-switch', '.next-btn']);
let cells = getCells(picker);
dp.show();
expect(dp.dates, 'to equal', [dateValue(2020, 1, 1)]);
expect(input.value, 'to be', '02/01/2020');
expect(viewSwitch.textContent, 'to be', '2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[1]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
// mouse operation
cells[0].click();
cells = getCells(picker);
expect(dp.dates, 'to equal', [dateValue(2020, 0, 1)]);
expect(input.value, 'to be', '01/01/2020');
expect(viewSwitch.textContent, 'to be', '2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[0]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[0]]);
nextBtn.click();
getCells(picker)[7].click();
cells = getCells(picker);
expect(dp.dates, 'to equal', [dateValue(2021, 7, 1)]);
expect(input.value, 'to be', '08/01/2021');
expect(viewSwitch.textContent, 'to be', '2021');
expect(filterCells(cells, '.selected'), 'to equal', [cells[7]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[7]]);
// keyboard operation
simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
simulant.fire(input, 'keydown', {key: 'Enter'});
cells = getCells(picker);
expect(dp.dates, 'to equal', [dateValue(2020, 6, 1)]);
expect(input.value, 'to be', '07/01/2020');
expect(viewSwitch.textContent, 'to be', '2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[6]]);
dp.enterEditMode();
input.value = '4/20/2021';
simulant.fire(input, 'keydown', {key: 'Enter'});
cells = getCells(picker);
expect(dp.dates, 'to equal', [dateValue(2021, 3, 1)]);
expect(input.value, 'to be', '04/01/2021');
expect(viewSwitch.textContent, 'to be', '2021');
expect(filterCells(cells, '.selected'), 'to equal', [cells[3]]);
// api call
viewSwitch.click();
dp.setDate('2/14/2022');
cells = getCells(picker);
expect(dp.dates, 'to equal', [dateValue(2022, 1, 1)]);
expect(input.value, 'to be', '02/01/2022');
expect(viewSwitch.textContent, 'to be', '2022');
expect(filterCells(cells, '.selected'), 'to equal', [cells[1]]);
viewSwitch.click();
dp.hide();
input.value = '3/14/2020';
dp.update();
dp.show();
cells = getCells(picker);
expect(dp.dates, 'to equal', [dateValue(2020, 2, 1)]);
expect(input.value, 'to be', '03/01/2020');
expect(viewSwitch.textContent, 'to be', '2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[2]]);
dp.destroy();
});
it('changes the selection level to year when 2', function () {
input.value = '2/14/2020';
const {dp, picker} = createDP(input, {pickLevel: 2});
const [viewSwitch, nextBtn] = getParts(picker, ['.view-switch', '.next-btn']);
let cells = getCells(picker);
dp.show();
expect(dp.dates, 'to equal', [dateValue(2020, 0, 1)]);
expect(input.value, 'to be', '01/01/2020');
expect(viewSwitch.textContent, 'to be', '2020-2029');
expect(filterCells(cells, '.selected'), 'to equal', [cells[1]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
// mouse operation
cells[2].click();
cells = getCells(picker);
expect(dp.dates, 'to equal', [dateValue(2021, 0, 1)]);
expect(input.value, 'to be', '01/01/2021');
expect(viewSwitch.textContent, 'to be', '2020-2029');
expect(filterCells(cells, '.selected'), 'to equal', [cells[2]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[2]]);
nextBtn.click();
getCells(picker)[7].click();
cells = getCells(picker);
expect(dp.dates, 'to equal', [dateValue(2036, 0, 1)]);
expect(input.value, 'to be', '01/01/2036');
expect(viewSwitch.textContent, 'to be', '2030-2039');
expect(filterCells(cells, '.selected'), 'to equal', [cells[7]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[7]]);
// keyboard operation
simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
simulant.fire(input, 'keydown', {key: 'Enter'});
cells = getCells(picker);
expect(dp.dates, 'to equal', [dateValue(2025, 0, 1)]);
expect(input.value, 'to be', '01/01/2025');
expect(viewSwitch.textContent, 'to be', '2020-2029');
expect(filterCells(cells, '.selected'), 'to equal', [cells[6]]);
dp.enterEditMode();
input.value = '4/20/2021';
simulant.fire(input, 'keydown', {key: 'Enter'});
cells = getCells(picker);
expect(dp.dates, 'to equal', [dateValue(2021, 0, 1)]);
expect(input.value, 'to be', '01/01/2021');
expect(viewSwitch.textContent, 'to be', '2020-2029');
expect(filterCells(cells, '.selected'), 'to equal', [cells[2]]);
// api call
viewSwitch.click();
dp.setDate('2/14/2032');
cells = getCells(picker);
expect(dp.dates, 'to equal', [dateValue(2032, 0, 1)]);
expect(input.value, 'to be', '01/01/2032');
expect(viewSwitch.textContent, 'to be', '2030-2039');
expect(filterCells(cells, '.selected'), 'to equal', [cells[3]]);
viewSwitch.click();
dp.hide();
input.value = '3/14/2020';
dp.update();
dp.show();
cells = getCells(picker);
expect(dp.dates, 'to equal', [dateValue(2020, 0, 1)]);
expect(input.value, 'to be', '01/01/2020');
expect(viewSwitch.textContent, 'to be', '2020-2029');
expect(filterCells(cells, '.selected'), 'to equal', [cells[1]]);
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
const viewSwitch = getViewSwitch(picker);
dp.setOptions({pickLevel: 1});
dp.show();
let cells = getCells(picker);
expect(viewSwitch.textContent, 'to be', '2020');
expect(cells[1].textContent, 'to be', 'Feb');
cells[1].click();
cells = getCells(picker);
expect(dp.dates, 'to equal', [dateValue(2020, 1, 1)]);
expect(input.value, 'to be', '02/01/2020');
expect(viewSwitch.textContent, 'to be', '2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[0]]);
dp.setOptions({pickLevel: 0});
cells = getCells(picker);
expect(viewSwitch.textContent, 'to be', 'February 2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[6]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[6]]);
viewSwitch.click();
getCells(picker)[3].click();
cells = getCells(picker);
expect(viewSwitch.textContent, 'to be', 'April 2020');
expect(filterCells(cells, '.selected'), 'to equal', []);
expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
dp.destroy();
});
});
describe('maxView', function () {
it('limits the maximum of available views', function () {
const {dp, picker} = createDP(input, {maxView: 1});
const viewSwitch = getViewSwitch(picker);
dp.show();
viewSwitch.click();
viewSwitch.click();
expect(viewSwitch.textContent, 'to be', '2020');
expect(getCells(picker)[0].textContent, 'to be', 'Jan');
dp.destroy();
});
it('cannot be smaller than pickLevel', function () {
const {dp, picker} = createDP(input, {maxView: 1, pickLevel: 2});
const viewSwitch = getViewSwitch(picker);
dp.show();
expect(viewSwitch.textContent, 'to be', '2020-2029');
expect(getCells(picker)[1].textContent, 'to be', '2020');
viewSwitch.click();
expect(viewSwitch.textContent, 'to be', '2020-2029');
expect(getCells(picker)[1].textContent, 'to be', '2020');
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
const viewSwitch = getViewSwitch(picker);
dp.setOptions({maxView: 2});
dp.show();
viewSwitch.click();
viewSwitch.click();
viewSwitch.click();
expect(viewSwitch.textContent, 'to be', '2020-2029');
expect(getCells(picker)[1].textContent, 'to be', '2020');
dp.setOptions({maxView: 0});
expect(viewSwitch.textContent, 'to be', 'February 2020');
viewSwitch.click();
expect(viewSwitch.textContent, 'to be', 'February 2020');
dp.destroy();
});
});
describe('startView', function () {
it('specifies the view desplayed on open', function () {
const {dp, picker} = createDP(input, {startView: 3});
const viewSwitch = getViewSwitch(picker);
dp.show();
expect(viewSwitch.textContent, 'to be', '2000-2090');
let cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
expect(cells[3].textContent, 'to be', '2020');
dp.destroy();
});
it('cannot be smaller than pickLevel', function () {
const {dp, picker} = createDP(input, {startView: 1, pickLevel: 2});
const viewSwitch = getViewSwitch(picker);
dp.show();
expect(viewSwitch.textContent, 'to be', '2020-2029');
let cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
expect(cells[1].textContent, 'to be', '2020');
dp.destroy();
});
it('cannot be larger than maxView', function () {
const {dp, picker} = createDP(input, {startView: 3, maxView: 2});
const viewSwitch = getViewSwitch(picker);
dp.show();
expect(viewSwitch.textContent, 'to be', '2020-2029');
let cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
expect(cells[1].textContent, 'to be', '2020');
dp.destroy();
});
it('can be updated with setOptions()', function () {
const {dp, picker} = createDP(input);
const viewSwitch = getViewSwitch(picker);
dp.setOptions({startView: 2});
dp.show();
expect(viewSwitch.textContent, 'to be', '2020-2029');
let cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
expect(cells[1].textContent, 'to be', '2020');
dp.hide();
dp.setOptions({startView: 0});
dp.show();
expect(viewSwitch.textContent, 'to be', 'February 2020');
cells = getCells(picker);
expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
expect(cells[19].textContent, 'to be', '14');
dp.destroy();
});
});
});