events.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. describe('events', function () {
  2. let clock;
  3. let input;
  4. let dp;
  5. let picker;
  6. let viewSwitch;
  7. before(function () {
  8. clock = sinon.useFakeTimers({now: new Date(2020, 1, 14)});
  9. input = document.createElement('input');
  10. testContainer.appendChild(input);
  11. ({dp, picker} = createDP(input));
  12. viewSwitch = getViewSwitch(picker);
  13. });
  14. after(function () {
  15. dp.destroy();
  16. testContainer.removeChild(input);
  17. clock.restore();
  18. });
  19. describe('changeDate', function () {
  20. it('is triggered when the selection is changed', function () {
  21. const spyChangeDate = sinon.spy();
  22. input.addEventListener('changeDate', spyChangeDate);
  23. // by setDate()
  24. dp.setDate('2/14/2020');
  25. expect(spyChangeDate.calledOnce, 'to be true');
  26. spyChangeDate.resetHistory();
  27. // by update()
  28. input.value = '4/22/2020';
  29. dp.update();
  30. expect(spyChangeDate.calledOnce, 'to be true');
  31. spyChangeDate.resetHistory();
  32. // by clicking on a day cell
  33. getCells(picker)[12].click();
  34. expect(spyChangeDate.calledOnce, 'to be true');
  35. spyChangeDate.resetHistory();
  36. // by hitting enter in edit mode
  37. dp.enterEditMode();
  38. input.value = '2/4/2020';
  39. simulant.fire(input, 'keydown', {key: 'Enter'});
  40. expect(spyChangeDate.calledOnce, 'to be true');
  41. spyChangeDate.resetHistory();
  42. // by hittin enter when the picker is hidden
  43. dp.hide();
  44. input.value = '3/20/2020';
  45. simulant.fire(input, 'keydown', {key: 'Enter'});
  46. expect(spyChangeDate.calledOnce, 'to be true');
  47. input.removeEventListener('changeDate', spyChangeDate);
  48. dp.setDate({clear: true});
  49. });
  50. it('is triggered when the selection is cleared', function () {
  51. const spyChangeDate = sinon.spy();
  52. dp.setDate('2/14/2020');
  53. input.addEventListener('changeDate', spyChangeDate);
  54. // by setDate()
  55. dp.setDate({clear: true});
  56. expect(spyChangeDate.calledOnce, 'to be true');
  57. dp.setDate('2/14/2020');
  58. spyChangeDate.resetHistory();
  59. // by update()
  60. input.value = '';
  61. dp.update();
  62. expect(spyChangeDate.calledOnce, 'to be true');
  63. dp.setDate('2/14/2020');
  64. spyChangeDate.resetHistory();
  65. // by hitting enter in edit mode
  66. dp.enterEditMode();
  67. input.value = '';
  68. simulant.fire(input, 'keydown', {key: 'Enter'});
  69. expect(spyChangeDate.calledOnce, 'to be true');
  70. dp.setDate('2/14/2020');
  71. spyChangeDate.resetHistory();
  72. // by hittin enter when the picker is hidden
  73. dp.hide();
  74. input.value = '';
  75. simulant.fire(input, 'keydown', {key: 'Enter'});
  76. expect(spyChangeDate.calledOnce, 'to be true');
  77. input.removeEventListener('changeDate', spyChangeDate);
  78. });
  79. it('is not triggered when trying to update with the current value', function () {
  80. const spyChangeDate = sinon.spy();
  81. dp.setDate('2/14/2020');
  82. input.addEventListener('changeDate', spyChangeDate);
  83. // by setDate()
  84. dp.setDate('2/14/2020');
  85. expect(spyChangeDate.called, 'to be false');
  86. // by update()
  87. dp.update();
  88. expect(spyChangeDate.called, 'to be false');
  89. // by clicking on a day cell
  90. getCells(picker)[19].click();
  91. expect(spyChangeDate.called, 'to be false');
  92. // by hitting enter in edit mode
  93. dp.enterEditMode();
  94. simulant.fire(input, 'keydown', {key: 'Enter'});
  95. expect(spyChangeDate.called, 'to be false');
  96. // by hittin enter when the picker is hidden
  97. dp.hide();
  98. simulant.fire(input, 'keydown', {key: 'Enter'});
  99. expect(spyChangeDate.called, 'to be false');
  100. input.removeEventListener('changeDate', spyChangeDate);
  101. dp.setDate({clear: true});
  102. });
  103. });
  104. describe('changeMonth', function () {
  105. let spyChangeMonth;
  106. beforeEach(function () {
  107. dp.show();
  108. spyChangeMonth = sinon.spy();
  109. input.addEventListener('changeMonth', spyChangeMonth);
  110. });
  111. afterEach(function () {
  112. input.removeEventListener('changeMonth', spyChangeMonth);
  113. dp.hide();
  114. });
  115. it('is triggered when prevBtn or nextBtn is clicked on days view', function () {
  116. const [prevBtn, nextBtn] = getParts(picker, ['.prev-btn', '.next-btn']);
  117. prevBtn.click();
  118. expect(spyChangeMonth.calledOnce, 'to be true');
  119. nextBtn.click();
  120. expect(spyChangeMonth.calledTwice, 'to be true');
  121. spyChangeMonth.resetHistory();
  122. // months view
  123. viewSwitch.click();
  124. prevBtn.click();
  125. expect(spyChangeMonth.called, 'to be false');
  126. nextBtn.click();
  127. expect(spyChangeMonth.called, 'to be false');
  128. // years view
  129. viewSwitch.click();
  130. prevBtn.click();
  131. expect(spyChangeMonth.called, 'to be false');
  132. nextBtn.click();
  133. expect(spyChangeMonth.called, 'to be false');
  134. // decades view
  135. viewSwitch.click();
  136. prevBtn.click();
  137. expect(spyChangeMonth.called, 'to be false');
  138. nextBtn.click();
  139. expect(spyChangeMonth.called, 'to be false');
  140. });
  141. it('is triggered when ctrl + arrow lett/right is pressed on days view', function () {
  142. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  143. expect(spyChangeMonth.called, 'to be false');
  144. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  145. expect(spyChangeMonth.called, 'to be false');
  146. simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
  147. expect(spyChangeMonth.calledOnce, 'to be true');
  148. simulant.fire(input, 'keydown', {key: 'ArrowRight', ctrlKey: true});
  149. expect(spyChangeMonth.calledTwice, 'to be true');
  150. spyChangeMonth.resetHistory();
  151. // months view
  152. viewSwitch.click();
  153. simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
  154. expect(spyChangeMonth.called, 'to be false');
  155. simulant.fire(input, 'keydown', {key: 'ArrowRight', ctrlKey: true});
  156. expect(spyChangeMonth.called, 'to be false');
  157. // years view
  158. viewSwitch.click();
  159. simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
  160. expect(spyChangeMonth.called, 'to be false');
  161. simulant.fire(input, 'keydown', {key: 'ArrowRight', ctrlKey: true});
  162. expect(spyChangeMonth.called, 'to be false');
  163. // decades view
  164. viewSwitch.click();
  165. simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
  166. expect(spyChangeMonth.called, 'to be false');
  167. simulant.fire(input, 'keydown', {key: 'ArrowRight', ctrlKey: true});
  168. expect(spyChangeMonth.called, 'to be false');
  169. });
  170. it('is triggered when a previous or next month\'s day is clicked', function () {
  171. getCells(picker)[19].click();
  172. expect(spyChangeMonth.called, 'to be false');
  173. getCells(picker)[2].click();
  174. expect(spyChangeMonth.calledOnce, 'to be true');
  175. getCells(picker)[40].click();
  176. expect(spyChangeMonth.calledTwice, 'to be true');
  177. dp.setDate({clear: true});
  178. });
  179. it('is triggered when view date is moved to a previous or next month\'s day by arrow keys', function () {
  180. // move to 2/1/2020
  181. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  182. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  183. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  184. // go to 1/31
  185. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  186. expect(spyChangeMonth.calledOnce, 'to be true');
  187. // go back to 2/1
  188. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  189. expect(spyChangeMonth.calledTwice, 'to be true');
  190. spyChangeMonth.resetHistory();
  191. // move to 2/3/2020
  192. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  193. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  194. // go to 1/27
  195. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  196. expect(spyChangeMonth.calledOnce, 'to be true');
  197. // go back to 2/3
  198. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  199. expect(spyChangeMonth.calledTwice, 'to be true');
  200. });
  201. it('is triggered when a different month is seleced on months view', function () {
  202. viewSwitch.click();
  203. getCells(picker)[1].click();
  204. expect(spyChangeMonth.called, 'to be false');
  205. viewSwitch.click();
  206. getCells(picker)[2].click();
  207. expect(spyChangeMonth.calledOnce, 'to be true');
  208. });
  209. it('is triggered when moving month by arrow keys on months view', function () {
  210. viewSwitch.click();
  211. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  212. expect(spyChangeMonth.callCount, 'to be', 1);
  213. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  214. expect(spyChangeMonth.callCount, 'to be', 2);
  215. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  216. expect(spyChangeMonth.callCount, 'to be', 3);
  217. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  218. expect(spyChangeMonth.callCount, 'to be', 4);
  219. });
  220. it('is triggered when the selection is updated to a different month by API calls', function () {
  221. dp.setDate('2/22/2020');
  222. expect(spyChangeMonth.called, 'to be false');
  223. dp.setDate('4/22/2020');
  224. expect(spyChangeMonth.calledOnce, 'to be true');
  225. input.value = '3/14/2020';
  226. dp.update();
  227. expect(spyChangeMonth.calledTwice, 'to be true');
  228. });
  229. it('is triggered when the selection is cleard from a date of a different month from default view date', function () {
  230. dp.setDate('2/22/2020');
  231. spyChangeMonth.resetHistory();
  232. dp.setDate({clear: true});
  233. expect(spyChangeMonth.called, 'to be false');
  234. dp.setDate('4/22/2020');
  235. spyChangeMonth.resetHistory();
  236. input.value = '';
  237. dp.update();
  238. expect(spyChangeMonth.calledOnce, 'to be true');
  239. dp.setDate('4/22/2020');
  240. spyChangeMonth.resetHistory();
  241. dp.enterEditMode();
  242. input.value = '';
  243. simulant.fire(input, 'keydown', {key: 'Enter'});
  244. expect(spyChangeMonth.calledOnce, 'to be true');
  245. });
  246. it('is triggered before changeDate when triggered on change of the selection', function () {
  247. const spyChangeDate = sinon.spy();
  248. input.addEventListener('changeDate', spyChangeDate);
  249. dp.setDate('4/22/2020');
  250. expect(spyChangeDate.called, 'to be true');
  251. expect(spyChangeMonth.called, 'to be true');
  252. expect(spyChangeMonth.calledBefore(spyChangeDate), 'to be true');
  253. input.removeEventListener('changeDate', spyChangeDate);
  254. dp.setDate({clear: true});
  255. });
  256. it('is triggered when view is reset from different month from default view date on hide', function () {
  257. picker.querySelector('.next-btn').click();
  258. spyChangeMonth.resetHistory();
  259. dp.hide();
  260. expect(spyChangeMonth.calledOnce, 'to be true');
  261. });
  262. it('is not triggered when view date is changed to the same month of different year', function () {
  263. viewSwitch.click();
  264. viewSwitch.click();
  265. // years view
  266. getCells(picker)[3].click();
  267. expect(spyChangeMonth.called, 'to be false');
  268. // months view
  269. getCells(picker)[1].click();
  270. expect(spyChangeMonth.called, 'to be false');
  271. viewSwitch.click();
  272. viewSwitch.click();
  273. // years view
  274. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  275. simulant.fire(input, 'keydown', {key: 'Enter'});
  276. expect(spyChangeMonth.called, 'to be false');
  277. // months view
  278. simulant.fire(input, 'keydown', {key: 'Enter'});
  279. expect(spyChangeMonth.called, 'to be false');
  280. });
  281. });
  282. describe('changeYear', function () {
  283. let spyChangeYear;
  284. beforeEach(function () {
  285. dp.show();
  286. spyChangeYear = sinon.spy();
  287. input.addEventListener('changeYear', spyChangeYear);
  288. });
  289. afterEach(function () {
  290. input.removeEventListener('changeYear', spyChangeYear);
  291. dp.hide();
  292. });
  293. it('is triggered when prevBtn on January on days view or nextBtn on December is clicked', function () {
  294. const [prevBtn, nextBtn] = getParts(picker, ['.prev-btn', '.next-btn']);
  295. // move to 1/15
  296. prevBtn.click();
  297. spyChangeYear.resetHistory();
  298. prevBtn.click();
  299. expect(spyChangeYear.calledOnce, 'to be true');
  300. nextBtn.click();
  301. expect(spyChangeYear.calledTwice, 'to be true');
  302. });
  303. it('is triggered when ctrl + arrow lett is pressed on January on days view or right on December', function () {
  304. // move to 1/15
  305. picker.querySelector('.prev-btn').click();
  306. spyChangeYear.resetHistory();
  307. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  308. expect(spyChangeYear.called, 'to be false');
  309. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  310. expect(spyChangeYear.called, 'to be false');
  311. simulant.fire(input, 'keydown', {key: 'ArrowLeft', ctrlKey: true});
  312. expect(spyChangeYear.calledOnce, 'to be true');
  313. simulant.fire(input, 'keydown', {key: 'ArrowRight', ctrlKey: true});
  314. expect(spyChangeYear.calledTwice, 'to be true');
  315. });
  316. it('is triggered when a previous month\'s day on January or a next month\'s on December is clicked', function () {
  317. // move to 1/15
  318. picker.querySelector('.prev-btn').click();
  319. spyChangeYear.resetHistory();
  320. getCells(picker)[19].click();
  321. expect(spyChangeYear.called, 'to be false');
  322. getCells(picker)[2].click();
  323. expect(spyChangeYear.calledOnce, 'to be true');
  324. getCells(picker)[40].click();
  325. expect(spyChangeYear.calledTwice, 'to be true');
  326. dp.setDate({clear: true});
  327. });
  328. it('is triggered when prevBtn or nextBtn is clicked on months view', function () {
  329. const [prevBtn, nextBtn] = getParts(picker, ['.prev-btn', '.next-btn']);
  330. viewSwitch.click();
  331. prevBtn.click();
  332. expect(spyChangeYear.calledOnce, 'to be true');
  333. nextBtn.click();
  334. expect(spyChangeYear.calledTwice, 'to be true');
  335. });
  336. it('is triggered when view month is moved to a previous or next year\'s by arrow keys', function () {
  337. viewSwitch.click();
  338. // move to january
  339. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  340. // go to last year's December
  341. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  342. expect(spyChangeYear.calledOnce, 'to be true');
  343. // go back to January
  344. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  345. expect(spyChangeYear.calledTwice, 'to be true');
  346. spyChangeYear.resetHistory();
  347. // go to last year's September
  348. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  349. expect(spyChangeYear.calledOnce, 'to be true');
  350. // go back to January
  351. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  352. expect(spyChangeYear.calledTwice, 'to be true');
  353. });
  354. it('is triggered when a different year/decade is seleced on years/decades view', function () {
  355. viewSwitch.click();
  356. viewSwitch.click();
  357. viewSwitch.click();
  358. // decades view
  359. getCells(picker)[0].click();
  360. expect(spyChangeYear.calledOnce, 'to be true');
  361. // years view
  362. getCells(picker)[2].click();
  363. expect(spyChangeYear.calledTwice, 'to be true');
  364. viewSwitch.click();
  365. viewSwitch.click();
  366. spyChangeYear.resetHistory();
  367. // decades view
  368. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  369. simulant.fire(input, 'keydown', {key: 'Enter'});
  370. expect(spyChangeYear.calledOnce, 'to be true');
  371. // years view
  372. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  373. simulant.fire(input, 'keydown', {key: 'Enter'});
  374. expect(spyChangeYear.calledTwice, 'to be true');
  375. });
  376. it('is triggered when moving year/decade by arrow keys on years/decades view', function () {
  377. viewSwitch.click();
  378. viewSwitch.click();
  379. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  380. expect(spyChangeYear.callCount, 'to be', 1);
  381. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  382. expect(spyChangeYear.callCount, 'to be', 2);
  383. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  384. expect(spyChangeYear.callCount, 'to be', 3);
  385. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  386. expect(spyChangeYear.callCount, 'to be', 4);
  387. viewSwitch.click();
  388. spyChangeYear.resetHistory();
  389. simulant.fire(input, 'keydown', {key: 'ArrowRight'});
  390. expect(spyChangeYear.callCount, 'to be', 1);
  391. simulant.fire(input, 'keydown', {key: 'ArrowLeft'});
  392. expect(spyChangeYear.callCount, 'to be', 2);
  393. simulant.fire(input, 'keydown', {key: 'ArrowDown'});
  394. expect(spyChangeYear.callCount, 'to be', 3);
  395. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  396. expect(spyChangeYear.callCount, 'to be', 4);
  397. });
  398. it('is triggered when the selection is updated to a different year by API calls', function () {
  399. dp.setDate('2/22/2020');
  400. expect(spyChangeYear.called, 'to be false');
  401. dp.setDate('4/22/2022');
  402. expect(spyChangeYear.calledOnce, 'to be true');
  403. input.value = '3/14/2018';
  404. dp.update();
  405. expect(spyChangeYear.calledTwice, 'to be true');
  406. });
  407. it('is triggered when the selection is cleard from a date of a different year from default view date', function () {
  408. dp.setDate('2/22/2020');
  409. spyChangeYear.resetHistory();
  410. dp.setDate({clear: true});
  411. expect(spyChangeYear.called, 'to be false');
  412. dp.setDate('4/22/2022');
  413. spyChangeYear.resetHistory();
  414. input.value = '';
  415. dp.update();
  416. expect(spyChangeYear.calledOnce, 'to be true');
  417. dp.setDate('4/22/2022');
  418. spyChangeYear.resetHistory();
  419. dp.enterEditMode();
  420. input.value = '';
  421. simulant.fire(input, 'keydown', {key: 'Enter'});
  422. expect(spyChangeYear.calledOnce, 'to be true');
  423. });
  424. it('is triggered before changeDate when triggered on change of the selection', function () {
  425. const spyChangeDate = sinon.spy();
  426. input.addEventListener('changeDate', spyChangeDate);
  427. dp.setDate('4/22/2022');
  428. expect(spyChangeDate.called, 'to be true');
  429. expect(spyChangeYear.called, 'to be true');
  430. expect(spyChangeYear.calledBefore(spyChangeDate), 'to be true');
  431. input.removeEventListener('changeDate', spyChangeDate);
  432. dp.setDate({clear: true});
  433. });
  434. it('is triggered when view is reset from different year from default view date on hide', function () {
  435. picker.querySelector('.prev-btn').click();
  436. picker.querySelector('.prev-btn').click();
  437. spyChangeYear.resetHistory();
  438. dp.hide();
  439. expect(spyChangeYear.calledOnce, 'to be true');
  440. });
  441. });
  442. describe('changeView', function () {
  443. let spyChangeView;
  444. beforeEach(function () {
  445. dp.show();
  446. spyChangeView = sinon.spy();
  447. input.addEventListener('changeView', spyChangeView);
  448. });
  449. afterEach(function () {
  450. input.removeEventListener('changeView', spyChangeView);
  451. dp.hide();
  452. });
  453. it('is triggered when view is changed by clicking view switch', function () {
  454. viewSwitch.click();
  455. expect(spyChangeView.calledOnce, 'to be true');
  456. // on months view
  457. viewSwitch.click();
  458. expect(spyChangeView.calledTwice, 'to be true');
  459. // on years view
  460. viewSwitch.click();
  461. expect(spyChangeView.calledThrice, 'to be true');
  462. // on decades view
  463. viewSwitch.click();
  464. expect(spyChangeView.calledThrice, 'to be true');
  465. });
  466. it('is triggered when view is changed by pressing ctrl + arrow up', function () {
  467. simulant.fire(input, 'keydown', {key: 'ArrowUp'});
  468. expect(spyChangeView.called, 'to be false');
  469. simulant.fire(input, 'keydown', {key: 'ArrowUp', ctrlKey: true});
  470. expect(spyChangeView.calledOnce, 'to be true');
  471. // on months view
  472. simulant.fire(input, 'keydown', {key: 'ArrowUp', ctrlKey: true});
  473. expect(spyChangeView.calledTwice, 'to be true');
  474. // on years view
  475. simulant.fire(input, 'keydown', {key: 'ArrowUp', ctrlKey: true});
  476. expect(spyChangeView.calledThrice, 'to be true');
  477. // on decades view
  478. simulant.fire(input, 'keydown', {key: 'ArrowUp', ctrlKey: true});
  479. expect(spyChangeView.calledThrice, 'to be true');
  480. });
  481. it('is triggered when view is changed by seleting a decade/year/month on decades/years/months view', function () {
  482. viewSwitch.click();
  483. viewSwitch.click();
  484. viewSwitch.click();
  485. spyChangeView.resetHistory();
  486. // on decades view
  487. getCells(picker)[2].click();
  488. expect(spyChangeView.calledOnce, 'to be true');
  489. // on years view
  490. getCells(picker)[2].click();
  491. expect(spyChangeView.calledTwice, 'to be true');
  492. // on months view
  493. getCells(picker)[2].click();
  494. expect(spyChangeView.calledThrice, 'to be true');
  495. // on days view
  496. getCells(picker)[12].click();
  497. expect(spyChangeView.calledThrice, 'to be true');
  498. viewSwitch.click();
  499. viewSwitch.click();
  500. viewSwitch.click();
  501. spyChangeView.resetHistory();
  502. simulant.fire(input, 'keydown', {key: 'Enter'});
  503. expect(spyChangeView.calledOnce, 'to be true');
  504. // on decades view
  505. simulant.fire(input, 'keydown', {key: 'Enter'});
  506. expect(spyChangeView.calledTwice, 'to be true');
  507. // on years view
  508. simulant.fire(input, 'keydown', {key: 'Enter'});
  509. expect(spyChangeView.calledThrice, 'to be true');
  510. // on months view
  511. simulant.fire(input, 'keydown', {key: 'Enter'});
  512. expect(spyChangeView.calledThrice, 'to be true');
  513. // on days view
  514. simulant.fire(input, 'keydown', {key: 'Enter'});
  515. expect(spyChangeView.calledThrice, 'to be true');
  516. });
  517. it('is triggered when view is reset on hide', function () {
  518. viewSwitch.click();
  519. spyChangeView.resetHistory();
  520. dp.hide();
  521. expect(spyChangeView.calledOnce, 'to be true');
  522. });
  523. });
  524. describe('show', function () {
  525. let spyShow;
  526. beforeEach(function () {
  527. dp.show();
  528. spyShow = sinon.spy();
  529. input.addEventListener('show', spyShow);
  530. });
  531. afterEach(function () {
  532. input.removeEventListener('show', spyShow);
  533. dp.hide();
  534. });
  535. it('is triggered when the picker becomes visible', function () {
  536. // not triggered if already shown
  537. dp.show();
  538. expect(spyShow.called, 'to be false');
  539. dp.hide();
  540. // by API call
  541. dp.show();
  542. expect(spyShow.calledOnce, 'to be true');
  543. dp.hide();
  544. input.blur();
  545. // by getting focus
  546. input.focus();
  547. expect(spyShow.calledTwice, 'to be true');
  548. // by toggling visibility by Esc key
  549. simulant.fire(input, 'keydown', {key: 'Escape'});
  550. expect(spyShow.calledTwice, 'to be true');
  551. simulant.fire(input, 'keydown', {key: 'Escape'});
  552. expect(spyShow.calledThrice, 'to be true');
  553. });
  554. });
  555. describe('hide', function () {
  556. let spyHide;
  557. beforeEach(function () {
  558. dp.show();
  559. spyHide = sinon.spy();
  560. input.addEventListener('hide', spyHide);
  561. });
  562. afterEach(function () {
  563. input.removeEventListener('hide', spyHide);
  564. dp.hide();
  565. });
  566. it('is triggered when the picker becomes hidden', function () {
  567. // by API call
  568. dp.hide();
  569. expect(spyHide.callCount, 'to be', 1);
  570. dp.show();
  571. // by clicking outside
  572. simulant.fire(testContainer, 'mousedown');
  573. expect(spyHide.callCount, 'to be', 2);
  574. dp.show();
  575. // by pressing tab key
  576. simulant.fire(input, 'keydown', {key: 'Tab'});
  577. expect(spyHide.callCount, 'to be', 3);
  578. dp.show();
  579. // by toggling visibility by Esc key
  580. simulant.fire(input, 'keydown', {key: 'Escape'});
  581. expect(spyHide.callCount, 'to be', 4);
  582. });
  583. });
  584. describe('event object', function () {
  585. const stubChangeDate = (ev) => {
  586. eventObj = ev;
  587. };
  588. let eventObj;
  589. before(function () {
  590. input.addEventListener('changeDate', stubChangeDate);
  591. dp.setDate('2/14/2020');
  592. });
  593. after(function () {
  594. input.removeEventListener('changeDate', stubChangeDate);
  595. });
  596. it('is a custom event object', function () {
  597. expect(eventObj, 'to be a', CustomEvent);
  598. });
  599. it('has the result of getDate() in detail.date', function () {
  600. expect(eventObj.detail.date, 'to equal', dp.getDate());
  601. });
  602. it('has a date object of view date in detail.viewDate', function () {
  603. expect(eventObj.detail.viewDate, 'to be a date');
  604. expect(eventObj.detail.viewDate.getTime(), 'to be', dateValue(2020, 1, 14));
  605. });
  606. it('has view mode of current view in detail.viewId', function () {
  607. expect(eventObj.detail.viewId, 'to be', 0);
  608. });
  609. it('has the Datepicker instance in detail.datepicker', function () {
  610. expect(eventObj.detail.datepicker, 'to be', dp);
  611. });
  612. });
  613. });