options.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. describe('DateRangePicker - options', function () {
  2. let clock;
  3. let elem;
  4. let input0;
  5. let input1;
  6. before(function () {
  7. clock = sinon.useFakeTimers({now: new Date(2020, 1, 14)});
  8. });
  9. after(function () {
  10. clock.restore();
  11. });
  12. beforeEach(function () {
  13. elem = domUtils.parseHTML('<div><input><input></div>').firstChild;
  14. [input0, input1] = elem.children;
  15. testContainer.appendChild(elem);
  16. });
  17. afterEach(function () {
  18. testContainer.removeChild(elem);
  19. });
  20. describe('allowOneSidedRange', function () {
  21. it('disables the requirement for both sides of range to be set/unset', function () {
  22. let {drp, picker0, picker1} = createDRP(elem, {allowOneSidedRange: true});
  23. let cells0 = getCells(picker0);
  24. let cells1 = getCells(picker1);
  25. drp.datepickers[0].show();
  26. cells0[16].click();
  27. expect(drp.dates, 'to equal', [dateValue(2020, 1, 11), undefined]);
  28. expect(drp.getDates(), 'to equal', [new Date(drp.dates[0]), undefined]);
  29. expect(input0.value, 'to be', '02/11/2020');
  30. expect(filterCells(cells0, '.selected'), 'to equal', [cells0[16]]);
  31. expect(filterCells(cells0, '.range-start'), 'to equal', [cells0[16]]);
  32. expect(filterCells(cells0, '.range-end'), 'to equal', []);
  33. expect(filterCells(cells0, '.range'), 'to equal', []);
  34. expect(filterCells(cells0, '.focused'), 'to equal', [cells0[16]]);
  35. expect(input1.value, 'to be', '');
  36. expect(filterCells(cells1, '.selected'), 'to equal', []);
  37. expect(filterCells(cells1, '.range-start'), 'to equal', [cells1[16]]);
  38. expect(filterCells(cells1, '.range-end'), 'to equal', []);
  39. expect(filterCells(cells1, '.range'), 'to equal', []);
  40. expect(filterCells(cells1, '.focused'), 'to equal', [cells1[19]]);
  41. drp.datepickers[1].show();
  42. cells1[25].click();
  43. expect(drp.dates, 'to equal', [dateValue(2020, 1, 11), dateValue(2020, 1, 20)]);
  44. expect(drp.getDates(), 'to equal', drp.dates.map(date => new Date(date)));
  45. expect(input0.value, 'to be', '02/11/2020');
  46. expect(filterCells(cells0, '.selected'), 'to equal', [cells0[16]]);
  47. expect(filterCells(cells0, '.range-start'), 'to equal', [cells0[16]]);
  48. expect(filterCells(cells0, '.range-end'), 'to equal', [cells0[25]]);
  49. expect(filterCells(cells0, '.range'), 'to equal', cells0.slice(17, 25));
  50. expect(filterCells(cells0, '.focused'), 'to equal', [cells0[16]]);
  51. expect(input1.value, 'to be', '02/20/2020');
  52. expect(filterCells(cells1, '.selected'), 'to equal', [cells1[25]]);
  53. expect(filterCells(cells1, '.range-start'), 'to equal', [cells1[16]]);
  54. expect(filterCells(cells1, '.range-end'), 'to equal', [cells1[25]]);
  55. expect(filterCells(cells1, '.range'), 'to equal', cells1.slice(17, 25));
  56. expect(filterCells(cells1, '.focused'), 'to equal', [cells1[25]]);
  57. simulant.fire(input0, 'keydown', {key: 'Escape'});
  58. input0.value = '';
  59. simulant.fire(input0, 'keydown', {key: 'Enter'});
  60. expect(drp.dates, 'to equal', [undefined, dateValue(2020, 1, 20)]);
  61. expect(drp.getDates(), 'to equal', [undefined, new Date(drp.dates[1])]);
  62. expect(input0.value, 'to be', '');
  63. expect(filterCells(cells0, '.selected'), 'to equal', []);
  64. expect(filterCells(cells0, '.range-start'), 'to equal', []);
  65. expect(filterCells(cells0, '.range-end'), 'to equal', [cells0[25]]);
  66. expect(filterCells(cells0, '.range'), 'to equal', []);
  67. expect(filterCells(cells0, '.focused'), 'to equal', [cells0[19]]);
  68. expect(input1.value, 'to be', '02/20/2020');
  69. expect(filterCells(cells1, '.selected'), 'to equal', [cells1[25]]);
  70. expect(filterCells(cells1, '.range-start'), 'to equal', []);
  71. expect(filterCells(cells1, '.range-end'), 'to equal', [cells1[25]]);
  72. expect(filterCells(cells1, '.range'), 'to equal', []);
  73. expect(filterCells(cells1, '.focused'), 'to equal', [cells1[25]]);
  74. drp.destroy();
  75. input0.value = '';
  76. input1.value = '';
  77. // by setDates()
  78. ({drp, picker0, picker1} = createDRP(elem, {allowOneSidedRange: true}));
  79. cells0 = getCells(picker0);
  80. cells1 = getCells(picker1);
  81. drp.setDates('02/11/2020');
  82. expect(drp.dates, 'to equal', [dateValue(2020, 1, 11), undefined]);
  83. expect(input0.value, 'to be', '02/11/2020');
  84. expect(filterCells(cells0, '.selected'), 'to equal', [cells0[16]]);
  85. expect(input1.value, 'to be', '');
  86. expect(filterCells(cells1, '.selected'), 'to equal', []);
  87. drp.setDates(undefined, '02/20/2020');
  88. expect(drp.dates, 'to equal', [dateValue(2020, 1, 11), dateValue(2020, 1, 20)]);
  89. expect(input0.value, 'to be', '02/11/2020');
  90. expect(filterCells(cells0, '.selected'), 'to equal', [cells0[16]]);
  91. expect(input1.value, 'to be', '02/20/2020');
  92. expect(filterCells(cells1, '.selected'), 'to equal', [cells1[25]]);
  93. drp.setDates({clear: true});
  94. expect(drp.dates, 'to equal', [undefined, dateValue(2020, 1, 20)]);
  95. expect(input0.value, 'to be', '');
  96. expect(filterCells(cells0, '.selected'), 'to equal', []);
  97. expect(input1.value, 'to be', '02/20/2020');
  98. expect(filterCells(cells1, '.selected'), 'to equal', [cells1[25]]);
  99. drp.setDates('02/11/2020', {clear: true});
  100. expect(drp.dates, 'to equal', [dateValue(2020, 1, 11), undefined]);
  101. expect(input0.value, 'to be', '02/11/2020');
  102. expect(filterCells(cells0, '.selected'), 'to equal', [cells0[16]]);
  103. expect(input1.value, 'to be', '');
  104. expect(filterCells(cells1, '.selected'), 'to equal', []);
  105. drp.setDates({clear: true}, '02/20/2020');
  106. expect(drp.dates, 'to equal', [undefined, dateValue(2020, 1, 20)]);
  107. expect(input0.value, 'to be', '');
  108. expect(filterCells(cells0, '.selected'), 'to equal', []);
  109. expect(input1.value, 'to be', '02/20/2020');
  110. expect(filterCells(cells1, '.selected'), 'to equal', [cells1[25]]);
  111. drp.setDates(undefined, {clear: true});
  112. expect(drp.dates, 'to equal', [undefined, undefined]);
  113. expect(input0.value, 'to be', '');
  114. expect(filterCells(cells0, '.selected'), 'to equal', []);
  115. expect(input1.value, 'to be', '');
  116. expect(filterCells(cells1, '.selected'), 'to equal', []);
  117. drp.destroy();
  118. });
  119. it('can be updated with setOptions()', function () {
  120. const drp = new DateRangePicker(elem);
  121. drp.setOptions({allowOneSidedRange: true});
  122. input0.value = '02/11/2020';
  123. simulant.fire(input0, 'keydown', {key: 'Enter'});
  124. expect(drp.dates, 'to equal', [dateValue(2020, 1, 11), undefined]);
  125. drp.setDates({clear: true}, '02/11/2020');
  126. expect(drp.dates, 'to equal', [undefined, dateValue(2020, 1, 11)]);
  127. drp.setOptions({allowOneSidedRange: false});
  128. input1.value = '02/20/2020';
  129. simulant.fire(input1, 'keydown', {key: 'Enter'});
  130. expect(drp.dates, 'to equal', [dateValue(2020, 1, 20), dateValue(2020, 1, 20)]);
  131. drp.setDates({clear: true});
  132. expect(drp.dates, 'to equal', [undefined, undefined]);
  133. drp.destroy();
  134. });
  135. });
  136. describe('pickLevel', function () {
  137. it('changes the span of range selection to 1st of a month → last day of a month when 1', function () {
  138. input0.value = '2/14/2020';
  139. input1.value = '2/14/2020';
  140. const {drp, picker0, picker1} = createDRP(elem, {pickLevel: 1});
  141. const viewSwitch0 = picker0.querySelector('.view-switch');
  142. const viewSwitch1 = picker1.querySelector('.view-switch');
  143. let cells0 = getCells(picker0);
  144. let cells1 = getCells(picker1);
  145. input0.focus();
  146. expect(drp.dates, 'to equal', [dateValue(2020, 1, 1), dateValue(2020, 1, 29)]);
  147. expect(input0.value, 'to be', '02/01/2020');
  148. expect(viewSwitch0.textContent, 'to be', '2020');
  149. expect(getCellIndices(cells0, '.selected'), 'to equal', [1]);
  150. expect(getCellIndices(cells0, '.range-start'), 'to equal', [1]);
  151. expect(getCellIndices(cells0, '.range-end'), 'to equal', [1]);
  152. expect(getCellIndices(cells0, '.range'), 'to equal', []);
  153. expect(getCellIndices(cells0, '.focused'), 'to equal', [1]);
  154. input1.focus();
  155. expect(input1.value, 'to be', '02/29/2020');
  156. expect(viewSwitch1.textContent, 'to be', '2020');
  157. expect(getCellIndices(cells1, '.selected'), 'to equal', [1]);
  158. expect(getCellIndices(cells1, '.range-start'), 'to equal', [1]);
  159. expect(getCellIndices(cells1, '.range-end'), 'to equal', [1]);
  160. expect(getCellIndices(cells1, '.range'), 'to equal', []);
  161. expect(getCellIndices(cells1, '.focused'), 'to equal', [1]);
  162. // mouse operation
  163. cells0[0].click();
  164. cells1[6].click();
  165. expect(drp.dates, 'to equal', [dateValue(2020, 0, 1), dateValue(2020, 6, 31)]);
  166. expect(input0.value, 'to be', '01/01/2020');
  167. expect(viewSwitch0.textContent, 'to be', '2020');
  168. expect(getCellIndices(cells0, '.selected'), 'to equal', [0]);
  169. expect(getCellIndices(cells0, '.range-start'), 'to equal', [0]);
  170. expect(getCellIndices(cells0, '.range-end'), 'to equal', [6]);
  171. input1.focus();
  172. expect(input1.value, 'to be', '07/31/2020');
  173. expect(viewSwitch1.textContent, 'to be', '2020');
  174. expect(getCellIndices(cells1, '.selected'), 'to equal', [6]);
  175. expect(getCellIndices(cells1, '.range-start'), 'to equal', [0]);
  176. expect(getCellIndices(cells1, '.range-end'), 'to equal', [6]);
  177. // api call
  178. drp.setDates('2/14/2021', '3/14/2020');
  179. expect(drp.dates, 'to equal', [dateValue(2020, 2, 1), dateValue(2021, 1, 28)]);
  180. expect(input0.value, 'to be', '03/01/2020');
  181. expect(viewSwitch0.textContent, 'to be', '2020');
  182. expect(getCellIndices(cells0, '.selected'), 'to equal', [2]);
  183. expect(getCellIndices(cells0, '.range-start'), 'to equal', [2]);
  184. expect(getCellIndices(cells0, '.range-end'), 'to equal', []);
  185. input1.focus();
  186. expect(input1.value, 'to be', '02/28/2021');
  187. expect(viewSwitch1.textContent, 'to be', '2021');
  188. expect(getCellIndices(cells1, '.selected'), 'to equal', [1]);
  189. expect(getCellIndices(cells1, '.range-start'), 'to equal', []);
  190. expect(getCellIndices(cells1, '.range-end'), 'to equal', [1]);
  191. drp.destroy();
  192. });
  193. it('changes the span of range selection to Jan 1st of a month → Dec 31st of a month when 2', function () {
  194. input0.value = '2/14/2020';
  195. input1.value = '2/14/2020';
  196. const {drp, picker0, picker1} = createDRP(elem, {pickLevel: 2});
  197. const viewSwitch0 = picker0.querySelector('.view-switch');
  198. const viewSwitch1 = picker1.querySelector('.view-switch');
  199. let cells0 = getCells(picker0);
  200. let cells1 = getCells(picker1);
  201. input0.focus();
  202. expect(drp.dates, 'to equal', [dateValue(2020, 0, 1), dateValue(2020, 11, 31)]);
  203. expect(input0.value, 'to be', '01/01/2020');
  204. expect(viewSwitch0.textContent, 'to be', '2020-2029');
  205. expect(getCellIndices(cells0, '.selected'), 'to equal', [1]);
  206. expect(getCellIndices(cells0, '.range-start'), 'to equal', [1]);
  207. expect(getCellIndices(cells0, '.range-end'), 'to equal', [1]);
  208. expect(getCellIndices(cells0, '.range'), 'to equal', []);
  209. expect(getCellIndices(cells0, '.focused'), 'to equal', [1]);
  210. input1.focus();
  211. expect(input1.value, 'to be', '12/31/2020');
  212. expect(viewSwitch1.textContent, 'to be', '2020-2029');
  213. expect(getCellIndices(cells1, '.selected'), 'to equal', [1]);
  214. expect(getCellIndices(cells1, '.range-start'), 'to equal', [1]);
  215. expect(getCellIndices(cells1, '.range-end'), 'to equal', [1]);
  216. expect(getCellIndices(cells1, '.range'), 'to equal', []);
  217. expect(getCellIndices(cells1, '.focused'), 'to equal', [1]);
  218. // mouse operation
  219. cells0[0].click();
  220. cells1[3].click();
  221. expect(drp.dates, 'to equal', [dateValue(2019, 0, 1), dateValue(2022, 11, 31)]);
  222. expect(input0.value, 'to be', '01/01/2019');
  223. expect(viewSwitch0.textContent, 'to be', '2010-2019');
  224. expect(getCellIndices(cells0, '.selected'), 'to equal', [10]);
  225. expect(getCellIndices(cells0, '.range-start'), 'to equal', [10]);
  226. expect(getCellIndices(cells0, '.range-end'), 'to equal', []);
  227. input1.focus();
  228. expect(input1.value, 'to be', '12/31/2022');
  229. expect(viewSwitch1.textContent, 'to be', '2020-2029');
  230. expect(getCellIndices(cells1, '.selected'), 'to equal', [3]);
  231. expect(getCellIndices(cells1, '.range-start'), 'to equal', [0]);
  232. expect(getCellIndices(cells1, '.range-end'), 'to equal', [3]);
  233. // api call
  234. drp.setDates('2/14/2025', '3/14/2021');
  235. expect(drp.dates, 'to equal', [dateValue(2021, 0, 1), dateValue(2025, 11, 31)]);
  236. expect(input0.value, 'to be', '01/01/2021');
  237. expect(viewSwitch0.textContent, 'to be', '2020-2029');
  238. expect(getCellIndices(cells0, '.selected'), 'to equal', [2]);
  239. expect(getCellIndices(cells0, '.range-start'), 'to equal', [2]);
  240. expect(getCellIndices(cells0, '.range-end'), 'to equal', [6]);
  241. input1.focus();
  242. expect(input1.value, 'to be', '12/31/2025');
  243. expect(viewSwitch1.textContent, 'to be', '2020-2029');
  244. expect(getCellIndices(cells1, '.selected'), 'to equal', [6]);
  245. expect(getCellIndices(cells1, '.range-start'), 'to equal', [2]);
  246. expect(getCellIndices(cells1, '.range-end'), 'to equal', [6]);
  247. drp.destroy();
  248. });
  249. });
  250. });