1
1
2
2
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
+
3
8
var dateUtil ;
4
9
5
10
beforeEach ( module ( 'material.components.calendar' ) ) ;
@@ -9,15 +14,15 @@ describe('$$mdDateUtil', function() {
9
14
} ) ) ;
10
15
11
16
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 ) ) ;
13
18
14
19
expect ( first . getFullYear ( ) ) . toBe ( 1985 ) ;
15
20
expect ( first . getMonth ( ) ) . toBe ( 9 ) ;
16
21
expect ( first . getDate ( ) ) . toBe ( 1 ) ;
17
22
} ) ;
18
23
19
24
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 ) ) ;
21
26
22
27
expect ( first . getFullYear ( ) ) . toBe ( 1985 ) ;
23
28
expect ( first . getMonth ( ) ) . toBe ( 9 ) ;
@@ -26,172 +31,256 @@ describe('$$mdDateUtil', function() {
26
31
27
32
it ( 'should get the number of days in a month' , function ( ) {
28
33
// 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 ) ;
30
35
31
36
// 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 ) ;
33
38
34
39
// 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 ) ;
36
41
37
42
// 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 ) ;
39
44
} ) ;
40
45
41
46
it ( 'should get an arbitrary day in the next month' , function ( ) {
42
47
// 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 ) ) ;
44
49
expect ( next . getMonth ( ) ) . toBe ( 1 ) ;
45
50
expect ( next . getFullYear ( ) ) . toBe ( 2015 ) ;
46
51
47
52
// Next month in the following year.
48
- next = dateUtil . getDateInNextMonth ( new Date ( 2015 , 11 , 1 ) ) ;
53
+ next = dateUtil . getDateInNextMonth ( new Date ( 2015 , DEC , 1 ) ) ;
49
54
expect ( next . getMonth ( ) ) . toBe ( 0 ) ;
50
55
expect ( next . getFullYear ( ) ) . toBe ( 2016 ) ;
51
56
} ) ;
52
57
53
58
it ( 'should get an arbitrary day in the previous month' , function ( ) {
54
59
// 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 ) ) ;
56
61
expect ( next . getMonth ( ) ) . toBe ( 5 ) ;
57
62
expect ( next . getFullYear ( ) ) . toBe ( 2015 ) ;
58
63
59
64
// Previous month in the past year.
60
- next = dateUtil . getDateInPreviousMonth ( new Date ( 2015 , 0 , 1 ) ) ;
65
+ next = dateUtil . getDateInPreviousMonth ( new Date ( 2015 , JAN , 1 ) ) ;
61
66
expect ( next . getMonth ( ) ) . toBe ( 11 ) ;
62
67
expect ( next . getFullYear ( ) ) . toBe ( 2014 ) ;
63
68
} ) ;
64
69
65
70
it ( 'should check whether two dates are in the same month and year' , function ( ) {
66
71
// 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 ) ;
69
74
expect ( dateUtil . isSameMonthAndYear ( first , second ) ) . toBe ( true ) ;
70
75
71
76
// 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 ) ;
74
79
expect ( dateUtil . isSameMonthAndYear ( first , second ) ) . toBe ( true ) ;
75
80
76
81
// 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 ) ;
79
84
expect ( dateUtil . isSameMonthAndYear ( first , second ) ) . toBe ( false ) ;
80
85
81
86
// 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 ) ;
84
89
expect ( dateUtil . isSameMonthAndYear ( first , second ) ) . toBe ( false ) ;
85
90
86
91
// 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 ) ;
89
94
expect ( dateUtil . isSameMonthAndYear ( first , second ) ) . toBe ( false ) ;
90
95
} ) ;
91
96
92
97
it ( 'should check whether two dates are the same day' , function ( ) {
93
98
// 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 ) ;
96
101
expect ( dateUtil . isSameDay ( first , second ) ) . toBe ( true ) ;
97
102
98
103
// 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 ) ;
101
106
expect ( dateUtil . isSameDay ( first , second ) ) . toBe ( true ) ;
102
107
103
108
// 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 ) ;
106
111
expect ( dateUtil . isSameDay ( first , second ) ) . toBe ( false ) ;
107
112
108
113
// 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 ) ;
111
116
expect ( dateUtil . isSameDay ( first , second ) ) . toBe ( false ) ;
112
117
113
118
// 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 ) ;
116
121
expect ( dateUtil . isSameDay ( first , second ) ) . toBe ( false ) ;
117
122
118
123
// 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 ) ;
121
126
expect ( dateUtil . isSameDay ( first , second ) ) . toBe ( false ) ;
122
127
} ) ;
123
128
124
129
it ( 'should check whether a date is in the next month' , function ( ) {
125
130
// 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 ) ;
128
133
expect ( dateUtil . isInNextMonth ( first , second ) ) . toBe ( true ) ;
129
134
130
135
// 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 ) ;
133
138
expect ( dateUtil . isInNextMonth ( first , second ) ) . toBe ( true ) ;
134
139
135
140
// 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 ) ;
138
143
expect ( dateUtil . isInNextMonth ( first , second ) ) . toBe ( false ) ;
139
144
140
145
// 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 ) ;
143
148
expect ( dateUtil . isInNextMonth ( first , second ) ) . toBe ( false ) ;
144
149
145
150
// 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 ) ;
148
153
expect ( dateUtil . isInNextMonth ( first , second ) ) . toBe ( false ) ;
149
154
} ) ;
150
155
151
156
it ( 'should check whether a date is in the previous month' , function ( ) {
152
157
// 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 ) ;
155
160
expect ( dateUtil . isInPreviousMonth ( first , second ) ) . toBe ( true ) ;
156
161
157
162
// 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 ) ;
160
165
expect ( dateUtil . isInPreviousMonth ( first , second ) ) . toBe ( true ) ;
161
166
162
167
// 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 ) ;
165
170
expect ( dateUtil . isInPreviousMonth ( first , second ) ) . toBe ( false ) ;
166
171
167
172
// 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 ) ;
170
175
expect ( dateUtil . isInPreviousMonth ( first , second ) ) . toBe ( false ) ;
171
176
172
177
// 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 ) ;
175
180
expect ( dateUtil . isInPreviousMonth ( first , second ) ) . toBe ( false ) ;
176
181
} ) ;
177
182
178
183
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 ) ;
181
186
var midpoint = dateUtil . getDateMidpoint ( start , end ) ;
182
187
183
- expect ( dateUtil . isSameDay ( midpoint , new Date ( 2010 , 2 , 15 ) ) ) . toBe ( true ) ;
188
+ expect ( dateUtil . isSameDay ( midpoint , new Date ( 2010 , MAR , 15 ) ) ) . toBe ( true ) ;
184
189
} ) ;
185
190
186
191
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 ) ;
187
209
} ) ;
188
210
189
211
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 ) ;
190
241
} ) ;
191
242
192
243
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 ) ;
193
263
} ) ;
194
264
195
265
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 ) ;
196
285
} ) ;
197
286
} ) ;
0 commit comments