summaryrefslogtreecommitdiffstats
path: root/test/jquery-ui-timepicker-addon_spec.js
blob: 70f44b227692cfc6bb7bffab69bff5c0ddc072d9 (plain)
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
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);
			});
		});
	});
});