api-methods.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. describe('Datepicker - API methods', function () {
  2. let clock;
  3. let input;
  4. let dp;
  5. let picker;
  6. beforeEach(function () {
  7. clock = sinon.useFakeTimers({now: new Date(2020, 2, 14)});
  8. input = parseHTML('<input type="text" value="04/22/2020">').firstChild;
  9. testContainer.appendChild(input);
  10. dp = new Datepicker(input);
  11. picker = document.querySelector('.datepicker');
  12. input.focus(); // Activate for visibility checks
  13. });
  14. afterEach(function () {
  15. if (input.datepicker) {
  16. dp.destroy();
  17. }
  18. testContainer.removeChild(input);
  19. clock.restore();
  20. });
  21. describe('getDate()', function () {
  22. it('returns a Date object of selected date', function () {
  23. const date = dp.getDate();
  24. expect(date, 'to be a date');
  25. expect(date.getTime(), 'to be', dateValue(2020, 3, 22));
  26. });
  27. it('returns a formatted date stirng of selected date if the format is specified', function () {
  28. expect(dp.getDate('yyyy-mm-dd'), 'to be', '2020-04-22');
  29. });
  30. it('returns undefined if no date is selected', function () {
  31. dp.destroy();
  32. input.value = '';
  33. dp = new Datepicker(input);
  34. expect(dp.getDate(), 'to be undefined');
  35. expect(dp.getDate('yyyy-mm-dd'), 'to be undefined');
  36. });
  37. });
  38. describe('setDate()', function () {
  39. it('changes the selected date to given date', function () {
  40. const spyChnageEvent = sinon.spy();
  41. input.addEventListener('change', spyChnageEvent);
  42. const viewSwitdh = getViewSwitch(picker);
  43. const date = new Date(2019, 11, 23);
  44. dp.setDate(date);
  45. expect(dp.dates, 'to equal', [date.getTime()]);
  46. expect(input.value, 'to be', '12/23/2019');
  47. expect(viewSwitdh.textContent, 'to be', 'December 2019');
  48. let cells = getCells(picker);
  49. expect(filterCells(cells, '.selected'), 'to equal', [cells[22]]);
  50. expect(filterCells(cells, '.focused'), 'to equal', [cells[22]]);
  51. expect(cells[22].textContent, 'to be', '23');
  52. dp.setDate('04/22/2020');
  53. expect(dp.dates, 'to equal', [dateValue(2020, 3, 22)]);
  54. expect(input.value, 'to be', '04/22/2020');
  55. expect(viewSwitdh.textContent, 'to be', 'April 2020');
  56. cells = getCells(picker);
  57. expect(filterCells(cells, '.selected'), 'to equal', [cells[24]]);
  58. expect(filterCells(cells, '.focused'), 'to equal', [cells[24]]);
  59. expect(cells[24].textContent, 'to be', '22');
  60. // change by api call should not be a trigger of change event
  61. // (issue #24)
  62. expect(spyChnageEvent.called, 'to be false');
  63. input.removeEventListener('change', spyChnageEvent);
  64. // change the view to the selected daye's days view
  65. // (issue #33)
  66. dp.picker.changeFocus(dateValue(2021, 3, 20)).changeView(2).render();
  67. dp.setDate('02/14/2020');
  68. expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
  69. expect(input.value, 'to be', '02/14/2020');
  70. expect(viewSwitdh.textContent, 'to be', 'February 2020');
  71. cells = getCells(picker);
  72. expect(filterCells(cells, '.selected'), 'to equal', [cells[19]]);
  73. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  74. expect(cells[19].textContent, 'to be', '14');
  75. });
  76. it('does nothing if no date or invalid date is given', function () {
  77. const viewSwitdh = getViewSwitch(picker);
  78. const origDates = [dateValue(2020, 3, 22)];
  79. dp.setDate();
  80. expect(dp.dates, 'to equal', origDates);
  81. expect(input.value, 'to be', '04/22/2020');
  82. expect(viewSwitdh.textContent, 'to be', 'April 2020');
  83. const cells = getCells(picker);
  84. expect(filterCells(cells, '.selected'), 'to equal', [cells[24]]);
  85. expect(filterCells(cells, '.focused'), 'to equal', [cells[24]]);
  86. expect(cells[24].textContent, 'to be', '22');
  87. dp.setDate('');
  88. expect(dp.dates, 'to equal', origDates);
  89. expect(input.value, 'to be', '04/22/2020');
  90. });
  91. it('clears the selection if no dates + clear: true option are given', function () {
  92. const spyChnageEvent = sinon.spy();
  93. input.addEventListener('change', spyChnageEvent);
  94. const viewSwitdh = getViewSwitch(picker);
  95. const today = dateUtils.today();
  96. dp.setDate({clear: true});
  97. expect(dp.dates, 'to equal', []);
  98. expect(input.value, 'to be', '');
  99. expect(viewSwitdh.textContent, 'to be', Datepicker.formatDate(today, 'MM yyyy'));
  100. // view date is changed to the default view date (current date)
  101. const cells = getCells(picker);
  102. const todayCell = filterCells(cells, el => el.dataset.date == today)[0];
  103. expect(todayCell.textContent, 'to be', Datepicker.formatDate(today, 'd'));
  104. expect(filterCells(cells, '.selected'), 'to equal', []);
  105. expect(filterCells(cells, '.focused'), 'to equal', [todayCell]);
  106. // change by api call should not be a trigger of change event
  107. // (issue #24)
  108. expect(spyChnageEvent.called, 'to be false');
  109. input.removeEventListener('change', spyChnageEvent);
  110. });
  111. it('omits updating the picker UI if render option = false', function () {
  112. const date = new Date(2019, 11, 23);
  113. dp.setDate(date, {render: false});
  114. expect(dp.dates, 'to equal', [date.getTime()]);
  115. expect(input.value, 'to be', '12/23/2019');
  116. const cells = getCells(picker);
  117. expect(filterCells(cells, '.selected'), 'to equal', [cells[24]]);
  118. expect(filterCells(cells, '.focused'), 'to equal', [cells[24]]);
  119. expect(cells[24].textContent, 'to be', '22');
  120. });
  121. it('hides the picker if both render and autohide options are true', function () {
  122. let date = new Date(2019, 11, 23);
  123. dp.setDate(date, {render: false, autohide: true});
  124. expect(dp.dates, 'to equal', [date.getTime()]);
  125. expect(input.value, 'to be', '12/23/2019');
  126. expect(getViewSwitch(picker).textContent, 'to be', 'April 2020');
  127. expect(filterCells(getCells(picker), '.selected')[0].textContent, 'to be', '22');
  128. expect(isVisible(picker), 'to be true');
  129. date = new Date(2018, 6, 14);
  130. dp.setDate(date, {autohide: true});
  131. expect(dp.dates, 'to equal', [date.getTime()]);
  132. expect(input.value, 'to be', '07/14/2018');
  133. expect(getViewSwitch(picker).textContent, 'to be', 'July 2018');
  134. expect(filterCells(getCells(picker), '.selected')[0].textContent, 'to be', '14');
  135. expect(isVisible(picker), 'to be false');
  136. });
  137. });
  138. describe('update()', function () {
  139. it('updates the selected date with the input element\'s value', function () {
  140. const viewSwitdh = getViewSwitch(picker);
  141. const date = new Date(2019, 11, 23);
  142. input.value = '12/23/2019';
  143. dp.update();
  144. expect(dp.dates, 'to equal', [date.getTime()]);
  145. expect(input.value, 'to be', '12/23/2019');
  146. expect(viewSwitdh.textContent, 'to be', 'December 2019');
  147. let cells = getCells(picker);
  148. expect(filterCells(cells, '.selected'), 'to equal', [cells[22]]);
  149. expect(filterCells(cells, '.focused'), 'to equal', [cells[22]]);
  150. expect(cells[22].textContent, 'to be', '23');
  151. // change the view to the selected daye's days view
  152. // (issue #33)
  153. dp.picker.changeFocus(dateValue(2021, 3, 20)).changeView(2).render();
  154. input.value = '02/14/2020';
  155. dp.update();
  156. expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
  157. expect(input.value, 'to be', '02/14/2020');
  158. expect(viewSwitdh.textContent, 'to be', 'February 2020');
  159. cells = getCells(picker);
  160. expect(filterCells(cells, '.selected'), 'to equal', [cells[19]]);
  161. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  162. expect(cells[19].textContent, 'to be', '14');
  163. });
  164. it('notmalizes iput text\'s format', function () {
  165. const date = new Date(2020, 6, 4);
  166. input.value = '7 4 2020';
  167. dp.update();
  168. expect(dp.dates, 'to equal', [date.getTime()]);
  169. expect(input.value, 'to be', '07/04/2020');
  170. expect(getViewSwitch(picker).textContent, 'to be', 'July 2020');
  171. expect(filterCells(getCells(picker), '.selected')[0].textContent, 'to be', '4');
  172. });
  173. });
  174. describe('refresh()', function () {
  175. it('refreshes the input element and picker UI to refrect the internal data', function () {
  176. const spyChnageEvent = sinon.spy();
  177. input.addEventListener('change', spyChnageEvent);
  178. dp.dates = [dateValue(2020, 1, 14)];
  179. dp.refresh();
  180. expect(input.value, 'to be', '02/14/2020');
  181. expect(getViewSwitch(picker).textContent, 'to be', 'February 2020');
  182. const cells = getCells(picker);
  183. expect(filterCells(cells, '.selected'), 'to equal', [cells[19]]);
  184. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  185. expect(cells[19].textContent, 'to be', '14');
  186. // change by api call should not be a trigger of change event
  187. // (issue #24)
  188. expect(spyChnageEvent.called, 'to be false');
  189. input.removeEventListener('change', spyChnageEvent);
  190. });
  191. it('also changes the view back to the selected date\'s days view', function () {
  192. dp.dates = [dateValue(2020, 1, 14)];
  193. dp.picker.changeFocus(dateValue(2021, 3, 20)).changeView(2).render();
  194. dp.refresh();
  195. expect(input.value, 'to be', '02/14/2020');
  196. expect(getViewSwitch(picker).textContent, 'to be', 'February 2020');
  197. let cells = getCells(picker);
  198. expect(filterCells(cells, '.selected'), 'to equal', [cells[19]]);
  199. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  200. expect(cells[19].textContent, 'to be', '14');
  201. // go back to the current date's days view if no date is selected
  202. dp.dates = [];
  203. dp.picker.changeFocus(dateValue(2019, 10, 22)).update().changeView(1).render();
  204. dp.refresh();
  205. expect(input.value, 'to be', '');
  206. expect(getViewSwitch(picker).textContent, 'to be', 'March 2020');
  207. cells = getCells(picker);
  208. expect(filterCells(cells, '.selected'), 'to equal', []);
  209. expect(filterCells(cells, '.focused'), 'to equal', [cells[13]]);
  210. expect(cells[13].textContent, 'to be', '14');
  211. clock.restore();
  212. });
  213. it('refresh only picker UI if target: "picker" is passed', function () {
  214. dp.dates = [dateValue(2020, 1, 14)];
  215. dp.refresh('picker');
  216. expect(input.value, 'to be', '04/22/2020');
  217. expect(getViewSwitch(picker).textContent, 'to be', 'February 2020');
  218. const cells = getCells(picker);
  219. expect(filterCells(cells, '.selected'), 'to equal', [cells[19]]);
  220. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  221. expect(cells[19].textContent, 'to be', '14');
  222. });
  223. it('refresh only input element if target: "input" is passed', function () {
  224. dp.dates = [dateValue(2020, 1, 14)];
  225. dp.refresh('input');
  226. expect(input.value, 'to be', '02/14/2020');
  227. expect(getViewSwitch(picker).textContent, 'to be', 'April 2020');
  228. const cells = getCells(picker);
  229. expect(filterCells(cells, '.selected'), 'to equal', [cells[24]]);
  230. expect(filterCells(cells, '.focused'), 'to equal', [cells[24]]);
  231. expect(cells[24].textContent, 'to be', '22');
  232. });
  233. it('re-renders the picker regardless of its state if forceRender true is passed', function () {
  234. let cells = getCells(picker);
  235. cells[16].classList.add('foo');
  236. cells[12].textContent = '♥︎';
  237. dp.dates = [dateValue(2020, 3, 10)];
  238. dp.refresh('picker');
  239. cells = getCells(picker);
  240. expect(input.value, 'to be', '04/22/2020');
  241. expect(getViewSwitch(picker).textContent, 'to be', 'April 2020');
  242. expect(filterCells(cells, '.selected'), 'to equal', [cells[12]]);
  243. expect(filterCells(cells, '.foo'), 'to equal', [cells[16]]);
  244. expect(cells[12].textContent, 'to be', '♥︎');
  245. dp.refresh('picker', true);
  246. cells = getCells(picker);
  247. expect(input.value, 'to be', '04/22/2020');
  248. expect(getViewSwitch(picker).textContent, 'to be', 'April 2020');
  249. expect(filterCells(cells, '.selected'), 'to equal', [cells[12]]);
  250. expect(filterCells(cells, '.foo'), 'to equal', []);
  251. expect(cells[12].textContent, 'to be', '10');
  252. cells[16].classList.add('foo');
  253. cells[12].textContent = '♥︎';
  254. dp.refresh(true);
  255. cells = getCells(picker);
  256. expect(input.value, 'to be', '04/10/2020');
  257. expect(getViewSwitch(picker).textContent, 'to be', 'April 2020');
  258. expect(filterCells(cells, '.selected'), 'to equal', [cells[12]]);
  259. expect(filterCells(cells, '.foo'), 'to equal', []);
  260. expect(cells[12].textContent, 'to be', '10');
  261. });
  262. });
  263. });