second commit
This commit is contained in:
310
node_modules/flowbite-datepicker/test/unit/lib/date-format.js
generated
vendored
Normal file
310
node_modules/flowbite-datepicker/test/unit/lib/date-format.js
generated
vendored
Normal 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
211
node_modules/flowbite-datepicker/test/unit/lib/date.js
generated
vendored
Normal 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
142
node_modules/flowbite-datepicker/test/unit/lib/dom.js
generated
vendored
Normal 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
207
node_modules/flowbite-datepicker/test/unit/lib/event.js
generated
vendored
Normal 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
135
node_modules/flowbite-datepicker/test/unit/lib/utils.js
generated
vendored
Normal 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');
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user