keyboard-operation.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. describe('keyboard operation', function () {
  2. let input;
  3. beforeEach(function () {
  4. input = document.createElement('input');
  5. testContainer.appendChild(input);
  6. });
  7. afterEach(function () {
  8. testContainer.removeChild(input);
  9. });
  10. describe('tab', function () {
  11. it('hides the picker', function () {
  12. const {dp, picker} = createDP(input);
  13. input.focus();
  14. simulant.fire(input, 'keydown', {key: 'Tab'});
  15. expect(isVisible(picker), 'to be false');
  16. dp.destroy();
  17. });
  18. it('updates the selected date with the input\'s value', function () {
  19. const clock = sinon.useFakeTimers({now: new Date(2020, 1, 14)});
  20. const dp = new Datepicker(input);
  21. // when picker is shown
  22. input.focus();
  23. input.value = 'foo';
  24. simulant.fire(input, 'keydown', {key: 'Tab'});
  25. expect(input.value, 'to be', '02/14/2020');
  26. expect(dp.getDate().getTime(), 'to be', dateValue(2020, 1, 14));
  27. // when picker is hidden
  28. input.focus();
  29. input.value = '04/22/2020';
  30. simulant.fire(input, 'keydown', {key: 'Tab'});
  31. expect(input.value, 'to be', '04/22/2020');
  32. expect(dp.getDate().getTime(), 'to be', dateValue(2020, 3, 22));
  33. input.focus();
  34. input.value = '';
  35. simulant.fire(input, 'keydown', {key: 'Tab'});
  36. expect(input.value, 'to be', '');
  37. expect(dp.getDate(), 'to be undefined');
  38. dp.destroy();
  39. clock.restore();
  40. });
  41. });
  42. describe('escape', function () {
  43. it('shows or hides the picker', function () {
  44. const {dp, picker} = createDP(input);
  45. simulant.fire(input, 'keydown', {key: 'Escape'});
  46. expect(isVisible(picker), 'to be true');
  47. simulant.fire(input, 'keydown', {key: 'Escape'});
  48. expect(isVisible(picker), 'to be false');
  49. dp.destroy();
  50. });
  51. });
  52. describe('enter', function () {
  53. it('sets the view date to the selection if the current view is days', function () {
  54. const clock = sinon.useFakeTimers({now: new Date(2020, 1, 14)});
  55. const {dp, picker} = createDP(input);
  56. input.focus();
  57. simulant.fire(input, 'keydown', {key: 'Enter'});
  58. expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
  59. expect(input.value, 'to be', '02/14/2020');
  60. let cells = getCells(picker);
  61. expect(filterCells(cells, '.selected'), 'to equal', [cells[19]]);
  62. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  63. expect(cells[19].textContent, 'to be', '14');
  64. dp.destroy();
  65. clock.reset();
  66. });
  67. it('changes the view to the next minor one if the current view is not days', function () {
  68. const clock = sinon.useFakeTimers({now: new Date(2020, 3, 22)});
  69. const {dp, picker} = createDP(input);
  70. const viewSwitch = getViewSwitch(picker);
  71. input.focus();
  72. viewSwitch.click();
  73. viewSwitch.click();
  74. viewSwitch.click();
  75. simulant.fire(input, 'keydown', {key: 'Enter'});
  76. expect(viewSwitch.textContent, 'to be', '2020-2029');
  77. let cells = getCells(picker);
  78. expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
  79. expect(cells[1].textContent, 'to be', '2020');
  80. simulant.fire(input, 'keydown', {key: 'Enter'});
  81. expect(viewSwitch.textContent, 'to be', '2020');
  82. cells = getCells(picker);
  83. expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
  84. simulant.fire(input, 'keydown', {key: 'Enter'});
  85. expect(viewSwitch.textContent, 'to be', 'April 2020');
  86. cells = getCells(picker);
  87. expect(filterCells(cells, '.focused'), 'to equal', [cells[24]]);
  88. expect(cells[24].textContent, 'to be', '22');
  89. // does nothig if the view has reached to the min view
  90. simulant.fire(input, 'keydown', {key: 'Enter'});
  91. expect(viewSwitch.textContent, 'to be', 'April 2020');
  92. dp.destroy();
  93. clock.reset();
  94. });
  95. it('updates the selection with the input text if the picker is hidden', function () {
  96. const {dp, picker} = createDP(input);
  97. const viewSwitch = getViewSwitch(picker);
  98. input.value = '7/4/2020';
  99. simulant.fire(input, 'keydown', {key: 'Enter'});
  100. expect(dp.dates, 'to equal', [dateValue(2020, 6, 4)]);
  101. expect(input.value, 'to be', '07/04/2020');
  102. dp.show();
  103. expect(viewSwitch.textContent, 'to be', 'July 2020');
  104. let cells = getCells(picker);
  105. expect(filterCells(cells, '.selected'), 'to equal', [cells[6]]);
  106. expect(filterCells(cells, '.focused'), 'to equal', [cells[6]]);
  107. expect(cells[6].textContent, 'to be', '4');
  108. dp.destroy();
  109. });
  110. });
  111. });