This repository was 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
/
Copy pathcalendar.spec.js
770 lines (618 loc) · 29.3 KB
/
calendar.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
describe('md-calendar', 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 ngElement, element, scope, pageScope, calendarMonthController, calendarYearController, calendarController;
var $rootScope, dateLocale, $mdUtil, $material, $compile, $timeout, keyCodes, dateUtil;
// List of calendar elements added to the DOM so we can remove them after every test.
var attachedCalendarElements = [];
/**
* To apply a change in the date, a scope $apply() AND a manual triggering of animation
* callbacks is necessary.
*/
function applyDateChange() {
$timeout.flush();
pageScope.$apply();
$material.flushOutstandingAnimations();
// Internally, the calendar sets scrollTop to scroll to the month for a change.
// The handler for that scroll won't be invoked unless we manually trigger it.
var activeViewController = calendarMonthController || calendarYearController;
if (activeViewController) {
angular.element(activeViewController.calendarScroller).triggerHandler('scroll');
}
}
/** Extracts text as an array (one element per cell) from a tr element. */
function extractRowText(tr) {
var cellContents = [];
angular.forEach(tr.children, function(tableElement) {
cellContents.push(tableElement.textContent);
});
return cellContents;
}
/** Finds a td given a label. */
function findCellByLabel(monthElement, day) {
var tds = monthElement.querySelectorAll('td');
var td;
for (var i = 0; i < tds.length; i++) {
td = tds[i];
if (td.textContent === day.toString()) {
return td;
}
}
}
/**
* Finds a month `tbody` in the calendar element given a date.
*/
function findMonthElement(date) {
var months = element.querySelectorAll('[md-calendar-month-body]');
var monthHeader = dateLocale.monthHeaderFormatter(date);
var month;
for (var i = 0; i < months.length; i++) {
month = months[i];
if (month.querySelector('tr:first-child td:first-child').textContent === monthHeader) {
return month;
}
}
return null;
}
/** Find the `tbody` for a year in the calendar. */
function findYearElement(year) {
var node = element[0] || element;
var years = node.querySelectorAll('[md-calendar-year-body]');
var yearHeader = year.toString();
var target;
for (var i = 0; i < years.length; i++) {
target = years[i];
if (target.querySelector('.md-calendar-month-label').textContent === yearHeader) {
return target;
}
}
return null;
}
/**
* Gets the group label for a given date cell.
* @param {HTMLElement|DocumentView} cell
* @returns {string}
*/
function getGroupLabelForDateCell(cell) {
return $mdUtil.getClosest(cell, 'tbody').querySelector('.md-calendar-month-label').textContent;
}
/** Creates and compiles an md-calendar element. */
function createElement(parentScope, templateOverride) {
var directiveScope = parentScope || $rootScope.$new();
var template = templateOverride || '<md-calendar md-min-date="minDate" md-max-date="maxDate" ' +
'ng-model="myDate"></md-calendar>';
var attachedElement = angular.element(template);
document.body.appendChild(attachedElement[0]);
var newElement = $compile(attachedElement)(directiveScope);
attachedCalendarElements.push(newElement);
applyDateChange();
return newElement;
}
/**
* Dispatches a KeyboardEvent for the calendar.
* @param {number} keyCode
* @param {Object=} opt_modifiers
*/
function dispatchKeyEvent(keyCode, opt_modifiers) {
var mod = opt_modifiers || {};
calendarController.$element.triggerHandler({
type: 'keydown',
keyCode: keyCode,
which: keyCode,
ctrlKey: mod.ctrl,
altKey: mod.alt,
metaKey: mod.meta,
shortKey: mod.shift
});
}
function getFocusedDateElement() {
return element.querySelector('.md-focus');
}
beforeEach(module('material.components.datepicker', 'ngAnimateMock'));
beforeEach(inject(function($injector) {
jasmine.addMatchers({
toBeSameDayAs: function() {
return {
compare: function(actual, expected) {
var results = {
pass: dateUtil.isSameDay(actual, expected)
};
var negation = !results.pass ? '' : 'not ';
results.message = [
'Expected',
actual,
negation + 'to be the same day as',
expected
].join(' ');
return results;
}
};
}
});
$material = $injector.get('$material');
$compile = $injector.get('$compile');
$rootScope = $injector.get('$rootScope');
$timeout = $injector.get('$timeout');
dateLocale = $injector.get('$mdDateLocale');
dateUtil = $injector.get('$$mdDateUtil');
$mdUtil = $injector.get('$mdUtil');
keyCodes = $injector.get('$mdConstant').KEY_CODE;
pageScope = $rootScope.$new();
pageScope.myDate = null;
ngElement = createElement(pageScope);
element = ngElement[0];
scope = ngElement.isolateScope();
calendarController = ngElement.controller('mdCalendar');
calendarMonthController = ngElement.find('md-calendar-month').controller('mdCalendarMonth');
calendarYearController = ngElement.find('md-calendar-year').controller('mdCalendarYear');
}));
afterEach(function() {
attachedCalendarElements.forEach(function(element) {
element.remove();
});
attachedCalendarElements = [];
});
describe('ngModel binding', function() {
it('should update the calendar based on ngModel change', function() {
pageScope.myDate = new Date(2014, MAY, 30);
applyDateChange();
var selectedDate = element.querySelector('.md-calendar-selected-date');
expect(getGroupLabelForDateCell(selectedDate)).toBe('May 2014');
expect(selectedDate.textContent).toBe('30');
});
});
describe('calendar construction', function() {
it('should be able to switch between views', function() {
expect(element.querySelector('md-calendar-month')).toBeTruthy();
calendarController.setCurrentView('year');
applyDateChange();
expect(element.querySelector('md-calendar-month')).toBeFalsy();
expect(element.querySelector('md-calendar-year')).toBeTruthy();
});
describe('month view', function() {
var monthCtrl;
beforeEach(function() {
monthCtrl = angular.element(element.querySelector('[md-calendar-month-body]'))
.controller('mdCalendarMonthBody');
});
it('should render a month correctly as a table', function() {
var date = new Date(2014, MAY, 30);
var monthElement = monthCtrl.buildCalendarForMonth(date);
var calendarRows = monthElement.querySelectorAll('tr');
var calendarDates = [];
angular.forEach(calendarRows, function(tr) {
calendarDates.push(extractRowText(tr));
});
var expectedDates = [
['May 2014', '', '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'],
['', '', '', '', '', '', ''],
];
expect(calendarDates).toEqual(expectedDates);
});
it('should render a month correctly when the first day of the week is Monday', function() {
dateLocale.firstDayOfWeek = 1;
var date = new Date(2014, MAY, 30);
var monthElement = monthCtrl.buildCalendarForMonth(date);
var calendarRows = monthElement.querySelectorAll('tr');
var calendarDates = [];
angular.forEach(calendarRows, function(tr) {
calendarDates.push(extractRowText(tr));
});
var expectedDates = [
['May 2014', '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', ''],
['', '', '', '', '', '', ''],
];
expect(calendarDates).toEqual(expectedDates);
dateLocale.firstDayOfWeek = 0;
});
it('should show the month on its own row if the first day is before Tuesday', function() {
var date = new Date(2014, JUN, 30); // 1st on Sunday
var monthElement = monthCtrl.buildCalendarForMonth(date);
var firstRow = monthElement.querySelector('tr');
expect(extractRowText(firstRow)).toEqual(['Jun 2014']);
});
it('should apply the locale-specific month header formatter', function() {
var date = new Date(2014, JUN, 30);
spyOn(dateLocale, 'monthHeaderFormatter').and.callFake(function(expectedDateArg) {
expect(expectedDateArg).toBeSameDayAs(date);
return 'Junz 2014';
});
var monthElement = monthCtrl.buildCalendarForMonth(date);
var monthHeader = monthElement.querySelector('tr');
expect(monthHeader.textContent).toEqual('Junz 2014');
});
it('should update the model on cell click', function() {
spyOn(scope, '$emit');
var date = new Date(2014, MAY, 30);
var monthElement = monthCtrl.buildCalendarForMonth(date);
var expectedDate = new Date(2014, MAY, 5);
findCellByLabel(monthElement, 5).click();
expect(pageScope.myDate).toBeSameDayAs(expectedDate);
expect(scope.$emit).toHaveBeenCalledWith('md-calendar-change', expectedDate);
});
it('should disable any dates outside the min/max date range', function() {
pageScope.minDate = new Date(2014, JUN, 10);
pageScope.maxDate = new Date(2014, JUN, 20);
pageScope.$apply();
var monthElement = monthCtrl.buildCalendarForMonth(new Date(2014, JUN, 15));
expect(findCellByLabel(monthElement, 5)).toHaveClass('md-calendar-date-disabled');
expect(findCellByLabel(monthElement, 10)).not.toHaveClass('md-calendar-date-disabled');
expect(findCellByLabel(monthElement, 20)).not.toHaveClass('md-calendar-date-disabled');
expect(findCellByLabel(monthElement, 25)).toHaveClass('md-calendar-date-disabled');
});
it('should not respond to disabled cell clicks', function() {
var initialDate = new Date(2014, JUN, 15);
pageScope.myDate = initialDate;
pageScope.minDate = new Date(2014, JUN, 10);
pageScope.maxDate = new Date(2014, JUN, 20);
pageScope.$apply();
var monthElement = monthCtrl.buildCalendarForMonth(pageScope.myDate);
findCellByLabel(monthElement, 5).click();
expect(pageScope.myDate).toBeSameDayAs(initialDate);
});
it('should ensure that all month elements have a height when the max ' +
'date is in the same month as the current date', function() {
ngElement.remove();
var newScope = $rootScope.$new();
newScope.myDate = new Date(2016, JUN, 15);
newScope.maxDate = new Date(2016, JUN, 20);
element = createElement(newScope)[0];
applyDateChange();
var monthWrapper = angular.element(element.querySelector('md-calendar-month'));
var scroller = monthWrapper.controller('mdCalendarMonth').calendarScroller;
scroller.scrollTop -= 50;
angular.element(scroller).triggerHandler('scroll');
var monthElements = $mdUtil.nodesToArray(
element.querySelectorAll('[md-calendar-month-body]')
);
expect(monthElements.every(function(currentMonthElement) {
return currentMonthElement.offsetHeight > 0;
})).toBe(true);
});
describe('weeks header', function() {
it('should display the weeks header in the first row', function() {
var header = element.querySelector('.md-calendar-day-header tr');
expect(extractRowText(header)).toEqual(['S', 'M', 'T', 'W', 'T', 'F' ,'S']);
});
it('should use $mdDateLocale.shortDays as weeks header values', function() {
var oldShortDays = dateLocale.shortDays;
dateLocale.shortDays = ['SZ', 'MZ', 'TZ', 'WZ', 'TZ', 'FZ', 'SZ'];
var newElement = createElement()[0];
var header = newElement.querySelector('.md-calendar-day-header tr');
expect(extractRowText(header)).toEqual(['SZ', 'MZ', 'TZ', 'WZ', 'TZ', 'FZ','SZ']);
dateLocale.shortDays = oldShortDays;
});
it('should allow changing the first day of the week to Monday', function() {
var oldShortDays = dateLocale.shortDays;
dateLocale.shortDays = ['SZ', 'MZ', 'TZ', 'WZ', 'TZ', 'FZ', 'SZ'];
dateLocale.firstDayOfWeek = 1;
var newElement = createElement()[0];
var header = newElement.querySelector('.md-calendar-day-header tr');
expect(extractRowText(header)).toEqual(['MZ', 'TZ', 'WZ', 'TZ', 'FZ','SZ', 'SZ']);
dateLocale.shortDays = oldShortDays;
dateLocale.firstDayOfWeek = 0;
});
});
it('should highlight today', function() {
pageScope.myDate = calendarController.today;
applyDateChange();
var todayElement = element.querySelector('.md-calendar-date-today');
expect(todayElement).not.toBeNull();
expect(todayElement.textContent).toBe(calendarController.today.getDate() + '');
});
it('should highlight the selected date', function() {
pageScope.myDate = calendarController.selectedDate = new Date(2014, JUN, 30);
applyDateChange();
var selectedElement = element.querySelector('.md-calendar-selected-date');
expect(selectedElement).not.toBeNull();
expect(selectedElement.textContent).toBe(calendarController.selectedDate.getDate() + '');
});
it('should block month transitions when a month transition is happening', function() {
var earlierDate = new Date(2014, FEB, 11);
var laterDate = new Date(2014, MAR, 11);
calendarMonthController.changeDisplayDate(earlierDate);
expect(calendarController.displayDate).toBeSameDayAs(earlierDate);
calendarMonthController.changeDisplayDate(laterDate);
expect(calendarController.displayDate).toBeSameDayAs(earlierDate);
$material.flushOutstandingAnimations();
calendarMonthController.changeDisplayDate(laterDate);
expect(calendarController.displayDate).toBeSameDayAs(laterDate);
});
it('should not render any months before the min date', function() {
ngElement.remove();
var newScope = $rootScope.$new();
newScope.minDate = new Date(2014, JUN, 5);
newScope.myDate = new Date(2014, JUN, 15);
newScope.$apply();
element = createElement(newScope)[0];
expect(findMonthElement(new Date(2014, JUL, 1))).not.toBeNull();
expect(findMonthElement(new Date(2014, JUN, 1))).not.toBeNull();
expect(findMonthElement(new Date(2014, MAY, 1))).toBeNull();
});
});
describe('year view', function() {
var yearCtrl;
beforeEach(function() {
calendarController.setCurrentView('year');
applyDateChange();
yearCtrl = angular.element(element.querySelector('[md-calendar-year-body]'))
.controller('mdCalendarYearBody');
});
it('should render a year correctly as a table', function() {
var date = new Date(2014, MAY, 30);
var yearElement = yearCtrl.buildCalendarForYear(date);
var calendarRows = yearElement.querySelectorAll('tr');
var calendarDates = [];
angular.forEach(calendarRows, function(tr) {
calendarDates.push(extractRowText(tr));
});
var expectedDates = [
['2014', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
['', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
];
expect(calendarDates).toEqual(expectedDates);
});
it('should jump to the first day of the relevant month on cell click', function() {
var date = new Date(2014, MAY, 30);
var yearElement = yearCtrl.buildCalendarForYear(date);
var expectedDate = new Date(2014, DEC, 1);
findCellByLabel(yearElement, 'Dec').click();
applyDateChange();
expect(calendarController.displayDate).toBeSameDayAs(expectedDate);
expect(calendarController.currentView).not.toBe('year');
});
it('should disable any months outside the min/max date range', function() {
pageScope.minDate = new Date(2014, JUN, 10);
pageScope.maxDate = new Date(2014, SEP, 20);
pageScope.$apply();
var yearElement = yearCtrl.buildCalendarForYear(new Date(2014, JAN, 1));
var disabledClass = 'md-calendar-date-disabled';
expect(findCellByLabel(yearElement, 'Jan')).toHaveClass(disabledClass);
expect(findCellByLabel(yearElement, 'Aug')).not.toHaveClass(disabledClass);
expect(findCellByLabel(yearElement, 'Oct')).toHaveClass(disabledClass);
});
it('should not respond to disabled cell clicks', function() {
var initialDate = new Date(2014, JUN, 15);
calendarController.displayDate = initialDate;
pageScope.minDate = new Date(2014, FEB, 10);
pageScope.maxDate = new Date(2014, AUG, 20);
pageScope.$apply();
var yearElement = yearCtrl.buildCalendarForYear(calendarController.displayDate);
findCellByLabel(yearElement, 'Jan').click();
expect(calendarController.displayDate).toBe(initialDate);
});
it('should highlight the current month', function() {
var todayElement = element.querySelector('.md-calendar-date-today');
expect(todayElement).not.toBeNull();
expect(todayElement.textContent).toBe(dateLocale.shortMonths[calendarController.today.getMonth()]);
});
it('should highlight the month of the selected date', function() {
ngElement.remove();
var newScope = $rootScope.$new();
newScope.myDate = new Date(2014, JUN, 30);
element = createElement(newScope)[0];
angular.element(element).controller('mdCalendar').setCurrentView('year');
applyDateChange();
var selectedElement = element.querySelector('.md-calendar-selected-date');
expect(selectedElement).not.toBeNull();
expect(selectedElement.textContent).toBe('Jun');
});
it('should not render any years before the min date', function() {
ngElement.remove();
var newScope = $rootScope.$new();
newScope.minDate = new Date(2014, JUN, 5);
newScope.myDate = new Date(2014, JUL, 15);
element = createElement(newScope);
element.controller('mdCalendar').setCurrentView('year');
applyDateChange();
expect(findYearElement(2014)).not.toBeNull();
expect(findYearElement(2013)).toBeNull();
});
it('should ensure that all year elements have a height when the ' +
'current date is in the same month as the max date', function() {
ngElement.remove();
var newScope = $rootScope.$new();
newScope.myDate = new Date(2016, JUN, 15);
newScope.maxDate = new Date(2016, JUN, 20);
element = createElement(newScope);
element.controller('mdCalendar').setCurrentView('year');
applyDateChange();
var yearWrapper = angular.element(element[0].querySelector('md-calendar-year'));
var scroller = yearWrapper.controller('mdCalendarYear').calendarScroller;
scroller.scrollTop -= 50;
angular.element(scroller).triggerHandler('scroll');
var yearElements = $mdUtil.nodesToArray(
element[0].querySelectorAll('[md-calendar-year-body]')
);
expect(yearElements.every(function(currentYearElement) {
return currentYearElement.offsetHeight > 0;
})).toBe(true);
});
});
it('should have ids for date elements unique to the directive instance', function() {
var controller = ngElement.controller('mdCalendar');
pageScope.myDate = calendarController.today;
applyDateChange();
var otherScope = $rootScope.$new();
var day = 15;
otherScope.myDate = calendarController.today;
var otherNgElement = createElement(otherScope);
var monthElement = element.querySelector('.md-calendar-month');
var dateElement = findCellByLabel(monthElement, day);
var otherMonthElement = otherNgElement[0].querySelector('.md-calendar-month');
var otherDateElement = findCellByLabel(otherMonthElement, day);
expect(dateElement.id).not.toEqual(otherDateElement.id);
});
});
describe('keyboard events', function() {
describe('month view', function() {
it('should navigate around the calendar based on key presses', function() {
pageScope.myDate = new Date(2014, FEB, 11);
applyDateChange();
var selectedDate = element.querySelector('.md-calendar-selected-date');
selectedDate.focus();
dispatchKeyEvent(keyCodes.LEFT_ARROW);
expect(getFocusedDateElement().textContent).toBe('10');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('Feb 2014');
dispatchKeyEvent(keyCodes.UP_ARROW);
expect(getFocusedDateElement().textContent).toBe('3');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('Feb 2014');
dispatchKeyEvent(keyCodes.RIGHT_ARROW);
expect(getFocusedDateElement().textContent).toBe('4');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('Feb 2014');
dispatchKeyEvent(keyCodes.DOWN_ARROW);
expect(getFocusedDateElement().textContent).toBe('11');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('Feb 2014');
dispatchKeyEvent(keyCodes.HOME);
expect(getFocusedDateElement().textContent).toBe('1');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('Feb 2014');
dispatchKeyEvent(keyCodes.END);
expect(getFocusedDateElement().textContent).toBe('28');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('Feb 2014');
dispatchKeyEvent(keyCodes.RIGHT_ARROW);
expect(getFocusedDateElement().textContent).toBe('1');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('Mar 2014');
dispatchKeyEvent(keyCodes.PAGE_UP);
expect(getFocusedDateElement().textContent).toBe('1');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('Feb 2014');
dispatchKeyEvent(keyCodes.PAGE_DOWN);
expect(getFocusedDateElement().textContent).toBe('1');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('Mar 2014');
dispatchKeyEvent(keyCodes.UP_ARROW, {meta: true});
expect(getFocusedDateElement().textContent).toBe('1');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('Feb 2014');
dispatchKeyEvent(keyCodes.DOWN_ARROW, {meta: true});
expect(getFocusedDateElement().textContent).toBe('1');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('Mar 2014');
dispatchKeyEvent(keyCodes.ENTER);
applyDateChange();
expect(calendarController.selectedDate).toBeSameDayAs(new Date(2014, MAR, 1));
});
it('should restrict date navigation to min/max dates', function() {
pageScope.minDate = new Date(2014, FEB, 5);
pageScope.maxDate = new Date(2014, FEB, 10);
pageScope.myDate = new Date(2014, FEB, 8);
applyDateChange();
var selectedDate = element.querySelector('.md-calendar-selected-date');
selectedDate.focus();
dispatchKeyEvent(keyCodes.UP_ARROW);
expect(getFocusedDateElement().textContent).toBe('5');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('Feb 2014');
dispatchKeyEvent(keyCodes.LEFT_ARROW);
expect(getFocusedDateElement().textContent).toBe('5');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('Feb 2014');
dispatchKeyEvent(keyCodes.DOWN_ARROW);
expect(getFocusedDateElement().textContent).toBe('10');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('Feb 2014');
dispatchKeyEvent(keyCodes.RIGHT_ARROW);
expect(getFocusedDateElement().textContent).toBe('10');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('Feb 2014');
dispatchKeyEvent(keyCodes.UP_ARROW, {meta: true});
expect(getFocusedDateElement().textContent).toBe('5');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('Feb 2014');
dispatchKeyEvent(keyCodes.DOWN_ARROW, {meta: true});
expect(getFocusedDateElement().textContent).toBe('10');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('Feb 2014');
});
});
describe('year view', function() {
it('should navigate around the calendar based on key presses', function() {
pageScope.myDate = new Date(2014, JUN, 1);
calendarController.setCurrentView('year');
applyDateChange();
var selectedDate = element.querySelector('.md-calendar-selected-date');
selectedDate.focus();
dispatchKeyEvent(keyCodes.LEFT_ARROW);
expect(getFocusedDateElement().textContent).toBe('May');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('2014');
dispatchKeyEvent(keyCodes.UP_ARROW);
expect(getFocusedDateElement().textContent).toBe('Nov');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('2013');
dispatchKeyEvent(keyCodes.RIGHT_ARROW);
expect(getFocusedDateElement().textContent).toBe('Dec');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('2013');
dispatchKeyEvent(keyCodes.DOWN_ARROW);
expect(getFocusedDateElement().textContent).toBe('Jun');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('2014');
dispatchKeyEvent(keyCodes.ENTER);
applyDateChange();
expect(calendarController.displayDate).toBeSameDayAs(new Date(2014, JUN, 1));
expect(calendarController.currentView).toBe('month');
expect(getFocusedDateElement().textContent).toBe('1');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('Jun 2014');
});
it('should restrict date navigation to min/max dates', function() {
pageScope.minDate = new Date(2014, JAN, 30);
pageScope.maxDate = new Date(2014, SEP, 1);
pageScope.myDate = new Date(2014, JUN, 1);
calendarController.setCurrentView('year');
applyDateChange();
var selectedDate = element.querySelector('.md-calendar-selected-date');
selectedDate.focus();
dispatchKeyEvent(keyCodes.UP_ARROW);
expect(getFocusedDateElement().textContent).toBe('Jan');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('2014');
dispatchKeyEvent(keyCodes.LEFT_ARROW);
expect(getFocusedDateElement().textContent).toBe('Jan');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('2014');
dispatchKeyEvent(keyCodes.DOWN_ARROW);
dispatchKeyEvent(keyCodes.RIGHT_ARROW);
expect(getFocusedDateElement().textContent).toBe('Aug');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('2014');
dispatchKeyEvent(keyCodes.RIGHT_ARROW);
expect(getFocusedDateElement().textContent).toBe('Sep');
expect(getGroupLabelForDateCell(getFocusedDateElement())).toBe('2014');
});
});
it('should fire an event when tabbing away', function() {
var tabHandler = jasmine.createSpy('tabHandler');
pageScope.$on('md-calendar-close', tabHandler);
pageScope.myDate = new Date(2014, FEB, 11);
applyDateChange();
var selectedDate = element.querySelector('.md-calendar-selected-date');
selectedDate.focus();
dispatchKeyEvent(keyCodes.TAB);
pageScope.$apply();
expect(tabHandler).toHaveBeenCalled();
});
});
it('should render one single-row month of disabled cells after the max date', function() {
ngElement.remove();
var newScope = $rootScope.$new();
newScope.myDate = new Date(2014, APR, 15);
newScope.maxDate = new Date(2014, APR, 30);
newScope.$apply();
element = createElement(newScope)[0];
expect(findMonthElement(new Date(2014, MAR, 1))).not.toBeNull();
expect(findMonthElement(new Date(2014, APR, 1))).not.toBeNull();
// First date of May 2014 on Thursday (i.e. has 3 dates on the first row).
var nextMonth = findMonthElement(new Date(2014, MAY, 1));
expect(nextMonth).not.toBeNull();
expect(nextMonth.querySelector('.md-calendar-month-label')).toHaveClass(
'md-calendar-month-label-disabled');
expect(nextMonth.querySelectorAll('tr').length).toBe(1);
var dates = nextMonth.querySelectorAll('.md-calendar-date');
for (var i = 0; i < dates.length; i++) {
date = dates[i];
if (date.textContent) {
expect(date).toHaveClass('md-calendar-date-disabled');
}
}
});
it('should have a configurable default view', function() {
ngElement.remove();
var calendar = createElement(null, '<md-calendar ng-model="myDate" md-current-view="year"></md-calendar>')[0];
expect(calendar.querySelector('md-calendar-month')).toBeFalsy();
expect(calendar.querySelector('md-calendar-year')).toBeTruthy();
});
});