buttons.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. describe('options - buttons', 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('clearBtn', function () {
  14. it('displays a button to clear the selection when true', function () {
  15. const {dp, picker} = createDP(input, {clearBtn: true});
  16. const [viewSwitch, clearBtn] = getParts(picker, ['.view-switch', '.clear-btn']);
  17. dp.show();
  18. expect(isVisible(clearBtn), 'to be true');
  19. expect(clearBtn.textContent, 'to be', 'Clear');
  20. // months view
  21. viewSwitch.click();
  22. expect(isVisible(clearBtn), 'to be true');
  23. // years view
  24. viewSwitch.click();
  25. expect(isVisible(clearBtn), 'to be true');
  26. // decades view
  27. viewSwitch.click();
  28. expect(isVisible(clearBtn), 'to be true');
  29. dp.destroy();
  30. });
  31. it('can be updated with setOptions()', function () {
  32. const {dp, picker} = createDP(input);
  33. dp.setOptions({clearBtn: true});
  34. dp.show();
  35. const clearBtn = picker.querySelector('.clear-btn');
  36. expect(isVisible(clearBtn), 'to be true');
  37. dp.setOptions({clearBtn: false});
  38. expect(isVisible(clearBtn), 'to be false');
  39. dp.destroy();
  40. });
  41. describe('clear button', function () {
  42. let dp;
  43. let picker;
  44. let clearBtn;
  45. beforeEach(function () {
  46. ({dp, picker} = createDP(input, {clearBtn: true}));
  47. clearBtn = picker.querySelector('.clear-btn');
  48. dp.show();
  49. });
  50. afterEach(function () {
  51. dp.destroy();
  52. });
  53. it('clears the selection', function () {
  54. dp.setDate('2/14/2020');
  55. clearBtn.click();
  56. expect(dp.dates, 'to equal', []);
  57. expect(input.value, 'to be', '');
  58. const viewSwitch = getViewSwitch(picker);
  59. // months view
  60. dp.setDate('2/14/2020');
  61. viewSwitch.click();
  62. clearBtn.click();
  63. expect(dp.dates, 'to equal', []);
  64. // years view
  65. dp.setDate('2/14/2020');
  66. viewSwitch.click();
  67. clearBtn.click();
  68. expect(dp.dates, 'to equal', []);
  69. // decades view
  70. dp.setDate('2/14/2020');
  71. viewSwitch.click();
  72. clearBtn.click();
  73. expect(dp.dates, 'to equal', []);
  74. });
  75. it('hides the picker as well when autohide = true', function () {
  76. dp.setDate('2/14/2020');
  77. dp.setOptions({autohide: true});
  78. clearBtn.click();
  79. expect(isVisible(picker), 'to be false');
  80. });
  81. });
  82. });
  83. describe('todayBtn', function () {
  84. it('displays a button to change the view date to current date when true', function () {
  85. const {dp, picker} = createDP(input, {todayBtn: true});
  86. const [viewSwitch, todayBtn] = getParts(picker, ['.view-switch', '.today-btn']);
  87. dp.show();
  88. expect(isVisible(todayBtn), 'to be true');
  89. expect(todayBtn.textContent, 'to be', 'Today');
  90. // months view
  91. viewSwitch.click();
  92. expect(isVisible(todayBtn), 'to be true');
  93. // years view
  94. viewSwitch.click();
  95. expect(isVisible(todayBtn), 'to be true');
  96. // decades view
  97. viewSwitch.click();
  98. expect(isVisible(todayBtn), 'to be true');
  99. dp.destroy();
  100. });
  101. it('today will be disabled if the current date is out of the range of minDate/maxDate', function () {
  102. const {dp, picker} = createDP(input, {todayBtn: true});
  103. const todayBtn = picker.querySelector('.today-btn');
  104. dp.show();
  105. expect(todayBtn.disabled, 'to be false');
  106. dp.setOptions({minDate: '2/20/2020'});
  107. expect(todayBtn.disabled, 'to be true');
  108. dp.setOptions({minDate: null, maxDate: '2/10/2020'});
  109. expect(todayBtn.disabled, 'to be true');
  110. dp.setOptions({minDate: '2/1/2020', maxDate: '2/29/2020'});
  111. expect(todayBtn.disabled, 'to be false');
  112. dp.destroy();
  113. });
  114. it('can be updated with setOptions()', function () {
  115. const {dp, picker} = createDP(input);
  116. dp.setOptions({todayBtn: true});
  117. dp.show();
  118. const todayBtn = picker.querySelector('.today-btn');
  119. expect(isVisible(todayBtn), 'to be true');
  120. dp.setOptions({todayBtn: false});
  121. expect(isVisible(todayBtn), 'to be false');
  122. dp.setOptions({todayBtn: 'true'});
  123. expect(isVisible(todayBtn), 'to be true');
  124. dp.destroy();
  125. });
  126. });
  127. describe('todayBtnMode', function () {
  128. let dp;
  129. let picker;
  130. let viewSwitch;
  131. let todayBtn;
  132. let cells;
  133. beforeEach(function () {
  134. ({dp, picker} = createDP(input, {todayBtn: true}));
  135. [viewSwitch, todayBtn] = getParts(picker, ['.view-switch', '.today-btn']);
  136. dp.show();
  137. });
  138. afterEach(function () {
  139. dp.destroy();
  140. });
  141. it('specifies the behavior of the today buton', function () {
  142. const date = dateValue(2020, 1, 11);
  143. // defualt to 0: focus-on (change view date)
  144. dp.setDate(date);
  145. todayBtn.click();
  146. cells = getCells(picker);
  147. expect(cells[19].textContent, 'to be', '14');
  148. expect(cells[19].classList.contains('focused'), 'to be true');
  149. expect(cells[16].classList.contains('selected'), 'to be true');
  150. expect(dp.dates, 'to equal', [date]);
  151. dp.destroy();
  152. // 1: select (change the selection)
  153. ({dp, picker} = createDP(input, {todayBtn: true, todayBtnMode: 1}));
  154. todayBtn = picker.querySelector('.today-btn');
  155. dp.setDate(date);
  156. dp.show();
  157. todayBtn.click();
  158. cells = getCells(picker);
  159. expect(cells[19].textContent, 'to be', '14');
  160. expect(cells[19].classList.contains('focused'), 'to be true');
  161. expect(cells[19].classList.contains('selected'), 'to be true');
  162. expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
  163. });
  164. it('can be updated with setOptions()', function () {
  165. const date = dateValue(2020, 1, 11);
  166. dp.setDate(date);
  167. dp.setOptions({todayBtnMode: 1});
  168. todayBtn.click();
  169. cells = getCells(picker);
  170. expect(cells[19].textContent, 'to be', '14');
  171. expect(cells[19].classList.contains('focused'), 'to be true');
  172. expect(cells[19].classList.contains('selected'), 'to be true');
  173. expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
  174. dp.setDate(date);
  175. dp.setOptions({todayBtnMode: 0});
  176. todayBtn.click();
  177. expect(cells[19].textContent, 'to be', '14');
  178. expect(cells[19].classList.contains('focused'), 'to be true');
  179. expect(cells[16].classList.contains('selected'), 'to be true');
  180. expect(dp.dates, 'to equal', [date]);
  181. });
  182. describe('today button', function () {
  183. it('changes the view date to the current date when todayBtnMode = 0', function () {
  184. dp.setDate('4/22/2020');
  185. todayBtn.click();
  186. expect(viewSwitch.textContent, 'to be', 'February 2020');
  187. cells = getCells(picker);
  188. expect(cells[19].textContent, 'to be', '14');
  189. expect(cells[19].classList.contains('focused'), 'to be true');
  190. expect(cells.find(el => el.classList.contains('selected')), 'to be undefined');
  191. expect(dp.dates, 'to equal', [dateValue(2020, 3, 22)]);
  192. expect(input.value, 'to be', '04/22/2020');
  193. dp.setDate({clear: true});
  194. });
  195. it('also changes the view to days view when todayBtnMode = 0', function () {
  196. // months view
  197. dp.setDate('4/22/2020');
  198. viewSwitch.click();
  199. todayBtn.click();
  200. expect(viewSwitch.textContent, 'to be', 'February 2020');
  201. // years view
  202. dp.setDate({clear: true});
  203. dp.setDate('4/22/2020');
  204. viewSwitch.click();
  205. viewSwitch.click();
  206. todayBtn.click();
  207. expect(viewSwitch.textContent, 'to be', 'February 2020');
  208. // decades view
  209. dp.setDate({clear: true});
  210. dp.setDate('4/22/2020');
  211. viewSwitch.click();
  212. viewSwitch.click();
  213. viewSwitch.click();
  214. todayBtn.click();
  215. expect(viewSwitch.textContent, 'to be', 'February 2020');
  216. dp.setDate({clear: true});
  217. });
  218. it('changes the selection to the current date when todayBtnMode = 1', function () {
  219. dp.setOptions({todayBtnMode: 1});
  220. dp.setDate('4/22/2020');
  221. todayBtn.click();
  222. expect(viewSwitch.textContent, 'to be', 'February 2020');
  223. cells = getCells(picker);
  224. expect(cells[19].textContent, 'to be', '14');
  225. expect(cells[19].classList.contains('focused'), 'to be true');
  226. expect(cells[19].classList.contains('selected'), 'to be true');
  227. expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
  228. expect(input.value, 'to be', '02/14/2020');
  229. dp.setDate({clear: true});
  230. dp.setDate('4/22/2020');
  231. viewSwitch.click();
  232. viewSwitch.click();
  233. todayBtn.click();
  234. expect(viewSwitch.textContent, 'to be', 'February 2020');
  235. expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
  236. dp.setDate({clear: true});
  237. });
  238. it('also hides the picker when todayBtnMode = 1 and autohide = true', function () {
  239. dp.setOptions({todayBtnMode: 1, autohide: true});
  240. dp.setDate('4/22/2020');
  241. todayBtn.click();
  242. expect(isVisible(picker), 'to be false');
  243. dp.setDate({clear: true});
  244. });
  245. it('always changes the view to current date\'s days view when todayBtnMode = 1', function () {
  246. const nextBtn = picker.querySelector('.next-btn');
  247. dp.setOptions({todayBtnMode: 1});
  248. // after moving other month or view while the current date is selected already
  249. // (issue #11)
  250. todayBtn.click();
  251. nextBtn.click();
  252. todayBtn.click();
  253. cells = getCells(picker);
  254. expect(viewSwitch.textContent, 'to be', 'February 2020');
  255. expect(cells[19].classList.contains('focused'), 'to be true');
  256. expect(cells[19].classList.contains('selected'), 'to be true');
  257. expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
  258. viewSwitch.click();
  259. nextBtn.click();
  260. todayBtn.click();
  261. cells = getCells(picker);
  262. expect(viewSwitch.textContent, 'to be', 'February 2020');
  263. expect(cells[19].classList.contains('focused'), 'to be true');
  264. expect(cells[19].classList.contains('selected'), 'to be true');
  265. expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
  266. viewSwitch.click();
  267. viewSwitch.click();
  268. nextBtn.click();
  269. nextBtn.click();
  270. todayBtn.click();
  271. cells = getCells(picker);
  272. expect(viewSwitch.textContent, 'to be', 'February 2020');
  273. expect(cells[19].classList.contains('focused'), 'to be true');
  274. expect(cells[19].classList.contains('selected'), 'to be true');
  275. expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
  276. // when current date is deslected by toggling in multi-date mode
  277. dp.setOptions({maxNumberOfDates: 3});
  278. nextBtn.click();
  279. getCells(picker)[20].click();
  280. todayBtn.click();
  281. cells = getCells(picker);
  282. expect(viewSwitch.textContent, 'to be', 'February 2020');
  283. expect(cells[19].classList.contains('focused'), 'to be true');
  284. expect(cells[19].classList.contains('selected'), 'to be false');
  285. expect(dp.dates, 'to equal', [dateValue(2020, 2, 21)]);
  286. nextBtn.click();
  287. todayBtn.click();
  288. cells = getCells(picker);
  289. expect(viewSwitch.textContent, 'to be', 'February 2020');
  290. expect(cells[19].classList.contains('selected'), 'to be true');
  291. expect(dp.dates, 'to equal', [dateValue(2020, 2, 21), dateValue(2020, 1, 14)]);
  292. viewSwitch.click();
  293. nextBtn.click();
  294. todayBtn.click();
  295. cells = getCells(picker);
  296. expect(viewSwitch.textContent, 'to be', 'February 2020');
  297. expect(cells[19].classList.contains('focused'), 'to be true');
  298. expect(cells[19].classList.contains('selected'), 'to be false');
  299. expect(dp.dates, 'to equal', [dateValue(2020, 2, 21)]);
  300. dp.setDate({clear: true});
  301. });
  302. });
  303. });
  304. });