1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
describe('datetimepicker', function() {
describe('utility functions', function() {
var util = $.timepicker._util;
describe('extendRemove', function() {
var target,
props;
beforeEach(function() {
target = {};
props = {};
});
it('should add a nonexistent property to the target', function() {
var expectedValue = "set",
propertyName = "prop";
props[propertyName] = expectedValue;
var newTarget = util._extendRemove(target, props);
expect(target[propertyName]).toBe(expectedValue);
expect(newTarget).toBe(target);
});
it('should change the value of an existing property', function() {
var expectedValue = "new",
originalValue = "old",
propertyName = "prop";
target[propertyName] = originalValue;
props[propertyName] = expectedValue;
util._extendRemove(target, props);
expect(target[propertyName]).not.toBe(originalValue);
expect(target[propertyName]).toBe(expectedValue);
});
it('should null the value of an existing property', function() {
var expectedValue = null,
propertyName = "prop";
target[propertyName] = "original";
props[propertyName] = expectedValue;
util._extendRemove(target, props);
expect(target[propertyName]).toBeNull();
});
});
describe('isEmptyObject', function() {
it('should say an empty object is empty', function() {
expect(util._isEmptyObject({})).toBe(true);
});
it('should say an object with a property is not empty', function() {
var testObject = {"prop": "value"};
expect(util._isEmptyObject(testObject)).toBe(false);
});
it('should say object with a supplemental prototype property is empty', function() {
var testObject = new Function();
testObject.prototype["prop"] = "something";
expect(util._isEmptyObject(testObject)).toBe(true);
})
});
describe('convert24to12', function() {
it('should return the value for a non-zero value less than 12', function() {
var expectedHour = 6;
expect(util._convert24to12(expectedHour)).toBe("" + expectedHour);
});
it('should return 12 hours less if the value is greater than 12 and less than 24', function() {
var expectedHour = 7;
expect(util._convert24to12(expectedHour + 12)).toBe("" + expectedHour);
});
it('should return 12 if the normalized value is 0', function() {
expect(util._convert24to12(0)).toBe('12');
});
it('should normalize values that are clearly out of the expected range', function() {
var expectedValue = 11;
expect(util._convert24to12(expectedValue + 12 * 3)).toBe("" + expectedValue);
})
});
describe('detectSupport', function() {
it('should detect support for hours', function() {
expect(util._detectSupport('H').hour).toBe(true);
expect(util._detectSupport('HH').hour).toBe(true);
expect(util._detectSupport('h').hour).toBe(true);
expect(util._detectSupport('hh').hour).toBe(true);
expect(util._detectSupport('asdf').hour).toBe(false);
});
it('should detect support for minutes', function() {
expect(util._detectSupport('m').minute).toBe(true);
expect(util._detectSupport('mm').minute).toBe(true);
expect(util._detectSupport('asdf').minute).toBe(false);
});
it('should detect support for seconds', function() {
expect(util._detectSupport('s').second).toBe(true);
expect(util._detectSupport('ss').second).toBe(true);
expect(util._detectSupport('acdf').second).toBe(false);
});
it('should detect support for milliseconds', function() {
expect(util._detectSupport('l').millisec).toBe(true);
expect(util._detectSupport('acdf').millisec).toBe(false);
});
it('should detect support for microseconds', function() {
expect(util._detectSupport('c').microsec).toBe(true);
expect(util._detectSupport('asdf').microsec).toBe(false);
});
it('should detect support for AM/PM', function() {
expect(util._detectSupport('h t').ampm).toBe(true);
expect(util._detectSupport('h tt').ampm).toBe(true);
expect(util._detectSupport('h T').ampm).toBe(true);
expect(util._detectSupport('h TT').ampm).toBe(true);
expect(util._detectSupport('t').ampm).toBe(false);
expect(util._detectSupport('h').ampm).toBe(false);
expect(util._detectSupport('H t').ampm).toBe(false);
expect(util._detectSupport('acdf').ampm).toBe(false);
});
it('should detect support for timezone', function() {
expect(util._detectSupport('z').timezone).toBe(true);
expect(util._detectSupport('Z').timezone).toBe(true);
expect(util._detectSupport('acdf').timezone).toBe(false);
});
it('should detect support for iso8601', function() {
expect(util._detectSupport('Z').iso8601).toBe(true);
expect(util._detectSupport('z').iso8601).toBe(false);
expect(util._detectSupport('acdf').iso8601).toBe(false);
});
});
describe('selectLocalTimezone', function() {
var timepicker,
timezoneOffset,
defaultTimezoneOffset;
beforeEach(function() {
timepicker = {
timezone_select: affix('select')
};
var now = new Date();
timezoneOffset = String(-now.getTimezoneOffset());
defaultTimezoneOffset = String(timezoneOffset - 60);
timepicker.timezone_select.affix('option').text(defaultTimezoneOffset);
timepicker.timezone_select.affix('option').text(timezoneOffset);
timepicker.timezone_select.affix('option').text(timezoneOffset + 60);
});
it('should do nothing for a falsey timepicker', function() {
util._selectLocalTimezone(undefined);
expect(timepicker.timezone_select.val()).toBe(defaultTimezoneOffset);
});
it('should do nothing for a timepicker with a falsey timezone_select', function() {
util._selectLocalTimezone({});
expect(timepicker.timezone_select.val()).toBe(defaultTimezoneOffset);
});
it('should select the current timezone with a valid timezone_select and no date', function() {
util._selectLocalTimezone(timepicker);
expect(timepicker.timezone_select.val()).toBe(timezoneOffset);
});
it('should select the current timezone with a valid timezone_select and a date', function() {
util._selectLocalTimezone(timepicker, new Date());
expect(timepicker.timezone_select.val()).toBe(timezoneOffset);
});
});
});
describe('timepicker functions', function() {
describe('timezoneOffsetNumber', function() {
it('returns 0 if the time zone string is iso8601 Zulu', function() {
expect($.timepicker.timezoneOffsetNumber('Z')).toBe(0);
expect($.timepicker.timezoneOffsetNumber('z')).toBe(0);
expect($.timepicker.timezoneOffsetNumber(':Z')).toBe(0);
});
it('returns a string that does not match the expected representations', function() {
expect($.timepicker.timezoneOffsetNumber('EDT')).toBe('EDT');
expect($.timepicker.timezoneOffsetNumber('1234')).toBe('1234');
expect($.timepicker.timezoneOffsetNumber('+123')).toBe('+123');
expect($.timepicker.timezoneOffsetNumber('-123')).toBe('-123');
expect($.timepicker.timezoneOffsetNumber('abc:def')).toBe('abc:def');
});
it('returns the minute offset from a time zone offset string', function() {
expect($.timepicker.timezoneOffsetNumber('-0000')).toBe(0);
expect($.timepicker.timezoneOffsetNumber('+0000')).toBe(0);
expect($.timepicker.timezoneOffsetNumber('-0400')).toBe(-240);
expect($.timepicker.timezoneOffsetNumber('+0400')).toBe(240);
});
});
describe('timezoneOffsetString', function() {
it('returns NaN if the input is NaN', function() {
expect($.timepicker.timezoneOffsetString(NaN)).toBeNaN();
});
it('returns the input if the input is greater than 840 (+14:00)', function() {
var expectedMinutes = 850;
var actualMinutes = $.timepicker.timezoneOffsetString(expectedMinutes);
expect(actualMinutes).toBe(expectedMinutes);
});
it('returns the input if the input is less than -720 (-12:00)', function() {
var expectedMinutes = -730;
var actualMinutes = $.timepicker.timezoneOffsetString(expectedMinutes);
expect(actualMinutes).toBe(expectedMinutes);
});
it('returns "Z" if the offset is 0 and iso8601 is true', function() {
expect($.timepicker.timezoneOffsetString(0, true)).toBe('Z');
});
it('returns the expected offset string for non-iso8601 values', function() {
expect($.timepicker.timezoneOffsetString(0, false)).toBe('+0000');
expect($.timepicker.timezoneOffsetString(60, false)).toBe('+0100');
expect($.timepicker.timezoneOffsetString(480, false)).toBe('+0800');
expect($.timepicker.timezoneOffsetString(-60, false)).toBe('-0100');
expect($.timepicker.timezoneOffsetString(-480, false)).toBe('-0800');
expect($.timepicker.timezoneOffsetString(-720, false)).toBe('-1200');
expect($.timepicker.timezoneOffsetString(840, false)).toBe('+1400');
});
it('returns the expected offset string for iso8601 values', function() {
expect($.timepicker.timezoneOffsetString(60, true)).toBe('+01:00');
expect($.timepicker.timezoneOffsetString(480, true)).toBe('+08:00');
expect($.timepicker.timezoneOffsetString(-60, true)).toBe('-01:00');
expect($.timepicker.timezoneOffsetString(-480, true)).toBe('-08:00');
expect($.timepicker.timezoneOffsetString(-720, true)).toBe('-12:00');
expect($.timepicker.timezoneOffsetString(840, true)).toBe('+14:00');
});
});
describe('timezoneAdjust', function() {
it('does not change the date if the timezone yields NaN for an offset', function() {
var expectedDate = new Date();
expect($.timepicker.timezoneAdjust(expectedDate, NaN)).toEqual(expectedDate);
});
it('changes the minutes by the time zone offset minutes', function() {
var inputDate,
originalMillis,
expectedDifference,
adjustedDate;
inputDate = new Date();
originalMillis = inputDate.getTime();
expectedDifference = -(inputDate.getTimezoneOffset() + 60) * 60 * 1000;
adjustedDate = $.timepicker.timezoneAdjust(inputDate, '+0100');
expect(adjustedDate.getTime() - originalMillis).toBe(expectedDifference);
});
});
describe('log', function() {
it('calls console.log with the message if the console exists', function() {
var expectedMessage = "Just what I expected!";
spyOn(window.console, "log");
$.timepicker.log(expectedMessage);
expect(window.console.log).toHaveBeenCalledWith(expectedMessage);
});
it('does not call console.log if there is no console', function() {
var originalConsole = window.console,
consoleLogSpy = spyOn(window.console, "log");
window.console = undefined;
$.timepicker.log("Don't care");
expect(consoleLogSpy).not.toHaveBeenCalled();
window.console = originalConsole;
});
});
});
});
|