format.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. describe('options - format & language', 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('format', function () {
  14. it('specifies the date format used to parse/format the date string in input', function () {
  15. const dp = new Datepicker(input, {format: 'yyyy-mm-dd'});
  16. dp.setDate(new Date(2020, 1, 14));
  17. expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
  18. expect(input.value, 'to be', '2020-02-14');
  19. input.value = '2020/4/22';
  20. dp.update();
  21. expect(dp.dates, 'to equal', [dateValue(2020, 3, 22)]);
  22. expect(input.value, 'to be', '2020-04-22');
  23. // when a date in a wrong format is given...
  24. dp.setDate('2/14/2020');
  25. expect(dp.dates, 'to equal', [dateValue(2, 13, 2020)]);
  26. expect(input.value, 'to be', '0008-08-12');
  27. input.value = '22/4/2020';
  28. dp.update();
  29. expect(dp.dates, 'to equal', [dateValue(22, 3, 2020)]);
  30. expect(input.value, 'to be', '0027-10-11');
  31. dp.destroy();
  32. input.value = '';
  33. });
  34. it('custom parser/fomatter can be used by providing them as toValue/toDisplay of an object', function () {
  35. const dp = new Datepicker(input, {
  36. format: {
  37. toDisplay(date) {
  38. return new Date(date.getTime() - date.getTimezoneOffset() * 60000).toISOString()
  39. .slice(0, 10)
  40. .replace(/-/g, '');
  41. },
  42. toValue(date) {
  43. const parts = [
  44. parseInt(date.slice(0, 4), 10),
  45. parseInt(date.slice(4, 6), 10) - 1,
  46. parseInt(date.slice(6, 8), 10),
  47. ];
  48. return dateValue(...parts);
  49. },
  50. },
  51. });
  52. dp.setDate(new Date(2020, 1, 14));
  53. expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
  54. expect(input.value, 'to be', '20200214');
  55. input.value = '20200422';
  56. dp.update();
  57. expect(dp.dates, 'to equal', [dateValue(2020, 3, 22)]);
  58. expect(input.value, 'to be', '20200422');
  59. dp.destroy();
  60. input.value = '';
  61. });
  62. it('can be updated with setOptions()', function () {
  63. const dp = new Datepicker(input);
  64. dp.setOptions({format: 'd M, \'yy'});
  65. dp.setDate('14/2/2020');
  66. expect(dp.dates, 'to equal', [dateValue(2020, 1, 14)]);
  67. expect(input.value, 'to be', '14 Feb, \'20');
  68. dp.setOptions({format: 'mm/dd/yyyy'});
  69. expect(input.value, 'to be', '02/14/2020');
  70. input.value = '4/22/2020';
  71. dp.update();
  72. expect(dp.dates, 'to equal', [dateValue(2020, 3, 22)]);
  73. expect(input.value, 'to be', '04/22/2020');
  74. dp.destroy();
  75. input.value = '';
  76. });
  77. });
  78. describe('language', function () {
  79. const getDayNames = picker => Array.from(picker.querySelectorAll('.dow')).map(el => el.textContent);
  80. it('specifies the language used for the month/day names, today/clear buttons and the default format/weekStart', function () {
  81. const locale = Datepicker.locales['zh-CN'];
  82. const {dp, picker} = createDP(input, {language: 'zh-CN', todayBtn: true, clearBtn: true});
  83. const viewSwitch = getViewSwitch(picker);
  84. dp.setDate(new Date(2020, 1, 14));
  85. dp.show();
  86. expect(viewSwitch.textContent, 'to be', '2020年02月');
  87. expect(input.value, 'to be', '2020-02-14');
  88. expect(picker.querySelector('.today-btn').textContent, 'to be', locale.today);
  89. expect(picker.querySelector('.clear-btn').textContent, 'to be', locale.clear);
  90. const dayNames = locale.daysMin.slice(1);
  91. dayNames.push(locale.daysMin[0]);
  92. expect(getDayNames(picker), 'to equal', dayNames);
  93. let cells = getCells(picker);
  94. expect(cells[0].textContent, 'to be', '27');
  95. expect(cells[5].textContent, 'to be', '1');
  96. expect(cells[33].textContent, 'to be', '29');
  97. expect(cells[41].textContent, 'to be', '8');
  98. expect(filterCells(cells, '.selected'), 'to equal', [cells[18]]);
  99. expect(filterCells(cells, '.focused'), 'to equal', [cells[18]]);
  100. expect(cells[18].textContent, 'to be', '14');
  101. viewSwitch.click();
  102. expect(viewSwitch.textContent, 'to be', '2020');
  103. cells = getCells(picker);
  104. expect(Array.from(cells).map(el => el.textContent), 'to equal', locale.monthsShort);
  105. cells[1].click();
  106. input.value = '2020-4-22';
  107. dp.update();
  108. expect(viewSwitch.textContent, 'to be', '2020年04月');
  109. expect(input.value, 'to be', '2020-04-22');
  110. cells = getCells(picker);
  111. expect(filterCells(cells, '.selected'), 'to equal', [cells[23]]);
  112. expect(filterCells(cells, '.focused'), 'to equal', [cells[23]]);
  113. expect(cells[23].textContent, 'to be', '22');
  114. dp.destroy();
  115. input.value = '';
  116. });
  117. it('default format/weekStart in the locale are overriden by user-specified ones', function () {
  118. const locale = Datepicker.locales['zh-CN'];
  119. const {dp, picker} = createDP(input, {language: 'zh-CN', format: 'yyyy年mm月dd日', weekStart: 0});
  120. const viewSwitch = getViewSwitch(picker);
  121. dp.setDate(new Date(2020, 1, 14));
  122. dp.show();
  123. expect(viewSwitch.textContent, 'to be', '2020年02月');
  124. expect(input.value, 'to be', '2020年02月14日');
  125. expect(getDayNames(picker), 'to equal', locale.daysMin);
  126. let cells = getCells(picker);
  127. expect(filterCells(cells, '.selected'), 'to equal', [cells[19]]);
  128. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  129. expect(cells[19].textContent, 'to be', '14');
  130. dp.destroy();
  131. input.value = '';
  132. });
  133. it('language code + tag not installed falls back to the language code without tag', function () {
  134. const locale = Datepicker.locales.fr;
  135. const {dp, picker} = createDP(input, {language: 'fr-CA', todayBtn: true, clearBtn: true});
  136. const viewSwitch = getViewSwitch(picker);
  137. dp.setDate(new Date(2020, 1, 14));
  138. dp.show();
  139. expect(viewSwitch.textContent, 'to be', 'février 2020');
  140. expect(input.value, 'to be', '14/02/2020');
  141. expect(picker.querySelector('.today-btn').textContent, 'to be', locale.today);
  142. expect(picker.querySelector('.clear-btn').textContent, 'to be', locale.clear);
  143. const dayNames = locale.daysMin.slice(1);
  144. dayNames.push(locale.daysMin[0]);
  145. expect(getDayNames(picker), 'to equal', dayNames);
  146. dp.destroy();
  147. input.value = '';
  148. });
  149. it('language code not installed falls back to "en"', function () {
  150. const locale = Datepicker.locales.en;
  151. const {dp, picker} = createDP(input, {language: 'it', todayBtn: true, clearBtn: true});
  152. const viewSwitch = getViewSwitch(picker);
  153. dp.setDate(new Date(2020, 1, 14));
  154. dp.show();
  155. expect(viewSwitch.textContent, 'to be', 'February 2020');
  156. expect(input.value, 'to be', '02/14/2020');
  157. expect(picker.querySelector('.today-btn').textContent, 'to be', locale.today);
  158. expect(picker.querySelector('.clear-btn').textContent, 'to be', locale.clear);
  159. expect(getDayNames(picker), 'to equal', locale.daysMin);
  160. dp.destroy();
  161. input.value = '';
  162. });
  163. it('can be updated with setOptions()', function () {
  164. const {dp, picker} = createDP(input, {todayBtn: true, clearBtn: true});
  165. const viewSwitch = getViewSwitch(picker);
  166. let locale = Datepicker.locales['zh-CN'];
  167. dp.setDate(new Date(2020, 1, 14));
  168. dp.setOptions({language: 'zh-CN'});
  169. dp.show();
  170. expect(viewSwitch.textContent, 'to be', '2020年02月');
  171. expect(input.value, 'to be', '2020-02-14');
  172. expect(picker.querySelector('.today-btn').textContent, 'to be', locale.today);
  173. expect(picker.querySelector('.clear-btn').textContent, 'to be', locale.clear);
  174. let dayNames = locale.daysMin.slice(1);
  175. dayNames.push(locale.daysMin[0]);
  176. expect(getDayNames(picker), 'to equal', dayNames);
  177. let cells = getCells(picker);
  178. expect(filterCells(cells, '.selected'), 'to equal', [cells[18]]);
  179. expect(filterCells(cells, '.focused'), 'to equal', [cells[18]]);
  180. expect(cells[18].textContent, 'to be', '14');
  181. locale = Datepicker.locales.fr;
  182. dp.setOptions({language: 'fr'});
  183. expect(viewSwitch.textContent, 'to be', 'février 2020');
  184. expect(input.value, 'to be', '14/02/2020');
  185. expect(picker.querySelector('.today-btn').textContent, 'to be', locale.today);
  186. expect(picker.querySelector('.clear-btn').textContent, 'to be', locale.clear);
  187. dayNames = locale.daysMin.slice(1);
  188. dayNames.push(locale.daysMin[0]);
  189. expect(getDayNames(picker), 'to equal', dayNames);
  190. cells = getCells(picker);
  191. expect(filterCells(cells, '.selected'), 'to equal', [cells[18]]);
  192. expect(filterCells(cells, '.focused'), 'to equal', [cells[18]]);
  193. expect(cells[18].textContent, 'to be', '14');
  194. locale = Datepicker.locales.en;
  195. dp.setOptions({language: 'en'});
  196. expect(viewSwitch.textContent, 'to be', 'February 2020');
  197. expect(input.value, 'to be', '02/14/2020');
  198. expect(picker.querySelector('.today-btn').textContent, 'to be', locale.today);
  199. expect(picker.querySelector('.clear-btn').textContent, 'to be', locale.clear);
  200. expect(getDayNames(picker), 'to equal', locale.daysMin);
  201. cells = getCells(picker);
  202. expect(filterCells(cells, '.selected'), 'to equal', [cells[19]]);
  203. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  204. expect(cells[19].textContent, 'to be', '14');
  205. dp.destroy();
  206. input.value = '';
  207. });
  208. it('user-specified format/weekStart other than old language\'s default are kept on being updated dynamically', function () {
  209. const {dp, picker} = createDP(input, {language: 'zh-CN', format: 'yyyy/mm/dd', weekStart: 0});
  210. dp.setDate(new Date(2020, 1, 14));
  211. dp.show();
  212. let locale = Datepicker.locales.fr;
  213. dp.setOptions({language: 'fr'});
  214. expect(input.value, 'to be', '2020/02/14');
  215. expect(getDayNames(picker), 'to equal', locale.daysMin);
  216. let cells = getCells(picker);
  217. expect(filterCells(cells, '.selected'), 'to equal', [cells[19]]);
  218. expect(filterCells(cells, '.focused'), 'to equal', [cells[19]]);
  219. expect(cells[19].textContent, 'to be', '14');
  220. dp.destroy();
  221. input.value = '';
  222. });
  223. });
  224. });