arrow-left.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. describe('keyboard operation - arrow-left', 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 left 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: 'ArrowLeft'});
  16. expect(viewSwitch.textContent, 'to be', 'June 2024');
  17. let cells = getCells(picker);
  18. expect(filterCells(cells, '.focused'), 'to equal', [cells[16]]);
  19. expect(cells[16].textContent, 'to be', '11');
  20. viewSwitch.click();
  21. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  22. expect(viewSwitch.textContent, 'to be', '2024');
  23. cells = getCells(picker);
  24. expect(filterCells(cells, '.focused'), 'to equal', [cells[4]]);
  25. viewSwitch.click();
  26. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  27. expect(viewSwitch.textContent, 'to be', '2020-2029');
  28. cells = getCells(picker);
  29. expect(filterCells(cells, '.focused'), 'to equal', [cells[4]]);
  30. expect(cells[4].textContent, 'to be', '2023');
  31. viewSwitch.click();
  32. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  33. expect(viewSwitch.textContent, 'to be', '2000-2090');
  34. cells = getCells(picker);
  35. expect(filterCells(cells, '.focused'), 'to equal', [cells[2]]);
  36. expect(cells[2].textContent, 'to be', '2010');
  37. clock.restore();
  38. dp.destroy();
  39. });
  40. it('also changes month of the days view if the current view date is the 1st', function () {
  41. const clock = sinon.useFakeTimers({now: new Date(2020, 2, 1)});
  42. const {dp, picker} = createDP(input);
  43. const viewSwitch = getViewSwitch(picker);
  44. input.focus();
  45. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  46. expect(viewSwitch.textContent, 'to be', 'February 2020');
  47. let cells = getCells(picker);
  48. expect(filterCells(cells, '.focused'), 'to equal', [cells[34]]);
  49. expect(cells[34].textContent, 'to be', '29');
  50. dp.destroy();
  51. clock.restore();
  52. });
  53. it('also changes year of the months view if the current view month is January', function () {
  54. const clock = sinon.useFakeTimers({now: new Date(2020, 0, 1)});
  55. const {dp, picker} = createDP(input);
  56. const viewSwitch = getViewSwitch(picker);
  57. input.focus();
  58. viewSwitch.click();
  59. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  60. expect(viewSwitch.textContent, 'to be', '2019');
  61. let cells = getCells(picker);
  62. expect(filterCells(cells, '.focused'), 'to equal', [cells[11]]);
  63. dp.destroy();
  64. clock.restore();
  65. });
  66. it('also changes decade of the years view if the current view year is the start of the decade', function () {
  67. const clock = sinon.useFakeTimers({now: new Date(2020, 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: 'ArrowLeft'});
  74. expect(viewSwitch.textContent, 'to be', '2010-2019');
  75. let cells = getCells(picker);
  76. expect(filterCells(cells, '.focused'), 'to equal', [cells[10]]);
  77. expect(cells[10].textContent, 'to be', '2019');
  78. dp.destroy();
  79. clock.restore();
  80. });
  81. it('also changes century of the decades view if the current view decade is the start of the century', function () {
  82. const clock = sinon.useFakeTimers({now: new Date(2000, 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: 'ArrowLeft'});
  90. expect(viewSwitch.textContent, 'to be', '1900-1990');
  91. let cells = getCells(picker);
  92. expect(filterCells(cells, '.focused'), 'to equal', [cells[10]]);
  93. expect(cells[10].textContent, 'to be', '1990');
  94. dp.destroy();
  95. clock.restore();
  96. });
  97. it('does nothing if the view date is 0000-01-01', function () {
  98. input.value = '01/01/0000';
  99. const {dp, picker} = createDP(input);
  100. const viewSwitch = getViewSwitch(picker);
  101. input.focus();
  102. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  103. expect(viewSwitch.textContent, 'to be', 'January 0');
  104. let cells = getCells(picker);
  105. expect(filterCells(cells, '.focused'), 'to equal', [cells[6]]);
  106. expect(cells[6].textContent, 'to be', '1');
  107. viewSwitch.click();
  108. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  109. expect(viewSwitch.textContent, 'to be', '0');
  110. cells = getCells(picker);
  111. expect(filterCells(cells, '.focused'), 'to equal', [cells[0]]);
  112. viewSwitch.click();
  113. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  114. expect(viewSwitch.textContent, 'to be', '0-9');
  115. cells = getCells(picker);
  116. expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
  117. expect(cells[1].textContent, 'to be', '0');
  118. viewSwitch.click();
  119. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  120. expect(viewSwitch.textContent, 'to be', '0-90');
  121. cells = getCells(picker);
  122. expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
  123. expect(cells[1].textContent, 'to be', '0');
  124. dp.destroy();
  125. });
  126. describe('with control', function () {
  127. it('functions as the shortcut key of the prev button', function () {
  128. const clock = sinon.useFakeTimers({now: new Date(2020, 3, 22)});
  129. const {dp, picker} = createDP(input);
  130. const viewSwitch = getViewSwitch(picker);
  131. input.focus();
  132. simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
  133. expect(viewSwitch.textContent, 'to be', 'March 2020');
  134. // view date is changed to the same day of the previous month
  135. let cells = getCells(picker);
  136. expect(filterCells(cells, '.focused'), 'to equal', [cells[21]]);
  137. expect(cells[21].textContent, 'to be', '22');
  138. viewSwitch.click();
  139. simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
  140. expect(viewSwitch.textContent, 'to be', '2019');
  141. // view date is changed to the same month of the previous year
  142. cells = getCells(picker);
  143. expect(filterCells(cells, '.focused'), 'to equal', [cells[2]]);
  144. expect(filterCells(cells, '.selected'), 'to equal', []);
  145. viewSwitch.click();
  146. simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
  147. expect(viewSwitch.textContent, 'to be', '2000-2009');
  148. cells = getCells(picker);
  149. expect(filterCells(cells, '.focused'), 'to equal', [cells[10]]);
  150. expect(cells[10].textContent, 'to be', '2009');
  151. viewSwitch.click();
  152. simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
  153. expect(viewSwitch.textContent, 'to be', '1900-1990');
  154. cells = getCells(picker);
  155. expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
  156. expect(cells[1].textContent, 'to be', '1900');
  157. dp.destroy();
  158. clock.reset();
  159. });
  160. });
  161. describe('with meta', function () {
  162. it('functions as a substitute for the "+ctrl" key combination', function () {
  163. let clock = sinon.useFakeTimers({now: new Date(2020, 3, 22)});
  164. let {dp, picker} = createDP(input);
  165. let viewSwitch = getViewSwitch(picker);
  166. input.focus();
  167. simulant.fire(input, 'keydown', {key: 'ArrowLeft', metaKey: true});
  168. expect(viewSwitch.textContent, 'to be', 'March 2020');
  169. // view date is changed to the same day of the previous month
  170. let cells = getCells(picker);
  171. expect(filterCells(cells, '.focused'), 'to equal', [cells[21]]);
  172. expect(cells[21].textContent, 'to be', '22');
  173. viewSwitch.click();
  174. simulant.fire(input, 'keydown', {key: 'ArrowLeft', metaKey: true});
  175. expect(viewSwitch.textContent, 'to be', '2019');
  176. // view date is changed to the same month of the previous year
  177. cells = getCells(picker);
  178. expect(filterCells(cells, '.focused'), 'to equal', [cells[2]]);
  179. expect(filterCells(cells, '.selected'), 'to equal', []);
  180. viewSwitch.click();
  181. simulant.fire(input, 'keydown', {key: 'ArrowLeft', metaKey: true});
  182. expect(viewSwitch.textContent, 'to be', '2000-2009');
  183. cells = getCells(picker);
  184. expect(filterCells(cells, '.focused'), 'to equal', [cells[10]]);
  185. expect(cells[10].textContent, 'to be', '2009');
  186. viewSwitch.click();
  187. simulant.fire(input, 'keydown', {key: 'ArrowLeft', metaKey: true});
  188. expect(viewSwitch.textContent, 'to be', '1900-1990');
  189. cells = getCells(picker);
  190. expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
  191. expect(cells[1].textContent, 'to be', '1900');
  192. dp.destroy();
  193. clock.restore();
  194. });
  195. });
  196. });