Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 8443fd5

Browse files
committed
fix(calendar): add remaining missing tests for dateUtil
1 parent b158d15 commit 8443fd5

File tree

1 file changed

+144
-55
lines changed

1 file changed

+144
-55
lines changed
+144-55
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11

22
describe('$$mdDateUtil', function() {
3+
// When constructing a Date, the month is zero-based. This can be confusing, since people are
4+
// used to seeing them one-based. So we create these aliases to make reading the tests easier.
5+
var JAN = 0, FEB = 1, MAR = 2, APR = 3, MAY = 4, JUN = 5, JUL = 6, AUG = 7, SEP = 8, OCT = 9,
6+
NOV = 10, DEC = 11;
7+
38
var dateUtil;
49

510
beforeEach(module('material.components.calendar'));
@@ -9,15 +14,15 @@ describe('$$mdDateUtil', function() {
914
}));
1015

1116
it('should get the first day of a month', function() {
12-
var first = dateUtil.getFirstDateOfMonth(new Date(1985, 9, 26));
17+
var first = dateUtil.getFirstDateOfMonth(new Date(1985, OCT, 26));
1318

1419
expect(first.getFullYear()).toBe(1985);
1520
expect(first.getMonth()).toBe(9);
1621
expect(first.getDate()).toBe(1);
1722
});
1823

1924
it('should get the first day of the month from the first day of the month', function() {
20-
var first = dateUtil.getFirstDateOfMonth(new Date(1985, 9, 1));
25+
var first = dateUtil.getFirstDateOfMonth(new Date(1985, OCT, 1));
2126

2227
expect(first.getFullYear()).toBe(1985);
2328
expect(first.getMonth()).toBe(9);
@@ -26,172 +31,256 @@ describe('$$mdDateUtil', function() {
2631

2732
it('should get the number of days in a month', function() {
2833
// Month with 31 days.
29-
expect(dateUtil.getNumberOfDaysInMonth(new Date(2015, 0, 1))).toBe(31);
34+
expect(dateUtil.getNumberOfDaysInMonth(new Date(2015, JAN, 1))).toBe(31);
3035

3136
// Month with 30 days.
32-
expect(dateUtil.getNumberOfDaysInMonth(new Date(2015, 3, 1))).toBe(30);
37+
expect(dateUtil.getNumberOfDaysInMonth(new Date(2015, APR, 1))).toBe(30);
3338

3439
// Month with 28 days
35-
expect(dateUtil.getNumberOfDaysInMonth(new Date(2015, 1, 1))).toBe(28);
40+
expect(dateUtil.getNumberOfDaysInMonth(new Date(2015, FEB, 1))).toBe(28);
3641

3742
// Month with 29 days.
38-
expect(dateUtil.getNumberOfDaysInMonth(new Date(2012, 1, 1))).toBe(29);
43+
expect(dateUtil.getNumberOfDaysInMonth(new Date(2012, FEB, 1))).toBe(29);
3944
});
4045

4146
it('should get an arbitrary day in the next month', function() {
4247
// Next month in the same year.
43-
var next = dateUtil.getDateInNextMonth(new Date(2015, 0, 1));
48+
var next = dateUtil.getDateInNextMonth(new Date(2015, JAN, 1));
4449
expect(next.getMonth()).toBe(1);
4550
expect(next.getFullYear()).toBe(2015);
4651

4752
// Next month in the following year.
48-
next = dateUtil.getDateInNextMonth(new Date(2015, 11, 1));
53+
next = dateUtil.getDateInNextMonth(new Date(2015, DEC, 1));
4954
expect(next.getMonth()).toBe(0);
5055
expect(next.getFullYear()).toBe(2016);
5156
});
5257

5358
it('should get an arbitrary day in the previous month', function() {
5459
// Previous month in the same year.
55-
var next = dateUtil.getDateInPreviousMonth(new Date(2015, 6, 1));
60+
var next = dateUtil.getDateInPreviousMonth(new Date(2015, JUL, 1));
5661
expect(next.getMonth()).toBe(5);
5762
expect(next.getFullYear()).toBe(2015);
5863

5964
// Previous month in the past year.
60-
next = dateUtil.getDateInPreviousMonth(new Date(2015, 0, 1));
65+
next = dateUtil.getDateInPreviousMonth(new Date(2015, JAN, 1));
6166
expect(next.getMonth()).toBe(11);
6267
expect(next.getFullYear()).toBe(2014);
6368
});
6469

6570
it('should check whether two dates are in the same month and year', function() {
6671
// Same month and year.
67-
var first = new Date(2015, 3, 30);
68-
var second = new Date(2015, 3, 1);
72+
var first = new Date(2015, APR, 30);
73+
var second = new Date(2015, APR, 1);
6974
expect(dateUtil.isSameMonthAndYear(first, second)).toBe(true);
7075

7176
// Same exact day.
72-
first = new Date(2015, 3, 1);
73-
second = new Date(2015, 3, 1);
77+
first = new Date(2015, APR, 1);
78+
second = new Date(2015, APR, 1);
7479
expect(dateUtil.isSameMonthAndYear(first, second)).toBe(true);
7580

7681
// Same month, different year.
77-
first = new Date(2015, 3, 30);
78-
second = new Date(2005, 3, 1);
82+
first = new Date(2015, APR, 30);
83+
second = new Date(2005, APR, 1);
7984
expect(dateUtil.isSameMonthAndYear(first, second)).toBe(false);
8085

8186
// Same year, different month.
82-
first = new Date(2015, 3, 30);
83-
second = new Date(2015, 6, 1);
87+
first = new Date(2015, APR, 30);
88+
second = new Date(2015, JUL, 1);
8489
expect(dateUtil.isSameMonthAndYear(first, second)).toBe(false);
8590

8691
// Different month and year.
87-
first = new Date(2012, 3, 30);
88-
second = new Date(2015, 6, 1);
92+
first = new Date(2012, APR, 30);
93+
second = new Date(2015, JUL, 1);
8994
expect(dateUtil.isSameMonthAndYear(first, second)).toBe(false);
9095
});
9196

9297
it('should check whether two dates are the same day', function() {
9398
// Same exact day and time.
94-
var first = new Date(2015, 3, 1);
95-
var second = new Date(2015, 3, 1);
99+
var first = new Date(2015, APR, 1);
100+
var second = new Date(2015, APR, 1);
96101
expect(dateUtil.isSameDay(first, second)).toBe(true);
97102

98103
// Same day, different time.
99-
first = new Date(2015, 3, 30, 3);
100-
second = new Date(2015, 3, 30, 4);
104+
first = new Date(2015, APR, 30, 3);
105+
second = new Date(2015, APR, 30, 4);
101106
expect(dateUtil.isSameDay(first, second)).toBe(true);
102107

103108
// Same month and year, different day.
104-
first = new Date(2015, 3, 30);
105-
second = new Date(2015, 3, 1);
109+
first = new Date(2015, APR, 30);
110+
second = new Date(2015, APR, 1);
106111
expect(dateUtil.isSameDay(first, second)).toBe(false);
107112

108113
// Same month, different year.
109-
first = new Date(2015, 3, 30);
110-
second = new Date(2005, 3, 30);
114+
first = new Date(2015, APR, 30);
115+
second = new Date(2005, APR, 30);
111116
expect(dateUtil.isSameDay(first, second)).toBe(false);
112117

113118
// Same year, different month.
114-
first = new Date(2015, 3, 30);
115-
second = new Date(2015, 6, 30);
119+
first = new Date(2015, APR, 30);
120+
second = new Date(2015, JUL, 30);
116121
expect(dateUtil.isSameDay(first, second)).toBe(false);
117122

118123
// Different month and year.
119-
first = new Date(2012, 3, 30);
120-
second = new Date(2015, 6, 30);
124+
first = new Date(2012, APR, 30);
125+
second = new Date(2015, JUL, 30);
121126
expect(dateUtil.isSameDay(first, second)).toBe(false);
122127
});
123128

124129
it('should check whether a date is in the next month', function() {
125130
// Next month within the same year.
126-
var first = new Date(2015, 6, 15);
127-
var second = new Date(2015, 7, 25);
131+
var first = new Date(2015, JUL, 15);
132+
var second = new Date(2015, AUG, 25);
128133
expect(dateUtil.isInNextMonth(first, second)).toBe(true);
129134

130135
// Next month across years.
131-
first = new Date(2015, 11, 15);
132-
second = new Date(2016, 0, 25);
136+
first = new Date(2015, DEC, 15);
137+
second = new Date(2016, JAN, 25);
133138
expect(dateUtil.isInNextMonth(first, second)).toBe(true);
134139

135140
// Not in the next month (past, same year).
136-
first = new Date(2015, 5, 15);
137-
second = new Date(2015, 3, 25);
141+
first = new Date(2015, JUN, 15);
142+
second = new Date(2015, APR, 25);
138143
expect(dateUtil.isInNextMonth(first, second)).toBe(false);
139144

140145
// Not in the next month (future, same year).
141-
first = new Date(2015, 5, 15);
142-
second = new Date(2015, 7, 25);
146+
first = new Date(2015, JUN, 15);
147+
second = new Date(2015, AUG, 25);
143148
expect(dateUtil.isInNextMonth(first, second)).toBe(false);
144149

145150
// Not in the next month (month + 1 in different year).
146-
first = new Date(2015, 5, 15);
147-
second = new Date(2016, 6, 25);
151+
first = new Date(2015, JUN, 15);
152+
second = new Date(2016, JUL, 25);
148153
expect(dateUtil.isInNextMonth(first, second)).toBe(false);
149154
});
150155

151156
it('should check whether a date is in the previous month', function() {
152157
// Previous month within the same year.
153-
var first = new Date(2015, 7, 15);
154-
var second = new Date(2015, 6, 25);
158+
var first = new Date(2015, AUG, 15);
159+
var second = new Date(2015, JUL, 25);
155160
expect(dateUtil.isInPreviousMonth(first, second)).toBe(true);
156161

157162
// Previous month across years.
158-
first = new Date(2015, 0, 15);
159-
second = new Date(2014, 11, 25);
163+
first = new Date(2015, JAN, 15);
164+
second = new Date(2014, DEC, 25);
160165
expect(dateUtil.isInPreviousMonth(first, second)).toBe(true);
161166

162167
// Not in the previous month (past, same year).
163-
first = new Date(2015, 5, 15);
164-
second = new Date(2015, 3, 25);
168+
first = new Date(2015, JUN, 15);
169+
second = new Date(2015, APR, 25);
165170
expect(dateUtil.isInPreviousMonth(first, second)).toBe(false);
166171

167172
// Not in the previous month (future, same year).
168-
first = new Date(2015, 5, 15);
169-
second = new Date(2015, 7, 25);
173+
first = new Date(2015, JUN, 15);
174+
second = new Date(2015, AUG, 25);
170175
expect(dateUtil.isInPreviousMonth(first, second)).toBe(false);
171176

172177
// Not in the previous month (month - 1 in different year).
173-
first = new Date(2015, 5, 15);
174-
second = new Date(2016, 4, 25);
178+
first = new Date(2015, JUN, 15);
179+
second = new Date(2016, MAY, 25);
175180
expect(dateUtil.isInPreviousMonth(first, second)).toBe(false);
176181
});
177182

178183
it('should get the midpoint between two dates', function() {
179-
var start = new Date(2010, 2, 10);
180-
var end = new Date(2010, 2, 20);
184+
var start = new Date(2010, MAR, 10);
185+
var end = new Date(2010, MAR, 20);
181186
var midpoint = dateUtil.getDateMidpoint(start, end);
182187

183-
expect(dateUtil.isSameDay(midpoint, new Date(2010, 2, 15))).toBe(true);
188+
expect(dateUtil.isSameDay(midpoint, new Date(2010, MAR, 15))).toBe(true);
184189
});
185190

186191
it('should get the week of the month in which a given date appears', function() {
192+
// May 2015 spans 6 weeks.
193+
expect(dateUtil.getWeekOfMonth(new Date(2015, MAY, 1))).toBe(0);
194+
expect(dateUtil.getWeekOfMonth(new Date(2015, MAY, 8))).toBe(1);
195+
expect(dateUtil.getWeekOfMonth(new Date(2015, MAY, 15))).toBe(2);
196+
expect(dateUtil.getWeekOfMonth(new Date(2015, MAY, 22))).toBe(3);
197+
expect(dateUtil.getWeekOfMonth(new Date(2015, MAY, 29))).toBe(4);
198+
expect(dateUtil.getWeekOfMonth(new Date(2015, MAY, 31))).toBe(5);
199+
200+
// Feb 2015 spans 4 weeks. Check both the first and last day of each week.
201+
expect(dateUtil.getWeekOfMonth(new Date(2015, FEB, 1))).toBe(0);
202+
expect(dateUtil.getWeekOfMonth(new Date(2015, FEB, 7))).toBe(0);
203+
expect(dateUtil.getWeekOfMonth(new Date(2015, FEB, 8))).toBe(1);
204+
expect(dateUtil.getWeekOfMonth(new Date(2015, FEB, 14))).toBe(1);
205+
expect(dateUtil.getWeekOfMonth(new Date(2015, FEB, 15))).toBe(2);
206+
expect(dateUtil.getWeekOfMonth(new Date(2015, FEB, 21))).toBe(2);
207+
expect(dateUtil.getWeekOfMonth(new Date(2015, FEB, 22))).toBe(3);
208+
expect(dateUtil.getWeekOfMonth(new Date(2015, FEB, 28))).toBe(3);
187209
});
188210

189211
it('should increment a date by a number of days', function() {
212+
// Increment by one.
213+
var start = new Date(2015, MAY, 15);
214+
var end = new Date(2015, MAY, 16);
215+
expect(dateUtil.isSameDay(dateUtil.incrementDays(start, 1), end)).toBe(true);
216+
217+
// Negative by negative one.
218+
start = new Date(2015, MAY, 15);
219+
end = new Date(2015, MAY, 14);
220+
expect(dateUtil.isSameDay(dateUtil.incrementDays(start, -1), end)).toBe(true);
221+
222+
// Into next month.
223+
start = new Date(2015, MAY, 31);
224+
end = new Date(2015, JUN, 1);
225+
expect(dateUtil.isSameDay(dateUtil.incrementDays(start, 1), end)).toBe(true);
226+
227+
// Into previous month.
228+
start = new Date(2015, MAY, 1);
229+
end = new Date(2015, APR, 30);
230+
expect(dateUtil.isSameDay(dateUtil.incrementDays(start, -1), end)).toBe(true);
231+
232+
// Into next year.
233+
start = new Date(2015, DEC, 31);
234+
end = new Date(2016, JAN, 1);
235+
expect(dateUtil.isSameDay(dateUtil.incrementDays(start, 1), end)).toBe(true);
236+
237+
// Into last year.
238+
start = new Date(2015, JAN, 1);
239+
end = new Date(2014, DEC, 31);
240+
expect(dateUtil.isSameDay(dateUtil.incrementDays(start, -1), end)).toBe(true);
190241
});
191242

192243
it('should increment a date by a number of months', function() {
244+
// Increment by one.
245+
var start = new Date(2015, MAY, 15);
246+
var end = new Date(2015, JUN, 15);
247+
expect(dateUtil.isSameDay(dateUtil.incrementMonths(start, 1), end)).toBe(true);
248+
249+
// Negative by negative one.
250+
start = new Date(2015, MAY, 15);
251+
end = new Date(2015, APR, 15);
252+
expect(dateUtil.isSameDay(dateUtil.incrementMonths(start, -1), end)).toBe(true);
253+
254+
// Next month has fewer days.
255+
start = new Date(2015, JAN, 30);
256+
end = new Date(2015, FEB, 28);
257+
expect(dateUtil.isSameDay(dateUtil.incrementMonths(start, 1), end)).toBe(true);
258+
259+
// Previous month has fewer days.
260+
start = new Date(2015, MAY, 31);
261+
end = new Date(2015, APR, 30);
262+
expect(dateUtil.isSameDay(dateUtil.incrementMonths(start, -1), end)).toBe(true);
193263
});
194264

195265
it('should get the last date of a month', function() {
266+
// Normal February
267+
var date = new Date(2015, FEB, 1);
268+
var lastOfMonth = new Date(2015, FEB, 28);
269+
expect(dateUtil.isSameDay(dateUtil.getLastDateOfMonth(date), lastOfMonth)).toBe(true);
270+
271+
// Leap year February
272+
date = new Date(2012, FEB, 1);
273+
lastOfMonth = new Date(2012, FEB, 29);
274+
expect(dateUtil.isSameDay(dateUtil.getLastDateOfMonth(date), lastOfMonth)).toBe(true);
275+
276+
// Month with 31 days.
277+
date = new Date(2015, DEC, 12);
278+
lastOfMonth = new Date(2015, DEC, 31);
279+
expect(dateUtil.isSameDay(dateUtil.getLastDateOfMonth(date), lastOfMonth)).toBe(true);
280+
281+
// Month with 30 days.
282+
date = new Date(2015, APR, 3);
283+
lastOfMonth = new Date(2015, APR, 30);
284+
expect(dateUtil.isSameDay(dateUtil.getLastDateOfMonth(date), lastOfMonth)).toBe(true);
196285
});
197286
});

0 commit comments

Comments
 (0)