pick-levle+view.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. describe('options - pick level & view', function () {
  2. let clock;
  3. let input;
  4. beforeEach(function () {
  5. clock = sinon.useFakeTimers({now: new Date(2020, 1, 14)});
  6. input = document.createElement('input');
  7. testContainer.appendChild(input);
  8. });
  9. afterEach(function () {
  10. testContainer.removeChild(input);
  11. clock.restore();
  12. });
  13. describe('pickLevel', function () {
  14. it('limits the minimum of available views', function () {
  15. const {dp, picker} = createDP(input, {pickLevel: 2});
  16. const viewSwitch = getViewSwitch(picker);
  17. const cells1 = getCells(picker)[1];
  18. dp.show();
  19. expect(viewSwitch.textContent, 'to be', '2020-2029');
  20. expect(cells1.textContent, 'to be', '2020');
  21. cells1.click();
  22. expect(viewSwitch.textContent, 'to be', '2020-2029');
  23. expect(getCells(picker)[1].textContent, 'to be', '2020');
  24. dp.destroy();
  25. });
  26. it('changes the selection level to month when 1', function () {
  27. input.value = '2/14/2020';
  28. const {dp, picker} = createDP(input, {pickLevel: 1});
  29. const [viewSwitch, nextBtn] = getParts(picker, ['.view-switch', '.next-btn']);
  30. let cells = getCells(picker);
  31. dp.show();
  32. expect(dp.dates, 'to equal', [dateValue(2020, 1, 1)]);
  33. expect(input.value, 'to be', '02/01/2020');
  34. expect(viewSwitch.textContent, 'to be', '2020');
  35. expect(filterCells(cells, '.selected'), 'to equal', [cells[1]]);
  36. expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
  37. // mouse operation
  38. cells[0].click();
  39. cells = getCells(picker);
  40. expect(dp.dates, 'to equal', [dateValue(2020, 0, 1)]);
  41. expect(input.value, 'to be', '01/01/2020');
  42. expect(viewSwitch.textContent, 'to be', '2020');
  43. expect(filterCells(cells, '.selected'), 'to equal', [cells[0]]);
  44. expect(filterCells(cells, '.focused'), 'to equal', [cells[0]]);
  45. nextBtn.click();
  46. getCells(picker)[7].click();
  47. cells = getCells(picker);
  48. expect(dp.dates, 'to equal', [dateValue(2021, 7, 1)]);
  49. expect(input.value, 'to be', '08/01/2021');
  50. expect(viewSwitch.textContent, 'to be', '2021');
  51. expect(filterCells(cells, '.selected'), 'to equal', [cells[7]]);
  52. expect(filterCells(cells, '.focused'), 'to equal', [cells[7]]);
  53. // keyboard operation
  54. simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
  55. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  56. simulant.fire(input, 'keydown', {key: 'Enter'});
  57. cells = getCells(picker);
  58. expect(dp.dates, 'to equal', [dateValue(2020, 6, 1)]);
  59. expect(input.value, 'to be', '07/01/2020');
  60. expect(viewSwitch.textContent, 'to be', '2020');
  61. expect(filterCells(cells, '.selected'), 'to equal', [cells[6]]);
  62. dp.enterEditMode();
  63. input.value = '4/20/2021';
  64. simulant.fire(input, 'keydown', {key: 'Enter'});
  65. cells = getCells(picker);
  66. expect(dp.dates, 'to equal', [dateValue(2021, 3, 1)]);
  67. expect(input.value, 'to be', '04/01/2021');
  68. expect(viewSwitch.textContent, 'to be', '2021');
  69. expect(filterCells(cells, '.selected'), 'to equal', [cells[3]]);
  70. // api call
  71. viewSwitch.click();
  72. dp.setDate('2/14/2022');
  73. cells = getCells(picker);
  74. expect(dp.dates, 'to equal', [dateValue(2022, 1, 1)]);
  75. expect(input.value, 'to be', '02/01/2022');
  76. expect(viewSwitch.textContent, 'to be', '2022');
  77. expect(filterCells(cells, '.selected'), 'to equal', [cells[1]]);
  78. viewSwitch.click();
  79. dp.hide();
  80. input.value = '3/14/2020';
  81. dp.update();
  82. dp.show();
  83. cells = getCells(picker);
  84. expect(dp.dates, 'to equal', [dateValue(2020, 2, 1)]);
  85. expect(input.value, 'to be', '03/01/2020');
  86. expect(viewSwitch.textContent, 'to be', '2020');
  87. expect(filterCells(cells, '.selected'), 'to equal', [cells[2]]);
  88. dp.destroy();
  89. });
  90. it('changes the selection level to year when 2', function () {
  91. input.value = '2/14/2020';
  92. const {dp, picker} = createDP(input, {pickLevel: 2});
  93. const [viewSwitch, nextBtn] = getParts(picker, ['.view-switch', '.next-btn']);
  94. let cells = getCells(picker);
  95. dp.show();
  96. expect(dp.dates, 'to equal', [dateValue(2020, 0, 1)]);
  97. expect(input.value, 'to be', '01/01/2020');
  98. expect(viewSwitch.textContent, 'to be', '2020-2029');
  99. expect(filterCells(cells, '.selected'), 'to equal', [cells[1]]);
  100. expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
  101. // mouse operation
  102. cells[2].click();
  103. cells = getCells(picker);
  104. expect(dp.dates, 'to equal', [dateValue(2021, 0, 1)]);
  105. expect(input.value, 'to be', '01/01/2021');
  106. expect(viewSwitch.textContent, 'to be', '2020-2029');
  107. expect(filterCells(cells, '.selected'), 'to equal', [cells[2]]);
  108. expect(filterCells(cells, '.focused'), 'to equal', [cells[2]]);
  109. nextBtn.click();
  110. getCells(picker)[7].click();
  111. cells = getCells(picker);
  112. expect(dp.dates, 'to equal', [dateValue(2036, 0, 1)]);
  113. expect(input.value, 'to be', '01/01/2036');
  114. expect(viewSwitch.textContent, 'to be', '2030-2039');
  115. expect(filterCells(cells, '.selected'), 'to equal', [cells[7]]);
  116. expect(filterCells(cells, '.focused'), 'to equal', [cells[7]]);
  117. // keyboard operation
  118. simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
  119. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  120. simulant.fire(input, 'keydown', {key: 'Enter'});
  121. cells = getCells(picker);
  122. expect(dp.dates, 'to equal', [dateValue(2025, 0, 1)]);
  123. expect(input.value, 'to be', '01/01/2025');
  124. expect(viewSwitch.textContent, 'to be', '2020-2029');
  125. expect(filterCells(cells, '.selected'), 'to equal', [cells[6]]);
  126. dp.enterEditMode();
  127. input.value = '4/20/2021';
  128. simulant.fire(input, 'keydown', {key: 'Enter'});
  129. cells = getCells(picker);
  130. expect(dp.dates, 'to equal', [dateValue(2021, 0, 1)]);
  131. expect(input.value, 'to be', '01/01/2021');
  132. expect(viewSwitch.textContent, 'to be', '2020-2029');
  133. expect(filterCells(cells, '.selected'), 'to equal', [cells[2]]);
  134. // api call
  135. viewSwitch.click();
  136. dp.setDate('2/14/2032');
  137. cells = getCells(picker);
  138. expect(dp.dates, 'to equal', [dateValue(2032, 0, 1)]);
  139. expect(input.value, 'to be', '01/01/2032');
  140. expect(viewSwitch.textContent, 'to be', '2030-2039');
  141. expect(filterCells(cells, '.selected'), 'to equal', [cells[3]]);
  142. viewSwitch.click();
  143. dp.hide();
  144. input.value = '3/14/2020';
  145. dp.update();
  146. dp.show();
  147. cells = getCells(picker);
  148. expect(dp.dates, 'to equal', [dateValue(2020, 0, 1)]);
  149. expect(input.value, 'to be', '01/01/2020');
  150. expect(viewSwitch.textContent, 'to be', '2020-2029');
  151. expect(filterCells(cells, '.selected'), 'to equal', [cells[1]]);
  152. dp.destroy();
  153. });
  154. it('can be updated with setOptions()', function () {
  155. const {dp, picker} = createDP(input);
  156. const viewSwitch = getViewSwitch(picker);
  157. dp.setOptions({pickLevel: 1});
  158. dp.show();
  159. let cells = getCells(picker);
  160. expect(viewSwitch.textContent, 'to be', '2020');
  161. expect(cells[1].textContent, 'to be', 'Feb');
  162. cells[1].click();
  163. cells = getCells(picker);
  164. expect(dp.dates, 'to equal', [dateValue(2020, 1, 1)]);
  165. expect(input.value, 'to be', '02/01/2020');
  166. expect(viewSwitch.textContent, 'to be', '2020');
  167. expect(filterCells(cells, '.selected'), 'to equal', [cells[0]]);
  168. dp.setOptions({pickLevel: 0});
  169. cells = getCells(picker);
  170. expect(viewSwitch.textContent, 'to be', 'February 2020');
  171. expect(filterCells(cells, '.selected'), 'to equal', [cells[6]]);
  172. expect(filterCells(cells, '.focused'), 'to equal', [cells[6]]);
  173. viewSwitch.click();
  174. getCells(picker)[3].click();
  175. cells = getCells(picker);
  176. expect(viewSwitch.textContent, 'to be', 'April 2020');
  177. expect(filterCells(cells, '.selected'), 'to equal', []);
  178. expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
  179. dp.destroy();
  180. });
  181. });
  182. describe('maxView', function () {
  183. it('limits the maximum of available views', function () {
  184. const {dp, picker} = createDP(input, {maxView: 1});
  185. const viewSwitch = getViewSwitch(picker);
  186. dp.show();
  187. viewSwitch.click();
  188. viewSwitch.click();
  189. expect(viewSwitch.textContent, 'to be', '2020');
  190. expect(getCells(picker)[0].textContent, 'to be', 'Jan');
  191. dp.destroy();
  192. });
  193. it('cannot be smaller than pickLevel', function () {
  194. const {dp, picker} = createDP(input, {maxView: 1, pickLevel: 2});
  195. const viewSwitch = getViewSwitch(picker);
  196. dp.show();
  197. expect(viewSwitch.textContent, 'to be', '2020-2029');
  198. expect(getCells(picker)[1].textContent, 'to be', '2020');
  199. viewSwitch.click();
  200. expect(viewSwitch.textContent, 'to be', '2020-2029');
  201. expect(getCells(picker)[1].textContent, 'to be', '2020');
  202. dp.destroy();
  203. });
  204. it('can be updated with setOptions()', function () {
  205. const {dp, picker} = createDP(input);
  206. const viewSwitch = getViewSwitch(picker);
  207. dp.setOptions({maxView: 2});
  208. dp.show();
  209. viewSwitch.click();
  210. viewSwitch.click();
  211. viewSwitch.click();
  212. expect(viewSwitch.textContent, 'to be', '2020-2029');
  213. expect(getCells(picker)[1].textContent, 'to be', '2020');
  214. dp.setOptions({maxView: 0});
  215. expect(viewSwitch.textContent, 'to be', 'February 2020');
  216. viewSwitch.click();
  217. expect(viewSwitch.textContent, 'to be', 'February 2020');
  218. dp.destroy();
  219. });
  220. });
  221. describe('startView', function () {
  222. it('specifies the view desplayed on open', function () {
  223. const {dp, picker} = createDP(input, {startView: 3});
  224. const viewSwitch = getViewSwitch(picker);
  225. dp.show();
  226. expect(viewSwitch.textContent, 'to be', '2000-2090');
  227. let cells = getCells(picker);
  228. expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
  229. expect(cells[3].textContent, 'to be', '2020');
  230. dp.destroy();
  231. });
  232. it('cannot be smaller than pickLevel', function () {
  233. const {dp, picker} = createDP(input, {startView: 1, pickLevel: 2});
  234. const viewSwitch = getViewSwitch(picker);
  235. dp.show();
  236. expect(viewSwitch.textContent, 'to be', '2020-2029');
  237. let cells = getCells(picker);
  238. expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
  239. expect(cells[1].textContent, 'to be', '2020');
  240. dp.destroy();
  241. });
  242. it('cannot be larger than maxView', function () {
  243. const {dp, picker} = createDP(input, {startView: 3, maxView: 2});
  244. const viewSwitch = getViewSwitch(picker);
  245. dp.show();
  246. expect(viewSwitch.textContent, 'to be', '2020-2029');
  247. let cells = getCells(picker);
  248. expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
  249. expect(cells[1].textContent, 'to be', '2020');
  250. dp.destroy();
  251. });
  252. it('can be updated with setOptions()', function () {
  253. const {dp, picker} = createDP(input);
  254. const viewSwitch = getViewSwitch(picker);
  255. dp.setOptions({startView: 2});
  256. dp.show();
  257. expect(viewSwitch.textContent, 'to be', '2020-2029');
  258. let cells = getCells(picker);
  259. expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
  260. expect(cells[1].textContent, 'to be', '2020');
  261. dp.hide();
  262. dp.setOptions({startView: 0});
  263. dp.show();
  264. expect(viewSwitch.textContent, 'to be', 'February 2020');
  265. cells = getCells(picker);
  266. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  267. expect(cells[19].textContent, 'to be', '14');
  268. dp.destroy();
  269. });
  270. });
  271. });