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,236 @@
describe('Datepicker', function () {
let input;
before(function () {
input = document.createElement('input');
testContainer.appendChild(input);
});
after(function () {
testContainer.removeChild(input);
});
it('input element\'s value is used for the initial date', function () {
input.value = '04/22/2020';
const dp = new Datepicker(input);
expect(dp.dates, 'to equal', [dateValue(2020, 3, 22)]);
dp.destroy();
input.value = '';
});
it('the picker is hidden at start', function () {
const dp = new Datepicker(input);
expect(isVisible(document.querySelector('.datepicker')), 'to be false');
expect(dp.active, 'to be false');
dp.destroy();
});
it('the picker becomes visible when the input element get focused', function () {
const dp = new Datepicker(input);
input.focus();
expect(isVisible(document.querySelector('.datepicker')), 'to be true');
expect(dp.active, 'to be true');
dp.destroy();
});
describe('hide()', function () {
it('makes the picker invisible', function () {
const dp = new Datepicker(input);
input.focus();
dp.hide();
expect(isVisible(document.querySelector('.datepicker')), 'to be false');
expect(dp.active, 'to be false');
dp.destroy();
});
});
describe('show()', function () {
it('makes the picker visible', function () {
const dp = new Datepicker(input);
dp.show();
expect(isVisible(document.querySelector('.datepicker')), 'to be true');
expect(dp.active, 'to be true');
dp.destroy();
});
it('moves the focus onto the input field', function () {
// related to issue #52
const dp = new Datepicker(input);
const spyShow = sinon.spy(dp, 'show');
input.blur();
dp.show();
expect(document.activeElement, 'to be', input);
// the focus listener's calling show() is prevented
expect(spyShow.calledOnce, 'to be true');
spyShow.restore();
dp.destroy();
});
it('adds or removes dir attribute to/from the picker if picker\'s text direction != input\'s', function (done) {
testContainer.dir = 'rtl';
const {dp, picker} = createDP(input);
dp.show();
expect(picker.dir, 'to be', 'rtl');
dp.hide();
testContainer.removeAttribute('dir');
dp.show();
expect(picker.hasAttribute('dir'), 'to be false');
dp.hide();
const htmlElem = document.querySelector('html');
htmlElem.dir = 'rtl';
input.style.direction = 'ltr';
dp.show();
expect(picker.dir, 'to be', 'ltr');
dp.hide();
input.removeAttribute('style');
dp.show();
expect(picker.hasAttribute('dir'), 'to be false');
dp.destroy();
htmlElem.removeAttribute('dir');
htmlElem.style.direction = 'ltr';
const checkDirChange = () => {
if (window.getComputedStyle(htmlElem).direction === 'ltr') {
htmlElem.removeAttribute('style');
done();
} else {
setTimeout(checkDirChange, 10);
}
};
checkDirChange();
});
});
describe('picker', function () {
it('displays current month with current date as focued date if no initial date is provided', function () {
let clock = sinon.useFakeTimers({now: new Date(2020, 1, 14)});
const {dp, picker} = createDP(input);
const days = Array.from(picker.querySelector('.datepicker-grid').children);
dp.show();
expect(getViewSwitch(picker).textContent, 'to be', 'February 2020');
expect(days, 'to have length', 42);
expect(filterCells(days, '.datepicker-cell'), 'to have length', 42);
expect(days[0].textContent, 'to be', '26');
expect(days[0].classList.contains('prev'), 'to be true');
expect(days[0].classList.contains('next'), 'to be false');
expect(days[5].textContent, 'to be', '31');
expect(days[5].classList.contains('prev'), 'to be true');
expect(days[5].classList.contains('next'), 'to be false');
expect(days[6].textContent, 'to be', '1');
expect(days[6].classList.contains('prev'), 'to be false');
expect(days[6].classList.contains('next'), 'to be false');
expect(days[34].textContent, 'to be', '29');
expect(days[34].classList.contains('prev'), 'to be false');
expect(days[34].classList.contains('next'), 'to be false');
expect(days[35].textContent, 'to be', '1');
expect(days[35].classList.contains('prev'), 'to be false');
expect(days[35].classList.contains('next'), 'to be true');
expect(days[41].textContent, 'to be', '7');
expect(days[41].classList.contains('prev'), 'to be false');
expect(days[41].classList.contains('next'), 'to be true');
expect(filterCells(days, '.focused'), 'to equal', [days[19]]);
expect(filterCells(days, '.selected'), 'to be empty');
expect(days[19].textContent, 'to be', '14');
dp.destroy();
clock.restore();
});
it('displays iniial date\'s month with the date as selected and focued date', function () {
input.value = '04/22/2020';
const {dp, picker} = createDP(input);
const days = Array.from(picker.querySelector('.datepicker-grid').children);
dp.show();
expect(getViewSwitch(picker).textContent, 'to be', 'April 2020');
expect(days, 'to have length', 42);
expect(filterCells(days, '.datepicker-cell'), 'to have length', 42);
expect(days[0].textContent, 'to be', '29');
expect(days[0].classList.contains('prev'), 'to be true');
expect(days[0].classList.contains('next'), 'to be false');
expect(days[2].textContent, 'to be', '31');
expect(days[2].classList.contains('prev'), 'to be true');
expect(days[2].classList.contains('next'), 'to be false');
expect(days[3].textContent, 'to be', '1');
expect(days[3].classList.contains('prev'), 'to be false');
expect(days[3].classList.contains('next'), 'to be false');
expect(days[32].textContent, 'to be', '30');
expect(days[32].classList.contains('prev'), 'to be false');
expect(days[32].classList.contains('next'), 'to be false');
expect(days[33].textContent, 'to be', '1');
expect(days[33].classList.contains('prev'), 'to be false');
expect(days[33].classList.contains('next'), 'to be true');
expect(days[41].textContent, 'to be', '9');
expect(days[41].classList.contains('prev'), 'to be false');
expect(days[41].classList.contains('next'), 'to be true');
expect(filterCells(days, '.focused'), 'to equal', [days[24]]);
expect(filterCells(days, '.selected'), 'to equal', [days[24]]);
expect(days[24].textContent, 'to be', '22');
dp.destroy();
input.value = '';
});
it('displays day names of week by default', function () {
const {dp, picker} = createDP(input);
const daysOfWeek = picker.querySelector('.days-of-week');
const days = Array.from(daysOfWeek.children).map(el => el.textContent);
expect(days, 'to equal', ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa']);
dp.destroy();
});
it('does not display calendar weeks by default', function () {
const {dp, picker} = createDP(input);
expect(picker.querySelectorAll('.calendar-weeks').length, 'to be', 0);
dp.destroy();
});
it('uses "button" for the main class of button element', function () {
const {dp, picker} = createDP(input);
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();
});
});
});

View File

@ -0,0 +1,328 @@
describe('Datepicker - API methods', function () {
let clock;
let input;
let dp;
let picker;
beforeEach(function () {
clock = sinon.useFakeTimers({now: new Date(2020, 2, 14)});
input = parseHTML('<input type="text" value="04/22/2020">').firstChild;
testContainer.appendChild(input);
dp = new Datepicker(input);
picker = document.querySelector('.datepicker');
input.focus(); // Activate for visibility checks
});
afterEach(function () {
if (input.datepicker) {
dp.destroy();
}
testContainer.removeChild(input);
clock.restore();
});
describe('getDate()', function () {
it('returns a Date object of selected date', function () {
const date = dp.getDate();
expect(date, 'to be a date');
expect(date.getTime(), 'to be', dateValue(2020, 3, 22));
});
it('returns a formatted date stirng of selected date if the format is specified', function () {
expect(dp.getDate('yyyy-mm-dd'), 'to be', '2020-04-22');
});
it('returns undefined if no date is selected', function () {
dp.destroy();
input.value = '';
dp = new Datepicker(input);
expect(dp.getDate(), 'to be undefined');
expect(dp.getDate('yyyy-mm-dd'), 'to be undefined');
});
});
describe('setDate()', function () {
it('changes the selected date to given date', function () {
const spyChnageEvent = sinon.spy();
input.addEventListener('change', spyChnageEvent);
const viewSwitdh = getViewSwitch(picker);
const date = new Date(2019, 11, 23);
dp.setDate(date);
expect(dp.dates, 'to equal', [date.getTime()]);
expect(input.value, 'to be', '12/23/2019');
expect(viewSwitdh.textContent, 'to be', 'December 2019');
let cells = getCells(picker);
expect(filterCells(cells, '.selected'), 'to equal', [cells[22]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[22]]);
expect(cells[22].textContent, 'to be', '23');
dp.setDate('04/22/2020');
expect(dp.dates, 'to equal', [dateValue(2020, 3, 22)]);
expect(input.value, 'to be', '04/22/2020');
expect(viewSwitdh.textContent, 'to be', 'April 2020');
cells = getCells(picker);
expect(filterCells(cells, '.selected'), 'to equal', [cells[24]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[24]]);
expect(cells[24].textContent, 'to be', '22');
// change by api call should not be a trigger of change event
// (issue #24)
expect(spyChnageEvent.called, 'to be false');
input.removeEventListener('change', spyChnageEvent);
// change the view to the selected daye's days view
// (issue #33)
dp.picker.changeFocus(dateValue(2021, 3, 20)).changeView(2).render();
dp.setDate('02/14/2020');
expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
expect(input.value, 'to be', '02/14/2020');
expect(viewSwitdh.textContent, 'to be', 'February 2020');
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');
});
it('does nothing if no date or invalid date is given', function () {
const viewSwitdh = getViewSwitch(picker);
const origDates = [dateValue(2020, 3, 22)];
dp.setDate();
expect(dp.dates, 'to equal', origDates);
expect(input.value, 'to be', '04/22/2020');
expect(viewSwitdh.textContent, 'to be', 'April 2020');
const cells = getCells(picker);
expect(filterCells(cells, '.selected'), 'to equal', [cells[24]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[24]]);
expect(cells[24].textContent, 'to be', '22');
dp.setDate('');
expect(dp.dates, 'to equal', origDates);
expect(input.value, 'to be', '04/22/2020');
});
it('clears the selection if no dates + clear: true option are given', function () {
const spyChnageEvent = sinon.spy();
input.addEventListener('change', spyChnageEvent);
const viewSwitdh = getViewSwitch(picker);
const today = dateUtils.today();
dp.setDate({clear: true});
expect(dp.dates, 'to equal', []);
expect(input.value, 'to be', '');
expect(viewSwitdh.textContent, 'to be', Datepicker.formatDate(today, 'MM yyyy'));
// view date is changed to the default view date (current date)
const cells = getCells(picker);
const todayCell = filterCells(cells, el => el.dataset.date == today)[0];
expect(todayCell.textContent, 'to be', Datepicker.formatDate(today, 'd'));
expect(filterCells(cells, '.selected'), 'to equal', []);
expect(filterCells(cells, '.focused'), 'to equal', [todayCell]);
// change by api call should not be a trigger of change event
// (issue #24)
expect(spyChnageEvent.called, 'to be false');
input.removeEventListener('change', spyChnageEvent);
});
it('omits updating the picker UI if render option = false', function () {
const date = new Date(2019, 11, 23);
dp.setDate(date, {render: false});
expect(dp.dates, 'to equal', [date.getTime()]);
expect(input.value, 'to be', '12/23/2019');
const cells = getCells(picker);
expect(filterCells(cells, '.selected'), 'to equal', [cells[24]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[24]]);
expect(cells[24].textContent, 'to be', '22');
});
it('hides the picker if both render and autohide options are true', function () {
let date = new Date(2019, 11, 23);
dp.setDate(date, {render: false, autohide: true});
expect(dp.dates, 'to equal', [date.getTime()]);
expect(input.value, 'to be', '12/23/2019');
expect(getViewSwitch(picker).textContent, 'to be', 'April 2020');
expect(filterCells(getCells(picker), '.selected')[0].textContent, 'to be', '22');
expect(isVisible(picker), 'to be true');
date = new Date(2018, 6, 14);
dp.setDate(date, {autohide: true});
expect(dp.dates, 'to equal', [date.getTime()]);
expect(input.value, 'to be', '07/14/2018');
expect(getViewSwitch(picker).textContent, 'to be', 'July 2018');
expect(filterCells(getCells(picker), '.selected')[0].textContent, 'to be', '14');
expect(isVisible(picker), 'to be false');
});
});
describe('update()', function () {
it('updates the selected date with the input element\'s value', function () {
const viewSwitdh = getViewSwitch(picker);
const date = new Date(2019, 11, 23);
input.value = '12/23/2019';
dp.update();
expect(dp.dates, 'to equal', [date.getTime()]);
expect(input.value, 'to be', '12/23/2019');
expect(viewSwitdh.textContent, 'to be', 'December 2019');
let cells = getCells(picker);
expect(filterCells(cells, '.selected'), 'to equal', [cells[22]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[22]]);
expect(cells[22].textContent, 'to be', '23');
// change the view to the selected daye's days view
// (issue #33)
dp.picker.changeFocus(dateValue(2021, 3, 20)).changeView(2).render();
input.value = '02/14/2020';
dp.update();
expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
expect(input.value, 'to be', '02/14/2020');
expect(viewSwitdh.textContent, 'to be', 'February 2020');
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');
});
it('notmalizes iput text\'s format', function () {
const date = new Date(2020, 6, 4);
input.value = '7 4 2020';
dp.update();
expect(dp.dates, 'to equal', [date.getTime()]);
expect(input.value, 'to be', '07/04/2020');
expect(getViewSwitch(picker).textContent, 'to be', 'July 2020');
expect(filterCells(getCells(picker), '.selected')[0].textContent, 'to be', '4');
});
});
describe('refresh()', function () {
it('refreshes the input element and picker UI to refrect the internal data', function () {
const spyChnageEvent = sinon.spy();
input.addEventListener('change', spyChnageEvent);
dp.dates = [dateValue(2020, 1, 14)];
dp.refresh();
expect(input.value, 'to be', '02/14/2020');
expect(getViewSwitch(picker).textContent, 'to be', 'February 2020');
const 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');
// change by api call should not be a trigger of change event
// (issue #24)
expect(spyChnageEvent.called, 'to be false');
input.removeEventListener('change', spyChnageEvent);
});
it('also changes the view back to the selected date\'s days view', function () {
dp.dates = [dateValue(2020, 1, 14)];
dp.picker.changeFocus(dateValue(2021, 3, 20)).changeView(2).render();
dp.refresh();
expect(input.value, 'to be', '02/14/2020');
expect(getViewSwitch(picker).textContent, 'to be', 'February 2020');
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');
// go back to the current date's days view if no date is selected
dp.dates = [];
dp.picker.changeFocus(dateValue(2019, 10, 22)).update().changeView(1).render();
dp.refresh();
expect(input.value, 'to be', '');
expect(getViewSwitch(picker).textContent, 'to be', 'March 2020');
cells = getCells(picker);
expect(filterCells(cells, '.selected'), 'to equal', []);
expect(filterCells(cells, '.focused'), 'to equal', [cells[13]]);
expect(cells[13].textContent, 'to be', '14');
clock.restore();
});
it('refresh only picker UI if target: "picker" is passed', function () {
dp.dates = [dateValue(2020, 1, 14)];
dp.refresh('picker');
expect(input.value, 'to be', '04/22/2020');
expect(getViewSwitch(picker).textContent, 'to be', 'February 2020');
const 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');
});
it('refresh only input element if target: "input" is passed', function () {
dp.dates = [dateValue(2020, 1, 14)];
dp.refresh('input');
expect(input.value, 'to be', '02/14/2020');
expect(getViewSwitch(picker).textContent, 'to be', 'April 2020');
const cells = getCells(picker);
expect(filterCells(cells, '.selected'), 'to equal', [cells[24]]);
expect(filterCells(cells, '.focused'), 'to equal', [cells[24]]);
expect(cells[24].textContent, 'to be', '22');
});
it('re-renders the picker regardless of its state if forceRender true is passed', function () {
let cells = getCells(picker);
cells[16].classList.add('foo');
cells[12].textContent = '♥︎';
dp.dates = [dateValue(2020, 3, 10)];
dp.refresh('picker');
cells = getCells(picker);
expect(input.value, 'to be', '04/22/2020');
expect(getViewSwitch(picker).textContent, 'to be', 'April 2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[12]]);
expect(filterCells(cells, '.foo'), 'to equal', [cells[16]]);
expect(cells[12].textContent, 'to be', '♥︎');
dp.refresh('picker', true);
cells = getCells(picker);
expect(input.value, 'to be', '04/22/2020');
expect(getViewSwitch(picker).textContent, 'to be', 'April 2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[12]]);
expect(filterCells(cells, '.foo'), 'to equal', []);
expect(cells[12].textContent, 'to be', '10');
cells[16].classList.add('foo');
cells[12].textContent = '♥︎';
dp.refresh(true);
cells = getCells(picker);
expect(input.value, 'to be', '04/10/2020');
expect(getViewSwitch(picker).textContent, 'to be', 'April 2020');
expect(filterCells(cells, '.selected'), 'to equal', [cells[12]]);
expect(filterCells(cells, '.foo'), 'to equal', []);
expect(cells[12].textContent, 'to be', '10');
});
});
});