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,192 @@
import './_setup.js';
import DateRangePicker from '../../js/DateRangePicker.js';
import {parseHTML} from '../../js/lib/dom.js';
// to spy constructor with sinon, import entire module
// @see: https://github.com/sinonjs/sinon/issues/1358#issuecomment-391643741
import * as DP from '../../js/Datepicker.js';
const Datepicker = DP.default;
describe('DateRangePicker', function () {
let elem;
let input0;
let input1;
beforeEach(function () {
elem = parseHTML('<div><input><input></div>').firstChild;
[input0, input1] = elem.children;
testContainer.appendChild(elem);
});
afterEach(function () {
document.querySelectorAll('.datepicker').forEach((el) => {
el.parentElement.removeChild(el);
});
delete input0.datepicker;
delete input1.datepicker;
delete elem.rangepicker;
testContainer.removeChild(elem);
});
describe('constructor', function () {
it('attachs the created instance to the bound element', function () {
const drp = new DateRangePicker(elem);
expect(elem.rangepicker, 'to be', drp);
});
it('configures the instance with the default values', function () {
let drp = new DateRangePicker(elem);
expect(drp.allowOneSidedRange, 'to be false');
});
it('creates datepicker for the inputs passing rangepicker and given options expect', function () {
const spyDPConstructor = sinon.spy(DP, 'default');
let drp = new DateRangePicker(elem);
expect(spyDPConstructor.args, 'to equal', [
[input0, {}, drp],
[input1, {}, drp],
]);
expect(input0.datepicker, 'to be a', Datepicker);
expect(input1.datepicker, 'to be a', Datepicker);
expect([input0.datepicker, input1.datepicker], 'to equal', drp.datepickers);
spyDPConstructor.resetHistory();
delete input0.datepicker;
delete input1.datepicker;
delete elem.rangepicker;
const fakeOptions = {foo: 123, bar: 456};
drp = new DateRangePicker(elem, fakeOptions);
expect(spyDPConstructor.args, 'to equal', [
[input0, fakeOptions, drp],
[input1, fakeOptions, drp],
]);
spyDPConstructor.restore();
});
it('makes datepickers property read-only/immutable', function () {
const drp = new DateRangePicker(elem);
expect(() => { drp.datepickers = []; }, 'to throw a', TypeError);
expect(() => { drp.datepickers[0] = null; }, 'to throw a', TypeError);
expect(() => { drp.datepickers[2] = {}; }, 'to throw a', TypeError);
});
it('excludes inputs, allowOneSidedRange and maxNumberOfDates from options to pass Datepicker container', function () {
const spyDPConstructor = sinon.spy(DP, 'default');
new DateRangePicker(elem, {
inputs: [input0, input1],
allowOneSidedRange: false,
maxNumberOfDates: 2,
foo: 123,
});
expect(spyDPConstructor.args[0][1], 'to equal', {foo: 123});
spyDPConstructor.restore();
});
it('works with arbitrary input elements if they are provided in the inputs option', function () {
const outsideEl = document.createElement('div');
const drp = new DateRangePicker(outsideEl, {inputs: [input0, input1]});
expect(outsideEl.rangepicker, 'to be', drp);
expect(input0.datepicker.rangepicker, 'to be', drp);
expect(input1.datepicker.rangepicker, 'to be', drp);
expect([input0.datepicker, input1.datepicker], 'to equal', drp.datepickers);
});
it('append datepicker elements to the container)', function () {
new DateRangePicker(elem);
const dpElems = Array.from(document.body.children).filter(el => el.matches('.datepicker'));
expect(dpElems, 'to have length', 2);
});
it('does not add the active class to the picker elements', function () {
new DateRangePicker(elem);
const dpElems = Array.from(document.querySelectorAll('.datepicker'));
expect(dpElems.filter(el => el.classList.contains('active')), 'to be empty');
});
it('does nothing but creating an instance if number of inputs < 2', function () {
elem.removeChild(input1);
let drp = new DateRangePicker(elem);
expect(elem, 'not to have property', 'rangepicker');
expect(drp, 'not to have properties', ['inputs', 'datepickers']);
expect(input0, 'not to have property', 'datepicker');
expect(document.querySelectorAll('.datepicker').length, 'to be', 0);
const outsideEl = document.createElement('div');
drp = new DateRangePicker(outsideEl, {inputs: [input0]});
expect(outsideEl, 'not to have property', 'rangepicker');
expect(input0, 'not to have property', 'datepicker');
expect(document.querySelectorAll('.datepicker').length, 'to be', 0);
});
});
describe('destroy()', function () {
let drp;
let spyDestroy0;
let spyDestroy1;
beforeEach(function () {
drp = new DateRangePicker(elem);
spyDestroy0 = sinon.spy(input0.datepicker, 'destroy');
spyDestroy1 = sinon.spy(input1.datepicker, 'destroy');
});
afterEach(function () {
spyDestroy0.restore();
spyDestroy1.restore();
});
it('calls destroy() of each datepickers', function () {
drp.destroy();
expect(spyDestroy0.called, 'to be true');
expect(spyDestroy1.called, 'to be true');
});
it('removes the instance from the bound element', function () {
drp.destroy();
expect(Object.prototype.hasOwnProperty.call(elem, 'rangepicker'), 'to be false');
});
});
describe('dates property', function () {
it('contains the array of the inputs\' selected dates', function () {
const drp = new DateRangePicker(elem);
expect(drp.dates, 'to equal', [undefined, undefined]);
const date0 = new Date(2020, 3, 20).setHours(0, 0, 0, 0);
const date1 = new Date(2020, 3, 22).setHours(0, 0, 0, 0);
drp.datepickers[0].dates = [date0];
drp.datepickers[1].dates = [date1];
expect(drp.dates, 'to equal', [date0, date1]);
});
});
describe('setOptions()', function () {
it('updates allowOneSidedRange but ignores inputs if they are in the given options', function () {
const input2 = document.createElement('input');
const drp = new DateRangePicker(elem);
drp.setOptions({allowOneSidedRange: true, inputs: [input2, input0]});
expect(drp.allowOneSidedRange, 'to be true');
expect(drp.inputs, 'to equal', [input0, input1]);
});
it('calls each datepicker\'s setOptions() with given options except inputs, allowOneSidedRange and maxNumberOfDates', function () {
const drp = new DateRangePicker(elem);
const stubDP0SetOptions = sinon.stub(drp.datepickers[0], 'setOptions').callsFake(() => {});
const stubDP1SetOptions = sinon.stub(drp.datepickers[1], 'setOptions').callsFake(() => {});
drp.setOptions({inputs: [], allowOneSidedRange: true, maxNumberOfDates: 2, foo: 123, bar: 456});
expect(stubDP0SetOptions.args, 'to equal', [[{foo: 123, bar: 456}]]);
expect(stubDP1SetOptions.args, 'to equal', [[{foo: 123, bar: 456}]]);
});
});
});

View File

