multidate.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. describe('options - multi date', 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('maxNumberOfDates', function () {
  14. it('specifies the muximum number of dates the datepicker accepts for the selection', function () {
  15. let {dp, picker} = createDP(input, {maxNumberOfDates: 2});
  16. dp.setDate('2/14/2020', '4/22/2020');
  17. expect(dp.dates, 'to equal', [dateValue(2020, 1, 14), dateValue(2020, 3, 22)]);
  18. expect(input.value, 'to be', '02/14/2020,04/22/2020');
  19. // the dates come later win
  20. dp.setDate('1/4/2020', '2/22/2020', '3/21/2020');
  21. expect(dp.dates, 'to equal', [dateValue(2020, 1, 22), dateValue(2020, 2, 21)]);
  22. expect(input.value, 'to be', '02/22/2020,03/21/2020');
  23. // repeated dates are eliminated
  24. dp.setDate('4/22/2020', '7/14/2020', '5/5/2020', '7/14/2020');
  25. expect(dp.dates, 'to equal', [dateValue(2020, 6, 14), dateValue(2020, 4, 5)]);
  26. expect(input.value, 'to be', '07/14/2020,05/05/2020');
  27. dp.destroy();
  28. input.value = '';
  29. ({dp, picker} = createDP(input, {maxNumberOfDates: 3}));
  30. dp.show();
  31. let cells = getCells(picker);
  32. cells[19].click();
  33. cells[9].click();
  34. expect(dp.dates, 'to equal', [dateValue(2020, 1, 14), dateValue(2020, 1, 4)]);
  35. expect(input.value, 'to be', '02/14/2020,02/04/2020');
  36. expect(filterCells(cells, '.selected'), 'to equal', [cells[19], cells[9]]);
  37. // view date is changed co the last selected item
  38. expect(filterCells(cells, '.focused'), 'to equal', [cells[9]]);
  39. input.value = '2/3/2020,2/22/2020';
  40. dp.update();
  41. expect(dp.dates, 'to equal', [dateValue(2020, 1, 3), dateValue(2020, 1, 22)]);
  42. expect(input.value, 'to be', '02/03/2020,02/22/2020');
  43. expect(filterCells(cells, '.selected'), 'to equal', [cells[8], cells[27]]);
  44. // view date is changed co the last item of the selection
  45. expect(filterCells(cells, '.focused'), 'to equal', [cells[27]]);
  46. dp.destroy();
  47. input.value = '';
  48. ({dp, picker} = createDP(input, {maxNumberOfDates: 3}));
  49. dp.setDate('2/14/2020', '4/22/2020', '3/21/2020');
  50. expect(dp.dates, 'to equal', [
  51. dateValue(2020, 1, 14),
  52. dateValue(2020, 3, 22),
  53. dateValue(2020, 2, 21),
  54. ]);
  55. expect(input.value, 'to be', '02/14/2020,04/22/2020,03/21/2020');
  56. dp.destroy();
  57. input.value = '';
  58. ({dp, picker} = createDP(input, {maxNumberOfDates: 3}));
  59. dp.show();
  60. getCells(picker)[1].click();
  61. getCells(picker)[40].click();
  62. cells = getCells(picker);
  63. cells[19].click();
  64. expect(dp.dates, 'to equal', [
  65. dateValue(2020, 0, 27),
  66. dateValue(2020, 1, 7),
  67. dateValue(2020, 1, 14),
  68. ]);
  69. expect(input.value, 'to be', '01/27/2020,02/07/2020,02/14/2020');
  70. expect(filterCells(cells, '.selected'), 'to equal', [cells[1], cells[12], cells[19]]);
  71. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  72. input.value = '2/3/2020,2/22/2020';
  73. dp.update();
  74. expect(dp.dates, 'to equal', [dateValue(2020, 1, 3), dateValue(2020, 1, 22)]);
  75. expect(input.value, 'to be', '02/03/2020,02/22/2020');
  76. expect(filterCells(cells, '.selected'), 'to equal', [cells[8], cells[27]]);
  77. expect(filterCells(cells, '.focused'), 'to equal', [cells[27]]);
  78. // setting initial dates does not cuase error
  79. // (issue #51)
  80. dp.destroy();
  81. input.value = '02/14/2020,04/22/2020,03/21/2020';
  82. ({dp, picker} = createDP(input, {maxNumberOfDates: 2}));
  83. expect(dp.dates, 'to equal', [
  84. dateValue(2020, 3, 22),
  85. dateValue(2020, 2, 21),
  86. ]);
  87. expect(input.value, 'to be', '04/22/2020,03/21/2020');
  88. dp.destroy();
  89. input.value = '';
  90. });
  91. it('makes the picker deselect the date when a selected date is clicked if value != 1', function () {
  92. const {dp, picker} = createDP(input, {maxNumberOfDates: 3});
  93. dp.show();
  94. let cells = getCells(picker);
  95. cells[19].click();
  96. cells[12].click();
  97. cells[19].click();
  98. expect(dp.dates, 'to equal', [dateValue(2020, 1, 7)]);
  99. expect(input.value, 'to be', '02/07/2020');
  100. expect(filterCells(cells, '.selected'), 'to equal', [cells[12]]);
  101. expect(filterCells(cells, '.focused'), 'to equal', [cells[12]]);
  102. cells[12].click();
  103. expect(dp.dates, 'to equal', []);
  104. expect(input.value, 'to be', '');
  105. expect(filterCells(cells, '.selected'), 'to equal', []);
  106. // view date is changed to the default view date
  107. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  108. dp.destroy();
  109. input.value = '';
  110. });
  111. it('makes the picker deselect the date when a selected date is clicked if value != 1', function () {
  112. const {dp, picker} = createDP(input, {maxNumberOfDates: 3});
  113. const cells = getCells(picker);
  114. dp.setDate('2/14/2020', '2/7/2020');
  115. dp.show();
  116. dp.setDate('2/14/2020');
  117. expect(dp.dates, 'to equal', [dateValue(2020, 1, 7)]);
  118. expect(input.value, 'to be', '02/07/2020');
  119. expect(filterCells(cells, '.selected'), 'to equal', [cells[12]]);
  120. expect(filterCells(cells, '.focused'), 'to equal', [cells[12]]);
  121. dp.setDate('2/11/2020', '2/7/2020', '2/14/2020');
  122. expect(dp.dates, 'to equal', [dateValue(2020, 1, 11), dateValue(2020, 1, 14)]);
  123. expect(input.value, 'to be', '02/11/2020,02/14/2020');
  124. expect(filterCells(cells, '.selected'), 'to equal', [cells[16], cells[19]]);
  125. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  126. dp.destroy();
  127. input.value = '';
  128. });
  129. it('setDate() replaces the selection instead of deselect/merg-ing if clear: true option is passed', function () {
  130. const {dp, picker} = createDP(input, {maxNumberOfDates: 3});
  131. const cells = getCells(picker);
  132. dp.setDate('2/14/2020', '2/7/2020');
  133. dp.show();
  134. dp.setDate('2/14/2020', {clear: true});
  135. expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
  136. expect(input.value, 'to be', '02/14/2020');
  137. expect(filterCells(cells, '.selected'), 'to equal', [cells[19]]);
  138. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  139. dp.setDate('2/11/2020', '2/7/2020', '2/14/2020', {clear: true});
  140. expect(dp.dates, 'to equal', [
  141. dateValue(2020, 1, 11),
  142. dateValue(2020, 1, 7),
  143. dateValue(2020, 1, 14),
  144. ]);
  145. expect(input.value, 'to be', '02/11/2020,02/07/2020,02/14/2020');
  146. expect(filterCells(cells, '.selected'), 'to equal', [cells[16], cells[12], cells[19]]);
  147. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  148. dp.destroy();
  149. input.value = '';
  150. });
  151. it('setDate() does nothing if no dates or all-invalid dates are passed', function () {
  152. const dp = new Datepicker(input, {maxNumberOfDates: 3});
  153. dp.setDate('2/14/2020', '2/7/2020');
  154. dp.show();
  155. const origDates = [...dp.dates];
  156. dp.setDate([]);
  157. expect(dp.dates, 'to equal', origDates);
  158. expect(input.value, 'to be', '02/14/2020,02/07/2020');
  159. dp.setDate();
  160. expect(dp.dates, 'to equal', origDates);
  161. expect(input.value, 'to be', '02/14/2020,02/07/2020');
  162. dp.setDate([false, NaN], {clear: true});
  163. expect(dp.dates, 'to equal', origDates);
  164. expect(input.value, 'to be', '02/14/2020,02/07/2020');
  165. dp.setDate('', null);
  166. expect(dp.dates, 'to equal', origDates);
  167. expect(input.value, 'to be', '02/14/2020,02/07/2020');
  168. dp.destroy();
  169. });
  170. it('setDate() clears all selected dates if no dates + clear: true option are passed', function () {
  171. const dp = new Datepicker(input, {maxNumberOfDates: 3});
  172. dp.setDate('2/14/2020', '2/7/2020');
  173. dp.show();
  174. dp.setDate([], {clear: true});
  175. expect(dp.dates, 'to equal', []);
  176. expect(input.value, 'to be', '');
  177. dp.setDate('2/14/2020', '2/7/2020');
  178. dp.setDate({clear: true});
  179. expect(dp.dates, 'to equal', []);
  180. expect(input.value, 'to be', '');
  181. dp.destroy();
  182. });
  183. it('setDate() does nothing if all-invalid dates + clear: true option are passed', function () {
  184. const dp = new Datepicker(input, {maxNumberOfDates: 3});
  185. dp.setDate('2/14/2020', '2/7/2020');
  186. dp.show();
  187. const origDates = [...dp.dates];
  188. dp.setDate([false, NaN], {clear: true});
  189. expect(dp.dates, 'to equal', origDates);
  190. expect(input.value, 'to be', '02/14/2020,02/07/2020');
  191. dp.setDate('', null, {clear: true});
  192. expect(dp.dates, 'to equal', origDates);
  193. expect(input.value, 'to be', '02/14/2020,02/07/2020');
  194. dp.destroy();
  195. });
  196. it('does not apply deselecting behavior to update()', function () {
  197. const {dp, picker} = createDP(input, {maxNumberOfDates: 3});
  198. const cells = getCells(picker);
  199. dp.setDate('2/14/2020', '2/7/2020');
  200. dp.show();
  201. input.value = '2/14/2020';
  202. dp.update();
  203. expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
  204. expect(input.value, 'to be', '02/14/2020');
  205. expect(filterCells(cells, '.selected'), 'to equal', [cells[19]]);
  206. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  207. input.value = '2/11/2020,2/7/2020,2/14/2020';
  208. dp.update();
  209. expect(dp.dates, 'to equal', [
  210. dateValue(2020, 1, 11),
  211. dateValue(2020, 1, 7),
  212. dateValue(2020, 1, 14),
  213. ]);
  214. expect(input.value, 'to be', '02/11/2020,02/07/2020,02/14/2020');
  215. expect(filterCells(cells, '.selected'), 'to equal', [cells[16], cells[12], cells[19]]);
  216. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  217. dp.destroy();
  218. input.value = '';
  219. });
  220. it('makes getDate() return array of dates if value != 1', function () {
  221. const dp = new Datepicker(input, {maxNumberOfDates: 3});
  222. expect(dp.getDate(), 'to equal', []);
  223. expect(dp.getDate('yyyy-mm-dd'), 'to equal', []);
  224. dp.setDate('2/11/2020', '2/7/2020', '2/14/2020');
  225. expect(dp.getDate(), 'to equal', [new Date(2020, 1, 11), new Date(2020, 1, 7), new Date(2020, 1, 14)]);
  226. expect(dp.getDate('yyyy-mm-dd'), 'to equal', ['2020-02-11', '2020-02-07', '2020-02-14']);
  227. dp.setDate('2/7/2020', {clear: true});
  228. expect(dp.getDate(), 'to equal', [new Date(2020, 1, 7)]);
  229. expect(dp.getDate('d M, yyyy'), 'to equal', ['7 Feb, 2020']);
  230. const changeDateListener = (e) => {
  231. evt = e;
  232. };
  233. let evt;
  234. input.addEventListener('changeDate', changeDateListener);
  235. dp.setDate('7/4/2020', '7/14/2020');
  236. expect(evt.detail.date, 'to equal', dp.getDate());
  237. input.removeEventListener('changeDate', changeDateListener);
  238. dp.destroy();
  239. input.value = '';
  240. });
  241. it('value 0 is considered unlimited', function () {
  242. if (window.navigator.userAgent.indexOf('Edge') > -1) {
  243. this.timeout(5000);
  244. }
  245. const max = new Date(2100, 0, 1).getTime();
  246. const generateDates = (dates, length, index = 0) => {
  247. const date = dateUtils.stripTime(Math.floor(Math.random() * max));
  248. if (dates.includes(date)) {
  249. return generateDates(dates, length, index);
  250. } else {
  251. dates.push(date);
  252. return index <= length
  253. ? generateDates(dates, length, index + 1)
  254. : dates;
  255. }
  256. };
  257. const dates = generateDates([], 3000);
  258. const dp = new Datepicker(input, {maxNumberOfDates: 0});
  259. dp.setDate(dates);
  260. expect(dp.dates, 'to equal', dates);
  261. dp.destroy();
  262. input.value = '';
  263. });
  264. it('can be updated with setOptions()', function () {
  265. const dp = new Datepicker(input);
  266. dp.setOptions({maxNumberOfDates: 3});
  267. dp.setDate('2/11/2020', '2/7/2020', '2/14/2020');
  268. expect(dp.dates, 'to equal', [
  269. dateValue(2020, 1, 11),
  270. dateValue(2020, 1, 7),
  271. dateValue(2020, 1, 14),
  272. ]);
  273. dp.setOptions({maxNumberOfDates: 1});
  274. dp.setDate('7/4/2020', '4/22/2020');
  275. expect(dp.dates, 'to equal', [dateValue(2020, 3, 22)]);
  276. expect(dp.getDate(), 'to be a date');
  277. dp.destroy();
  278. input.value = '';
  279. });
  280. });
  281. describe('dateDelimiter', function () {
  282. it('specifies the date delemiter for the input string', function () {
  283. const dp = new Datepicker(input, {maxNumberOfDates: 3, dateDelimiter: '|'});
  284. dp.setDate('2/14/2020', '4/22/2020');
  285. expect(input.value, 'to be', '02/14/2020|04/22/2020');
  286. input.value = '2/11/2020|2/7/2020|2/14/2020';
  287. dp.update();
  288. expect(dp.dates, 'to equal', [
  289. dateValue(2020, 1, 11),
  290. dateValue(2020, 1, 7),
  291. dateValue(2020, 1, 14),
  292. ]);
  293. dp.destroy();
  294. input.value = '';
  295. });
  296. it('can be updated with setOptions()', function () {
  297. const dp = new Datepicker(input, {maxNumberOfDates: 3});
  298. dp.setOptions({dateDelimiter: '_'});
  299. dp.setDate('2/11/2020', '2/7/2020', '2/14/2020');
  300. dp.setOptions({dateDelimiter: ' - '});
  301. expect(input.value, 'to be', '02/11/2020 - 02/07/2020 - 02/14/2020');
  302. dp.setOptions({dateDelimiter: ','});
  303. expect(input.value, 'to be', '02/11/2020,02/07/2020,02/14/2020');
  304. dp.destroy();
  305. input.value = '';
  306. });
  307. });
  308. });