Datepicker-object.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. describe('Datepicker', function () {
  2. let input;
  3. before(function () {
  4. input = document.createElement('input');
  5. testContainer.appendChild(input);
  6. });
  7. after(function () {
  8. testContainer.removeChild(input);
  9. });
  10. it('input element\'s value is used for the initial date', function () {
  11. input.value = '04/22/2020';
  12. const dp = new Datepicker(input);
  13. expect(dp.dates, 'to equal', [dateValue(2020, 3, 22)]);
  14. dp.destroy();
  15. input.value = '';
  16. });
  17. it('the picker is hidden at start', function () {
  18. const dp = new Datepicker(input);
  19. expect(isVisible(document.querySelector('.datepicker')), 'to be false');
  20. expect(dp.active, 'to be false');
  21. dp.destroy();
  22. });
  23. it('the picker becomes visible when the input element get focused', function () {
  24. const dp = new Datepicker(input);
  25. input.focus();
  26. expect(isVisible(document.querySelector('.datepicker')), 'to be true');
  27. expect(dp.active, 'to be true');
  28. dp.destroy();
  29. });
  30. describe('hide()', function () {
  31. it('makes the picker invisible', function () {
  32. const dp = new Datepicker(input);
  33. input.focus();
  34. dp.hide();
  35. expect(isVisible(document.querySelector('.datepicker')), 'to be false');
  36. expect(dp.active, 'to be false');
  37. dp.destroy();
  38. });
  39. });
  40. describe('show()', function () {
  41. it('makes the picker visible', function () {
  42. const dp = new Datepicker(input);
  43. dp.show();
  44. expect(isVisible(document.querySelector('.datepicker')), 'to be true');
  45. expect(dp.active, 'to be true');
  46. dp.destroy();
  47. });
  48. it('moves the focus onto the input field', function () {
  49. // related to issue #52
  50. const dp = new Datepicker(input);
  51. const spyShow = sinon.spy(dp, 'show');
  52. input.blur();
  53. dp.show();
  54. expect(document.activeElement, 'to be', input);
  55. // the focus listener's calling show() is prevented
  56. expect(spyShow.calledOnce, 'to be true');
  57. spyShow.restore();
  58. dp.destroy();
  59. });
  60. it('adds or removes dir attribute to/from the picker if picker\'s text direction != input\'s', function (done) {
  61. testContainer.dir = 'rtl';
  62. const {dp, picker} = createDP(input);
  63. dp.show();
  64. expect(picker.dir, 'to be', 'rtl');
  65. dp.hide();
  66. testContainer.removeAttribute('dir');
  67. dp.show();
  68. expect(picker.hasAttribute('dir'), 'to be false');
  69. dp.hide();
  70. const htmlElem = document.querySelector('html');
  71. htmlElem.dir = 'rtl';
  72. input.style.direction = 'ltr';
  73. dp.show();
  74. expect(picker.dir, 'to be', 'ltr');
  75. dp.hide();
  76. input.removeAttribute('style');
  77. dp.show();
  78. expect(picker.hasAttribute('dir'), 'to be false');
  79. dp.destroy();
  80. htmlElem.removeAttribute('dir');
  81. htmlElem.style.direction = 'ltr';
  82. const checkDirChange = () => {
  83. if (window.getComputedStyle(htmlElem).direction === 'ltr') {
  84. htmlElem.removeAttribute('style');
  85. done();
  86. } else {
  87. setTimeout(checkDirChange, 10);
  88. }
  89. };
  90. checkDirChange();
  91. });
  92. });
  93. describe('picker', function () {
  94. it('displays current month with current date as focued date if no initial date is provided', function () {
  95. let clock = sinon.useFakeTimers({now: new Date(2020, 1, 14)});
  96. const {dp, picker} = createDP(input);
  97. const days = Array.from(picker.querySelector('.datepicker-grid').children);
  98. dp.show();
  99. expect(getViewSwitch(picker).textContent, 'to be', 'February 2020');
  100. expect(days, 'to have length', 42);
  101. expect(filterCells(days, '.datepicker-cell'), 'to have length', 42);
  102. expect(days[0].textContent, 'to be', '26');
  103. expect(days[0].classList.contains('prev'), 'to be true');
  104. expect(days[0].classList.contains('next'), 'to be false');
  105. expect(days[5].textContent, 'to be', '31');
  106. expect(days[5].classList.contains('prev'), 'to be true');
  107. expect(days[5].classList.contains('next'), 'to be false');
  108. expect(days[6].textContent, 'to be', '1');
  109. expect(days[6].classList.contains('prev'), 'to be false');
  110. expect(days[6].classList.contains('next'), 'to be false');
  111. expect(days[34].textContent, 'to be', '29');
  112. expect(days[34].classList.contains('prev'), 'to be false');
  113. expect(days[34].classList.contains('next'), 'to be false');
  114. expect(days[35].textContent, 'to be', '1');
  115. expect(days[35].classList.contains('prev'), 'to be false');
  116. expect(days[35].classList.contains('next'), 'to be true');
  117. expect(days[41].textContent, 'to be', '7');
  118. expect(days[41].classList.contains('prev'), 'to be false');
  119. expect(days[41].classList.contains('next'), 'to be true');
  120. expect(filterCells(days, '.focused'), 'to equal', [days[19]]);
  121. expect(filterCells(days, '.selected'), 'to be empty');
  122. expect(days[19].textContent, 'to be', '14');
  123. dp.destroy();
  124. clock.restore();
  125. });
  126. it('displays iniial date\'s month with the date as selected and focued date', function () {
  127. input.value = '04/22/2020';
  128. const {dp, picker} = createDP(input);
  129. const days = Array.from(picker.querySelector('.datepicker-grid').children);
  130. dp.show();
  131. expect(getViewSwitch(picker).textContent, 'to be', 'April 2020');
  132. expect(days, 'to have length', 42);
  133. expect(filterCells(days, '.datepicker-cell'), 'to have length', 42);
  134. expect(days[0].textContent, 'to be', '29');
  135. expect(days[0].classList.contains('prev'), 'to be true');
  136. expect(days[0].classList.contains('next'), 'to be false');
  137. expect(days[2].textContent, 'to be', '31');
  138. expect(days[2].classList.contains('prev'), 'to be true');
  139. expect(days[2].classList.contains('next'), 'to be false');
  140. expect(days[3].textContent, 'to be', '1');
  141. expect(days[3].classList.contains('prev'), 'to be false');
  142. expect(days[3].classList.contains('next'), 'to be false');
  143. expect(days[32].textContent, 'to be', '30');
  144. expect(days[32].classList.contains('prev'), 'to be false');
  145. expect(days[32].classList.contains('next'), 'to be false');
  146. expect(days[33].textContent, 'to be', '1');
  147. expect(days[33].classList.contains('prev'), 'to be false');
  148. expect(days[33].classList.contains('next'), 'to be true');
  149. expect(days[41].textContent, 'to be', '9');
  150. expect(days[41].classList.contains('prev'), 'to be false');
  151. expect(days[41].classList.contains('next'), 'to be true');
  152. expect(filterCells(days, '.focused'), 'to equal', [days[24]]);
  153. expect(filterCells(days, '.selected'), 'to equal', [days[24]]);
  154. expect(days[24].textContent, 'to be', '22');
  155. dp.destroy();
  156. input.value = '';
  157. });
  158. it('displays day names of week by default', function () {
  159. const {dp, picker} = createDP(input);
  160. const daysOfWeek = picker.querySelector('.days-of-week');
  161. const days = Array.from(daysOfWeek.children).map(el => el.textContent);
  162. expect(days, 'to equal', ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa']);
  163. dp.destroy();
  164. });
  165. it('does not display calendar weeks by default', function () {
  166. const {dp, picker} = createDP(input);
  167. expect(picker.querySelectorAll('.calendar-weeks').length, 'to be', 0);
  168. dp.destroy();
  169. });
  170. it('uses "button" for the main class of button element', function () {
  171. const {dp, picker} = createDP(input);
  172. const [viewSwitch, prevBtn, nextBtn, todayBtn, clearBtn] = getParts(picker, [
  173. '.view-switch',
  174. '.prev-btn',
  175. '.next-btn',
  176. '.today-btn',
  177. '.clear-btn',
  178. ]);
  179. expect(viewSwitch.className, 'to be', 'button view-switch');
  180. expect(prevBtn.className, 'to be', 'button prev-btn');
  181. expect(nextBtn.className, 'to be', 'button next-btn');
  182. expect(todayBtn.className, 'to be', 'button today-btn');
  183. expect(clearBtn.className, 'to be', 'button clear-btn');
  184. dp.destroy();
  185. });
  186. });
  187. });