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