date-restrictions.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. describe('options - date restrictions', function () {
  2. const getDisabled = cells => filterCells(cells, '.disabled').map(el => [el, el.textContent]);
  3. let input;
  4. beforeEach(function () {
  5. input = document.createElement('input');
  6. testContainer.appendChild(input);
  7. });
  8. afterEach(function () {
  9. testContainer.removeChild(input);
  10. });
  11. describe('datesDisabled', function () {
  12. let clock;
  13. beforeEach(function () {
  14. clock = sinon.useFakeTimers({now: new Date(2020, 1, 14)});
  15. });
  16. afterEach(function () {
  17. clock.restore();
  18. });
  19. it('specifies unselectable dates', function () {
  20. const dp = new Datepicker(input, {
  21. datesDisabled: [new Date(2020, 1, 12), '2/13/2020', new Date(2020, 1, 13), '2/20/2020'],
  22. });
  23. const picker = document.querySelector('.datepicker');
  24. dp.show();
  25. expect(picker.querySelector('.prev-btn').disabled, 'to be false');
  26. expect(picker.querySelector('.next-btn').disabled, 'to be false');
  27. let cells = getCells(picker);
  28. expect(getDisabled(cells), 'to equal', [
  29. [cells[17], '12'],
  30. [cells[18], '13'],
  31. [cells[25], '20'],
  32. ]);
  33. cells[17].click();
  34. expect(dp.dates, 'to equal', []);
  35. expect(input.value, 'to be', '');
  36. expect(filterCells(cells, '.selected'), 'to equal', []);
  37. dp.setDate(new Date(2020, 1, 12));
  38. expect(dp.dates, 'to equal', []);
  39. expect(input.value, 'to be', '');
  40. expect(filterCells(cells, '.selected'), 'to equal', []);
  41. input.value = '2/12/2020';
  42. dp.update();
  43. expect(dp.dates, 'to equal', []);
  44. expect(input.value, 'to be', '2/12/2020');
  45. expect(filterCells(cells, '.selected'), 'to equal', []);
  46. cells[16].click();
  47. expect(dp.dates, 'to equal', [dateValue(2020, 1, 11)]);
  48. expect(input.value, 'to be', '02/11/2020');
  49. expect(filterCells(cells, '.selected'), 'to equal', [cells[16]]);
  50. dp.enterEditMode();
  51. input.value = '2/12/2020';
  52. simulant.fire(input, 'keydown', {key: 'Enter'});
  53. expect(dp.dates, 'to equal', [dateValue(2020, 1, 11)]);
  54. expect(input.value, 'to be', '2/12/2020');
  55. expect(filterCells(cells, '.selected'), 'to equal', [cells[16]]);
  56. dp.destroy();
  57. });
  58. it('makes the picker prevent those dates becoming view date', function () {
  59. const dp = new Datepicker(input, {
  60. datesDisabled: ['2/11/2020', '2/12/2020', '2/13/2020', '2/20/2020'],
  61. });
  62. const picker = document.querySelector('.datepicker');
  63. const cells = getCells(picker);
  64. dp.show();
  65. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  66. expect(filterCells(cells, '.focused'), 'to equal', [cells[15]]);
  67. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  68. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  69. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  70. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  71. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  72. // on 5th
  73. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  74. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  75. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  76. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  77. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  78. // on 27th
  79. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  80. expect(filterCells(cells, '.focused'), 'to equal', [cells[24]]);
  81. // on 19th
  82. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  83. expect(filterCells(cells, '.focused'), 'to equal', [cells[15]]);
  84. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  85. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  86. // on 4th
  87. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  88. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  89. dp.destroy();
  90. });
  91. it('can be updated with setOptions()', function () {
  92. const {dp, picker} = createDP(input);
  93. dp.setOptions({datesDisabled: [new Date(2020, 1, 11), new Date(2020, 1, 26)]});
  94. dp.show();
  95. let cells = getCells(picker);
  96. expect(getDisabled(cells), 'to equal', [
  97. [cells[16], '11'],
  98. [cells[31], '26'],
  99. ]);
  100. dp.setOptions({datesDisabled: []});
  101. cells = getCells(picker);
  102. expect(getDisabled(cells), 'to equal', []);
  103. dp.destroy();
  104. });
  105. });
  106. describe('daysOfWeekDisabled', function () {
  107. let clock;
  108. beforeEach(function () {
  109. clock = sinon.useFakeTimers({now: new Date(2020, 1, 14)});
  110. });
  111. afterEach(function () {
  112. clock.restore();
  113. });
  114. it('specifies unselectable days of week', function () {
  115. const {dp, picker} = createDP(input, {daysOfWeekDisabled: [0, 6]});
  116. dp.show();
  117. expect(picker.querySelector('.prev-btn').disabled, 'to be false');
  118. expect(picker.querySelector('.next-btn').disabled, 'to be false');
  119. let cells = getCells(picker);
  120. expect(getDisabled(cells), 'to equal', [
  121. [cells[0], '26'],
  122. [cells[6], '1'],
  123. [cells[7], '2'],
  124. [cells[13], '8'],
  125. [cells[14], '9'],
  126. [cells[20], '15'],
  127. [cells[21], '16'],
  128. [cells[27], '22'],
  129. [cells[28], '23'],
  130. [cells[34], '29'],
  131. [cells[35], '1'],
  132. [cells[41], '7'],
  133. ]);
  134. cells[14].click();
  135. expect(dp.dates, 'to equal', []);
  136. expect(input.value, 'to be', '');
  137. expect(filterCells(cells, '.selected'), 'to equal', []);
  138. dp.setDate(new Date(2020, 1, 9));
  139. expect(dp.dates, 'to equal', []);
  140. expect(input.value, 'to be', '');
  141. expect(filterCells(cells, '.selected'), 'to equal', []);
  142. input.value = '2/9/2020';
  143. dp.update();
  144. expect(dp.dates, 'to equal', []);
  145. expect(input.value, 'to be', '2/9/2020');
  146. expect(filterCells(cells, '.selected'), 'to equal', []);
  147. cells[15].click();
  148. expect(dp.dates, 'to equal', [dateValue(2020, 1, 10)]);
  149. expect(input.value, 'to be', '02/10/2020');
  150. expect(filterCells(cells, '.selected'), 'to equal', [cells[15]]);
  151. dp.enterEditMode();
  152. input.value = '2/9/2020';
  153. simulant.fire(input, 'keydown', {key: 'Enter'});
  154. expect(dp.dates, 'to equal', [dateValue(2020, 1, 10)]);
  155. expect(input.value, 'to be', '2/9/2020');
  156. expect(filterCells(cells, '.selected'), 'to equal', [cells[15]]);
  157. dp.destroy();
  158. });
  159. it('makes the picker prevent those dates becoming view date', function () {
  160. const {dp, picker} = createDP(input, {daysOfWeekDisabled: [0, 6]});
  161. const cells = getCells(picker);
  162. dp.show();
  163. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  164. expect(filterCells(cells, '.focused'), 'to equal', [cells[22]]);
  165. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  166. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  167. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  168. // on 7th
  169. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  170. expect(filterCells(cells, '.focused'), 'to equal', [cells[15]]);
  171. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  172. expect(filterCells(cells, '.focused'), 'to equal', [cells[12]]);
  173. dp.destroy();
  174. });
  175. it('can be updated with setOptions()', function () {
  176. const {dp, picker} = createDP(input);
  177. dp.setOptions({daysOfWeekDisabled: [4]});
  178. dp.show();
  179. let cells = getCells(picker);
  180. expect(getDisabled(cells), 'to equal', [
  181. [cells[4], '30'],
  182. [cells[11], '6'],
  183. [cells[18], '13'],
  184. [cells[25], '20'],
  185. [cells[32], '27'],
  186. [cells[39], '5'],
  187. ]);
  188. dp.setOptions({daysOfWeekDisabled: []});
  189. cells = getCells(picker);
  190. expect(getDisabled(cells), 'to equal', []);
  191. dp.destroy();
  192. });
  193. });
  194. describe('maxDate', function () {
  195. let clock;
  196. beforeEach(function () {
  197. clock = sinon.useFakeTimers({now: new Date(2020, 1, 14)});
  198. });
  199. afterEach(function () {
  200. clock.restore();
  201. });
  202. it('specifies the maximum selectable date', function () {
  203. const {dp, picker} = createDP(input, {maxDate: new Date(2020, 1, 25)});
  204. const viewSwitch = getViewSwitch(picker);
  205. dp.show();
  206. let cells = getCells(picker);
  207. expect(getDisabled(cells), 'to equal', [
  208. [cells[31], '26'],
  209. [cells[32], '27'],
  210. [cells[33], '28'],
  211. [cells[34], '29'],
  212. [cells[35], '1'],
  213. [cells[36], '2'],
  214. [cells[37], '3'],
  215. [cells[38], '4'],
  216. [cells[39], '5'],
  217. [cells[40], '6'],
  218. [cells[41], '7'],
  219. ]);
  220. viewSwitch.click();
  221. cells = getCells(picker);
  222. expect(getDisabled(cells), 'to equal', [
  223. [cells[2], 'Mar'],
  224. [cells[3], 'Apr'],
  225. [cells[4], 'May'],
  226. [cells[5], 'Jun'],
  227. [cells[6], 'Jul'],
  228. [cells[7], 'Aug'],
  229. [cells[8], 'Sep'],
  230. [cells[9], 'Oct'],
  231. [cells[10], 'Nov'],
  232. [cells[11], 'Dec'],
  233. ]);
  234. viewSwitch.click();
  235. cells = getCells(picker);
  236. expect(getDisabled(cells), 'to equal', [
  237. [cells[2], '2021'],
  238. [cells[3], '2022'],
  239. [cells[4], '2023'],
  240. [cells[5], '2024'],
  241. [cells[6], '2025'],
  242. [cells[7], '2026'],
  243. [cells[8], '2027'],
  244. [cells[9], '2028'],
  245. [cells[10], '2029'],
  246. [cells[11], '2030'],
  247. ]);
  248. viewSwitch.click();
  249. cells = getCells(picker);
  250. expect(getDisabled(cells), 'to equal', [
  251. [cells[4], '2030'],
  252. [cells[5], '2040'],
  253. [cells[6], '2050'],
  254. [cells[7], '2060'],
  255. [cells[8], '2070'],
  256. [cells[9], '2080'],
  257. [cells[10], '2090'],
  258. [cells[11], '2100'],
  259. ]);
  260. dp.hide();
  261. dp.show();
  262. cells = getCells(picker);
  263. cells[31].click();
  264. expect(dp.dates, 'to equal', []);
  265. expect(input.value, 'to be', '');
  266. expect(filterCells(cells, '.selected'), 'to equal', []);
  267. dp.setDate(new Date(2020, 1, 26));
  268. expect(dp.dates, 'to equal', []);
  269. expect(input.value, 'to be', '');
  270. expect(filterCells(cells, '.selected'), 'to equal', []);
  271. input.value = '2/26/2020';
  272. dp.update();
  273. expect(dp.dates, 'to equal', []);
  274. expect(input.value, 'to be', '2/26/2020');
  275. expect(filterCells(cells, '.selected'), 'to equal', []);
  276. cells[30].click();
  277. expect(dp.dates, 'to equal', [dateValue(2020, 1, 25)]);
  278. expect(input.value, 'to be', '02/25/2020');
  279. expect(filterCells(cells, '.selected'), 'to equal', [cells[30]]);
  280. dp.enterEditMode();
  281. input.value = '2/26/2020';
  282. simulant.fire(input, 'keydown', {key: 'Enter'});
  283. expect(dp.dates, 'to equal', [dateValue(2020, 1, 25)]);
  284. expect(input.value, 'to be', '2/26/2020');
  285. expect(filterCells(cells, '.selected'), 'to equal', [cells[30]]);
  286. dp.destroy();
  287. });
  288. it('makes the picker disallow to navigate to after the max date', function () {
  289. const {dp, picker} = createDP(input, {maxDate: '2/14/2020'});
  290. const [viewSwitch, prevBtn, nextBtn] = getParts(picker, ['.view-switch', '.prev-btn', '.next-btn']);
  291. dp.show();
  292. expect(prevBtn.disabled, 'to be false');
  293. expect(nextBtn.disabled, 'to be true');
  294. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  295. let cells = getCells(picker);
  296. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  297. simulant.fire(input, 'keydown', {key: 'ArrowRight', ctrlKey: true});
  298. cells = getCells(picker);
  299. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  300. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  301. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  302. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  303. expect(filterCells(cells, '.focused'), 'to equal', [cells[13]]);
  304. prevBtn.click();
  305. expect(prevBtn.disabled, 'to be false');
  306. expect(nextBtn.disabled, 'to be false');
  307. // move to Jan 22nd
  308. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  309. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  310. nextBtn.click();
  311. // view date is limited to the max date
  312. cells = getCells(picker);
  313. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  314. // the same goes for ctrl + ArrowRight key
  315. prevBtn.click();
  316. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  317. simulant.fire(input, 'keydown', {key: 'ArrowRight', ctrlKey: true});
  318. cells = getCells(picker);
  319. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  320. // months view
  321. viewSwitch.click();
  322. expect(prevBtn.disabled, 'to be false');
  323. expect(nextBtn.disabled, 'to be true');
  324. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  325. cells = getCells(picker);
  326. expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
  327. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  328. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  329. // on prev year's Nov
  330. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  331. cells = getCells(picker);
  332. expect(filterCells(cells, '.focused'), 'to equal', [cells[10]]);
  333. prevBtn.click();
  334. expect(prevBtn.disabled, 'to be false');
  335. expect(nextBtn.disabled, 'to be false');
  336. // move to Mar
  337. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  338. nextBtn.click();
  339. // view date is limited to the Feb
  340. cells = getCells(picker);
  341. expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
  342. // the same goes for ctrl + ArrowRight key
  343. prevBtn.click();
  344. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  345. simulant.fire(input, 'keydown', {key: 'ArrowRight', ctrlKey: true});
  346. cells = getCells(picker);
  347. expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
  348. // years view
  349. viewSwitch.click();
  350. expect(prevBtn.disabled, 'to be false');
  351. expect(nextBtn.disabled, 'to be true');
  352. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  353. cells = getCells(picker);
  354. expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
  355. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  356. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  357. // on 2017
  358. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  359. cells = getCells(picker);
  360. expect(filterCells(cells, '.focused'), 'to equal', [cells[8]]);
  361. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  362. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  363. prevBtn.click();
  364. expect(prevBtn.disabled, 'to be false');
  365. expect(nextBtn.disabled, 'to be false');
  366. // move to 2011
  367. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  368. nextBtn.click();
  369. // view year is limited to 2020
  370. cells = getCells(picker);
  371. expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
  372. // the same goes for ctrl + ArrowRight key
  373. prevBtn.click();
  374. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  375. simulant.fire(input, 'keydown', {key: 'ArrowRight', ctrlKey: true});
  376. cells = getCells(picker);
  377. expect(filterCells(cells, '.focused'), 'to equal', [cells[1]]);
  378. // decades view
  379. viewSwitch.click();
  380. expect(prevBtn.disabled, 'to be false');
  381. expect(nextBtn.disabled, 'to be true');
  382. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  383. cells = getCells(picker);
  384. expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
  385. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  386. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  387. // on 1990
  388. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  389. cells = getCells(picker);
  390. expect(filterCells(cells, '.focused'), 'to equal', [cells[10]]);
  391. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  392. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  393. prevBtn.click();
  394. expect(prevBtn.disabled, 'to be false');
  395. expect(nextBtn.disabled, 'to be false');
  396. // move to 1930
  397. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  398. nextBtn.click();
  399. // view decade is limited to 2020
  400. cells = getCells(picker);
  401. expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
  402. // the same goes for ctrl + ArrowRight key
  403. prevBtn.click();
  404. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  405. simulant.fire(input, 'keydown', {key: 'ArrowRight', ctrlKey: true});
  406. cells = getCells(picker);
  407. expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
  408. dp.destroy();
  409. });
  410. it('can be updated with setOptions()', function () {
  411. const {dp, picker} = createDP(input);
  412. dp.setOptions({maxDate: new Date(2020, 1, 28)});
  413. dp.show();
  414. let cells = getCells(picker);
  415. expect(getDisabled(cells), 'to equal', [
  416. [cells[34], '29'],
  417. [cells[35], '1'],
  418. [cells[36], '2'],
  419. [cells[37], '3'],
  420. [cells[38], '4'],
  421. [cells[39], '5'],
  422. [cells[40], '6'],
  423. [cells[41], '7'],
  424. ]);
  425. dp.setOptions({maxDate: null});
  426. cells = getCells(picker);
  427. expect(getDisabled(cells), 'to equal', []);
  428. dp.destroy();
  429. });
  430. });
  431. describe('minDate', function () {
  432. let clock;
  433. beforeEach(function () {
  434. clock = sinon.useFakeTimers({now: new Date(2022, 6, 14)});
  435. });
  436. afterEach(function () {
  437. clock.restore();
  438. });
  439. it('specifies the minimum selectable date', function () {
  440. const {dp, picker} = createDP(input, {minDate: new Date(2022, 6, 4)});
  441. const viewSwitch = getViewSwitch(picker);
  442. dp.show();
  443. let cells = getCells(picker);
  444. expect(getDisabled(cells), 'to equal', [
  445. [cells[0], '26'],
  446. [cells[1], '27'],
  447. [cells[2], '28'],
  448. [cells[3], '29'],
  449. [cells[4], '30'],
  450. [cells[5], '1'],
  451. [cells[6], '2'],
  452. [cells[7], '3'],
  453. ]);
  454. viewSwitch.click();
  455. cells = getCells(picker);
  456. expect(getDisabled(cells), 'to equal', [
  457. [cells[0], 'Jan'],
  458. [cells[1], 'Feb'],
  459. [cells[2], 'Mar'],
  460. [cells[3], 'Apr'],
  461. [cells[4], 'May'],
  462. [cells[5], 'Jun'],
  463. ]);
  464. viewSwitch.click();
  465. cells = getCells(picker);
  466. expect(getDisabled(cells), 'to equal', [
  467. [cells[0], '2019'],
  468. [cells[1], '2020'],
  469. [cells[2], '2021'],
  470. ]);
  471. viewSwitch.click();
  472. cells = getCells(picker);
  473. expect(getDisabled(cells), 'to equal', [
  474. [cells[0], '1990'],
  475. [cells[1], '2000'],
  476. [cells[2], '2010'],
  477. ]);
  478. dp.hide();
  479. dp.show();
  480. cells = getCells(picker);
  481. cells[7].click();
  482. expect(dp.dates, 'to equal', []);
  483. expect(input.value, 'to be', '');
  484. expect(filterCells(cells, '.selected'), 'to equal', []);
  485. dp.setDate(new Date(2022, 6, 3));
  486. expect(dp.dates, 'to equal', []);
  487. expect(input.value, 'to be', '');
  488. expect(filterCells(cells, '.selected'), 'to equal', []);
  489. input.value = '7/3/2022';
  490. dp.update();
  491. expect(dp.dates, 'to equal', []);
  492. expect(input.value, 'to be', '7/3/2022');
  493. expect(filterCells(cells, '.selected'), 'to equal', []);
  494. cells[8].click();
  495. expect(dp.dates, 'to equal', [dateValue(2022, 6, 4)]);
  496. expect(input.value, 'to be', '07/04/2022');
  497. expect(filterCells(cells, '.selected'), 'to equal', [cells[8]]);
  498. dp.enterEditMode();
  499. input.value = '7/3/2022';
  500. simulant.fire(input, 'keydown', {key: 'Enter'});
  501. expect(dp.dates, 'to equal', [dateValue(2022, 6, 4)]);
  502. expect(input.value, 'to be', '7/3/2022');
  503. expect(filterCells(cells, '.selected'), 'to equal', [cells[8]]);
  504. dp.destroy();
  505. });
  506. it('makes the picker disallow to navigate to before the min date', function () {
  507. const {dp, picker} = createDP(input, {minDate: '7/14/2022'});
  508. const [viewSwitch, prevBtn, nextBtn] = getParts(picker, ['.view-switch', '.prev-btn', '.next-btn']);
  509. dp.show();
  510. expect(prevBtn.disabled, 'to be true');
  511. expect(nextBtn.disabled, 'to be false');
  512. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  513. let cells = getCells(picker);
  514. expect(filterCells(cells, '.focused'), 'to equal', [cells[18]]);
  515. simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
  516. cells = getCells(picker);
  517. expect(filterCells(cells, '.focused'), 'to equal', [cells[18]]);
  518. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  519. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  520. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  521. expect(filterCells(cells, '.focused'), 'to equal', [cells[24]]);
  522. nextBtn.click();
  523. expect(prevBtn.disabled, 'to be false');
  524. expect(nextBtn.disabled, 'to be false');
  525. // move to Aug 13th
  526. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  527. prevBtn.click();
  528. // view date is limited to min date
  529. cells = getCells(picker);
  530. expect(filterCells(cells, '.focused'), 'to equal', [cells[18]]);
  531. // the same goes for ctrl + ArrowLeft key
  532. prevBtn.click();
  533. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  534. simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
  535. cells = getCells(picker);
  536. expect(filterCells(cells, '.focused'), 'to equal', [cells[18]]);
  537. // months view
  538. viewSwitch.click();
  539. expect(prevBtn.disabled, 'to be true');
  540. expect(nextBtn.disabled, 'to be false');
  541. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  542. cells = getCells(picker);
  543. expect(filterCells(cells, '.focused'), 'to equal', [cells[6]]);
  544. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  545. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  546. // on Oct
  547. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  548. cells = getCells(picker);
  549. expect(filterCells(cells, '.focused'), 'to equal', [cells[9]]);
  550. nextBtn.click();
  551. expect(prevBtn.disabled, 'to be false');
  552. expect(nextBtn.disabled, 'to be false');
  553. // move to Jun
  554. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  555. prevBtn.click();
  556. // view date is limited to Jul
  557. cells = getCells(picker);
  558. expect(filterCells(cells, '.focused'), 'to equal', [cells[6]]);
  559. // the same goes for ctrl + ArrowLeft key
  560. prevBtn.click();
  561. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  562. simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
  563. cells = getCells(picker);
  564. expect(filterCells(cells, '.focused'), 'to equal', [cells[6]]);
  565. // years view
  566. viewSwitch.click();
  567. expect(prevBtn.disabled, 'to be true');
  568. expect(nextBtn.disabled, 'to be false');
  569. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  570. cells = getCells(picker);
  571. expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
  572. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  573. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  574. // on 2025
  575. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  576. cells = getCells(picker);
  577. expect(filterCells(cells, '.focused'), 'to equal', [cells[6]]);
  578. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  579. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  580. nextBtn.click();
  581. expect(prevBtn.disabled, 'to be false');
  582. expect(nextBtn.disabled, 'to be false');
  583. // move to 2031
  584. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  585. prevBtn.click();
  586. // view year is limited to 2022
  587. cells = getCells(picker);
  588. expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
  589. // the same goes for ctrl + ArrowLeft key
  590. prevBtn.click();
  591. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  592. simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
  593. cells = getCells(picker);
  594. expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
  595. // decades view
  596. viewSwitch.click();
  597. expect(prevBtn.disabled, 'to be true');
  598. expect(nextBtn.disabled, 'to be false');
  599. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  600. cells = getCells(picker);
  601. expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
  602. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  603. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  604. // on 2050
  605. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  606. cells = getCells(picker);
  607. expect(filterCells(cells, '.focused'), 'to equal', [cells[6]]);
  608. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  609. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  610. nextBtn.click();
  611. expect(prevBtn.disabled, 'to be false');
  612. expect(nextBtn.disabled, 'to be false');
  613. // move to 2110
  614. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  615. prevBtn.click();
  616. // view decade is limited to 2020
  617. cells = getCells(picker);
  618. expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
  619. // the same goes for ctrl + ArrowLeft key
  620. prevBtn.click();
  621. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  622. simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
  623. cells = getCells(picker);
  624. expect(filterCells(cells, '.focused'), 'to equal', [cells[3]]);
  625. dp.destroy();
  626. });
  627. it('can be updated with setOptions()', function () {
  628. const {dp, picker} = createDP(input);
  629. dp.setOptions({minDate: new Date(2022, 6, 2)});
  630. dp.show();
  631. let cells = getCells(picker);
  632. expect(getDisabled(cells), 'to equal', [
  633. [cells[0], '26'],
  634. [cells[1], '27'],
  635. [cells[2], '28'],
  636. [cells[3], '29'],
  637. [cells[4], '30'],
  638. [cells[5], '1'],
  639. ]);
  640. dp.setOptions({minDate: null});
  641. cells = getCells(picker);
  642. expect(getDisabled(cells), 'to equal', []);
  643. dp.destroy();
  644. });
  645. });
  646. });