123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- describe('options - buttons', 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('clearBtn', function () {
- it('displays a button to clear the selection when true', function () {
- const {dp, picker} = createDP(input, {clearBtn: true});
- const [viewSwitch, clearBtn] = getParts(picker, ['.view-switch', '.clear-btn']);
- dp.show();
- expect(isVisible(clearBtn), 'to be true');
- expect(clearBtn.textContent, 'to be', 'Clear');
- // months view
- viewSwitch.click();
- expect(isVisible(clearBtn), 'to be true');
- // years view
- viewSwitch.click();
- expect(isVisible(clearBtn), 'to be true');
- // decades view
- viewSwitch.click();
- expect(isVisible(clearBtn), 'to be true');
- dp.destroy();
- });
- it('can be updated with setOptions()', function () {
- const {dp, picker} = createDP(input);
- dp.setOptions({clearBtn: true});
- dp.show();
- const clearBtn = picker.querySelector('.clear-btn');
- expect(isVisible(clearBtn), 'to be true');
- dp.setOptions({clearBtn: false});
- expect(isVisible(clearBtn), 'to be false');
- dp.destroy();
- });
- describe('clear button', function () {
- let dp;
- let picker;
- let clearBtn;
- beforeEach(function () {
- ({dp, picker} = createDP(input, {clearBtn: true}));
- clearBtn = picker.querySelector('.clear-btn');
- dp.show();
- });
- afterEach(function () {
- dp.destroy();
- });
- it('clears the selection', function () {
- dp.setDate('2/14/2020');
- clearBtn.click();
- expect(dp.dates, 'to equal', []);
- expect(input.value, 'to be', '');
- const viewSwitch = getViewSwitch(picker);
- // months view
- dp.setDate('2/14/2020');
- viewSwitch.click();
- clearBtn.click();
- expect(dp.dates, 'to equal', []);
- // years view
- dp.setDate('2/14/2020');
- viewSwitch.click();
- clearBtn.click();
- expect(dp.dates, 'to equal', []);
- // decades view
- dp.setDate('2/14/2020');
- viewSwitch.click();
- clearBtn.click();
- expect(dp.dates, 'to equal', []);
- });
- it('hides the picker as well when autohide = true', function () {
- dp.setDate('2/14/2020');
- dp.setOptions({autohide: true});
- clearBtn.click();
- expect(isVisible(picker), 'to be false');
- });
- });
- });
- describe('todayBtn', function () {
- it('displays a button to change the view date to current date when true', function () {
- const {dp, picker} = createDP(input, {todayBtn: true});
- const [viewSwitch, todayBtn] = getParts(picker, ['.view-switch', '.today-btn']);
- dp.show();
- expect(isVisible(todayBtn), 'to be true');
- expect(todayBtn.textContent, 'to be', 'Today');
- // months view
- viewSwitch.click();
- expect(isVisible(todayBtn), 'to be true');
- // years view
- viewSwitch.click();
- expect(isVisible(todayBtn), 'to be true');
- // decades view
- viewSwitch.click();
- expect(isVisible(todayBtn), 'to be true');
- dp.destroy();
- });
- it('today will be disabled if the current date is out of the range of minDate/maxDate', function () {
- const {dp, picker} = createDP(input, {todayBtn: true});
- const todayBtn = picker.querySelector('.today-btn');
- dp.show();
- expect(todayBtn.disabled, 'to be false');
- dp.setOptions({minDate: '2/20/2020'});
- expect(todayBtn.disabled, 'to be true');
- dp.setOptions({minDate: null, maxDate: '2/10/2020'});
- expect(todayBtn.disabled, 'to be true');
- dp.setOptions({minDate: '2/1/2020', maxDate: '2/29/2020'});
- expect(todayBtn.disabled, 'to be false');
- dp.destroy();
- });
- it('can be updated with setOptions()', function () {
- const {dp, picker} = createDP(input);
- dp.setOptions({todayBtn: true});
- dp.show();
- const todayBtn = picker.querySelector('.today-btn');
- expect(isVisible(todayBtn), 'to be true');
- dp.setOptions({todayBtn: false});
- expect(isVisible(todayBtn), 'to be false');
- dp.setOptions({todayBtn: 'true'});
- expect(isVisible(todayBtn), 'to be true');
- dp.destroy();
- });
- });
- describe('todayBtnMode', function () {
- let dp;
- let picker;
- let viewSwitch;
- let todayBtn;
- let cells;
- beforeEach(function () {
- ({dp, picker} = createDP(input, {todayBtn: true}));
- [viewSwitch, todayBtn] = getParts(picker, ['.view-switch', '.today-btn']);
- dp.show();
- });
- afterEach(function () {
- dp.destroy();
- });
- it('specifies the behavior of the today buton', function () {
- const date = dateValue(2020, 1, 11);
- // defualt to 0: focus-on (change view date)
- dp.setDate(date);
- todayBtn.click();
- cells = getCells(picker);
- expect(cells[19].textContent, 'to be', '14');
- expect(cells[19].classList.contains('focused'), 'to be true');
- expect(cells[16].classList.contains('selected'), 'to be true');
- expect(dp.dates, 'to equal', [date]);
- dp.destroy();
- // 1: select (change the selection)
- ({dp, picker} = createDP(input, {todayBtn: true, todayBtnMode: 1}));
- todayBtn = picker.querySelector('.today-btn');
- dp.setDate(date);
- dp.show();
- todayBtn.click();
- cells = getCells(picker);
- expect(cells[19].textContent, 'to be', '14');
- expect(cells[19].classList.contains('focused'), 'to be true');
- expect(cells[19].classList.contains('selected'), 'to be true');
- expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
- });
- it('can be updated with setOptions()', function () {
- const date = dateValue(2020, 1, 11);
- dp.setDate(date);
- dp.setOptions({todayBtnMode: 1});
- todayBtn.click();
- cells = getCells(picker);
- expect(cells[19].textContent, 'to be', '14');
- expect(cells[19].classList.contains('focused'), 'to be true');
- expect(cells[19].classList.contains('selected'), 'to be true');
- expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
- dp.setDate(date);
- dp.setOptions({todayBtnMode: 0});
- todayBtn.click();
- expect(cells[19].textContent, 'to be', '14');
- expect(cells[19].classList.contains('focused'), 'to be true');
- expect(cells[16].classList.contains('selected'), 'to be true');
- expect(dp.dates, 'to equal', [date]);
- });
- describe('today button', function () {
- it('changes the view date to the current date when todayBtnMode = 0', function () {
- dp.setDate('4/22/2020');
- todayBtn.click();
- expect(viewSwitch.textContent, 'to be', 'February 2020');
- cells = getCells(picker);
- expect(cells[19].textContent, 'to be', '14');
- expect(cells[19].classList.contains('focused'), 'to be true');
- expect(cells.find(el => el.classList.contains('selected')), 'to be undefined');
- expect(dp.dates, 'to equal', [dateValue(2020, 3, 22)]);
- expect(input.value, 'to be', '04/22/2020');
- dp.setDate({clear: true});
- });
- it('also changes the view to days view when todayBtnMode = 0', function () {
- // months view
- dp.setDate('4/22/2020');
- viewSwitch.click();
- todayBtn.click();
- expect(viewSwitch.textContent, 'to be', 'February 2020');
- // years view
- dp.setDate({clear: true});
- dp.setDate('4/22/2020');
- viewSwitch.click();
- viewSwitch.click();
- todayBtn.click();
- expect(viewSwitch.textContent, 'to be', 'February 2020');
- // decades view
- dp.setDate({clear: true});
- dp.setDate('4/22/2020');
- viewSwitch.click();
- viewSwitch.click();
- viewSwitch.click();
- todayBtn.click();
- expect(viewSwitch.textContent, 'to be', 'February 2020');
- dp.setDate({clear: true});
- });
- it('changes the selection to the current date when todayBtnMode = 1', function () {
- dp.setOptions({todayBtnMode: 1});
- dp.setDate('4/22/2020');
- todayBtn.click();
- expect(viewSwitch.textContent, 'to be', 'February 2020');
- cells = getCells(picker);
- expect(cells[19].textContent, 'to be', '14');
- expect(cells[19].classList.contains('focused'), 'to be true');
- expect(cells[19].classList.contains('selected'), 'to be true');
- expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
- expect(input.value, 'to be', '02/14/2020');
- dp.setDate({clear: true});
- dp.setDate('4/22/2020');
- viewSwitch.click();
- viewSwitch.click();
- todayBtn.click();
- expect(viewSwitch.textContent, 'to be', 'February 2020');
- expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
- dp.setDate({clear: true});
- });
- it('also hides the picker when todayBtnMode = 1 and autohide = true', function () {
- dp.setOptions({todayBtnMode: 1, autohide: true});
- dp.setDate('4/22/2020');
- todayBtn.click();
- expect(isVisible(picker), 'to be false');
- dp.setDate({clear: true});
- });
- it('always changes the view to current date\'s days view when todayBtnMode = 1', function () {
- const nextBtn = picker.querySelector('.next-btn');
- dp.setOptions({todayBtnMode: 1});
- // after moving other month or view while the current date is selected already
- // (issue #11)
- todayBtn.click();
- nextBtn.click();
- todayBtn.click();
- cells = getCells(picker);
- expect(viewSwitch.textContent, 'to be', 'February 2020');
- expect(cells[19].classList.contains('focused'), 'to be true');
- expect(cells[19].classList.contains('selected'), 'to be true');
- expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
- viewSwitch.click();
- nextBtn.click();
- todayBtn.click();
- cells = getCells(picker);
- expect(viewSwitch.textContent, 'to be', 'February 2020');
- expect(cells[19].classList.contains('focused'), 'to be true');
- expect(cells[19].classList.contains('selected'), 'to be true');
- expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
- viewSwitch.click();
- viewSwitch.click();
- nextBtn.click();
- nextBtn.click();
- todayBtn.click();
- cells = getCells(picker);
- expect(viewSwitch.textContent, 'to be', 'February 2020');
- expect(cells[19].classList.contains('focused'), 'to be true');
- expect(cells[19].classList.contains('selected'), 'to be true');
- expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
- // when current date is deslected by toggling in multi-date mode
- dp.setOptions({maxNumberOfDates: 3});
- nextBtn.click();
- getCells(picker)[20].click();
- todayBtn.click();
- cells = getCells(picker);
- expect(viewSwitch.textContent, 'to be', 'February 2020');
- expect(cells[19].classList.contains('focused'), 'to be true');
- expect(cells[19].classList.contains('selected'), 'to be false');
- expect(dp.dates, 'to equal', [dateValue(2020, 2, 21)]);
- nextBtn.click();
- todayBtn.click();
- cells = getCells(picker);
- expect(viewSwitch.textContent, 'to be', 'February 2020');
- expect(cells[19].classList.contains('selected'), 'to be true');
- expect(dp.dates, 'to equal', [dateValue(2020, 2, 21), dateValue(2020, 1, 14)]);
- viewSwitch.click();
- nextBtn.click();
- todayBtn.click();
- cells = getCells(picker);
- expect(viewSwitch.textContent, 'to be', 'February 2020');
- expect(cells[19].classList.contains('focused'), 'to be true');
- expect(cells[19].classList.contains('selected'), 'to be false');
- expect(dp.dates, 'to equal', [dateValue(2020, 2, 21)]);
- dp.setDate({clear: true});
- });
- });
- });
- });
|