123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- 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 = '';
- });
- });
- });
|