@ -0,0 +1,307 @@
import './_setup.js';
import Datepicker from '../../js/Datepicker.js';
import defaultOptions from '../../js/options/defaultOptions.js';
import {locales} from '../../js/i18n/base-locales.js';
import {dateValue, today} from '../../js/lib/date.js';
const esLocale = {
months: ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"],
monthsShort: ["Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Sep", "Oct", "Nov", "Dic"],
};
describe('Datepicker', function () {
it('has locales attribute with "en" locale', function () {
expect(Datepicker.locales, 'to be an object');
expect(Datepicker.locales.en, 'to equal', locales.en);
});
describe('constructor', function () {
let input;
beforeEach(function () {
input = document.createElement('input');
testContainer.appendChild(input);
});
after(function () {
document.querySelectorAll('.datepicker').forEach((el) => {
el.parentElement.removeChild(el);
});
delete input.datepicker;
testContainer.removeChild(input);
});
it('attachs the created instance to the bound element', function () {
const dp = new Datepicker(input);
expect(input.datepicker, 'to be', dp);
});
it('adds datepicker-input class to the bound element', function () {
new Datepicker(input);
expect(input.classList.contains('datepicker-input'), 'to be true');
});
it('configures the instance with the default options', function () {
const dp = new Datepicker(input);
// config items should be options + container, locale, multidate and weekEnd
const numOfOptions = Object.keys(defaultOptions).length;
expect(Object.keys(dp.config), 'to have length', numOfOptions + 5);
expect(dp.config.autohide, 'to be false');
expect(dp.config.beforeShowDay, 'to be null');
expect(dp.config.beforeShowDecade, 'to be null');
expect(dp.config.beforeShowMonth, 'to be null');
expect(dp.config.beforeShowYear, 'to be null');
expect(dp.config.buttonClass, 'to be', 'button');
expect(dp.config.calendarWeeks, 'to be false');
expect(dp.config.clearBtn, 'to be false');
expect(dp.config.container, 'to be', document.body);
expect(dp.config.dateDelimiter, 'to be', ',');
expect(dp.config.datesDisabled, 'to equal', []);
expect(dp.config.daysOfWeekDisabled, 'to equal', []);
expect(dp.config.daysOfWeekHighlighted, 'to equal', []);
expect(dp.config.defaultViewDate, 'to be', today());
expect(dp.config.disableTouchKeyboard, 'to be false');
expect(dp.config.format, 'to be', 'mm/dd/yyyy');
expect(dp.config.language, 'to be', 'en');
expect(dp.config.locale, 'to equal', Object.assign({
format: defaultOptions.format,
weekStart: defaultOptions.weekStart,
}, locales.en));
expect(dp.config.maxDate, 'to be undefined');
expect(dp.config.maxNumberOfDates, 'to be', 1);
expect(dp.config.maxView, 'to be', 3);
expect(dp.config.minDate, 'to be', dateValue(0, 0, 1));
expect(dp.config.multidate, 'to be false');
//
expect(dp.config.nextArrow, 'to be a', NodeList);
expect(dp.config.nextArrow.length, 'to be', 1);
expect(dp.config.nextArrow[0].wholeText, 'to be', '»');
//
expect(dp.config.orientation, 'to equal', {x: 'auto', y: 'auto'});
expect(dp.config.pickLevel, 'to be', 0);
//
expect(dp.config.prevArrow, 'to be a', NodeList);
expect(dp.config.prevArrow.length, 'to be', 1);
expect(dp.config.prevArrow[0].wholeText, 'to be', '«');
//
expect(dp.config.showDaysOfWeek, 'to be true');
expect(dp.config.showOnFocus, 'to be true');
expect(dp.config.startView, 'to be', 0);
expect(dp.config.title, 'to be', '');
expect(dp.config.todayBtn, 'to be false');
expect(dp.config.todayHighlight, 'to be false');
expect(dp.config.updateOnBlur, 'to be true');
expect(dp.config.weekStart, 'to be', 0);
expect(dp.config.weekEnd, 'to be', 6);
});
it('append datepicker element to the container)', function () {
new Datepicker(input);
const dpElem = Array.from(document.body.children).find(el => el.matches('.datepicker'));
expect(dpElem, 'not to be undefined');
});
it('does not add the active class to the picker element', function () {
new Datepicker(input);
const dpElem = document.querySelector('.datepicker');
expect(dpElem.classList.contains('active'), 'to be false');
});
it('sets rangepicker properties if DateRangePicker to link is passed', function () {
const fakeRangepicker = {
inputs: [input],
datepickers: [],
};
const dp = new Datepicker(input, {}, fakeRangepicker);
expect(dp.rangepicker, 'to be', fakeRangepicker);
});
it('adds itself to rangepicker.datepickers if DateRangePicker to link is passed', function () {
let fakeRangepicker = {
inputs: [input],
datepickers: [],
};
let dp = new Datepicker(input, {}, fakeRangepicker);
expect(fakeRangepicker.datepickers[0], 'to be', dp);
fakeRangepicker = {
inputs: [undefined, input],
datepickers: [],
};
dp = new Datepicker(input, {}, fakeRangepicker);
expect(fakeRangepicker.datepickers[1], 'to be', dp);
});
it('throws an error if invalid rangepicker is passed', function () {
const testFn = rangepicker => new Datepicker(input, {}, rangepicker);
const errMsg = 'Invalid rangepicker object.';
let fakeRangepicker = {
inputs: [],
datepickers: [],
};
expect(() => testFn(fakeRangepicker), 'to throw', errMsg);
fakeRangepicker = {
inputs: ['foo', 'bar', input],
datepickers: [],
};
expect(() => testFn(fakeRangepicker), 'to throw', errMsg);
fakeRangepicker = {
inputs: [input],
};
expect(() => testFn(fakeRangepicker), 'to throw', errMsg);
});
});
describe('destroy()', function () {
let input;
let dp;
let spyHide;
let returnVal;
before(function () {
input = document.createElement('input');
testContainer.appendChild(input);
dp = new Datepicker(input);
spyHide = sinon.spy(dp, 'hide');
returnVal = dp.destroy();
});
after(function () {
spyHide.restore();
document.querySelectorAll('.datepicker').forEach((el) => {
el.parentElement.removeChild(el);
});
delete input.datepicker;
testContainer.removeChild(input);
});
it('calls hide()', function () {
expect(spyHide.called, 'to be true');
});
it('removes datepicker element from its container', function () {
expect(document.body.querySelectorAll('.datepicker').length, 'to be', 0);
});
it('removes the instance from the bound element', function () {
expect(Object.prototype.hasOwnProperty.call(input, 'datepicker'), 'to be false');
});
it('removes datepicker-input class from the bound element', function () {
expect(input.classList.contains('datepicker-input'), 'to be false');
});
it('returns the instance', function () {
expect(returnVal, 'to be', dp);
});
});
describe('show()', function () {
let input;
let dp;
let dpElem;
before(function () {
input = document.createElement('input');
testContainer.appendChild(input);
dp = new Datepicker(input);
dpElem = document.querySelector('.datepicker');
dp.show();
});
after(function () {
dp.destroy();
testContainer.removeChild(input);
});
it('adds the "active" class to the datepicker element', function () {
expect(dpElem.classList.contains('active'), 'to be true');
});
it('sets true to the picker.active property', function () {
expect(dp.picker.active, 'to be true');
});
});
describe('hide()', function () {
let input;
let dp;
let dpElem;
before(function () {
input = document.createElement('input');
testContainer.appendChild(input);
dp = new Datepicker(input);
dpElem = document.querySelector('.datepicker');
dp.picker.active = true;
dpElem.classList.add('active', 'block');
dp.hide();
});
after(function () {
dp.destroy();
testContainer.removeChild(input);
});
it('removes the "active" class from the datepicker element', function () {
expect(dpElem.classList.contains('active'), 'to be false');
});
it('deletes the "picker.active" property', function () {
expect(dp.picker, 'to have property', 'active');
});
});
describe('static formatDate()', function () {
it('formats a date or time value', function () {
Datepicker.locales.es = esLocale;
let date = new Date(2020, 0, 4);
expect(Datepicker.formatDate(date, 'y-m-d'), 'to be', '2020-1-4');
expect(Datepicker.formatDate(date.getTime(), 'dd M yy'), 'to be', '04 Jan 20');
expect(Datepicker.formatDate(date, 'dd M yy', 'es'), 'to be', '04 Ene 20');
expect(Datepicker.formatDate(date.getTime(), 'MM d, y', 'es'), 'to be', 'Enero 4, 2020');
delete Datepicker.locales.es;
// fallback to en
expect(Datepicker.formatDate(date, 'dd M yy', 'es'), 'to be', '04 Jan 20');
expect(Datepicker.formatDate(date.getTime(), 'MM d, y', 'es'), 'to be', 'January 4, 2020');
});
});
describe('static parseDate()', function () {
it('parses a date string and returnes the time value of the date', function () {
Datepicker.locales.es = esLocale;
let timeValue = new Date(2020, 0, 4).getTime();
expect(Datepicker.parseDate('2020-1-4', 'y-m-d'), 'to be', timeValue);
expect(Datepicker.parseDate('04 Jan 2020', 'dd M yy'), 'to be', timeValue);
expect(Datepicker.parseDate('04 Ene 2020', 'dd M yy', 'es'), 'to be', timeValue);
expect(Datepicker.parseDate('Enero 4, 2020', 'MM d, y', 'es'), 'to be', timeValue);
expect(Datepicker.parseDate('04/20/2022', 'mm/dd/yyyy'), 'to equal', new Date(2022, 3, 20).getTime());
expect(Datepicker.parseDate('5/3/1994', 'd/m/y'), 'to equal', new Date(1994, 2, 5).getTime());
delete Datepicker.locales.es;
// fallback to en
const fallbackDate = new Date(timeValue).setMonth(new Date().getMonth());
expect(Datepicker.parseDate('04 Ene 2020', 'dd M yy', 'es'), 'to be', fallbackDate);
expect(Datepicker.parseDate('Enero 4, 2020', 'MM d, y', 'es'), 'to be', fallbackDate);
expect(Datepicker.parseDate('04 Jan 2020', 'dd M yy', 'es'), 'to be', timeValue);
expect(Datepicker.parseDate('2020-1-4', 'y-m-d', 'es'), 'to be', timeValue);
});
});
});

