jquery-ui-timepicker-addon_spec.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. describe('datetimepicker', function() {
  2. describe('utility functions', function() {
  3. var util = $.timepicker._util;
  4. describe('extendRemove', function() {
  5. var target,
  6. props;
  7. beforeEach(function() {
  8. target = {};
  9. props = {};
  10. });
  11. it('should add a nonexistent property to the target', function() {
  12. var expectedValue = "set",
  13. propertyName = "prop";
  14. props[propertyName] = expectedValue;
  15. var newTarget = util._extendRemove(target, props);
  16. expect(target[propertyName]).toBe(expectedValue);
  17. expect(newTarget).toBe(target);
  18. });
  19. it('should change the value of an existing property', function() {
  20. var expectedValue = "new",
  21. originalValue = "old",
  22. propertyName = "prop";
  23. target[propertyName] = originalValue;
  24. props[propertyName] = expectedValue;
  25. util._extendRemove(target, props);
  26. expect(target[propertyName]).not.toBe(originalValue);
  27. expect(target[propertyName]).toBe(expectedValue);
  28. });
  29. it('should null the value of an existing property', function() {
  30. var expectedValue = null,
  31. propertyName = "prop";
  32. target[propertyName] = "original";
  33. props[propertyName] = expectedValue;
  34. util._extendRemove(target, props);
  35. expect(target[propertyName]).toBeNull();
  36. });
  37. });
  38. describe('isEmptyObject', function() {
  39. it('should say an empty object is empty', function() {
  40. expect(util._isEmptyObject({})).toBe(true);
  41. });
  42. it('should say an object with a property is not empty', function() {
  43. var testObject = {"prop": "value"};
  44. expect(util._isEmptyObject(testObject)).toBe(false);
  45. });
  46. it('should say object with a supplemental prototype property is empty', function() {
  47. var testObject = function () {};
  48. testObject.prototype["prop"] = "something";
  49. expect(util._isEmptyObject(testObject)).toBe(true);
  50. });
  51. });
  52. describe('convert24to12', function() {
  53. it('should return the value for a non-zero value less than 12', function() {
  54. var expectedHour = 6;
  55. expect(util._convert24to12(expectedHour)).toBe("" + expectedHour);
  56. });
  57. it('should return 12 hours less if the value is greater than 12 and less than 24', function() {
  58. var expectedHour = 7;
  59. expect(util._convert24to12(expectedHour + 12)).toBe("" + expectedHour);
  60. });
  61. it('should return 12 if the normalized value is 0', function() {
  62. expect(util._convert24to12(0)).toBe('12');
  63. });
  64. it('should normalize values that are clearly out of the expected range', function() {
  65. var expectedValue = 11;
  66. expect(util._convert24to12(expectedValue + 12 * 3)).toBe("" + expectedValue);
  67. });
  68. });
  69. describe('detectSupport', function() {
  70. it('should detect support for hours', function() {
  71. expect(util._detectSupport('H').hour).toBe(true);
  72. expect(util._detectSupport('HH').hour).toBe(true);
  73. expect(util._detectSupport('h').hour).toBe(true);
  74. expect(util._detectSupport('hh').hour).toBe(true);
  75. expect(util._detectSupport('asdf').hour).toBe(false);
  76. });
  77. it('should detect support for minutes', function() {
  78. expect(util._detectSupport('m').minute).toBe(true);
  79. expect(util._detectSupport('mm').minute).toBe(true);
  80. expect(util._detectSupport('asdf').minute).toBe(false);
  81. });
  82. it('should detect support for seconds', function() {
  83. expect(util._detectSupport('s').second).toBe(true);
  84. expect(util._detectSupport('ss').second).toBe(true);
  85. expect(util._detectSupport('acdf').second).toBe(false);
  86. });
  87. it('should detect support for milliseconds', function() {
  88. expect(util._detectSupport('l').millisec).toBe(true);
  89. expect(util._detectSupport('acdf').millisec).toBe(false);
  90. });
  91. it('should detect support for microseconds', function() {
  92. expect(util._detectSupport('c').microsec).toBe(true);
  93. expect(util._detectSupport('asdf').microsec).toBe(false);
  94. });
  95. it('should detect support for AM/PM', function() {
  96. expect(util._detectSupport('h t').ampm).toBe(true);
  97. expect(util._detectSupport('h tt').ampm).toBe(true);
  98. expect(util._detectSupport('h T').ampm).toBe(true);
  99. expect(util._detectSupport('h TT').ampm).toBe(true);
  100. expect(util._detectSupport('t').ampm).toBe(false);
  101. expect(util._detectSupport('h').ampm).toBe(false);
  102. expect(util._detectSupport('H t').ampm).toBe(false);
  103. expect(util._detectSupport('acdf').ampm).toBe(false);
  104. });
  105. it('should detect support for timezone', function() {
  106. expect(util._detectSupport('z').timezone).toBe(true);
  107. expect(util._detectSupport('Z').timezone).toBe(true);
  108. expect(util._detectSupport('acdf').timezone).toBe(false);
  109. });
  110. it('should detect support for iso8601', function() {
  111. expect(util._detectSupport('Z').iso8601).toBe(true);
  112. expect(util._detectSupport('z').iso8601).toBe(false);
  113. expect(util._detectSupport('acdf').iso8601).toBe(false);
  114. });
  115. });
  116. describe('selectLocalTimezone', function() {
  117. var timepicker,
  118. timezoneOffset,
  119. defaultTimezoneOffset;
  120. beforeEach(function() {
  121. timepicker = {
  122. timezone_select: affix('select')
  123. };
  124. var now = new Date();
  125. timezoneOffset = String(-now.getTimezoneOffset());
  126. defaultTimezoneOffset = String(timezoneOffset - 60);
  127. timepicker.timezone_select.affix('option').text(defaultTimezoneOffset);
  128. timepicker.timezone_select.affix('option').text(timezoneOffset);
  129. timepicker.timezone_select.affix('option').text(timezoneOffset + 60);
  130. });
  131. it('should do nothing for a falsey timepicker', function() {
  132. util._selectLocalTimezone(undefined);
  133. expect(timepicker.timezone_select.val()).toBe(defaultTimezoneOffset);
  134. });
  135. it('should do nothing for a timepicker with a falsey timezone_select', function() {
  136. util._selectLocalTimezone({});
  137. expect(timepicker.timezone_select.val()).toBe(defaultTimezoneOffset);
  138. });
  139. it('should select the current timezone with a valid timezone_select and no date', function() {
  140. util._selectLocalTimezone(timepicker);
  141. expect(timepicker.timezone_select.val()).toBe(timezoneOffset);
  142. });
  143. it('should select the current timezone with a valid timezone_select and a date', function() {
  144. util._selectLocalTimezone(timepicker, new Date());
  145. expect(timepicker.timezone_select.val()).toBe(timezoneOffset);
  146. });
  147. });
  148. describe('computeEffectiveSetting', function() {
  149. it('pulls the setting from the passed settings object if it is there', function() {
  150. var expectedUniqueValue = 'This is very unique',
  151. settings = {
  152. property: expectedUniqueValue
  153. };
  154. expect(util._computeEffectiveSetting(settings, 'property')).toBe(expectedUniqueValue);
  155. });
  156. it('pulls the setting from the timepicker defaults if there are no passed settings', function() {
  157. var expectedValue = $.timepicker._defaults.separator;
  158. expect(expectedValue).toBeDefined();
  159. expect(util._computeEffectiveSetting(undefined, 'separator')).toBe(expectedValue);
  160. });
  161. it('pulls the setting from the timepicker defaults if not present in the passed settings', function() {
  162. var expectedValue = $.timepicker._defaults.separator,
  163. settings = {};
  164. expect(expectedValue).toBeDefined();
  165. expect(util._computeEffectiveSetting(settings, 'separator')).toBe(expectedValue);
  166. });
  167. });
  168. describe('splitDateTime', function() {
  169. var expectedDateString = '3/6/1967',
  170. expectedTimeString = '07:32';
  171. it('splits a date and time into its parts using the default separator', function() {
  172. var inputDateTimeString = expectedDateString + $.timepicker._defaults.separator + expectedTimeString,
  173. result;
  174. result = $.timepicker._util._splitDateTime(inputDateTimeString, {});
  175. expect(result).toEqual({dateString: expectedDateString, timeString: expectedTimeString});
  176. });
  177. it('splits a date and time into its parts using a supplied separator', function() {
  178. var separator = '-',
  179. inputDateTimeString = expectedDateString + separator + expectedTimeString,
  180. result;
  181. result = $.timepicker._util._splitDateTime(inputDateTimeString, {separator: separator});
  182. expect(result).toEqual({dateString: expectedDateString, timeString: expectedTimeString});
  183. });
  184. it('splits a date and time into its parts when there are multiple separators in the time format', function() {
  185. var timeFormat = 'hh mm tt',
  186. separator = ' ',
  187. alternateTimeString = '07 32 am',
  188. inputDateTimeString = expectedDateString + separator + alternateTimeString,
  189. timeSettings = {separator: separator, timeFormat: timeFormat},
  190. result;
  191. result = $.timepicker._util._splitDateTime(inputDateTimeString, timeSettings);
  192. expect(result).toEqual({dateString: expectedDateString, timeString: alternateTimeString});
  193. });
  194. it('splits only a date into itself', function() {
  195. var result = $.timepicker._util._splitDateTime(expectedDateString, {});
  196. expect(result).toEqual({dateString: expectedDateString, timeString: ''});
  197. });
  198. });
  199. describe('parseDateTimeInternal', function() {
  200. var dateFormat = 'mm/dd/yy';
  201. it('should return only a date if there is no time component', function() {
  202. var inputDateString = '9/11/2001',
  203. expectedDate = new Date(inputDateString),
  204. result;
  205. result = util._parseDateTimeInternal(dateFormat, undefined, inputDateString, undefined, undefined);
  206. expect(result.date).toEqual(expectedDate);
  207. expect(result.timeObj).toBeUndefined();
  208. });
  209. it('should return a date and a parsed time if a time is included', function() {
  210. var expectedDateString = '7/4/1976',
  211. expectedParsedTime = {
  212. hour: 1,
  213. minute: 23,
  214. second: 45,
  215. millisec: 678,
  216. microsec: 0
  217. },
  218. inputDateTimeString = expectedDateString + ' ' +
  219. expectedParsedTime.hour + ':' +
  220. expectedParsedTime.minute + ':' +
  221. expectedParsedTime.second + '.' +
  222. expectedParsedTime.millisec,
  223. expectedDate = new Date(expectedDateString),
  224. result;
  225. result = util._parseDateTimeInternal(dateFormat, 'H:m:s.l', inputDateTimeString, undefined, undefined);
  226. expect(result.date).toEqual(expectedDate);
  227. expect(result.timeObj).toEqual(expectedParsedTime);
  228. });
  229. it('should throw an exception if it cannot parse the time', function() {
  230. var inputDateString = '4/17/2008 11:22:33';
  231. expect(function() {
  232. util._parseDateTimeInternal(dateFormat, 'q', inputDateString, undefined, undefined);
  233. }).toThrow('Wrong time format');
  234. });
  235. });
  236. });
  237. describe('timepicker functions', function() {
  238. describe('timezoneOffsetNumber', function() {
  239. it('returns 0 if the time zone string is iso8601 Zulu', function() {
  240. expect($.timepicker.timezoneOffsetNumber('Z')).toBe(0);
  241. expect($.timepicker.timezoneOffsetNumber('z')).toBe(0);
  242. expect($.timepicker.timezoneOffsetNumber(':Z')).toBe(0);
  243. });
  244. it('returns a string that does not match the expected representations', function() {
  245. expect($.timepicker.timezoneOffsetNumber('EDT')).toBe('EDT');
  246. expect($.timepicker.timezoneOffsetNumber('1234')).toBe('1234');
  247. expect($.timepicker.timezoneOffsetNumber('+123')).toBe('+123');
  248. expect($.timepicker.timezoneOffsetNumber('-123')).toBe('-123');
  249. expect($.timepicker.timezoneOffsetNumber('abc:def')).toBe('abc:def');
  250. });
  251. it('returns the minute offset from a time zone offset string', function() {
  252. expect($.timepicker.timezoneOffsetNumber('-0000')).toBe(0);
  253. expect($.timepicker.timezoneOffsetNumber('+0000')).toBe(0);
  254. expect($.timepicker.timezoneOffsetNumber('-0400')).toBe(-240);
  255. expect($.timepicker.timezoneOffsetNumber('+0400')).toBe(240);
  256. });
  257. });
  258. describe('timezoneOffsetString', function() {
  259. it('returns NaN if the input is NaN', function() {
  260. expect($.timepicker.timezoneOffsetString(NaN, false)).toBeNaN();
  261. });
  262. it('returns the input if the input is greater than 840 (+14:00)', function() {
  263. var expectedMinutes = 850;
  264. var actualMinutes = $.timepicker.timezoneOffsetString(expectedMinutes, false);
  265. expect(actualMinutes).toBe(expectedMinutes);
  266. });
  267. it('returns the input if the input is less than -720 (-12:00)', function() {
  268. var expectedMinutes = -730;
  269. var actualMinutes = $.timepicker.timezoneOffsetString(expectedMinutes, false);
  270. expect(actualMinutes).toBe(expectedMinutes);
  271. });
  272. it('returns "Z" if the offset is 0 and iso8601 is true', function() {
  273. expect($.timepicker.timezoneOffsetString(0, true)).toBe('Z');
  274. });
  275. it('returns the expected offset string for non-iso8601 values', function() {
  276. expect($.timepicker.timezoneOffsetString(0, false)).toBe('+0000');
  277. expect($.timepicker.timezoneOffsetString(60, false)).toBe('+0100');
  278. expect($.timepicker.timezoneOffsetString(480, false)).toBe('+0800');
  279. expect($.timepicker.timezoneOffsetString(-60, false)).toBe('-0100');
  280. expect($.timepicker.timezoneOffsetString(-480, false)).toBe('-0800');
  281. expect($.timepicker.timezoneOffsetString(-720, false)).toBe('-1200');
  282. expect($.timepicker.timezoneOffsetString(840, false)).toBe('+1400');
  283. });
  284. it('returns the expected offset string for iso8601 values', function() {
  285. expect($.timepicker.timezoneOffsetString(60, true)).toBe('+01:00');
  286. expect($.timepicker.timezoneOffsetString(480, true)).toBe('+08:00');
  287. expect($.timepicker.timezoneOffsetString(-60, true)).toBe('-01:00');
  288. expect($.timepicker.timezoneOffsetString(-480, true)).toBe('-08:00');
  289. expect($.timepicker.timezoneOffsetString(-720, true)).toBe('-12:00');
  290. expect($.timepicker.timezoneOffsetString(840, true)).toBe('+14:00');
  291. });
  292. it('handles abnormal values reasonably', function() {
  293. expect($.timepicker.timezoneOffsetString(null, false)).toBe('+0000');
  294. expect($.timepicker.timezoneOffsetString(null, true)).toBe('Z');
  295. expect($.timepicker.timezoneOffsetString(undefined, false)).toBeUndefined();
  296. expect($.timepicker.timezoneOffsetString(undefined, true)).toBeUndefined();
  297. });
  298. });
  299. describe('timezoneAdjust', function() {
  300. it('does not change the date if the timezone yields NaN for an offset', function() {
  301. var expectedDate = new Date();
  302. expect($.timepicker.timezoneAdjust(expectedDate, NaN)).toEqual(expectedDate);
  303. });
  304. it('changes the minutes by the time zone offset minutes', function() {
  305. var inputDate,
  306. originalMillis,
  307. expectedDifference,
  308. adjustedDate;
  309. inputDate = new Date();
  310. originalMillis = inputDate.getTime();
  311. expectedDifference = -(inputDate.getTimezoneOffset() + 60) * 60 * 1000;
  312. adjustedDate = $.timepicker.timezoneAdjust(inputDate, '+0100');
  313. expect(adjustedDate.getTime() - originalMillis).toBe(expectedDifference);
  314. });
  315. });
  316. describe('log', function() {
  317. it('calls console.log with the message if the console exists', function() {
  318. var expectedMessage = "Just what I expected!";
  319. spyOn(window.console, "log");
  320. $.timepicker.log(expectedMessage);
  321. expect(window.console.log).toHaveBeenCalledWith(expectedMessage);
  322. });
  323. it('does not call console.log if there is no console', function() {
  324. var originalConsole = window.console,
  325. consoleLogSpy = spyOn(window.console, "log");
  326. window.console = undefined;
  327. $.timepicker.log("Don't care");
  328. expect(consoleLogSpy).not.toHaveBeenCalled();
  329. window.console = originalConsole;
  330. });
  331. });
  332. describe('range functions', function() {
  333. var startTime = $('<p>start</p>'),
  334. endTime = $('<p>end</p>'),
  335. options = {};
  336. describe('convenience functions', function() {
  337. beforeEach(function() {
  338. spyOn($.timepicker, 'handleRange');
  339. });
  340. it('timeRange calls handleRange the right way', function() {
  341. $.timepicker.timeRange(startTime, endTime, options);
  342. expect($.timepicker.handleRange).toHaveBeenCalledWith('timepicker', startTime, endTime, options);
  343. });
  344. it('datetimeRange calls handleRange the right way', function() {
  345. $.timepicker.datetimeRange(startTime, endTime, options);
  346. expect($.timepicker.handleRange).toHaveBeenCalledWith('datetimepicker', startTime, endTime, options);
  347. });
  348. it('dateRange calls handleRange the right way', function() {
  349. $.timepicker.dateRange(startTime, endTime, options);
  350. expect($.timepicker.handleRange).toHaveBeenCalledWith('datepicker', startTime, endTime, options);
  351. });
  352. });
  353. xdescribe('handleRange', function() {
  354. // TODO: Difficult to test. Needs attention.
  355. });
  356. });
  357. });
  358. describe('datepicker functions', function() {
  359. describe('formatTime', function() {
  360. describe('single formats, default options', function() {
  361. var emptyTime = {};
  362. describe('hours', function() {
  363. var earlyHour = {hour: 7},
  364. lateHour = {hour: 17};
  365. it('formats HH correctly', function() {
  366. expect($.datepicker.formatTime('HH', emptyTime)).toBe('00');
  367. expect($.datepicker.formatTime('HH', earlyHour)).toBe('07');
  368. expect($.datepicker.formatTime('HH', lateHour)).toBe('17');
  369. });
  370. it('formats H correctly', function() {
  371. expect($.datepicker.formatTime('H', emptyTime)).toBe('0');
  372. expect($.datepicker.formatTime('H', earlyHour)).toBe('7');
  373. expect($.datepicker.formatTime('H', lateHour)).toBe('17');
  374. });
  375. it('formats hh correctly', function() {
  376. expect($.datepicker.formatTime('hh', emptyTime)).toBe('12');
  377. expect($.datepicker.formatTime('hh', earlyHour)).toBe('07');
  378. expect($.datepicker.formatTime('hh', lateHour)).toBe('05');
  379. });
  380. it('formats h correctly', function() {
  381. expect($.datepicker.formatTime('h', emptyTime)).toBe('12');
  382. expect($.datepicker.formatTime('h', earlyHour)).toBe('7');
  383. expect($.datepicker.formatTime('h', lateHour)).toBe('5');
  384. });
  385. });
  386. describe('minutes', function() {
  387. var singleDigitMinute = {minute: 3},
  388. doubleDigitMinute = {minute: 42};
  389. it('formats mm correctly', function() {
  390. expect($.datepicker.formatTime('mm', emptyTime)).toBe('00');
  391. expect($.datepicker.formatTime('mm', singleDigitMinute)).toBe('03');
  392. expect($.datepicker.formatTime('mm', doubleDigitMinute)).toBe('42');
  393. });
  394. it('formats m correctly', function() {
  395. expect($.datepicker.formatTime('m', emptyTime)).toBe('0');
  396. expect($.datepicker.formatTime('m', singleDigitMinute)).toBe('3');
  397. expect($.datepicker.formatTime('m', doubleDigitMinute)).toBe('42');
  398. });
  399. });
  400. describe('seconds', function() {
  401. var singleDigitSecond = {second: 5},
  402. doubleDigitSecond = {second: 31};
  403. it('formats ss correctly', function() {
  404. expect($.datepicker.formatTime('ss', emptyTime)).toBe('00');
  405. expect($.datepicker.formatTime('ss', singleDigitSecond)).toBe('05');
  406. expect($.datepicker.formatTime('ss', doubleDigitSecond)).toBe('31');
  407. });
  408. it('formats s correctly', function() {
  409. expect($.datepicker.formatTime('s', emptyTime)).toBe('0');
  410. expect($.datepicker.formatTime('s', singleDigitSecond)).toBe('5');
  411. expect($.datepicker.formatTime('s', doubleDigitSecond)).toBe('31');
  412. });
  413. });
  414. describe('milliseconds', function() {
  415. it('formats l correctly', function() {
  416. var singleDigitMillis = {millisec: 3},
  417. doubleDigitMillis = {millisec: 17},
  418. tripleDigitMillis = {millisec: 123};
  419. expect($.datepicker.formatTime('l', emptyTime)).toBe('000');
  420. expect($.datepicker.formatTime('l', singleDigitMillis)).toBe('003');
  421. expect($.datepicker.formatTime('l', doubleDigitMillis)).toBe('017');
  422. expect($.datepicker.formatTime('l', tripleDigitMillis)).toBe('123');
  423. });
  424. });
  425. describe('microseconds', function() {
  426. it('formats c correctly', function() {
  427. var singleDigitMicros = {microsec: 3},
  428. doubleDigitMicros = {microsec: 17},
  429. tripleDigitMicros = {microsec: 123};
  430. expect($.datepicker.formatTime('c', emptyTime)).toBe('000');
  431. expect($.datepicker.formatTime('c', singleDigitMicros)).toBe('003');
  432. expect($.datepicker.formatTime('c', doubleDigitMicros)).toBe('017');
  433. expect($.datepicker.formatTime('c', tripleDigitMicros)).toBe('123');
  434. });
  435. });
  436. describe('timezone', function() {
  437. var nullTimezoneTime = {timezone: null},
  438. noTimezoneTime = emptyTime,
  439. timezoneTime = {timezone: -240},
  440. noTimezoneOptions = {},
  441. timezoneOptions = {timezone: 600};
  442. it('handles z correctly', function() {
  443. expect($.datepicker.formatTime('z', timezoneTime, noTimezoneOptions)).toBe('-0400');
  444. expect($.datepicker.formatTime('z', timezoneTime, timezoneOptions)).toBe('-0400');
  445. expect($.datepicker.formatTime('z', nullTimezoneTime, timezoneOptions)).toBe('+1000');
  446. expect($.datepicker.formatTime('z', noTimezoneTime, timezoneOptions)).toBe('+1000');
  447. expect($.datepicker.formatTime('z', nullTimezoneTime, noTimezoneOptions)).toBe('+0000');
  448. expect($.datepicker.formatTime('z', noTimezoneTime, noTimezoneOptions)).toBe('+0000');
  449. });
  450. it('handles Z correctly', function() {
  451. expect($.datepicker.formatTime('Z', timezoneTime, noTimezoneOptions)).toBe('-04:00');
  452. expect($.datepicker.formatTime('Z', timezoneTime, timezoneOptions)).toBe('-04:00');
  453. expect($.datepicker.formatTime('Z', nullTimezoneTime, timezoneOptions)).toBe('+10:00');
  454. expect($.datepicker.formatTime('Z', noTimezoneTime, timezoneOptions)).toBe('+10:00');
  455. expect($.datepicker.formatTime('Z', nullTimezoneTime, noTimezoneOptions)).toBe('Z');
  456. expect($.datepicker.formatTime('Z', noTimezoneTime, noTimezoneOptions)).toBe('Z');
  457. });
  458. });
  459. describe('am/pm', function() {
  460. var morningHour = {hour: 3},
  461. afternoonHour = {hour: 15};
  462. it('formats t correctly', function() {
  463. expect($.datepicker.formatTime('t', emptyTime)).toBe('a');
  464. expect($.datepicker.formatTime('t', morningHour)).toBe('a');
  465. expect($.datepicker.formatTime('t', afternoonHour)).toBe('p');
  466. });
  467. it('formats T correctly', function() {
  468. expect($.datepicker.formatTime('T', emptyTime)).toBe('A');
  469. expect($.datepicker.formatTime('T', morningHour)).toBe('A');
  470. expect($.datepicker.formatTime('T', afternoonHour)).toBe('P');
  471. });
  472. it('formats tt correctly', function() {
  473. expect($.datepicker.formatTime('tt', emptyTime)).toBe('am');
  474. expect($.datepicker.formatTime('tt', morningHour)).toBe('am');
  475. expect($.datepicker.formatTime('tt', afternoonHour)).toBe('pm');
  476. });
  477. it('formats TT correctly', function() {
  478. expect($.datepicker.formatTime('TT', emptyTime)).toBe('AM');
  479. expect($.datepicker.formatTime('TT', morningHour)).toBe('AM');
  480. expect($.datepicker.formatTime('TT', afternoonHour)).toBe('PM');
  481. });
  482. });
  483. describe('literals', function() {
  484. it('handles literals correctly', function() {
  485. expect($.datepicker.formatTime('', emptyTime)).toBe('');
  486. expect($.datepicker.formatTime("'abc'", emptyTime)).toBe('abc');
  487. expect($.datepicker.formatTime("'", emptyTime)).toBe("'");
  488. expect($.datepicker.formatTime("''", emptyTime)).toBe("");
  489. expect($.datepicker.formatTime("'abc' h 'def'", emptyTime)).toBe('abc 12 def');
  490. });
  491. it('does not treat double quotes as literals', function() {
  492. expect($.datepicker.formatTime('"ab"', emptyTime)).toBe('"ab"');
  493. expect($.datepicker.formatTime('"abc"', emptyTime)).toBe('"ab000"');
  494. });
  495. });
  496. });
  497. describe('preserves whitespace in formats', function() {
  498. it('preserves leading whitespace', function() {
  499. expect($.datepicker.formatTime(' H', {hour: 3})).toBe(' 3');
  500. });
  501. it('preserves trailing whitespace', function() {
  502. expect($.datepicker.formatTime('H ', {hour: 3})).toBe('3 ');
  503. });
  504. });
  505. });
  506. });
  507. describe('methods', function() {
  508. describe('setDate', function() {
  509. it('should accept null as date', function() {
  510. var $input = affix('input').datetimepicker();
  511. $input.datetimepicker('setDate', '2013-11-25 15:30:25');
  512. $input.datetimepicker('setDate', null);
  513. expect($input.datetimepicker('getDate')).toBeNull();
  514. });
  515. });
  516. });
  517. describe('altField', function() {
  518. var $input;
  519. var $altField;
  520. var inputFocusSpy;
  521. beforeEach(function() {
  522. $input = affix('input');
  523. $altField = affix('input');
  524. inputFocusSpy = jasmine.createSpy();
  525. $input.focus(inputFocusSpy);
  526. });
  527. it('should redirect focus to main field', function() {
  528. $input.datetimepicker({
  529. showOn: 'button',
  530. altField: $altField
  531. });
  532. $altField.trigger('focus');
  533. expect(inputFocusSpy).toHaveBeenCalled();
  534. });
  535. it('should not redirect focus to main field if altRedirectFocus is false', function() {
  536. $input.datetimepicker({
  537. showOn: 'button',
  538. altField: $altField,
  539. altRedirectFocus: false
  540. });
  541. $altField.trigger('focus');
  542. expect(inputFocusSpy).not.toHaveBeenCalled();
  543. });
  544. });
  545. });