arrow-right.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. describe('keyboard operation - arrow-right', 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. it('moves the view date/month/year/decade to 1 step right side', function () {
  11. const clock = sinon.useFakeTimers({now: new Date(2024, 5, 12)});
  12. const {dp, picker} = createDP(input);
  13. const viewSwitch = getViewSwitch(picker);
  14. input.focus();
  15. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  16. expect(viewSwitch.textContent, 'to be', 'June 2024');
  17. let cells = getCells(picker);
  18. expect(filterCells(cells, '.focused'), 'to equal', [cells[10]]);
  19. expect(cells[18].textContent, 'to be', '13');
  20. viewSwitch.click();
  21. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  22. expect(viewSwitch.textContent, 'to be', '2024');
  23. cells = getCells(picker);
  24. expect(filterCells(cells, '.focused'), 'to equal', [cells[6]]);
  25. viewSwitch.click();
  26. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  27. expect(viewSwitch.textContent, 'to be', '2020-2029');
  28. cells = getCells(picker);
  29. expect(filterCells(cells, '.focused'), 'to equal', [cells[6]]);
  30. expect(cells[6].textContent, 'to be', '2025');
  31. viewSwitch.click();
  32. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  33. expect(viewSwitch.textContent, 'to be', '2000-2090');
  34. cells = getCells(picker);
  35. expect(filterCells(cells, '.focused'), 'to equal', [cells[4]]);
  36. expect(cells[4].textContent, 'to be', '2030');
  37. dp.destroy();
  38. clock.restore();
  39. });
  40. it('also changes month of the days view if the current view date is the last day', function () {
  41. const clock = sinon.useFakeTimers({now: new Date(2020, 1, 29)});
  42. const {dp, picker} = createDP(input);
  43. const viewSwitch = getViewSwitch(picker);
  44. input.focus();
  45. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  46. expect(viewSwitch.textContent, 'to be', 'March 2020');
  47. let cells = getCells(picker);
  48. expect(filterCells(cells, '.focused'), 'to equal', [cells[0]]);
  49. expect(cells[0].textContent, 'to be', '1');
  50. dp.destroy();
  51. clock.restore();
  52. });
  53. it('also changes year of the months view if the current view month is December', function () {
  54. const clock = sinon.useFakeTimers({now: new Date(2020, 11, 1)});
  55. const {dp, picker} = createDP(input);
  56. const viewSwitch = getViewSwitch(picker);
  57. input.focus();
  58. viewSwitch.click();
  59. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  60. expect(viewSwitch.textContent, 'to be', '2021');
  61. let cells = getCells(picker);
  62. expect(filterCells(cells, '.focused'), 'to equal', [cells[0]]);
  63. dp.destroy();
  64. clock.restore();
  65. });
  66. it('also changes decade of the years view if the current view year is the end of the decade', function () {
  67. const clock = sinon.useFakeTimers({now: new Date(2019, 1, 1)});
  68. const {dp, picker} = createDP(input);
  69. const viewSwitch = getViewSwitch(picker);
  70. input.focus();
  71. viewSwitch.click();
  72. viewSwitch.click();
  73. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  74. expect(viewSwitch.textContent, 'to be', '2020-2029');
  75. let cells = getCells(picker);
  76. expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
  77. expect(cells[1].textContent, 'to be', '2020');
  78. dp.destroy();
  79. clock.restore();
  80. });
  81. it('also changes century of the decades view if the current view decade is the end of the century', function () {
  82. const clock = sinon.useFakeTimers({now: new Date(1990, 1, 1)});
  83. const {dp, picker} = createDP(input);
  84. const viewSwitch = getViewSwitch(picker);
  85. input.focus();
  86. viewSwitch.click();
  87. viewSwitch.click();
  88. viewSwitch.click();
  89. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  90. expect(viewSwitch.textContent, 'to be', '2000-2090');
  91. let cells = getCells(picker);
  92. expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
  93. expect(cells[1].textContent, 'to be', '2000');
  94. dp.destroy();
  95. clock.restore();
  96. });
  97. describe('with control', function () {
  98. it('functions as the shortcut key of the next button', function () {
  99. const clock = sinon.useFakeTimers({now: new Date(2020, 3, 22)});
  100. const {dp, picker} = createDP(input);
  101. const viewSwitch = getViewSwitch(picker);
  102. input.focus();
  103. simulant.fire(input, 'keydown', {key: 'ArrowRight', ctrlKey: true});
  104. expect(viewSwitch.textContent, 'to be', 'May 2020');
  105. // view date is changed to the same day of the next month
  106. let cells = getCells(picker);
  107. expect(filterCells(cells, '.focused'), 'to equal', [cells[26]]);
  108. expect(cells[26].textContent, 'to be', '22');
  109. viewSwitch.click();
  110. simulant.fire(input, 'keydown', {key: 'ArrowRight', ctrlKey: true});
  111. expect(viewSwitch.textContent, 'to be', '2021');
  112. // view date is changed to the same month of the previous year
  113. cells = getCells(picker);
  114. expect(filterCells(cells, '.focused'), 'to equal', [cells[4]]);
  115. expect(filterCells(cells, '.selected'), 'to equal', []);
  116. viewSwitch.click();
  117. simulant.fire(input, 'keydown', {key: 'ArrowRight', ctrlKey: true});
  118. expect(viewSwitch.textContent, 'to be', '2030-2039');
  119. cells = getCells(picker);
  120. expect(filterCells(cells, '.focused'), 'to equal', [cells[2]]);
  121. expect(cells[2].textContent, 'to be', '2031');
  122. viewSwitch.click();
  123. simulant.fire(input, 'keydown', {key: 'ArrowRight', ctrlKey: true});
  124. expect(viewSwitch.textContent, 'to be', '2100-2190');
  125. cells = getCells(picker);
  126. expect(filterCells(cells, '.focused'), 'to equal', [cells[2]]);
  127. expect(cells[4].textContent, 'to be', '2130');
  128. dp.destroy();
  129. clock.reset();
  130. });
  131. });
  132. describe('with meta', function () {
  133. it('functions as a substitute for the "+ctrl" key combination', function () {
  134. let clock = sinon.useFakeTimers({now: new Date(2020, 3, 22)});
  135. let {dp, picker} = createDP(input);
  136. let viewSwitch = getViewSwitch(picker);
  137. input.focus();
  138. simulant.fire(input, 'keydown', {key: 'ArrowRight', metaKey: true});
  139. expect(viewSwitch.textContent, 'to be', 'May 2020');
  140. // view date is changed to the same day of the next month
  141. let cells = getCells(picker);
  142. expect(filterCells(cells, '.focused'), 'to equal', [cells[26]]);
  143. expect(cells[26].textContent, 'to be', '22');
  144. viewSwitch.click();
  145. simulant.fire(input, 'keydown', {key: 'ArrowRight', metaKey: true});
  146. expect(viewSwitch.textContent, 'to be', '2021');
  147. // view date is changed to the same month of the previous year
  148. cells = getCells(picker);
  149. expect(filterCells(cells, '.focused'), 'to equal', [cells[4]]);
  150. expect(filterCells(cells, '.selected'), 'to equal', []);
  151. viewSwitch.click();
  152. simulant.fire(input, 'keydown', {key: 'ArrowRight', metaKey: true});
  153. expect(viewSwitch.textContent, 'to be', '2030-2039');
  154. cells = getCells(picker);
  155. expect(filterCells(cells, '.focused'), 'to equal', [cells[2]]);
  156. expect(cells[2].textContent, 'to be', '2031');
  157. viewSwitch.click();
  158. simulant.fire(input, 'keydown', {key: 'ArrowRight', metaKey: true});
  159. expect(viewSwitch.textContent, 'to be', '2100-2190');
  160. cells = getCells(picker);
  161. expect(filterCells(cells, '.focused'), 'to equal', [cells[2]]);
  162. expect(cells[4].textContent, 'to be', '2130');
  163. dp.destroy();
  164. clock.restore();
  165. });
  166. });
  167. });