28
node_modules/flowbite-datepicker/test/unit/_setup.js generated vendored Normal file
View File

@ -0,0 +1,28 @@
global.expect = require('unexpected');
global.sinon = require('sinon');
const jsdom = require('jsdom');
const {JSDOM} = jsdom;
const {window} = new JSDOM('<body><div id="test-container"></div></body>', {
pretendToBeVisual: true
});
const document = window.document;
global.JSDOM = JSDOM;
global.window = window;
global.testContainer = document.getElementById('test-container');
const exposeToGlobal = [
'document',
'CustomEvent',
'DocumentFragment',
'Event',
'EventTarget',
'NodeList',
'Range',
];
exposeToGlobal.forEach((prop) => {
global[prop] = window[prop];
});
global.simulant = require('simulant');

View File

@ -0,0 +1,310 @@
import '../_setup.js';
import {parseDate, formatDate} from '../../../js/lib/date-format.js';
describe('lib/date', function () {
const locales = {
en: {
days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"],
months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
today: "Today",
clear: "Clear",
titleFormat: "MM yyyy"
},
de: {
days: ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"],
daysShort: ["Son", "Mon", "Die", "Mit", "Don", "Fre", "Sam"],
months: ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"],
monthsShort: ["Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"],
},
es: {
days: ["Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado"],
daysShort: ["Dom", "Lun", "Mar", "Mié", "Jue", "Vie", "Sáb"],
months: ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"],
monthsShort: ["Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Sep", "Oct", "Nov", "Dic"],
},
fr: {
days: ["dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"],
daysShort: ["dim.", "lun.", "mar.", "mer.", "jeu.", "ven.", "sam."],
daysMin: ["d", "l", "ma", "me", "j", "v", "s"],
months: ["janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"],
monthsShort: ["janv.", "févr.", "mars", "avril", "mai", "juin", "juil.", "août", "sept.", "oct.", "nov.", "déc."],
}
};
describe('parseDate()', function () {
const today = new Date().setHours(0, 0, 0, 0);
const thisYear = new Date().getFullYear();
it('return undefined if the given date is falsy value other than 0 or invalid date', function () {
expect(parseDate(), 'to be undefined');
expect(parseDate(''), 'to be undefined');
expect(parseDate(new Date('')), 'to be undefined');
expect(parseDate(0), 'not to be undefined');
});
it('returns time value of the same day\'s 00:00:00 local time if the given date is a Date object or time value', function () {
const origDate = new Date().setHours(0, 0, 0, 0);
expect(parseDate(new Date()), 'to be', origDate);
expect(parseDate(Date.now()), 'to be', origDate);
});
it('invokes custom parse fucntion and returns the result if it\'s given to format.toValue', function () {
const date = new Date();
const format = {toValue: sinon.stub()};
format.toValue.returns(date);
expect(parseDate('2020-01-01', format, locales.en), 'to be', date.setHours(0, 0, 0, 0));
expect(format.toValue.calledWith('2020-01-01', format, locales.en), 'to be true');
});
it('returns the date that the word means if given date is "today"', function () {
expect(parseDate('today'), 'to be', today);
});
it('uses format: "d" or "dd" as day of month to parse date string', function () {
expect(parseDate('2012-03-5', 'yyyy-mm-d'), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('2012-03-15', 'yyyy-mm-d'), 'to be', new Date(2012, 2, 15).getTime());
expect(parseDate('2012-03-05', 'yyyy-mm-d'), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('2012-03-5', 'yyyy-mm-dd'), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('2012-03-15', 'yyyy-mm-dd'), 'to be', new Date(2012, 2, 15).getTime());
expect(parseDate('2012-03-05', 'yyyy-mm-dd'), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('5/03/2012', 'dd/mm/yyyy'), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('15/03/2012', 'd/mm/yyyy'), 'to be', new Date(2012, 2, 15).getTime());
});
it('accepts 0 and number larger than the end of the month for "d", "dd"', function () {
expect(parseDate('2012-03-0', 'yyyy-mm-d'), 'to be', new Date(2012, 1, 29).getTime());
expect(parseDate('2012-03-33', 'yyyy-mm-d'), 'to be', new Date(2012, 3, 2).getTime());
expect(parseDate('2013-02-60', 'yyyy-mm-d'), 'to be', new Date(2013, 3, 1).getTime());
});
it('uses format: "m", "mm", "M" or "MM" as month to parse date string', function () {
const fakeToday = new Date(thisYear, 2, 31);
const clock = sinon.useFakeTimers({now: fakeToday});
// month number
expect(parseDate('2012-3-5', 'yyyy-m-d'), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('2012-12-15', 'yyyy-m-d'), 'to be', new Date(2012, 11, 15).getTime());
expect(parseDate('2012-03-05', 'yyyy-m-d'), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('2012-3-5', 'yyyy-mm-dd'), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('2012-12-15', 'yyyy-mm-dd'), 'to be', new Date(2012, 11, 15).getTime());
expect(parseDate('2012-03-05', 'yyyy-mm-dd'), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('5/3/2012', 'dd/mm/yyyy'), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('15/12/2012', 'd/m/yyyy'), 'to be', new Date(2012, 11, 15).getTime());
// ensure setting a month w/ < 31days on the 31 of a month works correctly
expect(parseDate('2012-02-28', 'yyyy-mm-dd'), 'to be', new Date(2012, 1, 28).getTime());
expect(parseDate('2012-09-15', 'yyyy-mm-dd'), 'to be', new Date(2012, 8, 15).getTime());
// month name
expect(parseDate('Mar 5, 2012', 'M d, yyyy', locales.en), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('Dec 15, 2012', 'M d, yyyy', locales.en), 'to be', new Date(2012, 11, 15).getTime());
expect(parseDate('Mär 5, 2012', 'M d, yyyy', locales.de), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('Dez 15, 2012', 'M d, yyyy', locales.de), 'to be', new Date(2012, 11, 15).getTime());
expect(parseDate('mars 5, 2012', 'M d, yyyy', locales.fr), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('déc. 15, 2012', 'M d, yyyy', locales.fr), 'to be', new Date(2012, 11, 15).getTime());
expect(parseDate('March 5, 2012', 'MM d, yyyy', locales.en), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('December 15, 2012', 'MM d, yyyy', locales.en), 'to be', new Date(2012, 11, 15).getTime());
expect(parseDate('März 5, 2012', 'MM d, yyyy', locales.de), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('Dezember 15, 2012', 'MM d, yyyy', locales.de), 'to be', new Date(2012, 11, 15).getTime());
expect(parseDate('mars 5, 2012', 'MM d, yyyy', locales.fr), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('décembre 15, 2012', 'MM d, yyyy', locales.fr), 'to be', new Date(2012, 11, 15).getTime());
// month number for "M" and "MM"
expect(parseDate('3/5/2012', 'M/dd/yyyy', locales.en), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('12/15/2012', 'MM/dd/yyyy', locales.en), 'to be', new Date(2012, 11, 15).getTime());
// month name for "m" and "mm"
expect(parseDate('Mar/5/2012', 'm/d/yyyy', locales.en), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('December/15/2012', 'mm/dd/yyyy', locales.en), 'to be', new Date(2012, 11, 15).getTime());
clock.restore();
});
it('accepts 0 and number larger than 12 for "m", "mm"', function () {
const fakeToday = new Date(thisYear, 2, 31);
const clock = sinon.useFakeTimers({now: fakeToday});
expect(parseDate('2012-0-05', 'yyyy-m-dd'), 'to be', new Date(2011, 11, 5).getTime());
expect(parseDate('2012-16-30', 'yyyy-m-d'), 'to be', new Date(2013, 3, 30).getTime());
expect(parseDate('2012-32-30', 'yyyy-m-d'), 'to be', new Date(2014, 7, 30).getTime());
clock.restore();
});
it('evaluates month name with case-insensible begin-with match', function () {
const fakeToday = new Date(thisYear, 2, 31);
const clock = sinon.useFakeTimers({now: fakeToday});
expect(parseDate('march 5, 2012', 'M d, yyyy', locales.en), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('DEC 15, 2012', 'MM d, yyyy', locales.en), 'to be', new Date(2012, 11, 15).getTime());
expect(parseDate('MA 5, 2012', 'MM d, yyyy', locales.en), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('j 5, 2012', 'MM d, yyyy', locales.en), 'to be', new Date(2012, 0, 5).getTime());
expect(parseDate('ju 5, 2012', 'MM d, yyyy', locales.en), 'to be', new Date(2012, 5, 5).getTime());
expect(parseDate('march/5/2012', 'm/d/yyyy', locales.en), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('DEC/15/2012', 'mm/dd/yyyy', locales.en), 'to be', new Date(2012, 11, 15).getTime());
expect(parseDate('MA/05/2012', 'mm/dd/yyyy', locales.en), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('j/05/2012', 'mm/dd/yyyy', locales.en), 'to be', new Date(2012, 0, 5).getTime());
expect(parseDate('ju/05/2012', 'mm/dd/yyyy', locales.en), 'to be', new Date(2012, 5, 5).getTime());
clock.restore();
});
it('uses format: "y", "yy" or "yyyy" as year to parse date string', function () {
expect(parseDate('2012-3-5', 'y-m-d'), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('1984-3-15', 'y-m-d'), 'to be', new Date(1984, 2, 15).getTime());
expect(parseDate('12-03-05', 'y-m-d'), 'to be', new Date(0, 2, 5).setFullYear(12));
expect(parseDate('2012-3-5', 'yy-m-d'), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('1984-3-15', 'yy-m-d'), 'to be', new Date(1984, 2, 15).getTime());
expect(parseDate('12-03-05', 'yy-m-d'), 'to be', new Date(0, 2, 5).setFullYear(12));
expect(parseDate('2012-03-5', 'yyyy-mm-dd'), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('1984-03-15', 'yyyy-mm-dd'), 'to be', new Date(1984, 2, 15).getTime());
expect(parseDate('12-03-05', 'yyyy-mm-dd'), 'to be', new Date(0, 2, 5).setFullYear(12));
expect(parseDate('5/03/2012', 'dd/mm/yyyy'), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('15/03/1984', 'd/m/yy'), 'to be', new Date(1984, 2, 15).getTime());
});
it('ignores "D" and "DD" (day of week)', function () {
let date = parseDate('2012-03-05', 'yyyy-mm-dd-D');
expect(date, 'to be', new Date(2012, 2, 5).getTime());
date = parseDate('Sat, Dec 15, 2012', 'DD, M dd, yyyy', locales.en);
expect(date, 'to be', new Date(2012, 11, 15).getTime());
});
it('uses current date\'s year/month/day to complement undefined, missing or unparsable parts', function () {
const fakeToday = new Date(thisYear, 2, 31);
const clock = sinon.useFakeTimers({now: fakeToday});
expect(parseDate('03-05', 'mm-dd'), 'to be', new Date(thisYear, 2, 5).getTime());
expect(parseDate('2012-06', 'yyyy-mm'), 'to be', new Date(2012, 5, 30).getTime());
expect(parseDate('5, 2012', 'd, yyyy'), 'to be', new Date(2012, 2, 5).getTime());
expect(parseDate('Mai/10', 'M/d', locales.en), 'to be', new Date(thisYear, 2, 10).getTime());
expect(parseDate('Maya/10', 'M/d', locales.en), 'to be', new Date(thisYear, 2, 10).getTime());
expect(parseDate('03-05', 'yyyy-mm-dd'), 'to be', new Date(0, 4, 31).setFullYear(3));
expect(parseDate('Sun, 23', 'DD, mm-dd'), 'to be', new Date(thisYear + 1, 10, 30).getTime());
expect(parseDate('Mar/05/12', 'yyyy/mm/dd', locales.en), 'to be', new Date(thisYear, 4, 12).getTime());
expect(parseDate('05/Mar/12', 'yyyy/mm/dd', locales.en), 'to be', new Date(0, 2, 12).setFullYear(5));
expect(parseDate('2012年十二月十五日', 'yyyy年mm月dd日', locales.en), 'to be', new Date(2012, 2, 31).getTime());
clock.restore();
});
it('throws an Error if format is neither a valid format string nor an object w/ toValue property', function () {
expect(() => parseDate('01-01-01', {}), 'to throw error');
expect(() => parseDate('01-01-01', 1), 'to throw error');
expect(() => parseDate('01-01-01', 'aa-bb-cc'), 'to throw error');
});
});
describe('formatDate()', function () {
it('return empty string if the given date is falsy value other than 0 or invalid date', function () {
expect(formatDate(), 'to be', '');
expect(formatDate(''), 'to be', '');
expect(formatDate(new Date('')), 'to be', '');
expect(formatDate(0, 'yyyy', locales.en), 'not to be', '');
});
it('invokes custom format fucntion and returns the result if it\'s given to format.toDisplay', function () {
const date = new Date(2012, 2, 5);
const format = {toDisplay: sinon.stub()};
format.toDisplay.returns('foo-bar');
expect(formatDate(date, format, locales.en), 'to be', 'foo-bar');
expect(format.toDisplay.calledWith(date, format, locales.en), 'to be true');
});
it('uses format: "d" as day of month, no leading zero to format date', function () {
expect(formatDate(new Date(2012, 2, 5), 'yyyy-mm-d', locales.en), 'to be', '2012-03-5');
expect(formatDate(0, 'd', locales.en), 'to be', String(new Date(0).getDate()));
});
it('uses format: "dd" as day of month, leading zero to format date', function () {
const date = new Date(2012, 2, 5);
expect(formatDate(date, 'yyyy-mm-dd', locales.en), 'to be', '2012-03-05');
expect(formatDate(date.getTime(), 'yyyy-mm-dd', locales.en), 'to be', '2012-03-05');
});
it('uses format: "D" as short day of week in given language to format date', function () {
const date = new Date(2012, 2, 5);
expect(formatDate(date, 'yyyy-mm-dd-D', locales.en), 'to be', '2012-03-05-Mon');
expect(formatDate(date, 'yyyy-mm-dd-D', locales.es), 'to be', '2012-03-05-Lun');
});
it('uses format: "DD" as long day of week in given language to format date', function () {
const date = new Date(2012, 2, 5);
expect(formatDate(date, 'yyyy-mm-dd-DD', locales.en), 'to be', '2012-03-05-Monday');
expect(formatDate(date, 'yyyy-mm-dd-DD', locales.es), 'to be', '2012-03-05-Lunes');
});
it('uses format: "m" as Month, no leading zero. to format date', function () {
expect(formatDate(new Date(2012, 2, 5), 'yyyy-m-dd', locales.en), 'to be', '2012-3-05');
});
it('uses format: "mm" as Month, leading zero. to format date', function () {
expect(formatDate(new Date(2012, 2, 5), 'yyyy-mm-dd', locales.en), 'to be', '2012-03-05');
});
it('uses format: "M" as month shortname in given language to format date', function () {
const date = new Date(2012, 2, 5);
expect(formatDate(date, 'yyyy-M-dd', locales.en), 'to be', '2012-Mar-05');
expect(formatDate(date, 'yyyy-M-dd', locales.de), 'to be', '2012-Mär-05');
});
it('uses format: "MM" as month full name in given language to format date', function () {
const date = new Date(2012, 2, 5);
expect(formatDate(date, 'yyyy-MM-dd', locales.en), 'to be', '2012-March-05');
expect(formatDate(date, 'yyyy-MM-dd', locales.de), 'to be', '2012-März-05');
});
it('uses format: "y" as Year, no leading zero. to format date', function () {
expect(formatDate(new Date(0, 2, 5).setFullYear(2), 'y-m-d', locales.en), 'to be', '2-3-5');
expect(formatDate(new Date(0, 2, 5).setFullYear(12), 'y-m-d', locales.en), 'to be', '12-3-5');
expect(formatDate(new Date(2012, 2, 5), 'y-m-d', locales.en), 'to be', '2012-3-5');
});
it('uses format: "yy" as Year, two-digit. to format date', function () {
expect(formatDate(new Date(0, 2, 5).setFullYear(2), 'yy-mm-dd', locales.en), 'to be', '02-03-05');
expect(formatDate(new Date(0, 2, 5).setFullYear(12), 'yy-mm-dd', locales.en), 'to be', '12-03-05');
expect(formatDate(new Date(2012, 2, 5), 'yy-mm-dd', locales.en), 'to be', '12-03-05');
});
it('uses format: "yyyy" as Year, four-digit. to format date', function () {
expect(formatDate(new Date(0, 2, 5).setFullYear(2), 'yyyy-mm-dd', locales.en), 'to be', '0002-03-05');
expect(formatDate(new Date(0, 2, 5).setFullYear(12), 'yyyy-mm-dd', locales.en), 'to be', '0012-03-05');
expect(formatDate(new Date(2012, 2, 5), 'yyyy-mm-dd', locales.en), 'to be', '2012-03-05');
});
it('accepts separators come before and after the date numbers', function () {
expect(formatDate(new Date(2012, 2, 5), '西暦yyyy年mm月dd日', locales.en), 'to be', '西暦2012年03月05日');
});
it('throws an Error if format is neither a valid format string nor an object w/ toValue property', function () {
const date = new Date(2012, 2, 5);
expect(() => formatDate(date, {}), 'to throw error');
expect(() => formatDate(date, 1), 'to throw error');
expect(() => formatDate(date, 'aa-bb-cc'), 'to throw error');
});
});
});

