Skip to content

Commit 356868a

Browse files
committed
Start the tests for $.datepicker.formatTime(), fix a bug in it for microsecond handling, and change the method comments to jsdoc.
1 parent b872d74 commit 356868a

File tree

2 files changed

+119
-7
lines changed

2 files changed

+119
-7
lines changed

jquery-ui-timepicker-addon.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,12 +1251,13 @@
12511251
return strictParse(timeFormat, timeString, o);
12521252
};
12531253

1254-
/*
1255-
* Public utility to format the time
1256-
* format = string format of the time
1257-
* time = a {}, not a Date() for timezones
1258-
* options = essentially the regional[].. amNames, pmNames, ampm
1259-
*/
1254+
/**
1255+
* Public utility to format the time
1256+
* @param {string} format format of the time
1257+
* @param {Object} time Object not a Date for timezones
1258+
* @param {Object} options essentially the regional[].. amNames, pmNames, ampm
1259+
* @returns {string} the formatted time
1260+
*/
12601261
$.datepicker.formatTime = function(format, time, options) {
12611262
options = options || {};
12621263
options = $.extend({}, $.timepicker._defaults, options);
@@ -1265,6 +1266,7 @@
12651266
minute: 0,
12661267
second: 0,
12671268
millisec: 0,
1269+
microsec: 0,
12681270
timezone: 0
12691271
}, time);
12701272

test/jquery-ui-timepicker-addon_spec.js

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ describe('datetimepicker', function() {
8787
var expectedValue = 11;
8888

8989
expect(util._convert24to12(expectedValue + 12 * 3)).toBe("" + expectedValue);
90-
})
90+
});
9191
});
9292

9393
describe('detectSupport', function() {
@@ -461,4 +461,114 @@ describe('datetimepicker', function() {
461461
});
462462
});
463463
});
464+
465+
describe('datepicker functions', function() {
466+
describe('formatTime', function() {
467+
describe('single formats, default options', function() {
468+
var emptyTime = {};
469+
470+
describe('hours', function() {
471+
var earlyHour = {hour: 7},
472+
lateHour = {hour: 17};
473+
474+
it('formats HH correctly', function() {
475+
expect($.datepicker.formatTime('HH', emptyTime)).toBe('00');
476+
expect($.datepicker.formatTime('HH', earlyHour)).toBe('07');
477+
expect($.datepicker.formatTime('HH', lateHour)).toBe('17');
478+
});
479+
480+
it('formats H correctly', function() {
481+
expect($.datepicker.formatTime('H', emptyTime)).toBe('0');
482+
expect($.datepicker.formatTime('H', earlyHour)).toBe('7');
483+
expect($.datepicker.formatTime('H', lateHour)).toBe('17');
484+
});
485+
486+
it('formats hh correctly', function() {
487+
expect($.datepicker.formatTime('hh', emptyTime)).toBe('12');
488+
expect($.datepicker.formatTime('hh', earlyHour)).toBe('07');
489+
expect($.datepicker.formatTime('hh', lateHour)).toBe('05');
490+
});
491+
492+
it('formats h correctly', function() {
493+
expect($.datepicker.formatTime('h', emptyTime)).toBe('12');
494+
expect($.datepicker.formatTime('h', earlyHour)).toBe('7');
495+
expect($.datepicker.formatTime('h', lateHour)).toBe('5');
496+
});
497+
});
498+
499+
describe('minutes', function() {
500+
var singleDigitMinute = {minute: 3},
501+
doubleDigitMinute = {minute: 42};
502+
503+
it('formats mm correctly', function() {
504+
expect($.datepicker.formatTime('mm', emptyTime)).toBe('00');
505+
expect($.datepicker.formatTime('mm', singleDigitMinute)).toBe('03');
506+
expect($.datepicker.formatTime('mm', doubleDigitMinute)).toBe('42');
507+
});
508+
509+
it('formats m correctly', function() {
510+
expect($.datepicker.formatTime('m', emptyTime)).toBe('0');
511+
expect($.datepicker.formatTime('m', singleDigitMinute)).toBe('3');
512+
expect($.datepicker.formatTime('m', doubleDigitMinute)).toBe('42');
513+
});
514+
});
515+
516+
describe('seconds', function() {
517+
var singleDigitSecond = {second: 5},
518+
doubleDigitSecond = {second: 31};
519+
520+
it('formats ss correctly', function() {
521+
expect($.datepicker.formatTime('ss', emptyTime)).toBe('00');
522+
expect($.datepicker.formatTime('ss', singleDigitSecond)).toBe('05');
523+
expect($.datepicker.formatTime('ss', doubleDigitSecond)).toBe('31');
524+
});
525+
526+
it('formats s correctly', function() {
527+
expect($.datepicker.formatTime('s', emptyTime)).toBe('0');
528+
expect($.datepicker.formatTime('s', singleDigitSecond)).toBe('5');
529+
expect($.datepicker.formatTime('s', doubleDigitSecond)).toBe('31');
530+
});
531+
});
532+
533+
describe('milliseconds', function() {
534+
it('formats l correctly', function() {
535+
var singleDigitMillis = {millisec: 3},
536+
doubleDigitMillis = {millisec: 17},
537+
tripleDigitMillis = {millisec: 123};
538+
539+
expect($.datepicker.formatTime('l', emptyTime)).toBe('000');
540+
expect($.datepicker.formatTime('l', singleDigitMillis)).toBe('003');
541+
expect($.datepicker.formatTime('l', doubleDigitMillis)).toBe('017');
542+
expect($.datepicker.formatTime('l', tripleDigitMillis)).toBe('123');
543+
});
544+
});
545+
546+
// TODO: Should microseconds be three digits or six? Does this complement millis or replace? The regexp looks like it replaces.
547+
describe('microseconds', function() {
548+
it('formats c correctly', function() {
549+
var singleDigitMillis = {microsec: 3},
550+
doubleDigitMillis = {microsec: 17},
551+
tripleDigitMillis = {microsec: 123};
552+
553+
expect($.datepicker.formatTime('c', emptyTime)).toBe('000');
554+
expect($.datepicker.formatTime('c', singleDigitMillis)).toBe('003');
555+
expect($.datepicker.formatTime('c', doubleDigitMillis)).toBe('017');
556+
expect($.datepicker.formatTime('c', tripleDigitMillis)).toBe('123');
557+
});
558+
});
559+
560+
describe('timezone', function() {
561+
// TODO: Finish
562+
});
563+
564+
describe('am/pm', function() {
565+
// TODO: Finish
566+
});
567+
568+
describe('other', function() {
569+
// TODO: Finish
570+
});
571+
});
572+
});
573+
});
464574
});

0 commit comments

Comments
 (0)