@@ -19,6 +19,7 @@ describe('MatCalendarHeader', () => {
19
19
declarations : [
20
20
// Test components.
21
21
StandardCalendar ,
22
+ CalendarWithMinMaxDate ,
22
23
] ,
23
24
providers : [
24
25
MatDatepickerIntl ,
@@ -148,7 +149,174 @@ describe('MatCalendarHeader', () => {
148
149
expect ( calendarInstance . activeDate ) . toEqual ( new Date ( 2016 , DEC , 31 ) ) ;
149
150
expect ( testComponent . selected ) . toBeFalsy ( 'no date should be selected yet' ) ;
150
151
} ) ;
152
+ } ) ;
153
+
154
+ describe ( 'calendar with minDate only' , ( ) => {
155
+ let fixture : ComponentFixture < CalendarWithMinMaxDate > ;
156
+ let testComponent : CalendarWithMinMaxDate ;
157
+ let calendarElement : HTMLElement ;
158
+ let periodButton : HTMLButtonElement ;
159
+ let prevButton : HTMLButtonElement ;
160
+ let nextButton : HTMLButtonElement ;
161
+ let calendarInstance : MatCalendar < Date > ;
162
+
163
+ beforeEach ( ( ) => {
164
+ fixture = TestBed . createComponent ( CalendarWithMinMaxDate ) ;
165
+ fixture . detectChanges ( ) ;
166
+
167
+ let calendarDebugElement = fixture . debugElement . query ( By . directive ( MatCalendar ) ) ;
168
+ calendarElement = calendarDebugElement . nativeElement ;
169
+ periodButton =
170
+ calendarElement . querySelector ( '.mat-calendar-period-button' ) as HTMLButtonElement ;
171
+ prevButton =
172
+ calendarElement . querySelector ( '.mat-calendar-previous-button' ) as HTMLButtonElement ;
173
+ nextButton =
174
+ calendarElement . querySelector ( '.mat-calendar-next-button' ) as HTMLButtonElement ;
175
+ calendarInstance = calendarDebugElement . componentInstance ;
176
+ testComponent = fixture . componentInstance ;
177
+ } ) ;
178
+
179
+ it ( 'should start the first page with minDate' , ( ) => {
180
+ testComponent . minDate = new Date ( 2010 , JAN , 1 ) ;
181
+ periodButton . click ( ) ;
182
+ fixture . detectChanges ( ) ;
183
+
184
+ expect ( calendarInstance . currentView ) . toBe ( 'multi-year' ) ;
185
+ expect ( periodButton . innerText . trim ( ) ) . toEqual ( '2010 \u2013 2033' ) ;
186
+ } ) ;
187
+
188
+
189
+ it ( 'should disable the page before the one showing minDate' , ( ) => {
190
+ testComponent . minDate = new Date ( 2010 , JAN , 1 ) ;
191
+ periodButton . click ( ) ;
192
+ fixture . detectChanges ( ) ;
193
+
194
+ expect ( calendarInstance . currentView ) . toBe ( 'multi-year' ) ;
195
+ expect ( prevButton . disabled ) . toBe ( true ) ;
196
+ } ) ;
151
197
198
+ it ( 'should enable the page after the one showing minDate' , ( ) => {
199
+ testComponent . minDate = new Date ( 2010 , JAN , 1 ) ;
200
+ periodButton . click ( ) ;
201
+ fixture . detectChanges ( ) ;
202
+
203
+ expect ( calendarInstance . currentView ) . toBe ( 'multi-year' ) ;
204
+ expect ( nextButton . disabled ) . toBe ( false ) ;
205
+ } ) ;
206
+ } ) ;
207
+
208
+ describe ( 'calendar with maxDate only' , ( ) => {
209
+ let fixture : ComponentFixture < CalendarWithMinMaxDate > ;
210
+ let testComponent : CalendarWithMinMaxDate ;
211
+ let calendarElement : HTMLElement ;
212
+ let periodButton : HTMLButtonElement ;
213
+ let prevButton : HTMLButtonElement ;
214
+ let nextButton : HTMLButtonElement ;
215
+ let calendarInstance : MatCalendar < Date > ;
216
+
217
+ beforeEach ( ( ) => {
218
+ fixture = TestBed . createComponent ( CalendarWithMinMaxDate ) ;
219
+ fixture . detectChanges ( ) ;
220
+
221
+ let calendarDebugElement = fixture . debugElement . query ( By . directive ( MatCalendar ) ) ;
222
+ calendarElement = calendarDebugElement . nativeElement ;
223
+ periodButton =
224
+ calendarElement . querySelector ( '.mat-calendar-period-button' ) as HTMLButtonElement ;
225
+ prevButton =
226
+ calendarElement . querySelector ( '.mat-calendar-previous-button' ) as HTMLButtonElement ;
227
+ nextButton =
228
+ calendarElement . querySelector ( '.mat-calendar-next-button' ) as HTMLButtonElement ;
229
+ calendarInstance = calendarDebugElement . componentInstance ;
230
+ testComponent = fixture . componentInstance ;
231
+ } ) ;
232
+
233
+ it ( 'should end the last page with maxDate' , ( ) => {
234
+ testComponent . maxDate = new Date ( 2020 , JAN , 1 ) ;
235
+ periodButton . click ( ) ;
236
+ fixture . detectChanges ( ) ;
237
+
238
+ expect ( calendarInstance . currentView ) . toBe ( 'multi-year' ) ;
239
+ expect ( periodButton . innerText . trim ( ) ) . toEqual ( '1997 \u2013 2020' ) ;
240
+ } ) ;
241
+
242
+ it ( 'should disable the page after the one showing maxDate' , ( ) => {
243
+ testComponent . maxDate = new Date ( 2020 , JAN , 1 ) ;
244
+ periodButton . click ( ) ;
245
+ fixture . detectChanges ( ) ;
246
+
247
+ expect ( calendarInstance . currentView ) . toBe ( 'multi-year' ) ;
248
+ expect ( nextButton . disabled ) . toBe ( true ) ;
249
+ } ) ;
250
+
251
+ it ( 'should enable the page before the one showing maxDate' , ( ) => {
252
+ testComponent . maxDate = new Date ( 2020 , JAN , 1 ) ;
253
+ periodButton . click ( ) ;
254
+ fixture . detectChanges ( ) ;
255
+
256
+ expect ( calendarInstance . currentView ) . toBe ( 'multi-year' ) ;
257
+ expect ( prevButton . disabled ) . toBe ( false ) ;
258
+ } ) ;
259
+ } ) ;
260
+
261
+ describe ( 'calendar with minDate and maxDate' , ( ) => {
262
+ let fixture : ComponentFixture < CalendarWithMinMaxDate > ;
263
+ let testComponent : CalendarWithMinMaxDate ;
264
+ let calendarElement : HTMLElement ;
265
+ let periodButton : HTMLButtonElement ;
266
+ let prevButton : HTMLButtonElement ;
267
+ let nextButton : HTMLButtonElement ;
268
+ let calendarInstance : MatCalendar < Date > ;
269
+
270
+ beforeEach ( ( ) => {
271
+ fixture = TestBed . createComponent ( CalendarWithMinMaxDate ) ;
272
+ fixture . detectChanges ( ) ;
273
+
274
+ let calendarDebugElement = fixture . debugElement . query ( By . directive ( MatCalendar ) ) ;
275
+ calendarElement = calendarDebugElement . nativeElement ;
276
+ periodButton =
277
+ calendarElement . querySelector ( '.mat-calendar-period-button' ) as HTMLButtonElement ;
278
+ prevButton =
279
+ calendarElement . querySelector ( '.mat-calendar-previous-button' ) as HTMLButtonElement ;
280
+ nextButton =
281
+ calendarElement . querySelector ( '.mat-calendar-next-button' ) as HTMLButtonElement ;
282
+ calendarInstance = calendarDebugElement . componentInstance ;
283
+ testComponent = fixture . componentInstance ;
284
+ } ) ;
285
+
286
+ it ( 'should end the last page with maxDate' , ( ) => {
287
+ testComponent . minDate = new Date ( 1993 , JAN , 1 ) ;
288
+ testComponent . maxDate = new Date ( 2020 , JAN , 1 ) ;
289
+ periodButton . click ( ) ;
290
+ fixture . detectChanges ( ) ;
291
+
292
+ expect ( calendarInstance . currentView ) . toBe ( 'multi-year' ) ;
293
+ expect ( periodButton . innerText . trim ( ) ) . toEqual ( '1997 \u2013 2020' ) ;
294
+ } ) ;
295
+
296
+ it ( 'should disable the page after the one showing maxDate' , ( ) => {
297
+ testComponent . minDate = new Date ( 1993 , JAN , 1 ) ;
298
+ testComponent . maxDate = new Date ( 2020 , JAN , 1 ) ;
299
+ periodButton . click ( ) ;
300
+ fixture . detectChanges ( ) ;
301
+
302
+ expect ( calendarInstance . currentView ) . toBe ( 'multi-year' ) ;
303
+ expect ( nextButton . disabled ) . toBe ( true ) ;
304
+ } ) ;
305
+
306
+ it ( 'should disable the page before the one showing minDate' , ( ) => {
307
+ testComponent . minDate = new Date ( 1993 , JAN , 1 ) ;
308
+ testComponent . maxDate = new Date ( 2020 , JAN , 1 ) ;
309
+ periodButton . click ( ) ;
310
+ fixture . detectChanges ( ) ;
311
+
312
+ expect ( calendarInstance . currentView ) . toBe ( 'multi-year' ) ;
313
+
314
+ prevButton . click ( ) ;
315
+ fixture . detectChanges ( ) ;
316
+
317
+ expect ( calendarInstance . activeDate ) . toEqual ( new Date ( 2018 - yearsPerPage , JAN , 1 ) ) ;
318
+ expect ( prevButton . disabled ) . toBe ( true ) ;
319
+ } ) ;
152
320
} ) ;
153
321
} ) ;
154
322
@@ -167,3 +335,18 @@ class StandardCalendar {
167
335
selectedMonth : Date ;
168
336
startDate = new Date ( 2017 , JAN , 31 ) ;
169
337
}
338
+
339
+ @Component ( {
340
+ template : `
341
+ <mat-calendar
342
+ [startAt]="startAt"
343
+ [minDate]="minDate"
344
+ [maxDate]="maxDate">
345
+ </mat-calendar>
346
+ `
347
+ } )
348
+ class CalendarWithMinMaxDate {
349
+ startAt = new Date ( 2018 , JAN , 1 ) ;
350
+ minDate : Date | null ;
351
+ maxDate : Date | null ;
352
+ }
0 commit comments