211
node_modules/flowbite-datepicker/test/unit/lib/date.js generated vendored Normal file
View File

@ -0,0 +1,211 @@
import '../_setup.js';
import {
stripTime,
dateValue,
today,
addDays,
addWeeks,
addMonths,
addYears,
dayOfTheWeekOf,
getWeek,
startOfYearPeriod
} from '../../../js/lib/date.js';
describe('lib/date', function () {
describe('stripTime()', function () {
it('returns the time value of 00:00:00 local time of given time value or date', function () {
let origDate = new Date(2016, 8, 24, 13, 55, 32, 235);
expect(stripTime(origDate.getTime()), 'to be', new Date(2016, 8, 24).getTime());
origDate = new Date(2019, 1, 14, 20, 55);
expect(stripTime(origDate), 'to be', new Date(2019, 1, 14).getTime());
});
});
describe('today()', function () {
it('returns the time value of 00:00:00 local time of current date', function () {
const now = new Date();
const timeValue = today();
expect(timeValue, 'to be', now.setHours(0, 0, 0, 0));
});
});
describe('dateValue()', function () {
it('returns the time value of 00:00:00 local time of the date created by given Date constructor parameters', function () {
// from Date object
let origDate = new Date(2016, 8, 24, 13, 55, 32, 235);
let timeValue = dateValue(origDate);
expect(timeValue, 'to be', new Date(2016, 8, 24).getTime());
// from Date constructor args
timeValue = dateValue(2019, 1, 17, 9, 43, 18, 953);
expect(timeValue, 'to be', new Date(2019, 1, 17).getTime());
// from string
let origDateStr = '2020-04-20T02:27:15Z';
origDate = new Date(origDateStr);
timeValue = dateValue(origDateStr);
expect(timeValue, 'to be', origDate.setHours(0, 0, 0, 0));
// from time value
origDate = new Date(2016, 8, 24, 13, 55, 32, 235);
timeValue = dateValue(origDate.getTime());
expect(timeValue, 'to be', new Date(2016, 8, 24).getTime());
});
it('does not map 2-digit year to 1900\'s', function () {
let date = new Date(dateValue(86, 8, 24));
expect(date.getFullYear(), 'to be', 86);
expect(date.getMonth(), 'to be', 8);
expect(date.getDate(), 'to be', 24);
date = new Date(dateValue(19, 1, 17));
expect(date.getFullYear(), 'to be', 19);
expect(date.getMonth(), 'to be', 1);
expect(date.getDate(), 'to be', 17);
});
});
describe('addDays()', function () {
it('returns the time value of N days after a date or time value', function () {
let date1 = new Date(2019, 11, 31);
let date2 = addDays(date1, 1);
expect(date2, 'to be', new Date(2020, 0, 1).getTime());
expect(addDays(date2, 30), 'to be', new Date(2020, 0, 31).getTime());
date1 = new Date(2020, 0, 1);
expect(addDays(date1, -1), 'to be', new Date(2019, 11, 31).getTime());
expect(addDays(date1, -10), 'to be', new Date(2019, 11, 22).getTime());
});
});
describe('addWeeks()', function () {
it('returns the time value of N weeks after a date or time value', function () {
let date1 = new Date(2019, 11, 28);
let date2 = addWeeks(date1, 1);
expect(date2, 'to be', new Date(2020, 0, 4).getTime());
expect(addWeeks(date2, 4), 'to be', new Date(2020, 1, 1).getTime());
date1 = new Date(2020, 0, 4);
expect(addWeeks(date1, -1), 'to be', new Date(2019, 11, 28).getTime());
expect(addWeeks(date1, -8), 'to be', new Date(2019, 10, 9).getTime());
});
});
describe('addMonths()', function () {
it('returns the time value of N months after a date or time value', function () {
let date1 = new Date(2019, 11, 31);
let date2 = addMonths(date1, 1);
expect(date2, 'to be', new Date(2020, 0, 31).getTime());
expect(addMonths(date2, 29), 'to be', new Date(2022, 5, 30).getTime());
date1 = new Date(2020, 0, 1);
expect(addMonths(date1, -1), 'to be', new Date(2019, 11, 1).getTime());
expect(addMonths(date1, -18), 'to be', new Date(2018, 6, 1).getTime());
});
it('returns the end of the next/prev month if the day of the old date is not in the new month', function () {
let date = new Date(2019, 9, 31);
expect(addMonths(date, 1), 'to be', new Date(2019, 10, 30).getTime());
expect(addMonths(date, 18), 'to be', new Date(2021, 3, 30).getTime());
date = new Date(2019, 11, 31);
expect(addMonths(date, -1), 'to be', new Date(2019, 10, 30).getTime());
expect(addMonths(date, -30), 'to be', new Date(2017, 5, 30).getTime());
date = new Date(2019, 0, 31);
expect(addMonths(date, 1), 'to be', new Date(2019, 1, 28).getTime());
expect(addMonths(date, 13), 'to be', new Date(2020, 1, 29).getTime());
date = new Date(2020, 0, 31);
expect(addMonths(date, 1), 'to be', new Date(2020, 1, 29).getTime());
expect(addMonths(date, 13), 'to be', new Date(2021, 1, 28).getTime());
date = new Date(2019, 2, 30);
expect(addMonths(date, -1), 'to be', new Date(2019, 1, 28).getTime());
expect(addMonths(date, -37), 'to be', new Date(2016, 1, 29).getTime());
date = new Date(2020, 2, 30);
expect(addMonths(date, -1), 'to be', new Date(2020, 1, 29).getTime());
expect(addMonths(date, -37), 'to be', new Date(2017, 1, 28).getTime());
date = new Date(2020, 1, 29);
expect(addMonths(date, 12), 'to be', new Date(2021, 1, 28).getTime());
expect(addMonths(date, -24), 'to be', new Date(2018, 1, 28).getTime());
});
});
describe('addYears()', function () {
it('returns the time value of N years after a date or time value', function () {
let date1 = new Date(2019, 11, 31);
let date2 = addYears(date1, 1);
expect(date2, 'to be', new Date(2020, 11, 31).getTime());
expect(addYears(date2, 3), 'to be', new Date(2023, 11, 31).getTime());
date1 = new Date(2020, 0, 1);
expect(addYears(date1, -1), 'to be', new Date(2019, 0, 1).getTime());
expect(addYears(date1, -3), 'to be', new Date(2017, 0, 1).getTime());
});
it('returns 2/28 of the year if the old date is 2/29 and the new year is not a leap year', function () {
const date = new Date(2020, 1, 29);
expect(addYears(date, 1), 'to be', new Date(2021, 1, 28).getTime());
expect(addYears(date, -1), 'to be', new Date(2019, 1, 28).getTime());
expect(addYears(date, -4), 'to be', new Date(2016, 1, 29).getTime());
});
});
describe('dayOfTheWeekOf()', function () {
it('returns a date object of given day of the week of given date', function () {
expect(dayOfTheWeekOf(new Date(2017, 0, 1), 0), 'to be', new Date(2017, 0, 1).getTime());
expect(dayOfTheWeekOf(new Date(2017, 3, 5), 1), 'to be', new Date(2017, 3, 3).getTime());
expect(dayOfTheWeekOf(new Date(2017, 5, 8), 2), 'to be', new Date(2017, 5, 6).getTime());
expect(dayOfTheWeekOf(new Date(2017, 9, 31), 3), 'to be', new Date(2017, 10, 1).getTime());
expect(dayOfTheWeekOf(new Date(2018, 2, 10), 4), 'to be', new Date(2018, 2, 8).getTime());
expect(dayOfTheWeekOf(new Date(2018, 5, 19), 5), 'to be', new Date(2018, 5, 22).getTime());
expect(dayOfTheWeekOf(new Date(2018, 10, 20), 6), 'to be', new Date(2018, 10, 24).getTime());
});
it('uses sepecified start day of week if it is given', function () {
expect(dayOfTheWeekOf(new Date(2017, 0, 1), 0, 1), 'to be', new Date(2017, 0, 1).getTime());
expect(dayOfTheWeekOf(new Date(2017, 3, 5), 1, 3), 'to be', new Date(2017, 3, 10).getTime());
expect(dayOfTheWeekOf(new Date(2017, 5, 8), 2, 4), 'to be', new Date(2017, 5, 13).getTime());
expect(dayOfTheWeekOf(new Date(2017, 9, 31), 3, 6), 'to be', new Date(2017, 10, 1).getTime());
expect(dayOfTheWeekOf(new Date(2018, 2, 10), 4, 5), 'to be', new Date(2018, 2, 15).getTime());
expect(dayOfTheWeekOf(new Date(2018, 5, 19), 5, 2), 'to be', new Date(2018, 5, 22).getTime());
});
});
describe('getWeek()', function () {
it('returns ISO week number of given date', function () {
expect(getWeek(new Date(2015, 0, 1)), 'to be', 1);
expect(getWeek(new Date(2015, 3, 26)), 'to be', 17);
expect(getWeek(new Date(2015, 8, 12)), 'to be', 37);
expect(getWeek(new Date(2015, 10, 15)), 'to be', 46);
expect(getWeek(new Date(2016, 0, 1)), 'to be', 53);
expect(getWeek(new Date(2016, 0, 7)), 'to be', 1);
expect(getWeek(new Date(2016, 6, 21)), 'to be', 29);
expect(getWeek(new Date(2016, 8, 6)), 'to be', 36);
expect(getWeek(new Date(2016, 11, 1)), 'to be', 48);
expect(getWeek(new Date(2017, 0, 1)), 'to be', 52);
});
});
describe('startOfYearPeriod()', function () {
it('returns the start year of given length of period of years', function () {
let date = new Date(2121, 0, 1);
expect(startOfYearPeriod(date, 10), 'to be', 2120);
expect(startOfYearPeriod(date, 100), 'to be', 2100);
expect(startOfYearPeriod(date, 1000), 'to be', 2000);
date = new Date(1984, 0, 1);
expect(startOfYearPeriod(date, 10), 'to be', 1980);
expect(startOfYearPeriod(date, 100), 'to be', 1900);
expect(startOfYearPeriod(date, 1000), 'to be', 1000);
expect(startOfYearPeriod(new Date(738, 5, 30), 20), 'to be', 720);
expect(startOfYearPeriod(new Date(0, 5, 30).setFullYear(88), 25), 'to be', 75);
});
});
});

