arrow-down.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. describe('keyboard operation - arrow-down', function () {
  2. const replaceInput = () => {
  3. const newInput = document.createElement('input');
  4. testContainer.replaceChild(newInput, input);
  5. return newInput;
  6. };
  7. let input;
  8. beforeEach(function () {
  9. input = document.createElement('input');
  10. testContainer.appendChild(input);
  11. });
  12. afterEach(function () {
  13. testContainer.removeChild(input);
  14. });
  15. it('shows the picker if it is hidden', function () {
  16. const {dp, picker} = createDP(input);
  17. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  18. expect(isVisible(picker), 'to be true');
  19. dp.destroy();
  20. });
  21. it('moves the view date/month/year/decade to 1 step down side', function () {
  22. const clock = sinon.useFakeTimers({now: new Date(2044, 5, 15)});
  23. const {dp, picker} = createDP(input);
  24. const viewSwitch = getViewSwitch(picker);
  25. input.focus();
  26. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  27. expect(viewSwitch.textContent, 'to be', 'June 2044');
  28. let cells = getCells(picker);
  29. expect(filterCells(cells, '.focused'), 'to equal', [cells[24]]);
  30. expect(cells[24].textContent, 'to be', '22');
  31. viewSwitch.click();
  32. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  33. expect(viewSwitch.textContent, 'to be', '2044');
  34. cells = getCells(picker);
  35. expect(filterCells(cells, '.focused'), 'to equal', [cells[9]]);
  36. viewSwitch.click();
  37. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  38. expect(viewSwitch.textContent, 'to be', '2040-2049');
  39. cells = getCells(picker);
  40. expect(filterCells(cells, '.focused'), 'to equal', [cells[9]]);
  41. expect(cells[9].textContent, 'to be', '2048');
  42. viewSwitch.click();
  43. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  44. expect(viewSwitch.textContent, 'to be', '2000-2090');
  45. cells = getCells(picker);
  46. expect(filterCells(cells, '.focused'), 'to equal', [cells[9]]);
  47. expect(cells[9].textContent, 'to be', '2080');
  48. dp.destroy();
  49. clock.restore();
  50. });
  51. it('also changes month of the days view if the current view date >= last 7 days of month', function () {
  52. let clock = sinon.useFakeTimers({now: new Date(2020, 1, 23)});
  53. let {dp, picker} = createDP(input);
  54. let viewSwitch = getViewSwitch(picker);
  55. input.focus();
  56. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  57. expect(viewSwitch.textContent, 'to be', 'March 2020');
  58. let cells = getCells(picker);
  59. expect(filterCells(cells, '.focused'), 'to equal', [cells[0]]);
  60. expect(cells[0].textContent, 'to be', '1');
  61. dp.destroy();
  62. clock.restore();
  63. input = replaceInput();
  64. clock = sinon.useFakeTimers({now: new Date(2020, 1, 26)});
  65. ({dp, picker} = createDP(input));
  66. viewSwitch = getViewSwitch(picker);
  67. input.focus();
  68. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  69. expect(viewSwitch.textContent, 'to be', 'March 2020');
  70. cells = getCells(picker);
  71. expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
  72. expect(cells[3].textContent, 'to be', '4');
  73. dp.destroy();
  74. clock.restore();
  75. input = replaceInput();
  76. clock = sinon.useFakeTimers({now: new Date(2020, 1, 29)});
  77. ({dp, picker} = createDP(input));
  78. viewSwitch = getViewSwitch(picker);
  79. input.focus();
  80. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  81. expect(viewSwitch.textContent, 'to be', 'March 2020');
  82. cells = getCells(picker);
  83. expect(filterCells(cells, '.focused'), 'to equal', [cells[6]]);
  84. expect(cells[6].textContent, 'to be', '7');
  85. dp.destroy();
  86. clock.restore();
  87. });
  88. it('also changes year of the months view if the current view month is Sep/Oct/Npv/Dec', function () {
  89. let clock = sinon.useFakeTimers({now: new Date(2020, 8, 1)});
  90. let {dp, picker} = createDP(input);
  91. let viewSwitch = getViewSwitch(picker);
  92. input.focus();
  93. viewSwitch.click();
  94. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  95. expect(viewSwitch.textContent, 'to be', '2021');
  96. let cells = getCells(picker);
  97. expect(filterCells(cells, '.focused'), 'to equal', [cells[0]]);
  98. dp.destroy();
  99. clock.restore();
  100. input = replaceInput();
  101. clock = sinon.useFakeTimers({now: new Date(2020, 9, 1)});
  102. ({dp, picker} = createDP(input));
  103. viewSwitch = getViewSwitch(picker);
  104. input.focus();
  105. viewSwitch.click();
  106. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  107. expect(viewSwitch.textContent, 'to be', '2021');
  108. cells = getCells(picker);
  109. expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
  110. dp.destroy();
  111. clock.restore();
  112. input = replaceInput();
  113. clock = sinon.useFakeTimers({now: new Date(2020, 10, 1)});
  114. ({dp, picker} = createDP(input));
  115. viewSwitch = getViewSwitch(picker);
  116. input.focus();
  117. viewSwitch.click();
  118. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  119. expect(viewSwitch.textContent, 'to be', '2021');
  120. cells = getCells(picker);
  121. expect(filterCells(cells, '.focused'), 'to equal', [cells[2]]);
  122. dp.destroy();
  123. clock.restore();
  124. input = replaceInput();
  125. clock = sinon.useFakeTimers({now: new Date(2020, 11, 1)});
  126. ({dp, picker} = createDP(input));
  127. viewSwitch = getViewSwitch(picker);
  128. input.focus();
  129. viewSwitch.click();
  130. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  131. expect(viewSwitch.textContent, 'to be', '2021');
  132. cells = getCells(picker);
  133. expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
  134. dp.destroy();
  135. clock.restore();
  136. });
  137. it('also changes decade of the years view if the current view year is the first 4 of the decade', function () {
  138. let clock = sinon.useFakeTimers({now: new Date(2016, 1, 1)});
  139. let {dp, picker} = createDP(input);
  140. let viewSwitch = getViewSwitch(picker);
  141. input.focus();
  142. viewSwitch.click();
  143. viewSwitch.click();
  144. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  145. expect(viewSwitch.textContent, 'to be', '2020-2029');
  146. let cells = getCells(picker);
  147. expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
  148. expect(cells[1].textContent, 'to be', '2020');
  149. dp.destroy();
  150. clock.restore();
  151. input = replaceInput();
  152. clock = sinon.useFakeTimers({now: new Date(2018, 1, 1)});
  153. ({dp, picker} = createDP(input));
  154. viewSwitch = getViewSwitch(picker);
  155. input.focus();
  156. viewSwitch.click();
  157. viewSwitch.click();
  158. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  159. expect(viewSwitch.textContent, 'to be', '2020-2029');
  160. cells = getCells(picker);
  161. expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
  162. expect(cells[3].textContent, 'to be', '2022');
  163. dp.destroy();
  164. clock.restore();
  165. input = replaceInput();
  166. clock = sinon.useFakeTimers({now: new Date(2019, 1, 1)});
  167. ({dp, picker} = createDP(input));
  168. viewSwitch = getViewSwitch(picker);
  169. input.focus();
  170. viewSwitch.click();
  171. viewSwitch.click();
  172. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  173. expect(viewSwitch.textContent, 'to be', '2020-2029');
  174. cells = getCells(picker);
  175. expect(filterCells(cells, '.focused'), 'to equal', [cells[4]]);
  176. expect(cells[4].textContent, 'to be', '2023');
  177. dp.destroy();
  178. clock.restore();
  179. });
  180. it('also changes century of the decades view if the current view decade is the first 4 of the century', function () {
  181. let clock = sinon.useFakeTimers({now: new Date(1960, 1, 1)});
  182. let {dp, picker} = createDP(input);
  183. let viewSwitch = getViewSwitch(picker);
  184. input.focus();
  185. viewSwitch.click();
  186. viewSwitch.click();
  187. viewSwitch.click();
  188. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  189. expect(viewSwitch.textContent, 'to be', '2000-2090');
  190. let cells = getCells(picker);
  191. expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
  192. expect(cells[1].textContent, 'to be', '2000');
  193. dp.destroy();
  194. clock.restore();
  195. input = replaceInput();
  196. clock = sinon.useFakeTimers({now: new Date(1980, 1, 1)});
  197. ({dp, picker} = createDP(input));
  198. viewSwitch = getViewSwitch(picker);
  199. input.focus();
  200. viewSwitch.click();
  201. viewSwitch.click();
  202. viewSwitch.click();
  203. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  204. expect(viewSwitch.textContent, 'to be', '2000-2090');
  205. cells = getCells(picker);
  206. expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
  207. expect(cells[3].textContent, 'to be', '2020');
  208. dp.destroy();
  209. clock.restore();
  210. input = replaceInput();
  211. clock = sinon.useFakeTimers({now: new Date(1990, 1, 1)});
  212. ({dp, picker} = createDP(input));
  213. viewSwitch = getViewSwitch(picker);
  214. input.focus();
  215. viewSwitch.click();
  216. viewSwitch.click();
  217. viewSwitch.click();
  218. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  219. expect(viewSwitch.textContent, 'to be', '2000-2090');
  220. cells = getCells(picker);
  221. expect(filterCells(cells, '.focused'), 'to equal', [cells[4]]);
  222. expect(cells[4].textContent, 'to be', '2030');
  223. dp.destroy();
  224. clock.restore();
  225. });
  226. });