api-methods.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. describe('DateRangePicker - API methods', 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('getDates()', function () {
  21. it('returns an array of the Date objects of selected dates', function () {
  22. input0.value = '04/20/2020';
  23. input1.value = '04/22/2020';
  24. const drp = new DateRangePicker(elem);
  25. expect(drp.getDates(), 'to equal', [
  26. new Date(dateValue(2020, 3, 20)),
  27. new Date(dateValue(2020, 3, 22)),
  28. ]);
  29. drp.destroy();
  30. input0.value = '';
  31. input1.value = '';
  32. });
  33. it('returns a formatted date stirngs of selected dates if the format is specified', function () {
  34. input0.value = '04/20/2020';
  35. input1.value = '04/22/2020';
  36. const drp = new DateRangePicker(elem);
  37. expect(drp.getDates('yyyy-mm-dd'), 'to equal', ['2020-04-20', '2020-04-22']);
  38. expect(drp.getDates('d M, yy'), 'to equal', ['20 Apr, 20', '22 Apr, 20']);
  39. drp.destroy();
  40. input0.value = '';
  41. input1.value = '';
  42. });
  43. it('uses undefined instead of Date object if date is not selected', function () {
  44. const drp = new DateRangePicker(elem);
  45. expect(drp.getDates(), 'to equal', [undefined, undefined]);
  46. expect(drp.getDates('yyyy-mm-dd'), 'to equal', [undefined, undefined]);
  47. drp.destroy();
  48. });
  49. });
  50. describe('setDates()', function () {
  51. let drp;
  52. let picker0;
  53. let picker1;
  54. let viewSwitch0;
  55. let viewSwitch1;
  56. let cells0;
  57. let cells1;
  58. it('changes the selected dates to given dates', function () {
  59. ({drp, picker0, picker1} = createDRP(elem));
  60. viewSwitch0 = picker0.querySelector('.view-switch');
  61. viewSwitch1 = picker1.querySelector('.view-switch');
  62. cells0 = getCells(picker0);
  63. cells1 = getCells(picker1);
  64. drp.setDates('2/11/2020', '2/14/2020');
  65. expect(drp.datepickers[0].dates, 'to equal', [dateValue(2020, 1, 11)]);
  66. expect(input0.value, 'to be', '02/11/2020');
  67. expect(viewSwitch0.textContent, 'to be', 'February 2020');
  68. expect(getCellIndices(cells0, '.selected'), 'to equal', [16]);
  69. expect(getCellIndices(cells0, '.range-start'), 'to equal', [16]);
  70. expect(getCellIndices(cells0, '.range-end'), 'to equal', [19]);
  71. expect(getCellIndices(cells0, '.range'), 'to equal', [17, 18]);
  72. expect(getCellIndices(cells0, '.focused'), 'to equal', [16]);
  73. expect(drp.datepickers[1].dates, 'to equal', [dateValue(2020, 1, 14)]);
  74. expect(input1.value, 'to be', '02/14/2020');
  75. expect(viewSwitch1.textContent, 'to be', 'February 2020');
  76. expect(getCellIndices(cells1, '.selected'), 'to equal', [19]);
  77. expect(getCellIndices(cells1, '.range-start'), 'to equal', [16]);
  78. expect(getCellIndices(cells1, '.range-end'), 'to equal', [19]);
  79. expect(getCellIndices(cells1, '.range'), 'to equal', [17, 18]);
  80. expect(getCellIndices(cells1, '.focused'), 'to equal', [19]);
  81. drp.setDates(new Date(2020, 4, 31), new Date(2020, 6, 5).getTime());
  82. expect(drp.datepickers[0].dates, 'to equal', [dateValue(2020, 4, 31)]);
  83. expect(input0.value, 'to be', '05/31/2020');
  84. expect(viewSwitch0.textContent, 'to be', 'May 2020');
  85. expect(getCellIndices(cells0, '.selected'), 'to equal', [35]);
  86. expect(getCellIndices(cells0, '.range-start'), 'to equal', [35]);
  87. expect(getCellIndices(cells0, '.range-end'), 'to equal', []);
  88. expect(getCellIndices(cells0, '.range'), 'to equal', [36, 37, 38, 39, 40, 41]);
  89. expect(getCellIndices(cells0, '.focused'), 'to equal', [35]);
  90. expect(drp.datepickers[1].dates, 'to equal', [dateValue(2020, 6, 5)]);
  91. expect(input1.value, 'to be', '07/05/2020');
  92. expect(viewSwitch1.textContent, 'to be', 'July 2020');
  93. expect(getCellIndices(cells1, '.selected'), 'to equal', [7]);
  94. expect(getCellIndices(cells1, '.range-start'), 'to equal', []);
  95. expect(getCellIndices(cells1, '.range-end'), 'to equal', [7]);
  96. expect(getCellIndices(cells1, '.range'), 'to equal', [0, 1, 2, 3, 4, 5, 6]);
  97. expect(getCellIndices(cells1, '.focused'), 'to equal', [7]);
  98. drp.destroy();
  99. input0.value = '';
  100. input1.value = '';
  101. });
  102. it('swapps star↔︎end dates if given start date > end date', function () {
  103. ({drp, picker0, picker1} = createDRP(elem));
  104. viewSwitch0 = picker0.querySelector('.view-switch');
  105. viewSwitch1 = picker1.querySelector('.view-switch');
  106. cells0 = getCells(picker0);
  107. cells1 = getCells(picker1);
  108. drp.setDates(new Date(2020, 6, 5).getTime(), new Date(2020, 4, 31));
  109. expect(drp.datepickers[0].dates, 'to equal', [dateValue(2020, 4, 31)]);
  110. expect(input0.value, 'to be', '05/31/2020');
  111. expect(viewSwitch0.textContent, 'to be', 'May 2020');
  112. expect(getCellIndices(cells0, '.selected'), 'to equal', [35]);
  113. expect(drp.datepickers[1].dates, 'to equal', [dateValue(2020, 6, 5)]);
  114. expect(input1.value, 'to be', '07/05/2020');
  115. expect(viewSwitch1.textContent, 'to be', 'July 2020');
  116. expect(getCellIndices(cells1, '.selected'), 'to equal', [7]);
  117. drp.destroy();
  118. input0.value = '';
  119. input1.value = '';
  120. });
  121. it('ignores invalid dates and the same date as the current one and leaves that side untouched', function () {
  122. input0.value = '02/11/2020';
  123. input1.value = '02/14/2020';
  124. ({drp, picker0, picker1} = createDRP(elem));
  125. viewSwitch0 = picker0.querySelector('.view-switch');
  126. viewSwitch1 = picker1.querySelector('.view-switch');
  127. cells0 = getCells(picker0);
  128. cells1 = getCells(picker1);
  129. drp.setDates('', '3/14/2020');
  130. expect(drp.datepickers[0].dates, 'to equal', [dateValue(2020, 1, 11)]);
  131. expect(input0.value, 'to be', '02/11/2020');
  132. expect(viewSwitch0.textContent, 'to be', 'February 2020');
  133. expect(getCellIndices(cells0, '.selected'), 'to equal', [16]);
  134. expect(drp.datepickers[1].dates, 'to equal', [dateValue(2020, 2, 14)]);
  135. expect(input1.value, 'to be', '03/14/2020');
  136. expect(viewSwitch1.textContent, 'to be', 'March 2020');
  137. expect(getCellIndices(cells1, '.selected'), 'to equal', [13]);
  138. let date0Str;
  139. let date0YM;
  140. let date0Indices;
  141. if (new Date(0).getDate() === 1) {
  142. date0Str = '01/01/1970';
  143. date0YM = 'January 1970';
  144. date0Indices = [4];
  145. } else {
  146. date0Str = '12/31/1969';
  147. date0YM = 'December 1969';
  148. date0Indices = [31];
  149. }
  150. drp.setDates(0, new Date(-1, 11, 31));
  151. expect(drp.datepickers[0].dates, 'to equal', [dateValue(0)]);
  152. expect(input0.value, 'to be', date0Str);
  153. expect(viewSwitch0.textContent, 'to be', date0YM);
  154. expect(getCellIndices(cells0, '.selected'), 'to equal', date0Indices);
  155. expect(drp.datepickers[1].dates, 'to equal', [dateValue(2020, 2, 14)]);
  156. expect(input1.value, 'to be', '03/14/2020');
  157. expect(viewSwitch1.textContent, 'to be', 'March 2020');
  158. expect(getCellIndices(cells1, '.selected'), 'to equal', [13]);
  159. input1.value = 'foo';
  160. drp.setDates('2/11/2020', '3/14/2020');
  161. expect(drp.datepickers[0].dates, 'to equal', [dateValue(2020, 1, 11)]);
  162. expect(input0.value, 'to be', '02/11/2020');
  163. expect(viewSwitch0.textContent, 'to be', 'February 2020');
  164. expect(getCellIndices(cells0, '.selected'), 'to equal', [16]);
  165. expect(drp.datepickers[1].dates, 'to equal', [dateValue(2020, 2, 14)]);
  166. expect(input1.value, 'to be', '03/14/2020');
  167. expect(viewSwitch1.textContent, 'to be', 'March 2020');
  168. expect(getCellIndices(cells1, '.selected'), 'to equal', [13]);
  169. input0.value = 'foo';
  170. drp.setDates('2/11/2020', '2/14/2020');
  171. expect(drp.datepickers[0].dates, 'to equal', [dateValue(2020, 1, 11)]);
  172. expect(input0.value, 'to be', '02/11/2020');
  173. expect(viewSwitch0.textContent, 'to be', 'February 2020');
  174. expect(getCellIndices(cells0, '.selected'), 'to equal', [16]);
  175. expect(drp.datepickers[1].dates, 'to equal', [dateValue(2020, 1, 14)]);
  176. expect(input1.value, 'to be', '02/14/2020');
  177. expect(viewSwitch1.textContent, 'to be', 'February 2020');
  178. expect(getCellIndices(cells1, '.selected'), 'to equal', [19]);
  179. drp.destroy();
  180. input0.value = '';
  181. input1.value = '';
  182. });
  183. it('sets the same date to both sides if called with one side only when range is not selected', function () {
  184. ({drp, picker0, picker1} = createDRP(elem));
  185. viewSwitch0 = picker0.querySelector('.view-switch');
  186. viewSwitch1 = picker1.querySelector('.view-switch');
  187. cells0 = getCells(picker0);
  188. cells1 = getCells(picker1);
  189. drp.setDates('2/11/2020');
  190. expect(drp.datepickers[0].dates, 'to equal', [dateValue(2020, 1, 11)]);
  191. expect(input0.value, 'to be', '02/11/2020');
  192. expect(viewSwitch0.textContent, 'to be', 'February 2020');
  193. expect(getCellIndices(cells0, '.selected'), 'to equal', [16]);
  194. expect(drp.datepickers[1].dates, 'to equal', [dateValue(2020, 1, 11)]);
  195. expect(input1.value, 'to be', '02/11/2020');
  196. expect(viewSwitch1.textContent, 'to be', 'February 2020');
  197. expect(getCellIndices(cells1, '.selected'), 'to equal', [16]);
  198. drp.destroy();
  199. input0.value = '';
  200. input1.value = '';
  201. ({drp, picker0, picker1} = createDRP(elem));
  202. viewSwitch0 = picker0.querySelector('.view-switch');
  203. viewSwitch1 = picker1.querySelector('.view-switch');
  204. cells0 = getCells(picker0);
  205. cells1 = getCells(picker1);
  206. drp.setDates(undefined, '3/14/2020');
  207. expect(drp.datepickers[0].dates, 'to equal', [dateValue(2020, 2, 14)]);
  208. expect(input0.value, 'to be', '03/14/2020');
  209. expect(viewSwitch0.textContent, 'to be', 'March 2020');
  210. expect(getCellIndices(cells0, '.selected'), 'to equal', [13]);
  211. expect(drp.datepickers[1].dates, 'to equal', [dateValue(2020, 2, 14)]);
  212. expect(input1.value, 'to be', '03/14/2020');
  213. expect(viewSwitch1.textContent, 'to be', 'March 2020');
  214. expect(getCellIndices(cells1, '.selected'), 'to equal', [13]);
  215. drp.destroy();
  216. input0.value = '';
  217. input1.value = '';
  218. });
  219. it('clears both sides if {clear: true} is passed as the last effective argument instrad of a date', function () {
  220. input0.value = '02/11/2020';
  221. input1.value = '02/14/2020';
  222. ({drp, picker0, picker1} = createDRP(elem));
  223. viewSwitch0 = picker0.querySelector('.view-switch');
  224. viewSwitch1 = picker1.querySelector('.view-switch');
  225. cells0 = getCells(picker0);
  226. cells1 = getCells(picker1);
  227. // start: clear + end: ineffective (unspecified)
  228. drp.setDates({clear: true});
  229. expect(drp.datepickers[0].dates, 'to equal', []);
  230. expect(input0.value, 'to be', '');
  231. expect(viewSwitch0.textContent, 'to be', 'February 2020');
  232. expect(getCellIndices(cells0, '.selected'), 'to equal', []);
  233. expect(drp.datepickers[1].dates, 'to equal', []);
  234. expect(input1.value, 'to be', '');
  235. expect(viewSwitch1.textContent, 'to be', 'February 2020');
  236. expect(getCellIndices(cells1, '.selected'), 'to equal', []);
  237. drp.destroy();
  238. input0.value = '02/11/2020';
  239. input1.value = '02/14/2020';
  240. ({drp, picker0, picker1} = createDRP(elem));
  241. viewSwitch0 = picker0.querySelector('.view-switch');
  242. viewSwitch1 = picker1.querySelector('.view-switch');
  243. cells0 = getCells(picker0);
  244. cells1 = getCells(picker1);
  245. // start: clear + end: ineffective (same date)
  246. drp.setDates({clear: true}, '2/14/2020');
  247. expect(drp.datepickers[0].dates, 'to equal', []);
  248. expect(input0.value, 'to be', '');
  249. expect(viewSwitch0.textContent, 'to be', 'February 2020');
  250. expect(getCellIndices(cells0, '.selected'), 'to equal', []);
  251. expect(drp.datepickers[1].dates, 'to equal', []);
  252. expect(input1.value, 'to be', '');
  253. expect(viewSwitch1.textContent, 'to be', 'February 2020');
  254. expect(getCellIndices(cells1, '.selected'), 'to equal', []);
  255. drp.destroy();
  256. input0.value = '02/11/2020';
  257. input1.value = '02/14/2020';
  258. ({drp, picker0, picker1} = createDRP(elem));
  259. viewSwitch0 = picker0.querySelector('.view-switch');
  260. viewSwitch1 = picker1.querySelector('.view-switch');
  261. cells0 = getCells(picker0);
  262. cells1 = getCells(picker1);
  263. // start: valid date + end: clear
  264. drp.setDates('4/20/2020', {clear: true});
  265. expect(drp.datepickers[0].dates, 'to equal', []);
  266. expect(input0.value, 'to be', '');
  267. expect(viewSwitch0.textContent, 'to be', 'February 2020');
  268. expect(getCellIndices(cells0, '.selected'), 'to equal', []);
  269. expect(drp.datepickers[1].dates, 'to equal', []);
  270. expect(input1.value, 'to be', '');
  271. expect(viewSwitch1.textContent, 'to be', 'February 2020');
  272. expect(getCellIndices(cells1, '.selected'), 'to equal', []);
  273. drp.destroy();
  274. input0.value = '02/11/2020';
  275. input1.value = '02/14/2020';
  276. ({drp, picker0, picker1} = createDRP(elem));
  277. viewSwitch0 = picker0.querySelector('.view-switch');
  278. viewSwitch1 = picker1.querySelector('.view-switch');
  279. cells0 = getCells(picker0);
  280. cells1 = getCells(picker1);
  281. // start: ineffective (same date) + end: clear
  282. drp.setDates('2/11/2020', {clear: true});
  283. expect(drp.datepickers[0].dates, 'to equal', []);
  284. expect(input0.value, 'to be', '');
  285. expect(viewSwitch0.textContent, 'to be', 'February 2020');
  286. expect(getCellIndices(cells0, '.selected'), 'to equal', []);
  287. expect(drp.datepickers[1].dates, 'to equal', []);
  288. expect(input1.value, 'to be', '');
  289. expect(viewSwitch1.textContent, 'to be', 'February 2020');
  290. expect(getCellIndices(cells1, '.selected'), 'to equal', []);
  291. drp.destroy();
  292. });
  293. it('sets the end date to both sides if {clear: true} is passed to start and an eefective date to end', function () {
  294. input0.value = '02/11/2020';
  295. input1.value = '02/11/2020';
  296. ({drp, picker0, picker1} = createDRP(elem));
  297. viewSwitch0 = picker0.querySelector('.view-switch');
  298. viewSwitch1 = picker1.querySelector('.view-switch');
  299. cells0 = getCells(picker0);
  300. cells1 = getCells(picker1);
  301. drp.setDates({clear: true}, '2/14/2020');
  302. expect(drp.datepickers[0].dates, 'to equal', [dateValue(2020, 1, 14)]);
  303. expect(input0.value, 'to be', '02/14/2020');
  304. expect(viewSwitch0.textContent, 'to be', 'February 2020');
  305. expect(getCellIndices(cells0, '.selected'), 'to equal', [19]);
  306. expect(drp.datepickers[1].dates, 'to equal', [dateValue(2020, 1, 14)]);
  307. expect(input1.value, 'to be', '02/14/2020');
  308. expect(viewSwitch1.textContent, 'to be', 'February 2020');
  309. expect(getCellIndices(cells1, '.selected'), 'to equal', [19]);
  310. drp.destroy();
  311. input0.value = '';
  312. input1.value = '';
  313. ({drp, picker0, picker1} = createDRP(elem));
  314. viewSwitch0 = picker0.querySelector('.view-switch');
  315. viewSwitch1 = picker1.querySelector('.view-switch');
  316. cells0 = getCells(picker0);
  317. cells1 = getCells(picker1);
  318. drp.setDates({clear: true}, '3/14/2020');
  319. expect(drp.datepickers[0].dates, 'to equal', [dateValue(2020, 2, 14)]);
  320. expect(input0.value, 'to be', '03/14/2020');
  321. expect(viewSwitch0.textContent, 'to be', 'March 2020');
  322. expect(getCellIndices(cells0, '.selected'), 'to equal', [13]);
  323. expect(drp.datepickers[1].dates, 'to equal', [dateValue(2020, 2, 14)]);
  324. expect(input1.value, 'to be', '03/14/2020');
  325. expect(viewSwitch1.textContent, 'to be', 'March 2020');
  326. expect(getCellIndices(cells1, '.selected'), 'to equal', [13]);
  327. drp.destroy();
  328. input0.value = '';
  329. input1.value = '';
  330. });
  331. });
  332. });