142
node_modules/flowbite-datepicker/test/unit/lib/dom.js generated vendored Normal file
View File

@ -0,0 +1,142 @@
import '../_setup.js';
import {
parseHTML,
isVisible,
hideElement,
showElement,
emptyChildNodes,
replaceChildNodes,
} from '../../../js/lib/dom.js';
describe('lib/dom', function () {
let el;
before(function () {
testContainer.innerHTML = '<div class="test"></div>';
el = testContainer.firstChild;
});
after(function () {
testContainer.removeChild(el);
});
describe('parseHTML()', function () {
it('parses an html fragment with Range.prototype.createContextualFragment()', function () {
const spyFragment = sinon.spy(Range.prototype, 'createContextualFragment');
const html = '<div>test</div>';
const result = parseHTML(html);
expect(spyFragment.calledWith(html), 'to be true');
expect(spyFragment.returned(result), 'to be true');
spyFragment.restore();
});
});
describe('isVisible()', function () {
it('returns true if either offsetHeight > 0, offsetWidth > 0 or getClientRects() has an item', function () {
expect(isVisible(el), 'to be false');
let stub = sinon.stub(el, "offsetHeight").get(() => 10);
expect(isVisible(el), 'to be true');
stub.restore();
stub = sinon.stub(el, "offsetWidth").get(() => 10);
expect(isVisible(el), 'to be true');
stub.restore();
stub = sinon.stub(el, 'getClientRects').callsFake(() => [{x: 10}, {x: 20}]);
expect(isVisible(el), 'to be true');
stub.restore();
});
});
describe('hideElement()', function () {
it('sets none to style.display', function () {
hideElement(el);
expect(el.style.display, 'to be', 'none');
el.removeAttribute('style');
});
it('copies style.display to data-style-display attribute if other than none is set', function () {
el.style.display = 'flex';
hideElement(el);
expect(el.style.display, 'to be', 'none');
expect(el.getAttribute('data-style-display'), 'to be', 'flex');
el.removeAttribute('data-style-display');
el.style.display = 'none';
hideElement(el);
expect(el.hasAttribute('data-style-display'), 'to be false');
el.removeAttribute('style');
});
});
describe('showElement()', function () {
it('clears style.display if none is set', function () {
el.style.display = 'none';
showElement(el);
expect(el.style.display, 'to be', '');
el.style.display = 'inline';
showElement(el);
expect(el.style.display, 'to be', 'inline');
el.removeAttribute('style');
});
it('restores the value of data-style-display attribute to style.display if it exists', function () {
el.style.display = 'none';
el.setAttribute('data-style-display', 'flex');
showElement(el);
expect(el.style.display, 'to be', 'flex');
expect(el.hasAttribute('data-style-display'), 'to be false');
el.removeAttribute('style');
});
});
describe('emptyChildNodes()', function () {
it('removes all child nodes', function () {
el.innerHTML = '<div>test</div><!-- comment--><div></div>';
emptyChildNodes(el);
expect(el.innerHTML, 'to be empty');
});
});
describe('replaceChildNodes()', function () {
it('replace all child nodes with given nodes', function () {
const htmlBefore = '<div>test</div><!-- comment--><div></div>';
const htmlAfter = '<div id="foo">foo</div><div id="bar">bar</div>';
// with html
el.innerHTML = htmlBefore;
replaceChildNodes(el, htmlAfter);
expect(el.innerHTML, 'to be', htmlAfter);
// with DocumentFragment
const fragment = document.createRange().createContextualFragment(htmlAfter);
el.innerHTML = htmlBefore;
replaceChildNodes(el, fragment);
expect(el.innerHTML, 'to be', htmlAfter);
// with NodeList
const nodeList = el.querySelectorAll('div');
el.innerHTML = htmlBefore;
replaceChildNodes(el, nodeList);
expect(el.innerHTML, 'to be', htmlAfter);
// with array
el.innerHTML = htmlBefore;
replaceChildNodes(el, Array.from(nodeList));
expect(el.innerHTML, 'to be', htmlAfter);
el.innerHTML = '';
});
});
});

