@@ -50,6 +50,23 @@ describe('md-calendar', function() {
50
50
}
51
51
}
52
52
53
+ /**
54
+ * Finds a month `tbody` in the calendar element given a date.
55
+ */
56
+ function findMonthElement ( date ) {
57
+ var months = element . querySelectorAll ( '[md-calendar-month]' ) ;
58
+ var monthHeader = dateLocale . monthHeaderFormatter ( date ) ;
59
+ var month ;
60
+
61
+ for ( var i = 0 ; i < months . length ; i ++ ) {
62
+ month = months [ i ] ;
63
+ if ( month . querySelector ( 'tr:first-child td:first-child' ) . textContent === monthHeader ) {
64
+ return month ;
65
+ }
66
+ }
67
+ return null ;
68
+ }
69
+
53
70
/**
54
71
* Gets the month label for a given date cell.
55
72
* @param {HTMLElement|DocumentView } cell
@@ -63,7 +80,8 @@ describe('md-calendar', function() {
63
80
/** Creates and compiles an md-calendar element. */
64
81
function createElement ( parentScope ) {
65
82
var directiveScope = parentScope || $rootScope . $new ( ) ;
66
- var template = '<md-calendar ng-model="myDate"></md-calendar>' ;
83
+ var template = '<md-calendar md-min-date="minDate" md-max-date="maxDate" ' +
84
+ 'ng-model="myDate"></md-calendar>' ;
67
85
var attachedElement = angular . element ( template ) ;
68
86
document . body . appendChild ( attachedElement [ 0 ] ) ;
69
87
var newElement = $compile ( attachedElement ) ( directiveScope ) ;
@@ -135,7 +153,7 @@ describe('md-calendar', function() {
135
153
136
154
ngElement = createElement ( pageScope ) ;
137
155
element = ngElement [ 0 ] ;
138
- scope = ngElement . scope ( ) ;
156
+ scope = ngElement . isolateScope ( ) ;
139
157
controller = ngElement . controller ( 'mdCalendar' ) ;
140
158
} ) ) ;
141
159
@@ -227,6 +245,40 @@ describe('md-calendar', function() {
227
245
var monthHeader = monthElement . querySelector ( 'tr' ) ;
228
246
expect ( monthHeader . textContent ) . toEqual ( 'Junz 2014' ) ;
229
247
} ) ;
248
+
249
+ it ( 'should update the model on cell click' , function ( ) {
250
+ spyOn ( scope , '$emit' ) ;
251
+ var date = new Date ( 2014 , MAY , 30 ) ;
252
+ var monthElement = monthCtrl . buildCalendarForMonth ( date ) ;
253
+ var expectedDate = new Date ( 2014 , MAY , 5 ) ;
254
+ findDateElement ( monthElement , 5 ) . click ( ) ;
255
+ expect ( pageScope . myDate ) . toBeSameDayAs ( expectedDate ) ;
256
+ expect ( scope . $emit ) . toHaveBeenCalledWith ( 'md-calendar-change' , expectedDate ) ;
257
+ } ) ;
258
+
259
+ it ( 'should disable any dates outside the min/max date range' , function ( ) {
260
+ pageScope . minDate = new Date ( 2014 , JUN , 10 ) ;
261
+ pageScope . maxDate = new Date ( 2014 , JUN , 20 ) ;
262
+ pageScope . $apply ( ) ;
263
+
264
+ var monthElement = monthCtrl . buildCalendarForMonth ( new Date ( 2014 , JUN , 15 ) ) ;
265
+ expect ( findDateElement ( monthElement , 5 ) ) . toHaveClass ( 'md-calendar-date-disabled' ) ;
266
+ expect ( findDateElement ( monthElement , 10 ) ) . not . toHaveClass ( 'md-calendar-date-disabled' ) ;
267
+ expect ( findDateElement ( monthElement , 20 ) ) . not . toHaveClass ( 'md-calendar-date-disabled' ) ;
268
+ expect ( findDateElement ( monthElement , 25 ) ) . toHaveClass ( 'md-calendar-date-disabled' ) ;
269
+ } ) ;
270
+
271
+ it ( 'should not respond to disabled cell clicks' , function ( ) {
272
+ var initialDate = new Date ( 2014 , JUN , 15 ) ;
273
+ pageScope . myDate = initialDate ;
274
+ pageScope . minDate = new Date ( 2014 , JUN , 10 ) ;
275
+ pageScope . maxDate = new Date ( 2014 , JUN , 20 ) ;
276
+ pageScope . $apply ( ) ;
277
+
278
+ var monthElement = monthCtrl . buildCalendarForMonth ( pageScope . myDate ) ;
279
+ findDateElement ( monthElement , 5 ) . click ( ) ;
280
+ expect ( pageScope . myDate ) . toBeSameDayAs ( initialDate ) ;
281
+ } ) ;
230
282
} ) ;
231
283
232
284
it ( 'should highlight today' , function ( ) {
@@ -325,6 +377,41 @@ describe('md-calendar', function() {
325
377
expect ( controller . selectedDate ) . toBeSameDayAs ( new Date ( 2014 , MAR , 1 ) ) ;
326
378
} ) ;
327
379
380
+ it ( 'should restrict date navigation to min/max dates' , function ( ) {
381
+ pageScope . minDate = new Date ( 2014 , FEB , 5 ) ;
382
+ pageScope . maxDate = new Date ( 2014 , FEB , 10 ) ;
383
+ pageScope . myDate = new Date ( 2014 , FEB , 8 ) ;
384
+ applyDateChange ( ) ;
385
+
386
+ var selectedDate = element . querySelector ( '.md-calendar-selected-date' ) ;
387
+ selectedDate . focus ( ) ;
388
+
389
+ dispatchKeyEvent ( keyCodes . UP_ARROW ) ;
390
+ expect ( getFocusedDateElement ( ) . textContent ) . toBe ( '5' ) ;
391
+ expect ( getMonthLabelForDateCell ( getFocusedDateElement ( ) ) ) . toBe ( 'Feb 2014' ) ;
392
+
393
+ dispatchKeyEvent ( keyCodes . LEFT_ARROW ) ;
394
+ expect ( getFocusedDateElement ( ) . textContent ) . toBe ( '5' ) ;
395
+ expect ( getMonthLabelForDateCell ( getFocusedDateElement ( ) ) ) . toBe ( 'Feb 2014' ) ;
396
+
397
+ dispatchKeyEvent ( keyCodes . DOWN_ARROW ) ;
398
+ expect ( getFocusedDateElement ( ) . textContent ) . toBe ( '10' ) ;
399
+ expect ( getMonthLabelForDateCell ( getFocusedDateElement ( ) ) ) . toBe ( 'Feb 2014' ) ;
400
+
401
+ dispatchKeyEvent ( keyCodes . RIGHT_ARROW ) ;
402
+ expect ( getFocusedDateElement ( ) . textContent ) . toBe ( '10' ) ;
403
+ expect ( getMonthLabelForDateCell ( getFocusedDateElement ( ) ) ) . toBe ( 'Feb 2014' ) ;
404
+
405
+ dispatchKeyEvent ( keyCodes . UP_ARROW , { meta : true } ) ;
406
+ expect ( getFocusedDateElement ( ) . textContent ) . toBe ( '5' ) ;
407
+ expect ( getMonthLabelForDateCell ( getFocusedDateElement ( ) ) ) . toBe ( 'Feb 2014' ) ;
408
+
409
+ dispatchKeyEvent ( keyCodes . DOWN_ARROW , { meta : true } ) ;
410
+ expect ( getFocusedDateElement ( ) . textContent ) . toBe ( '10' ) ;
411
+ expect ( getMonthLabelForDateCell ( getFocusedDateElement ( ) ) ) . toBe ( 'Feb 2014' ) ;
412
+
413
+ } ) ;
414
+
328
415
it ( 'should fire an event when escape is pressed' , function ( ) {
329
416
var escapeHandler = jasmine . createSpy ( 'escapeHandler' ) ;
330
417
pageScope . $on ( 'md-calendar-close' , escapeHandler ) ;
@@ -354,4 +441,44 @@ describe('md-calendar', function() {
354
441
controller . changeDisplayDate ( laterDate ) ;
355
442
expect ( controller . displayDate ) . toBeSameDayAs ( laterDate ) ;
356
443
} ) ;
444
+
445
+ it ( 'should not render any months before the min date' , function ( ) {
446
+ ngElement . remove ( ) ;
447
+ var newScope = $rootScope . $new ( ) ;
448
+ newScope . minDate = new Date ( 2014 , JUN , 5 ) ;
449
+ newScope . myDate = new Date ( 2014 , JUN , 15 ) ;
450
+ newScope . $apply ( ) ;
451
+ element = createElement ( newScope ) [ 0 ] ;
452
+
453
+ expect ( findMonthElement ( new Date ( 2014 , JUL , 1 ) ) ) . not . toBeNull ( ) ;
454
+ expect ( findMonthElement ( new Date ( 2014 , JUN , 1 ) ) ) . not . toBeNull ( ) ;
455
+ expect ( findMonthElement ( new Date ( 2014 , MAY , 1 ) ) ) . toBeNull ( ) ;
456
+ } ) ;
457
+
458
+ it ( 'should render one single-row month of disabled cells after the max date' , function ( ) {
459
+ ngElement . remove ( ) ;
460
+ var newScope = $rootScope . $new ( ) ;
461
+ newScope . myDate = new Date ( 2014 , APR , 15 ) ;
462
+ newScope . maxDate = new Date ( 2014 , APR , 30 ) ;
463
+ newScope . $apply ( ) ;
464
+ element = createElement ( newScope ) [ 0 ] ;
465
+
466
+ expect ( findMonthElement ( new Date ( 2014 , MAR , 1 ) ) ) . not . toBeNull ( ) ;
467
+ expect ( findMonthElement ( new Date ( 2014 , APR , 1 ) ) ) . not . toBeNull ( ) ;
468
+
469
+ // First date of May 2014 on Thursday (i.e. has 3 dates on the first row).
470
+ var nextMonth = findMonthElement ( new Date ( 2014 , MAY , 1 ) ) ;
471
+ expect ( nextMonth ) . not . toBeNull ( ) ;
472
+ expect ( nextMonth . querySelector ( '.md-calendar-month-label' ) ) . toHaveClass (
473
+ 'md-calendar-month-label-disabled' ) ;
474
+ expect ( nextMonth . querySelectorAll ( 'tr' ) . length ) . toBe ( 1 ) ;
475
+
476
+ var dates = nextMonth . querySelectorAll ( '.md-calendar-date' ) ;
477
+ for ( var i = 0 ; i < dates . length ; i ++ ) {
478
+ date = dates [ i ] ;
479
+ if ( date . textContent ) {
480
+ expect ( date ) . toHaveClass ( 'md-calendar-date-disabled' ) ;
481
+ }
482
+ }
483
+ } ) ;
357
484
} ) ;
0 commit comments