date-selection.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. describe('DateRangePicker - date selection', function () {
  2. let clock;
  3. let elem;
  4. let input0;
  5. let input1;
  6. //
  7. let drp;
  8. let picker0;
  9. let picker1;
  10. let viewSwitch0;
  11. let viewSwitch1;
  12. let nextBtn0;
  13. let nextBtn1;
  14. let cells0;
  15. let cells1;
  16. before(function () {
  17. clock = sinon.useFakeTimers({now: new Date(2020, 1, 14)});
  18. });
  19. after(function () {
  20. clock.restore();
  21. });
  22. beforeEach(function () {
  23. elem = domUtils.parseHTML('<div><input><input></div>').firstChild;
  24. [input0, input1] = elem.children;
  25. testContainer.appendChild(elem);
  26. });
  27. afterEach(function () {
  28. testContainer.removeChild(elem);
  29. });
  30. it('same date is set to both sides if a date is selected on one side when selections are none', function () {
  31. let selectDate = dateValue(2020, 1, 11);
  32. ({drp, picker0, picker1} = createDRP(elem));
  33. cells0 = getCells(picker0);
  34. cells1 = getCells(picker1);
  35. drp.datepickers[0].show();
  36. cells0[16].click();
  37. expect(drp.dates, 'to equal', [selectDate, selectDate]);
  38. expect(input0.value, 'to be', '02/11/2020');
  39. expect(filterCells(cells0, '.selected'), 'to equal', [cells0[16]]);
  40. expect(filterCells(cells0, '.range-start'), 'to equal', [cells0[16]]);
  41. expect(filterCells(cells0, '.range-end'), 'to equal', [cells0[16]]);
  42. expect(filterCells(cells0, '.range'), 'to equal', []);
  43. expect(filterCells(cells0, '.focused'), 'to equal', [cells0[16]]);
  44. expect(input1.value, 'to be', '02/11/2020');
  45. expect(filterCells(cells1, '.selected'), 'to equal', [cells1[16]]);
  46. expect(filterCells(cells1, '.range-start'), 'to equal', [cells1[16]]);
  47. expect(filterCells(cells1, '.range-end'), 'to equal', [cells1[16]]);
  48. expect(filterCells(cells1, '.range'), 'to equal', []);
  49. expect(filterCells(cells1, '.focused'), 'to equal', [cells1[16]]);
  50. drp.destroy();
  51. input0.value = '';
  52. input1.value = '';
  53. ({drp, picker0, picker1} = createDRP(elem));
  54. cells0 = getCells(picker0);
  55. cells1 = getCells(picker1);
  56. drp.datepickers[1].show();
  57. cells1[16].click();
  58. expect(drp.dates, 'to equal', [selectDate, selectDate]);
  59. expect(input0.value, 'to be', '02/11/2020');
  60. expect(filterCells(cells0, '.selected'), 'to equal', [cells0[16]]);
  61. expect(filterCells(cells0, '.range-start'), 'to equal', [cells0[16]]);
  62. expect(filterCells(cells0, '.range-end'), 'to equal', [cells0[16]]);
  63. expect(filterCells(cells0, '.range'), 'to equal', []);
  64. expect(filterCells(cells0, '.focused'), 'to equal', [cells0[16]]);
  65. expect(input1.value, 'to be', '02/11/2020');
  66. expect(filterCells(cells1, '.selected'), 'to equal', [cells1[16]]);
  67. expect(filterCells(cells1, '.range-start'), 'to equal', [cells1[16]]);
  68. expect(filterCells(cells1, '.range-end'), 'to equal', [cells1[16]]);
  69. expect(filterCells(cells1, '.range'), 'to equal', []);
  70. expect(filterCells(cells1, '.focused'), 'to equal', [cells1[16]]);
  71. drp.destroy();
  72. input0.value = '';
  73. input1.value = '';
  74. // other month than default view date's
  75. // (issue #17, #19)
  76. let partsClasses = ['.view-switch', '.next-btn'];
  77. selectDate = dateValue(2020, 2, 10);
  78. ({drp, picker0, picker1} = createDRP(elem));
  79. [viewSwitch0, nextBtn0] = getParts(picker0, partsClasses);
  80. [viewSwitch1, nextBtn1] = getParts(picker1, partsClasses);
  81. drp.datepickers[0].show();
  82. nextBtn0.click();
  83. getCells(picker0)[9].click();
  84. cells0 = getCells(picker0);
  85. cells1 = getCells(picker1);
  86. expect(drp.dates, 'to equal', [selectDate, selectDate]);
  87. expect(input0.value, 'to be', '03/10/2020');
  88. expect(viewSwitch0.textContent, 'to equal', 'March 2020');
  89. expect(filterCells(cells0, '.selected'), 'to equal', [cells0[9]]);
  90. expect(filterCells(cells0, '.range-start'), 'to equal', [cells0[9]]);
  91. expect(filterCells(cells0, '.range-end'), 'to equal', [cells0[9]]);
  92. expect(filterCells(cells0, '.range'), 'to equal', []);
  93. expect(filterCells(cells0, '.focused'), 'to equal', [cells0[9]]);
  94. expect(input1.value, 'to be', '03/10/2020');
  95. expect(viewSwitch1.textContent, 'to equal', 'March 2020');
  96. expect(filterCells(cells1, '.selected'), 'to equal', [cells1[9]]);
  97. expect(filterCells(cells1, '.range-start'), 'to equal', [cells1[9]]);
  98. expect(filterCells(cells1, '.range-end'), 'to equal', [cells1[9]]);
  99. expect(filterCells(cells1, '.range'), 'to equal', []);
  100. expect(filterCells(cells1, '.focused'), 'to equal', [cells1[9]]);
  101. drp.datepickers[1].show();
  102. nextBtn1.click();
  103. expect(viewSwitch1.textContent, 'to equal', 'April 2020');
  104. drp.destroy();
  105. input0.value = '';
  106. input1.value = '';
  107. ({drp, picker0, picker1} = createDRP(elem));
  108. [viewSwitch0, nextBtn0] = getParts(picker0, partsClasses);
  109. [viewSwitch1, nextBtn1] = getParts(picker1, partsClasses);
  110. drp.datepickers[1].show();
  111. nextBtn1.click();
  112. getCells(picker1)[9].click();
  113. cells0 = getCells(picker0);
  114. cells1 = getCells(picker1);
  115. expect(drp.dates, 'to equal', [selectDate, selectDate]);
  116. expect(input0.value, 'to be', '03/10/2020');
  117. expect(viewSwitch0.textContent, 'to equal', 'March 2020');
  118. expect(filterCells(cells0, '.selected'), 'to equal', [cells0[9]]);
  119. expect(filterCells(cells0, '.range-start'), 'to equal', [cells0[9]]);
  120. expect(filterCells(cells0, '.range-end'), 'to equal', [cells0[9]]);
  121. expect(filterCells(cells0, '.range'), 'to equal', []);
  122. expect(filterCells(cells0, '.focused'), 'to equal', [cells0[9]]);
  123. expect(input1.value, 'to be', '03/10/2020');
  124. expect(viewSwitch1.textContent, 'to equal', 'March 2020');
  125. expect(filterCells(cells1, '.selected'), 'to equal', [cells1[9]]);
  126. expect(filterCells(cells1, '.range-start'), 'to equal', [cells1[9]]);
  127. expect(filterCells(cells1, '.range-end'), 'to equal', [cells1[9]]);
  128. expect(filterCells(cells1, '.range'), 'to equal', []);
  129. expect(filterCells(cells1, '.focused'), 'to equal', [cells1[9]]);
  130. drp.datepickers[0].show();
  131. nextBtn0.click();
  132. expect(viewSwitch0.textContent, 'to equal', 'April 2020');
  133. drp.destroy();
  134. input0.value = '';
  135. input1.value = '';
  136. });
  137. it('selections are cleared from both sides if selected date on one side is cleared', function () {
  138. input0.value = '02/11/2020';
  139. input1.value = '02/11/2020';
  140. ({drp, picker0, picker1} = createDRP(elem));
  141. cells0 = getCells(picker0);
  142. cells1 = getCells(picker1);
  143. input0.value = '';
  144. simulant.fire(input0, 'keydown', {key: 'Enter'});
  145. expect(drp.dates, 'to equal', [undefined, undefined]);
  146. expect(input0.value, 'to be', '');
  147. expect(filterCells(cells0, '.selected'), 'to equal', []);
  148. expect(filterCells(cells0, '.range-start'), 'to equal', []);
  149. expect(filterCells(cells0, '.range-end'), 'to equal', []);
  150. expect(filterCells(cells0, '.range'), 'to equal', []);
  151. expect(filterCells(cells0, '.focused'), 'to equal', [cells0[19]]);
  152. expect(input1.value, 'to be', '');
  153. expect(filterCells(cells1, '.selected'), 'to equal', []);
  154. expect(filterCells(cells1, '.range-start'), 'to equal', []);
  155. expect(filterCells(cells1, '.range-end'), 'to equal', []);
  156. expect(filterCells(cells1, '.range'), 'to equal', []);
  157. expect(filterCells(cells1, '.focused'), 'to equal', [cells1[19]]);
  158. drp.destroy();
  159. input0.value = '02/11/2020';
  160. input1.value = '02/11/2020';
  161. ({drp, picker0, picker1} = createDRP(elem));
  162. cells0 = getCells(picker0);
  163. cells1 = getCells(picker1);
  164. input1.value = '';
  165. simulant.fire(input1, 'keydown', {key: 'Enter'});
  166. expect(drp.dates, 'to equal', [undefined, undefined]);
  167. expect(input0.value, 'to be', '');
  168. expect(filterCells(cells0, '.selected'), 'to equal', []);
  169. expect(filterCells(cells0, '.range-start'), 'to equal', []);
  170. expect(filterCells(cells0, '.range-end'), 'to equal', []);
  171. expect(filterCells(cells0, '.range'), 'to equal', []);
  172. expect(filterCells(cells0, '.focused'), 'to equal', [cells0[19]]);
  173. expect(input1.value, 'to be', '');
  174. expect(filterCells(cells1, '.selected'), 'to equal', []);
  175. expect(filterCells(cells1, '.range-start'), 'to equal', []);
  176. expect(filterCells(cells1, '.range-end'), 'to equal', []);
  177. expect(filterCells(cells1, '.range'), 'to equal', []);
  178. expect(filterCells(cells1, '.focused'), 'to equal', [cells1[19]]);
  179. drp.destroy();
  180. });
  181. it('dates are swapped if a date later than the 2nd picker\'s selection is seleted on the 1st picker', function () {
  182. input0.value = '02/11/2020';
  183. input1.value = '02/11/2020';
  184. ({drp, picker0, picker1} = createDRP(elem));
  185. cells0 = getCells(picker0);
  186. cells1 = getCells(picker1);
  187. drp.datepickers[0].show();
  188. cells0[20].click();
  189. expect(drp.dates, 'to equal', [dateValue(2020, 1, 11), dateValue(2020, 1, 15)]);
  190. expect(input0.value, 'to be', '02/11/2020');
  191. expect(filterCells(cells0, '.selected'), 'to equal', [cells0[16]]);
  192. expect(filterCells(cells0, '.range-start'), 'to equal', [cells0[16]]);
  193. expect(filterCells(cells0, '.range-end'), 'to equal', [cells0[20]]);
  194. expect(filterCells(cells0, '.range'), 'to equal', [cells0[17], cells0[18], cells0[19]]);
  195. expect(filterCells(cells0, '.focused'), 'to equal', [cells0[16]]);
  196. expect(input1.value, 'to be', '02/15/2020');
  197. expect(filterCells(cells1, '.selected'), 'to equal', [cells1[20]]);
  198. expect(filterCells(cells1, '.range-start'), 'to equal', [cells1[16]]);
  199. expect(filterCells(cells1, '.range-end'), 'to equal', [cells1[20]]);
  200. expect(filterCells(cells1, '.range'), 'to equal', [cells1[17], cells1[18], cells1[19]]);
  201. expect(filterCells(cells1, '.focused'), 'to equal', [cells1[20]]);
  202. drp.destroy();
  203. input0.value = '';
  204. input1.value = '';
  205. });
  206. it('dates are swapped if a date earlier than the 1st picker\'s selection is seleted on the 2nd picker', function () {
  207. input0.value = '02/11/2020';
  208. input1.value = '02/11/2020';
  209. ({drp, picker0, picker1} = createDRP(elem));
  210. cells0 = getCells(picker0);
  211. cells1 = getCells(picker1);
  212. drp.datepickers[1].show();
  213. cells1[12].click();
  214. expect(drp.dates, 'to equal', [dateValue(2020, 1, 7), dateValue(2020, 1, 11)]);
  215. expect(input0.value, 'to be', '02/07/2020');
  216. expect(filterCells(cells0, '.selected'), 'to equal', [cells0[12]]);
  217. expect(filterCells(cells0, '.range-start'), 'to equal', [cells0[12]]);
  218. expect(filterCells(cells0, '.range-end'), 'to equal', [cells0[16]]);
  219. expect(filterCells(cells0, '.range'), 'to equal', [cells0[13], cells0[14], cells0[15]]);
  220. expect(filterCells(cells0, '.focused'), 'to equal', [cells0[12]]);
  221. expect(input1.value, 'to be', '02/11/2020');
  222. expect(filterCells(cells1, '.selected'), 'to equal', [cells1[16]]);
  223. expect(filterCells(cells1, '.range-start'), 'to equal', [cells1[12]]);
  224. expect(filterCells(cells1, '.range-end'), 'to equal', [cells1[16]]);
  225. expect(filterCells(cells1, '.range'), 'to equal', [cells1[13], cells1[14], cells1[15]]);
  226. expect(filterCells(cells1, '.focused'), 'to equal', [cells1[16]]);
  227. drp.destroy();
  228. input0.value = '';
  229. input1.value = '';
  230. });
  231. describe('range between different months', function () {
  232. it('each picker displays the month of corresponding end of the range', function () {
  233. input0.value = '02/25/2020';
  234. input1.value = '03/05/2020';
  235. ({drp, picker0, picker1} = createDRP(elem));
  236. viewSwitch0 = picker0.querySelector('.view-switch');
  237. viewSwitch1 = picker1.querySelector('.view-switch');
  238. cells0 = getCells(picker0);
  239. cells1 = getCells(picker1);
  240. expect(viewSwitch0.textContent, 'to be', 'February 2020');
  241. expect(filterCells(cells0, '.selected'), 'to equal', [cells0[30]]);
  242. expect(filterCells(cells0, '.range-start'), 'to equal', [cells0[30]]);
  243. expect(filterCells(cells0, '.range-end'), 'to equal', [cells0[39]]);
  244. expect(filterCells(cells0, '.range'), 'to equal', cells0.slice(31, 39));
  245. expect(viewSwitch1.textContent, 'to be', 'March 2020');
  246. expect(filterCells(cells1, '.selected'), 'to equal', [cells1[4]]);
  247. expect(filterCells(cells1, '.range-start'), 'to equal', []);
  248. expect(filterCells(cells1, '.range-end'), 'to equal', [cells1[4]]);
  249. expect(filterCells(cells1, '.range'), 'to equal', cells1.slice(0, 4));
  250. drp.datepickers[1].show();
  251. picker1.querySelector('.next-btn').click();
  252. cells1 = getCells(picker1);
  253. cells1[24].click();
  254. expect(drp.dates, 'to equal', [dateValue(2020, 1, 25), dateValue(2020, 3, 22)]);
  255. expect(input0.value, 'to be', '02/25/2020');
  256. expect(input1.value, 'to be', '04/22/2020');
  257. expect(viewSwitch0.textContent, 'to be', 'February 2020');
  258. expect(filterCells(cells0, '.selected'), 'to equal', [cells0[30]]);
  259. expect(filterCells(cells0, '.range-start'), 'to equal', [cells0[30]]);
  260. expect(filterCells(cells0, '.range-end'), 'to equal', []);
  261. expect(filterCells(cells0, '.range'), 'to equal', cells0.slice(31));
  262. expect(viewSwitch1.textContent, 'to be', 'April 2020');
  263. expect(filterCells(cells1, '.selected'), 'to equal', [cells1[24]]);
  264. expect(filterCells(cells1, '.range-start'), 'to equal', []);
  265. expect(filterCells(cells1, '.range-end'), 'to equal', [cells1[24]]);
  266. expect(filterCells(cells1, '.range'), 'to equal', cells1.slice(0, 24));
  267. input0.value = '02/14/1998';
  268. simulant.fire(input0, 'keydown', {key: 'Enter'});
  269. cells0 = getCells(picker0);
  270. expect(drp.dates, 'to equal', [dateValue(1998, 1, 14), dateValue(2020, 3, 22)]);
  271. expect(input0.value, 'to be', '02/14/1998');
  272. expect(input1.value, 'to be', '04/22/2020');
  273. expect(viewSwitch0.textContent, 'to be', 'February 1998');
  274. expect(filterCells(cells0, '.selected'), 'to equal', [cells0[13]]);
  275. expect(filterCells(cells0, '.range-start'), 'to equal', [cells0[13]]);
  276. expect(filterCells(cells0, '.range-end'), 'to equal', []);
  277. expect(filterCells(cells0, '.range'), 'to equal', cells0.slice(14));
  278. expect(viewSwitch1.textContent, 'to be', 'April 2020');
  279. expect(filterCells(cells1, '.selected'), 'to equal', [cells1[24]]);
  280. expect(filterCells(cells1, '.range-start'), 'to equal', []);
  281. expect(filterCells(cells1, '.range-end'), 'to equal', [cells1[24]]);
  282. expect(filterCells(cells1, '.range'), 'to equal', cells1.slice(0, 24));
  283. drp.datepickers[0].show();
  284. // months view
  285. viewSwitch0.click();
  286. cells0 = getCells(picker0);
  287. expect(viewSwitch0.textContent, 'to be', '1998');
  288. expect(filterCells(cells0, '.selected'), 'to equal', [cells0[1]]);
  289. viewSwitch1.click();
  290. cells1 = getCells(picker1);
  291. expect(viewSwitch1.textContent, 'to be', '2020');
  292. expect(filterCells(cells1, '.selected'), 'to equal', [cells1[3]]);
  293. // years view
  294. viewSwitch0.click();
  295. cells0 = getCells(picker0);
  296. expect(viewSwitch0.textContent, 'to be', '1990-1999');
  297. expect(filterCells(cells0, '.selected'), 'to equal', [cells0[9]]);
  298. viewSwitch1.click();
  299. cells1 = getCells(picker1);
  300. expect(viewSwitch1.textContent, 'to be', '2020-2029');
  301. expect(filterCells(cells1, '.selected'), 'to equal', [cells1[1]]);
  302. // decades view
  303. viewSwitch0.click();
  304. cells0 = getCells(picker0);
  305. expect(viewSwitch0.textContent, 'to be', '1900-1990');
  306. expect(filterCells(cells0, '.selected'), 'to equal', [cells0[10]]);
  307. viewSwitch1.click();
  308. cells1 = getCells(picker1);
  309. expect(viewSwitch1.textContent, 'to be', '2000-2090');
  310. expect(filterCells(cells1, '.selected'), 'to equal', [cells1[3]]);
  311. drp.destroy();
  312. input0.value = '';
  313. input1.value = '';
  314. });
  315. it('dates are swapped if a date later than the 2nd picker\'s selection is seleted on the 1st picker', function () {
  316. ({drp, picker0, picker1} = createDRP(elem));
  317. [viewSwitch0, nextBtn0] = getParts(picker0, ['.view-switch', '.next-btn']);
  318. viewSwitch1 = picker1.querySelector('.view-switch');
  319. drp.datepickers[1].show();
  320. getCells(picker1)[16].click();
  321. drp.datepickers[1].hide();
  322. drp.datepickers[0].show();
  323. expect(viewSwitch0.textContent, 'to equal', 'February 2020');
  324. nextBtn0.click();
  325. expect(viewSwitch0.textContent, 'to equal', 'March 2020');
  326. getCells(picker0)[9].click();
  327. cells0 = getCells(picker0);
  328. cells1 = getCells(picker1);
  329. expect(drp.dates, 'to equal', [dateValue(2020, 1, 11), dateValue(2020, 2, 10)]);
  330. expect(input0.value, 'to be', '02/11/2020');
  331. expect(viewSwitch0.textContent, 'to equal', 'February 2020');
  332. expect(filterCells(cells0, '.selected'), 'to equal', [cells0[16]]);
  333. expect(filterCells(cells0, '.range-start'), 'to equal', [cells0[16]]);
  334. expect(filterCells(cells0, '.range-end'), 'to equal', []);
  335. expect(filterCells(cells0, '.range'), 'to equal', cells0.slice(17));
  336. expect(filterCells(cells0, '.focused'), 'to equal', [cells0[16]]);
  337. expect(input1.value, 'to be', '03/10/2020');
  338. expect(viewSwitch1.textContent, 'to equal', 'March 2020');
  339. expect(filterCells(cells1, '.selected'), 'to equal', [cells1[9]]);
  340. expect(filterCells(cells1, '.range-start'), 'to equal', []);
  341. expect(filterCells(cells1, '.range-end'), 'to equal', [cells1[9]]);
  342. expect(filterCells(cells1, '.range'), 'to equal', cells1.slice(0, 9));
  343. expect(filterCells(cells1, '.focused'), 'to equal', [cells1[9]]);
  344. drp.destroy();
  345. input0.value = '';
  346. input1.value = '';
  347. });
  348. it('dates are swapped if a date earlier than the 1st picker\'s selection is seleted on the 2nd picker', function () {
  349. let prevBtn1;
  350. ({drp, picker0, picker1} = createDRP(elem));
  351. [viewSwitch0, nextBtn0] = getParts(picker0, ['.view-switch', '.next-btn']);
  352. [viewSwitch1, prevBtn1] = getParts(picker1, ['.view-switch', '.prev-btn']);
  353. drp.datepickers[0].show();
  354. nextBtn0.click();
  355. getCells(picker0)[9].click();
  356. drp.datepickers[0].hide();
  357. drp.datepickers[1].show();
  358. expect(viewSwitch1.textContent, 'to equal', 'March 2020');
  359. prevBtn1.click();
  360. expect(viewSwitch1.textContent, 'to equal', 'February 2020');
  361. getCells(picker1)[16].click();
  362. cells0 = getCells(picker0);
  363. cells1 = getCells(picker1);
  364. expect(drp.dates, 'to equal', [dateValue(2020, 1, 11), dateValue(2020, 2, 10)]);
  365. expect(input0.value, 'to be', '02/11/2020');
  366. expect(viewSwitch0.textContent, 'to equal', 'February 2020');
  367. expect(filterCells(cells0, '.selected'), 'to equal', [cells0[16]]);
  368. expect(filterCells(cells0, '.range-start'), 'to equal', [cells0[16]]);
  369. expect(filterCells(cells0, '.range-end'), 'to equal', []);
  370. expect(filterCells(cells0, '.range'), 'to equal', cells0.slice(17));
  371. expect(filterCells(cells0, '.focused'), 'to equal', [cells0[16]]);
  372. expect(input1.value, 'to be', '03/10/2020');
  373. expect(viewSwitch1.textContent, 'to equal', 'March 2020');
  374. expect(filterCells(cells1, '.selected'), 'to equal', [cells1[9]]);
  375. expect(filterCells(cells1, '.range-start'), 'to equal', []);
  376. expect(filterCells(cells1, '.range-end'), 'to equal', [cells1[9]]);
  377. expect(filterCells(cells1, '.range'), 'to equal', cells1.slice(0, 9));
  378. expect(filterCells(cells1, '.focused'), 'to equal', [cells1[9]]);
  379. drp.destroy();
  380. input0.value = '';
  381. input1.value = '';
  382. });
  383. });
  384. });