207
node_modules/flowbite-datepicker/test/unit/lib/event.js generated vendored Normal file
View File

@ -0,0 +1,207 @@
import '../_setup.js';
describe('lib/evnet', function () {
let listenerRegistry;
let spyAEL;
let spyREL;
//
let registerListeners;
let unregisterListeners;
let findElementInEventPath;
before(function () {
spyAEL = sinon.spy(EventTarget.prototype, 'addEventListener');
spyREL = sinon.spy(EventTarget.prototype, 'removeEventListener');
const origWeakMap = global.WeakMap;
global.WeakMap = function (...args) {
return listenerRegistry = new origWeakMap(...args);
};
return import('../../../js/lib/event.js')
.then((module) => {
global.WeakMap = origWeakMap;
registerListeners = module.registerListeners;
unregisterListeners = module.unregisterListeners;
findElementInEventPath = module.findElementInEventPath;
});
});
after(function () {
spyAEL.restore();
spyREL.restore();
});
afterEach(function () {
spyAEL.resetHistory();
spyREL.resetHistory();
});
describe('registerListeners()', function () {
let target;
before(function () {
target = document.createElement('input');
testContainer.appendChild(target);
});
after(function () {
testContainer.removeChild(target);
});
it('registers event listeres with key object calling addEventListener()', function () {
const keyObj = {};
const onClick = sinon.spy();
const onKeydown = sinon.spy();
const onMounseEnter = () => {};
const listeners = [
[document, 'click', onClick],
[target, 'keydown', onKeydown, {capture: false}],
[target, 'mouseenter', onMounseEnter, true],
];
registerListeners(keyObj, listeners);
expect(listenerRegistry.get(keyObj), 'to equal', listeners);
expect(spyAEL.args, 'to equal', listeners.map(listener => listener.slice(1)));
testContainer.click();
expect(onClick.called, 'to be true');
simulant.fire(target, 'keydown', {key: 'enter'});
expect(onKeydown.called, 'to be true');
listeners.forEach((listener) => {
EventTarget.prototype.removeEventListener.call(...listener);
});
listenerRegistry.delete(keyObj);
});
it('appends listeners if registration for key object already exists', function () {
const keyObj = {};
const onClick = () => {};
const onKeydown = () => {};
const onMounseEnter = () => {};
const listeners = [
[document, 'click', onClick],
];
const listenersToAdd = [
[target, 'keydown', onKeydown, {capture: false}],
[target, 'mouseenter', onMounseEnter, true],
];
listenerRegistry.set(keyObj, listeners);
registerListeners(keyObj, listenersToAdd);
expect(listeners.length, 'to be', 3);
expect(listenerRegistry.get(keyObj), 'to equal', listeners);
expect(spyAEL.args, 'to equal', listenersToAdd.map(listener => listener.slice(1)));
listenersToAdd.forEach((listener) => {
EventTarget.prototype.removeEventListener.call(...listener);
});
listenerRegistry.delete(keyObj);
});
});
describe('unregisterListeners()', function () {
let target;
before(function () {
target = document.createElement('input');
testContainer.appendChild(target);
});
after(function () {
testContainer.removeChild(target);
});
it('unregisters all event listeres for key object calling removeEventListener()', function () {
const keyObj = {};
const onClick = sinon.spy();
const onKeydown = sinon.spy();
const onMounseEnter = () => {};
const listeners = [
[document, 'click', onClick],
[target, 'keydown', onKeydown, {capture: false}],
[target, 'mouseenter', onMounseEnter, true],
];
listeners.forEach((listener) => {
EventTarget.prototype.addEventListener.call(...listener);
});
listenerRegistry.set(keyObj, listeners);
unregisterListeners(keyObj);
expect(listenerRegistry.get(keyObj), 'to be undefined');
expect(spyREL.args, 'to equal', listeners.map(listener => listener.slice(1)));
testContainer.click();
expect(onClick.called, 'to be false');
simulant.fire(target, 'keydown', {key: 'enter'});
expect(onKeydown.called, 'to be false');
});
});
describe('findElementInEventPath()', function () {
let field;
let control;
let input;
before(function () {
input = document.createElement('input');
control = document.createElement('div');
control.className = 'control';
control.appendChild(input);
field = document.createElement('div');
field.className = 'field';
field.appendChild(control);
testContainer.appendChild(field);
});
after(function () {
testContainer.removeChild(field);
});
it('returns the first element in event\'s path that matches given selector', function () {
const test = (ev) => {
expect(findElementInEventPath(ev, '.control'), 'to be', control);
expect(findElementInEventPath(ev, '.field'), 'to be', field);
expect(findElementInEventPath(ev, 'div'), 'to be', control);
};
document.addEventListener('click', test);
simulant.fire(input, 'click');
document.removeEventListener('click', test);
});
it('returns undefined if no matched element is in event\'s path', function () {
const test = (ev) => {
expect(findElementInEventPath(ev, '.foo'), 'to be undefined',);
};
document.addEventListener('click', test);
simulant.fire(input, 'click');
document.removeEventListener('click', test);
});
it('stops searching when it reaches event\'s currentTarget', function () {
const test = (ev) => {
expect(findElementInEventPath(ev, '.control'), 'to be', control);
expect(findElementInEventPath(ev, '.field'), 'to be undefined',);
};
control.addEventListener('click', test);
simulant.fire(input, 'click');
control.removeEventListener('click', test);
});
it('function can be used to evaluate the condition instead of css selector', function () {
const test = (ev) => {
expect(findElementInEventPath(ev, target => [field, control].includes(target)), 'to be', control);
expect(findElementInEventPath(ev, target => target === field), 'to be', field);
expect(findElementInEventPath(ev, target => target.tagName === 'DIV'), 'to be', control);
};
document.addEventListener('click', test);
simulant.fire(input, 'click');
document.removeEventListener('click', test);
});
});
});

