123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- describe('keyboard operation - edit mode', function () {
- let input;
- beforeEach(function () {
- input = document.createElement('input');
- testContainer.appendChild(input);
- });
- afterEach(function () {
- testContainer.removeChild(input);
- });
- it('turns on when Datepicker.enterEditMode() is called', function () {
- const dp = new Datepicker(input);
- input.focus();
- dp.enterEditMode();
- expect(dp.editMode, 'to be true');
- expect(input.classList.contains('in-edit'), 'to be true');
- dp.destroy();
- input.classList.remove('in-edit');
- });
- it('turns on when a printable letter, backspace or delete key is pressed without ctrl/meta', function () {
- const dp = new Datepicker(input);
- input.focus();
- simulant.fire(input, 'keydown', {key: '1'});
- expect(dp.editMode, 'to be true');
- delete dp.editMode;
- input.classList.remove('in-edit');
- simulant.fire(input, 'keydown', {key: 'J'});
- expect(dp.editMode, 'to be true');
- delete dp.editMode;
- input.classList.remove('in-edit');
- simulant.fire(input, 'keydown', {key: '/'});
- expect(dp.editMode, 'to be true');
- delete dp.editMode;
- input.classList.remove('in-edit');
- simulant.fire(input, 'keydown', {key: ' '});
- expect(dp.editMode, 'to be true');
- delete dp.editMode;
- input.classList.remove('in-edit');
- simulant.fire(input, 'keydown', {key: 'Backspace'});
- expect(dp.editMode, 'to be true');
- delete dp.editMode;
- input.classList.remove('in-edit');
- simulant.fire(input, 'keydown', {key: 'Delete'});
- expect(dp.editMode, 'to be true');
- delete dp.editMode;
- input.classList.remove('in-edit');
- // with modifier key
- simulant.fire(input, 'keydown', {key: '1', shiftKey: true});
- expect(dp.editMode, 'to be true');
- delete dp.editMode;
- input.classList.remove('in-edit');
- simulant.fire(input, 'keydown', {key: '1', altKey: true});
- expect(dp.editMode, 'to be true');
- delete dp.editMode;
- input.classList.remove('in-edit');
- simulant.fire(input, 'keydown', {key: '1', ctrlKey: true});
- expect(dp.editMode, 'to be undefined');
- expect(input.classList.contains('in-edit'), 'to be false');
- simulant.fire(input, 'keydown', {key: '1', metaKey: true});
- expect(dp.editMode, 'to be undefined');
- expect(input.classList.contains('in-edit'), 'to be false');
- // non-pritable-letter key
- simulant.fire(input, 'keydown', {key: 'PageDown'});
- expect(dp.editMode, 'to be undefined');
- expect(input.classList.contains('in-edit'), 'to be false');
- simulant.fire(input, 'keydown', {key: 'Escape', ctrlKey: true});
- expect(dp.editMode, 'to be undefined');
- dp.destroy();
- });
- it('turns on when shift + either of arrow keys is pressed without ctrl/meta', function () {
- const dp = new Datepicker(input);
- input.focus();
- // shift + arrow
- simulant.fire(input, 'keydown', {key: 'ArrowLeft', shiftKey: true});
- expect(dp.editMode, 'to be true');
- delete dp.editMode;
- input.classList.remove('in-edit');
- simulant.fire(input, 'keydown', {key: 'ArrowRight', shiftKey: true});
- expect(dp.editMode, 'to be true');
- delete dp.editMode;
- input.classList.remove('in-edit');
- simulant.fire(input, 'keydown', {key: 'ArrowUp', shiftKey: true});
- expect(dp.editMode, 'to be true');
- delete dp.editMode;
- input.classList.remove('in-edit');
- simulant.fire(input, 'keydown', {key: 'ArrowDown', shiftKey: true});
- expect(dp.editMode, 'to be true');
- delete dp.editMode;
- input.classList.remove('in-edit');
- // arrow + other modifire key
- // arrow-left
- simulant.fire(input, 'keydown', {key: 'ArrowLeft', altKey: true});
- expect(dp.editMode, 'to be undefined');
- simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
- expect(dp.editMode, 'to be undefined');
- simulant.fire(input, 'keydown', {key: 'ArrowLeft', metaKey: true});
- expect(dp.editMode, 'to be undefined');
- // arrow-right
- simulant.fire(input, 'keydown', {key: 'ArrowRight', altKey: true});
- expect(dp.editMode, 'to be undefined');
- simulant.fire(input, 'keydown', {key: 'ArrowRight', ctrlKey: true});
- expect(dp.editMode, 'to be undefined');
- simulant.fire(input, 'keydown', {key: 'ArrowRight', metaKey: true});
- expect(dp.editMode, 'to be undefined');
- // arrow-up
- simulant.fire(input, 'keydown', {key: 'ArrowUp', altKey: true});
- expect(dp.editMode, 'to be undefined');
- simulant.fire(input, 'keydown', {key: 'ArrowUp', ctrlKey: true});
- expect(dp.editMode, 'to be undefined');
- simulant.fire(input, 'keydown', {key: 'ArrowUp', metaKey: true});
- expect(dp.editMode, 'to be undefined');
- // arrow-down
- simulant.fire(input, 'keydown', {key: 'ArrowDown', altKey: true});
- expect(dp.editMode, 'to be undefined');
- simulant.fire(input, 'keydown', {key: 'ArrowDown', ctrlKey: true});
- expect(dp.editMode, 'to be undefined');
- simulant.fire(input, 'keydown', {key: 'ArrowDown', metaKey: true});
- expect(dp.editMode, 'to be undefined');
- // shift + arrow with other modifier key
- // arrow-left
- simulant.fire(input, 'keydown', {key: 'ArrowLeft', shiftKey: true, altKey: true});
- expect(dp.editMode, 'to be true');
- delete dp.editMode;
- input.classList.remove('in-edit');
- simulant.fire(input, 'keydown', {key: 'ArrowLeft', shiftKey: true, ctrlKey: true});
- expect(dp.editMode, 'to be undefined');
- simulant.fire(input, 'keydown', {key: 'ArrowLeft', shiftKey: true, metaKey: true});
- expect(dp.editMode, 'to be undefined');
- // arrow-right
- simulant.fire(input, 'keydown', {key: 'ArrowRight', shiftKey: true, altKey: true});
- expect(dp.editMode, 'to be true');
- delete dp.editMode;
- input.classList.remove('in-edit');
- simulant.fire(input, 'keydown', {key: 'ArrowRight', shiftKey: true, ctrlKey: true});
- expect(dp.editMode, 'to be undefined');
- simulant.fire(input, 'keydown', {key: 'ArrowRight', shiftKey: true, metaKey: true});
- expect(dp.editMode, 'to be undefined');
- // arrow-up
- simulant.fire(input, 'keydown', {key: 'ArrowUp', shiftKey: true, altKey: true});
- expect(dp.editMode, 'to be true');
- delete dp.editMode;
- input.classList.remove('in-edit');
- simulant.fire(input, 'keydown', {key: 'ArrowUp', shiftKey: true, ctrlKey: true});
- expect(dp.editMode, 'to be undefined');
- simulant.fire(input, 'keydown', {key: 'ArrowUp', shiftKey: true, metaKey: true});
- expect(dp.editMode, 'to be undefined');
- // arrow-down
- simulant.fire(input, 'keydown', {key: 'ArrowDown', shiftKey: true, altKey: true});
- expect(dp.editMode, 'to be true');
- delete dp.editMode;
- input.classList.remove('in-edit');
- simulant.fire(input, 'keydown', {key: 'ArrowDown', shiftKey: true, ctrlKey: true});
- expect(dp.editMode, 'to be undefined');
- simulant.fire(input, 'keydown', {key: 'ArrowDown', shiftKey: true, metaKey: true});
- expect(dp.editMode, 'to be undefined');
- dp.destroy();
- });
- it('turns on when input is clicked', function () {
- const dp = new Datepicker(input);
- input.focus();
- simulant.fire(input, 'mousedown');
- input.click();
- expect(dp.editMode, 'to be true');
- expect(input.classList.contains('in-edit'), 'to be true');
- dp.destroy();
- input.classList.remove('in-edit');
- });
- it('does not turn on when the picker is hidden', function () {
- const dp = new Datepicker(input);
- dp.enterEditMode();
- expect(dp.editMode, 'to be undefined');
- expect(input.classList.contains('in-edit'), 'to be false');
- simulant.fire(input, 'keydown', {key: '1'});
- expect(dp.editMode, 'to be undefined');
- simulant.fire(input, 'keydown', {key: 'J'});
- expect(dp.editMode, 'to be undefined');
- simulant.fire(input, 'mousedown');
- input.click();
- expect(dp.editMode, 'to be undefined');
- dp.destroy();
- });
- it('disables the arrow-key operation of the picker', function () {
- const clock = sinon.useFakeTimers({now: new Date(2020, 1, 14)});
- const {dp, picker} = createDP(input);
- const viewSwitch = getViewSwitch(picker);
- input.focus();
- dp.enterEditMode();
- simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
- expect(viewSwitch.textContent, 'to be', 'February 2020');
- let cells = getCells(picker);
- expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
- expect(cells[19].textContent, 'to be', '14');
- simulant.fire(input, 'keydown', {key: 'ArrowRight'});
- expect(viewSwitch.textContent, 'to be', 'February 2020');
- cells = getCells(picker);
- expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
- simulant.fire(input, 'keydown', {key: 'ArrowUp'});
- expect(viewSwitch.textContent, 'to be', 'February 2020');
- cells = getCells(picker);
- expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
- simulant.fire(input, 'keydown', {key: 'ArrowDownt'});
- expect(viewSwitch.textContent, 'to be', 'February 2020');
- cells = getCells(picker);
- expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
- viewSwitch.click();
- simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
- expect(viewSwitch.textContent, 'to be', '2020');
- cells = getCells(picker);
- expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
- viewSwitch.click();
- simulant.fire(input, 'keydown', {key: 'ArrowRight'});
- expect(viewSwitch.textContent, 'to be', '2020-2029');
- cells = getCells(picker);
- expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
- viewSwitch.click();
- simulant.fire(input, 'keydown', {key: 'ArrowDown'});
- expect(viewSwitch.textContent, 'to be', '2000-2090');
- cells = getCells(picker);
- expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
- dp.destroy();
- clock.restore();
- });
- it('turns off when Datepicker.exitEditMode() is called', function () {
- const dp = new Datepicker(input);
- input.focus();
- dp.enterEditMode();
- dp.exitEditMode();
- expect(dp.editMode, 'to be undefined');
- expect(input.classList.contains('in-edit'), 'to be false');
- dp.destroy();
- });
- it('turns off when the picker hides', function () {
- const {dp, picker} = createDP(input);
- input.focus();
- dp.enterEditMode();
- dp.hide();
- expect(dp.editMode, 'to be undefined');
- expect(input.classList.contains('in-edit'), 'to be false');
- dp.destroy();
- });
- it('turns off when escape key is pressed', function () {
- const dp = new Datepicker(input);
- input.focus();
- dp.enterEditMode();
- simulant.fire(input, 'keydown', {key: 'Escape'});
- expect(dp.editMode, 'to be undefined');
- expect(input.classList.contains('in-edit'), 'to be false');
- dp.destroy();
- });
- it('leaves the edit on the input as-is by default when turning off', function () {
- const dp = new Datepicker(input);
- const date = dateValue(2020, 1, 14);
- dp.setDate(date);
- input.focus();
- dp.enterEditMode();
- input.value = '4/22/2020';
- dp.exitEditMode();
- expect(input.value, 'to be', '4/22/2020');
- expect(dp.dates, 'to equal', [date]);
- dp.show();
- dp.enterEditMode();
- input.value = '3/8/2020';
- dp.hide();
- expect(input.value, 'to be', '3/8/2020');
- expect(dp.dates, 'to equal', [date]);
- dp.show();
- dp.enterEditMode();
- input.value = '02/14/2020';
- simulant.fire(input, 'keydown', {key: 'Escape'});
- expect(input.value, 'to be', '02/14/2020');
- expect(dp.dates, 'to equal', [date]);
- dp.destroy();
- });
- it('hides the picker when turning off by escape key press', function () {
- const {dp, picker} = createDP(input);
- input.focus();
- dp.enterEditMode();
- simulant.fire(input, 'keydown', {key: 'Escape'});
- expect(isVisible(picker), 'to be false');
- dp.show();
- dp.enterEditMode();
- dp.exitEditMode();
- expect(isVisible(picker), 'to be true');
- dp.destroy();
- });
- it('updates the selection with the input when turning off by exitEditMode() call with update: true option', function () {
- const dp = new Datepicker(input);
- const date = dateValue(2020, 3, 22);
- dp.setDate('02/14/2020');
- input.focus();
- dp.enterEditMode();
- input.value = '4/22/2020';
- dp.exitEditMode({update: true});
- expect(dp.editMode, 'to be undefined');
- expect(input.classList.contains('in-edit'), 'to be false');
- expect(input.value, 'to be', '04/22/2020');
- expect(dp.dates, 'to equal', [date]);
- dp.destroy();
- });
- it('updates the selection with the input when turning off by enter key press', function () {
- const dp = new Datepicker(input);
- const date = dateValue(2020, 3, 22);
- dp.setDate('02/14/2020');
- input.focus();
- dp.enterEditMode();
- input.value = '4/22/2020';
- simulant.fire(input, 'keydown', {key: 'Enter'});
- expect(dp.editMode, 'to be undefined');
- expect(input.classList.contains('in-edit'), 'to be false');
- expect(input.value, 'to be', '04/22/2020');
- expect(dp.dates, 'to equal', [date]);
- dp.destroy();
- });
- it('updates the selection with the input when turning off being induced by unfocusing input element', function () {
- const outsider = document.createElement('p');
- testContainer.appendChild(outsider);
- const dp = new Datepicker(input);
- const date = dateValue(2020, 3, 22);
- // by tab key-press
- dp.setDate('02/14/2020');
- input.focus();
- dp.enterEditMode();
- input.value = '4/22/2020';
- simulant.fire(input, 'keydown', {key: 'Tab'});
- expect(dp.editMode, 'to be undefined');
- expect(input.classList.contains('in-edit'), 'to be false');
- expect(input.value, 'to be', '04/22/2020');
- expect(dp.dates, 'to equal', [date]);
- //by clicking outside
- dp.setDate('02/14/2020');
- dp.show();
- dp.enterEditMode();
- input.value = '4/22/2020';
- simulant.fire(outsider, 'mousedown');
- expect(dp.editMode, 'to be undefined');
- expect(input.classList.contains('in-edit'), 'to be false');
- expect(input.value, 'to be', '04/22/2020');
- expect(dp.dates, 'to equal', [date]);
- dp.destroy();
- testContainer.removeChild(outsider);
- });
- });
|