This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
dateUtil.spec.js
500 lines (410 loc) · 18.8 KB
/
dateUtil.spec.js
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
describe('$$mdDateUtil', function() {
// When constructing a Date, the month is zero-based. This can be confusing, since people are
// used to seeing them one-based. So we create these aliases to make reading the tests easier.
var JAN = 0, FEB = 1, MAR = 2, APR = 3, MAY = 4, JUN = 5, JUL = 6, AUG = 7, SEP = 8, OCT = 9,
NOV = 10, DEC = 11;
var dateUtil;
beforeEach(module('material.components.datepicker'));
beforeEach(inject(function($$mdDateUtil) {
dateUtil = $$mdDateUtil;
}));
it('should get the first day of a month', function() {
var first = dateUtil.getFirstDateOfMonth(new Date(1985, OCT, 26));
expect(first.getFullYear()).toBe(1985);
expect(first.getMonth()).toBe(9);
expect(first.getDate()).toBe(1);
});
it('should get the first day of the month from the first day of the month', function() {
var first = dateUtil.getFirstDateOfMonth(new Date(1985, OCT, 1));
expect(first.getFullYear()).toBe(1985);
expect(first.getMonth()).toBe(9);
expect(first.getDate()).toBe(1);
});
it('should get the number of days in a month', function() {
// Month with 31 days.
expect(dateUtil.getNumberOfDaysInMonth(new Date(2015, JAN, 1))).toBe(31);
// Month with 30 days.
expect(dateUtil.getNumberOfDaysInMonth(new Date(2015, APR, 1))).toBe(30);
// Month with 28 days
expect(dateUtil.getNumberOfDaysInMonth(new Date(2015, FEB, 1))).toBe(28);
// Month with 29 days.
expect(dateUtil.getNumberOfDaysInMonth(new Date(2012, FEB, 1))).toBe(29);
});
it('should get an arbitrary day in the next month', function() {
// Next month in the same year.
var next = dateUtil.getDateInNextMonth(new Date(2015, JAN, 1));
expect(next.getMonth()).toBe(1);
expect(next.getFullYear()).toBe(2015);
// Next month in the following year.
next = dateUtil.getDateInNextMonth(new Date(2015, DEC, 1));
expect(next.getMonth()).toBe(0);
expect(next.getFullYear()).toBe(2016);
});
it('should get an arbitrary day in the previous month', function() {
// Previous month in the same year.
var next = dateUtil.getDateInPreviousMonth(new Date(2015, JUL, 1));
expect(next.getMonth()).toBe(5);
expect(next.getFullYear()).toBe(2015);
// Previous month in the past year.
next = dateUtil.getDateInPreviousMonth(new Date(2015, JAN, 1));
expect(next.getMonth()).toBe(11);
expect(next.getFullYear()).toBe(2014);
});
it('should check whether two dates are in the same month and year', function() {
// Same month and year.
var first = new Date(2015, APR, 30);
var second = new Date(2015, APR, 1);
expect(dateUtil.isSameMonthAndYear(first, second)).toBe(true);
// Same exact day.
first = new Date(2015, APR, 1);
second = new Date(2015, APR, 1);
expect(dateUtil.isSameMonthAndYear(first, second)).toBe(true);
// Same month, different year.
first = new Date(2015, APR, 30);
second = new Date(2005, APR, 1);
expect(dateUtil.isSameMonthAndYear(first, second)).toBe(false);
// Same year, different month.
first = new Date(2015, APR, 30);
second = new Date(2015, JUL, 1);
expect(dateUtil.isSameMonthAndYear(first, second)).toBe(false);
// Different month and year.
first = new Date(2012, APR, 30);
second = new Date(2015, JUL, 1);
expect(dateUtil.isSameMonthAndYear(first, second)).toBe(false);
});
it('should check whether two dates are the same day', function() {
// Same exact day and time.
var first = new Date(2015, APR, 1);
var second = new Date(2015, APR, 1);
expect(dateUtil.isSameDay(first, second)).toBe(true);
// Same day, different time.
first = new Date(2015, APR, 30, 3);
second = new Date(2015, APR, 30, 4);
expect(dateUtil.isSameDay(first, second)).toBe(true);
// Same month and year, different day.
first = new Date(2015, APR, 30);
second = new Date(2015, APR, 1);
expect(dateUtil.isSameDay(first, second)).toBe(false);
// Same month, different year.
first = new Date(2015, APR, 30);
second = new Date(2005, APR, 30);
expect(dateUtil.isSameDay(first, second)).toBe(false);
// Same year, different month.
first = new Date(2015, APR, 30);
second = new Date(2015, JUL, 30);
expect(dateUtil.isSameDay(first, second)).toBe(false);
// Different month and year.
first = new Date(2012, APR, 30);
second = new Date(2015, JUL, 30);
expect(dateUtil.isSameDay(first, second)).toBe(false);
});
it('should check whether a date is in the next month', function() {
// Next month within the same year.
var first = new Date(2015, JUL, 15);
var second = new Date(2015, AUG, 25);
expect(dateUtil.isInNextMonth(first, second)).toBe(true);
// Next month across years.
first = new Date(2015, DEC, 15);
second = new Date(2016, JAN, 25);
expect(dateUtil.isInNextMonth(first, second)).toBe(true);
// Not in the next month (past, same year).
first = new Date(2015, JUN, 15);
second = new Date(2015, APR, 25);
expect(dateUtil.isInNextMonth(first, second)).toBe(false);
// Not in the next month (future, same year).
first = new Date(2015, JUN, 15);
second = new Date(2015, AUG, 25);
expect(dateUtil.isInNextMonth(first, second)).toBe(false);
// Not in the next month (month + 1 in different year).
first = new Date(2015, JUN, 15);
second = new Date(2016, JUL, 25);
expect(dateUtil.isInNextMonth(first, second)).toBe(false);
});
it('should check whether a date is in the previous month', function() {
// Previous month within the same year.
var first = new Date(2015, AUG, 15);
var second = new Date(2015, JUL, 25);
expect(dateUtil.isInPreviousMonth(first, second)).toBe(true);
// Previous month across years.
first = new Date(2015, JAN, 15);
second = new Date(2014, DEC, 25);
expect(dateUtil.isInPreviousMonth(first, second)).toBe(true);
// Not in the previous month (past, same year).
first = new Date(2015, JUN, 15);
second = new Date(2015, APR, 25);
expect(dateUtil.isInPreviousMonth(first, second)).toBe(false);
// Not in the previous month (future, same year).
first = new Date(2015, JUN, 15);
second = new Date(2015, AUG, 25);
expect(dateUtil.isInPreviousMonth(first, second)).toBe(false);
// Not in the previous month (month - 1 in different year).
first = new Date(2015, JUN, 15);
second = new Date(2016, MAY, 25);
expect(dateUtil.isInPreviousMonth(first, second)).toBe(false);
});
it('should get the midpoint between two dates', function() {
var start = new Date(2010, MAR, 10);
var end = new Date(2010, MAR, 20);
var midpoint = dateUtil.getDateMidpoint(start, end);
expect(midpoint.getTime()).toEqual(new Date(2010, MAR, 15).getTime());
});
it('should get the week of the month in which a given date appears', function() {
// May 2015 spans 6 weeks.
expect(dateUtil.getWeekOfMonth(new Date(2015, MAY, 1))).toBe(0);
expect(dateUtil.getWeekOfMonth(new Date(2015, MAY, 8))).toBe(1);
expect(dateUtil.getWeekOfMonth(new Date(2015, MAY, 15))).toBe(2);
expect(dateUtil.getWeekOfMonth(new Date(2015, MAY, 22))).toBe(3);
expect(dateUtil.getWeekOfMonth(new Date(2015, MAY, 29))).toBe(4);
expect(dateUtil.getWeekOfMonth(new Date(2015, MAY, 31))).toBe(5);
// Feb 2015 spans 4 weeks. Check both the first and last day of each week.
expect(dateUtil.getWeekOfMonth(new Date(2015, FEB, 1))).toBe(0);
expect(dateUtil.getWeekOfMonth(new Date(2015, FEB, 7))).toBe(0);
expect(dateUtil.getWeekOfMonth(new Date(2015, FEB, 8))).toBe(1);
expect(dateUtil.getWeekOfMonth(new Date(2015, FEB, 14))).toBe(1);
expect(dateUtil.getWeekOfMonth(new Date(2015, FEB, 15))).toBe(2);
expect(dateUtil.getWeekOfMonth(new Date(2015, FEB, 21))).toBe(2);
expect(dateUtil.getWeekOfMonth(new Date(2015, FEB, 22))).toBe(3);
expect(dateUtil.getWeekOfMonth(new Date(2015, FEB, 28))).toBe(3);
});
it('should increment a date by a number of days', function() {
// Increment by one.
var start = new Date(2015, MAY, 15);
var end = new Date(2015, MAY, 16);
expect(dateUtil.isSameDay(dateUtil.incrementDays(start, 1), end)).toBe(true);
// Negative by negative one.
start = new Date(2015, MAY, 15);
end = new Date(2015, MAY, 14);
expect(dateUtil.isSameDay(dateUtil.incrementDays(start, -1), end)).toBe(true);
// Into next month.
start = new Date(2015, MAY, 31);
end = new Date(2015, JUN, 1);
expect(dateUtil.isSameDay(dateUtil.incrementDays(start, 1), end)).toBe(true);
// Into previous month.
start = new Date(2015, MAY, 1);
end = new Date(2015, APR, 30);
expect(dateUtil.isSameDay(dateUtil.incrementDays(start, -1), end)).toBe(true);
// Into next year.
start = new Date(2015, DEC, 31);
end = new Date(2016, JAN, 1);
expect(dateUtil.isSameDay(dateUtil.incrementDays(start, 1), end)).toBe(true);
// Into last year.
start = new Date(2015, JAN, 1);
end = new Date(2014, DEC, 31);
expect(dateUtil.isSameDay(dateUtil.incrementDays(start, -1), end)).toBe(true);
});
it('should increment a date by a number of months', function() {
// Increment by one.
var start = new Date(2015, MAY, 15);
var end = new Date(2015, JUN, 15);
expect(dateUtil.isSameDay(dateUtil.incrementMonths(start, 1), end)).toBe(true);
// Negative by negative one.
start = new Date(2015, MAY, 15);
end = new Date(2015, APR, 15);
expect(dateUtil.isSameDay(dateUtil.incrementMonths(start, -1), end)).toBe(true);
// Next month has fewer days.
start = new Date(2015, JAN, 30);
end = new Date(2015, FEB, 28);
expect(dateUtil.isSameDay(dateUtil.incrementMonths(start, 1), end)).toBe(true);
// Previous month has fewer days.
start = new Date(2015, MAY, 31);
end = new Date(2015, APR, 30);
expect(dateUtil.isSameDay(dateUtil.incrementMonths(start, -1), end)).toBe(true);
});
it('should get the last date of a month', function() {
// Normal February
var date = new Date(2015, FEB, 1);
var lastOfMonth = new Date(2015, FEB, 28);
expect(dateUtil.isSameDay(dateUtil.getLastDateOfMonth(date), lastOfMonth)).toBe(true);
// Leap year February
date = new Date(2012, FEB, 1);
lastOfMonth = new Date(2012, FEB, 29);
expect(dateUtil.isSameDay(dateUtil.getLastDateOfMonth(date), lastOfMonth)).toBe(true);
// Month with 31 days.
date = new Date(2015, DEC, 12);
lastOfMonth = new Date(2015, DEC, 31);
expect(dateUtil.isSameDay(dateUtil.getLastDateOfMonth(date), lastOfMonth)).toBe(true);
// Month with 30 days.
date = new Date(2015, APR, 3);
lastOfMonth = new Date(2015, APR, 30);
expect(dateUtil.isSameDay(dateUtil.getLastDateOfMonth(date), lastOfMonth)).toBe(true);
});
it('should create a date at midnight today', function() {
var today = new Date();
var todayAtMidnight = dateUtil.createDateAtMidnight();
expect(dateUtil.isSameDay(todayAtMidnight, today)).toBe(true);
expect(todayAtMidnight.getHours()).toBe(0);
expect(todayAtMidnight.getMinutes()).toBe(0);
expect(todayAtMidnight.getSeconds()).toBe(0);
expect(todayAtMidnight.getMilliseconds()).toBe(0);
});
it('should create a date at midnight the day of a timestamp', function() {
var day = new Date(2015, JUN, 1, 12, 30);
var dayAtMidnight = dateUtil.createDateAtMidnight(day.getTime());
expect(dateUtil.isSameDay(dayAtMidnight, day)).toBe(true);
expect(dayAtMidnight.getHours()).toBe(0);
expect(dayAtMidnight.getMinutes()).toBe(0);
expect(dayAtMidnight.getSeconds()).toBe(0);
expect(dayAtMidnight.getMilliseconds()).toBe(0);
});
it('should not error when trying to set an invalid date to midnight', function() {
dateUtil.setDateTimeToMidnight(new Date(NaN));
dateUtil.setDateTimeToMidnight(null);
dateUtil.setDateTimeToMidnight(undefined);
});
it('should determine whether dates are valid', function() {
expect(dateUtil.isValidDate(null)).toBeFalsy();
expect(dateUtil.isValidDate(undefined)).toBeFalsy();
expect(dateUtil.isValidDate('')).toBeFalsy();
expect(dateUtil.isValidDate(0)).toBeFalsy();
expect(dateUtil.isValidDate(NaN)).toBeFalsy();
expect(dateUtil.isValidDate(123456789)).toBeFalsy();
expect(dateUtil.isValidDate('123456789')).toBeFalsy();
expect(dateUtil.isValidDate(new Date(''))).toBeFalsy();
expect(dateUtil.isValidDate(new Date('Banjo'))).toBeFalsy();
expect(dateUtil.isValidDate(new Date(NaN))).toBeFalsy();
expect(dateUtil.isValidDate(new Date())).toBe(true);
});
it('should return true when a date is in range', function() {
var date = new Date(2015, JUN, 2);
var minDate = new Date(2015, JUN, 1);
var maxDate = new Date(2015, JUN, 3);
expect(dateUtil.isDateWithinRange(date, minDate, maxDate)).toBeTruthy();
});
it('should return false when a date is before the range', function() {
var date = new Date(2015, MAY, 29);
var minDate = new Date(2015, JUN, 1);
var maxDate = new Date(2015, JUN, 3);
expect(dateUtil.isDateWithinRange(date, minDate, maxDate)).toBeFalsy();
});
it('should return false when a date is after the range', function() {
var date = new Date(2015, JUN, 5);
var minDate = new Date(2015, JUN, 1);
var maxDate = new Date(2015, JUN, 3);
expect(dateUtil.isDateWithinRange(date, minDate, maxDate)).toBeFalsy();
});
it('should set the time to midnight before checking the min date', function() {
var date = new Date(2015, JUN, 1, 11, 0, 0);
var minDate = new Date(2015, JUN, 1, 0, 0, 0);
var maxDate = new Date(2015, JUN, 3);
expect(dateUtil.isDateWithinRange(date, minDate, maxDate)).toBeTruthy();
});
it('should set the time to midnight before checking the max date', function() {
var date = new Date(2015, JUN, 3, 13, 0, 0);
var minDate = new Date(2015, 5, 1);
var maxDate = new Date(2015, JUN, 3, 12, 0, 0);
expect(dateUtil.isDateWithinRange(date, minDate, maxDate)).toBeTruthy();
});
it('should ignore an invalid minDate when checking if the date is in range', function() {
var date = new Date(2015, JUN, 2);
var minDate = null;
var maxDate = new Date(2015, JUN, 3);
expect(dateUtil.isDateWithinRange(date, minDate, maxDate)).toBeTruthy();
});
it('should ignore an invalid maxDate when checking if the date is in range', function() {
var date = new Date(2015, JUN, 2);
var minDate = new Date(2015, JUN, 1);
var maxDate = null;
expect(dateUtil.isDateWithinRange(date, minDate, maxDate)).toBeTruthy();
});
it('should increment a date by a number of years', function() {
// Increment by one.
var start = new Date(2015, MAY, 15);
var end = new Date(2016, MAY, 15);
expect(dateUtil.isSameDay(dateUtil.incrementYears(start, 1), end)).toBe(true);
// Negative by negative one.
start = new Date(2015, MAY, 15);
end = new Date(2014, MAY, 15);
expect(dateUtil.isSameDay(dateUtil.incrementYears(start, -1), end)).toBe(true);
});
it('should get the distance between years', function() {
// In the future
var start = new Date(2016, JAN, 15);
var end = new Date(2017, JUN, 15);
expect(dateUtil.getYearDistance(start, end)).toBe(1);
// In the past
start = new Date(2016, JAN, 15);
end = new Date(2014, JUN, 15);
expect(dateUtil.getYearDistance(start, end)).toBe(-2);
});
it('should limit a date between a minimum and a maximum', function() {
var min = new Date(2016, MAY, 1);
var max = new Date(2016, JUN, 1);
// Before the minimum
var target = new Date(2016, APR, 1);
expect(dateUtil.isSameDay(dateUtil.clampDate(target, min, max), min)).toBe(true);
// After the maximum
target = new Date(2016, AUG, 1);
expect(dateUtil.isSameDay(dateUtil.clampDate(target, min, max), max)).toBe(true);
// Within range
target = new Date(2016, MAY, 15);
expect(dateUtil.clampDate(target, min, max)).toBe(target);
});
it('should parse the timestamp from a DOM node', function() {
var node = document.createElement('td');
// With no arguments
expect(function() {
dateUtil.getTimestampFromNode();
}).not.toThrow();
// Without a timestamp
expect(dateUtil.getTimestampFromNode(node)).toBeFalsy();
// With a timestamp
var time = new Date().getTime();
node.setAttribute('data-timestamp', time);
var result = dateUtil.getTimestampFromNode(node);
expect(angular.isNumber(result)).toBe(true);
expect(result).toBe(time);
node = null;
});
describe('isMonthWithinRange method', function() {
it('should return true when a month is in range', function() {
var date = new Date(2015, JUN, 1);
var minDate = new Date(2015, MAY, 1);
var maxDate = new Date(2015, JUL, 1);
expect(dateUtil.isMonthWithinRange(date, minDate, maxDate)).toBeTruthy();
});
it('should return false when a month is before the range', function() {
var date = new Date(2015, MAY, 1);
var minDate = new Date(2015, JUN, 1);
var maxDate = new Date(2015, JUL, 1);
expect(dateUtil.isMonthWithinRange(date, minDate, maxDate)).toBeFalsy();
});
it('should return false when a month is after the range', function() {
var date = new Date(2015, AUG, 1);
var minDate = new Date(2015, JUN, 1);
var maxDate = new Date(2015, JUL, 1);
expect(dateUtil.isMonthWithinRange(date, minDate, maxDate)).toBeFalsy();
});
it('should ignore an invalid minDate when checking if the month is in range', function() {
var date = new Date(2015, JUN, 1);
var minDate = null;
var maxDate = new Date(2015, JUL, 1);
expect(dateUtil.isMonthWithinRange(date, minDate, maxDate)).toBeTruthy();
});
it('should ignore an invalid maxDate when checking if the month is in range', function() {
var date = new Date(2015, JUN, 1);
var minDate = new Date(2015, JUN, 1);
var maxDate = null;
expect(dateUtil.isMonthWithinRange(date, minDate, maxDate)).toBeTruthy();
});
it('should take the year into account when comparing with the min date', function() {
var date = new Date(2015, MAR, 1);
var minDate = new Date(2014, JUN, 1);
expect(dateUtil.isMonthWithinRange(date, minDate)).toBeTruthy();
});
it('should take the year into account when comparing with the max date', function() {
var date = new Date(2015, JUL, 1);
var maxDate = new Date(2016, FEB, 1);
expect(dateUtil.isMonthWithinRange(date, null, maxDate)).toBeTruthy();
});
it('should return true, even though parts of the month are before the minDate', function() {
var date = new Date(2015, MAY, 1);
var minDate = new Date(2015, MAY, 20);
expect(dateUtil.isMonthWithinRange(date, minDate)).toBeTruthy();
});
it('should return true, even though parts of the month are after the maxDate', function() {
var date = new Date(2015, JUN, 20);
var maxDate = new Date(2015, JUN, 1);
expect(dateUtil.isMonthWithinRange(date, null, maxDate)).toBeTruthy();
});
});
});