135
node_modules/flowbite-datepicker/test/unit/lib/utils.js generated vendored Normal file
View File

@ -0,0 +1,135 @@
import '../_setup.js';
import {
lastItemOf,
pushUnique,
stringToArray,
isInRange,
limitToRange,
createTagRepeat,
optimizeTemplateHTML
} from '../../../js/lib/utils.js';
describe('lib/utils', function () {
describe('lastItemOf()', function () {
it('returns the last item of array', function () {
expect(lastItemOf([1, 2, 3]), 'to be', 3);
expect(lastItemOf(['foo']), 'to be', 'foo');
expect(lastItemOf([]), 'to be undefined');
});
});
describe('pushUnique()', function () {
it('add items to an array if the item is not in the array', function () {
let arr = ['foo', 'bar'];
pushUnique(arr, 'foo');
expect(arr, 'to equal', arr);
pushUnique(arr, 'baz');
expect(arr, 'to equal', ['foo', 'bar', 'baz']);
arr = ['foo', 'bar'];
pushUnique(arr, 'bar', 'baz', 'bam', 'baz');
expect(arr, 'to equal', ['foo', 'bar', 'baz', 'bam']);
});
it('returns the given array', function () {
let arr = ['foo', 'bar'];
expect(pushUnique(arr, 'baz'), 'to be', arr);
});
});
describe('stringToArray()', function () {
it('converts a string to array by spliting it with given separator', function () {
expect(stringToArray('foo,bar,baz', ','), 'to equal', ['foo', 'bar', 'baz']);
expect(stringToArray('abc-def', '-'), 'to equal', ['abc', 'def']);
});
it('return an empty array if string is empty', function () {
expect(stringToArray('', ','), 'to equal', []);
});
});
describe('isInRange()', function () {
it('returns whether a value is between given min & max values', function () {
expect(isInRange(0, 1, 3), 'to be false');
expect(isInRange(1, 1, 3), 'to be true');
expect(isInRange(2, 1, 3), 'to be true');
expect(isInRange(3, 1, 3), 'to be true');
expect(isInRange(4, 1, 3), 'to be false');
//
expect(isInRange('abb', 'abc', 'ccc'), 'to be false');
expect(isInRange('bbc', 'abc', 'ccc'), 'to be true');
expect(isInRange('ccb', 'abc', 'ccc'), 'to be true');
expect(isInRange('ccd', 'abc', 'ccc'), 'to be false');
});
it('omits minimum check if min = undefined', function () {
expect(isInRange(0, undefined, 3), 'to be true');
expect(isInRange(4, undefined, 3), 'to be false');
expect(isInRange(-Infinity, undefined, 3), 'to be true');
});
it('omits maximum check if max = undefined', function () {
expect(isInRange(0, 1, undefined), 'to be false');
expect(isInRange(4, 1, undefined), 'to be true');
expect(isInRange(Infinity, 1, undefined), 'to be true');
});
});
describe('limitToRange()', function () {
it('returns min value if it\'s specified and the value < min', function () {
expect(limitToRange(0, 1, undefined), 'to be', 1);
expect(limitToRange(0, 1, 3), 'to be', 1);
expect(limitToRange('abb', 'abc', 'ccc'), 'to be', 'abc');
});
it('returns max value if it\'s specified and the value > max', function () {
expect(limitToRange(4, undefined, 3), 'to be', 3);
expect(limitToRange(4, 1, 3), 'to be', 3);
expect(limitToRange('ccd', 'abc', 'ccc'), 'to be', 'ccc');
});
it('returns the given value if it is within the specified min/max', function () {
expect(limitToRange(1, undefined, undefined), 'to be', 1);
expect(limitToRange(1, undefined, 3), 'to be', 1);
expect(limitToRange(3, 1, undefined), 'to be', 3);
expect(limitToRange(1, 1, 3), 'to be', 1);
expect(limitToRange(3, 1, 3), 'to be', 3);
expect(limitToRange('abc', 'aaa', 'ccc'), 'to be', 'abc');
expect(limitToRange('aaa', 'aaa', 'ccc'), 'to be', 'aaa');
expect(limitToRange('ccc', 'aaa', 'ccc'), 'to be', 'ccc');
});
});
describe('createTagRepeat()', function () {
it('returns HTML of a tag repeated specified times', function () {
expect(createTagRepeat('div', 3), 'to be', '<div></div><div></div><div></div>');
expect(createTagRepeat('span', 2), 'to be', '<span></span><span></span>');
// returns at least 1 tag
expect(createTagRepeat('p', 0), 'to be', '<p></p>');
});
it('adds addributes if name:value pairs are given', function () {
expect(createTagRepeat('p', 2, {class: 'foo bar'}), 'to be', '<p class="foo bar"></p><p class="foo bar"></p>');
expect(createTagRepeat('p', 2, {'data-x': '0', 'data-y': '1'}), 'to be', '<p data-x="0" data-y="1"></p><p data-x="0" data-y="1"></p>');
});
it('accepts function that takes current index as the argument for attribute value', function () {
expect(createTagRepeat('li', 3, {'data-cnt': ix => ix + 1}), 'to be', '<li data-cnt="1"></li><li data-cnt="2"></li><li data-cnt="3"></li>');
});
});
describe('optimizeTemplateHTML()', function () {
it('removes spacing before and after tags', function () {
let text = `<div id="test">
<div>
test 123
</div>
</div>`;
expect(optimizeTemplateHTML(text), 'to be', '<div id="test"><div>test 123</div></div>');
expect(optimizeTemplateHTML('foo'), 'to be', 'foo');
});
});
});