This repository was archived by the owner on May 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
Copy pathdatepicker.spec.js
2433 lines (2016 loc) · 86 KB
/
datepicker.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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
describe('datepicker directive', function () {
var $rootScope, $compile, $templateCache, element;
beforeEach(module('ui.bootstrap.datepicker'));
beforeEach(module('template/datepicker/datepicker.html'));
beforeEach(module('template/datepicker/day.html'));
beforeEach(module('template/datepicker/month.html'));
beforeEach(module('template/datepicker/year.html'));
beforeEach(module('template/datepicker/popup.html'));
beforeEach(module(function($compileProvider) {
$compileProvider.directive('dateModel', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attrs, modelController) {
modelController.$formatters.push(function(object) {
return new Date(object.date);
});
modelController.$parsers.push(function(date) {
return {
type: 'date',
date: date.toUTCString()
};
});
}
};
});
}));
function getTitleButton() {
return element.find('th').eq(1).find('button').first();
}
function getTitle() {
return getTitleButton().text();
}
function clickTitleButton() {
getTitleButton().click();
}
function clickPreviousButton(times) {
var el = element.find('th').eq(0).find('button').eq(0);
for (var i = 0, n = times || 1; i < n; i++) {
el.click();
}
}
function clickNextButton() {
element.find('th').eq(2).find('button').eq(0).click();
}
function getLabelsRow() {
return element.find('thead').find('tr').eq(1);
}
function getLabels(dayMode) {
var els = getLabelsRow().find('th'),
labels = [];
for (var i = dayMode ? 1 : 0, n = els.length; i < n; i++) {
labels.push( els.eq(i).text() );
}
return labels;
}
function getWeeks() {
var rows = element.find('tbody').find('tr'),
weeks = [];
for (var i = 0, n = rows.length; i < n; i++) {
weeks.push( rows.eq(i).find('td').eq(0).first().text() );
}
return weeks;
}
function getOptions(dayMode) {
var tr = element.find('tbody').find('tr');
var rows = [];
for (var j = 0, numRows = tr.length; j < numRows; j++) {
var cols = tr.eq(j).find('td'), days = [];
for (var i = dayMode ? 1 : 0, n = cols.length; i < n; i++) {
days.push( cols.eq(i).find('button').text() );
}
rows.push(days);
}
return rows;
}
function clickOption(index) {
getAllOptionsEl().eq(index).click();
}
function getAllOptionsEl(dayMode) {
return element.find('tbody').find('button');
}
function selectedElementIndex() {
var buttons = getAllOptionsEl();
for (var i = 0; i < buttons.length; i++) {
if (angular.element(buttons[i]).hasClass('btn-info')) {
return i;
}
}
}
function expectSelectedElement(index) {
var buttons = getAllOptionsEl();
angular.forEach( buttons, function( button, idx ) {
expect(angular.element(button).hasClass('btn-info')).toBe( idx === index );
});
}
function triggerKeyDown(element, key, ctrl) {
var keyCodes = {
'enter': 13,
'space': 32,
'pageup': 33,
'pagedown': 34,
'end': 35,
'home': 36,
'left': 37,
'up': 38,
'right': 39,
'down': 40,
'esc': 27
};
var e = $.Event('keydown');
e.which = keyCodes[key];
if (ctrl) {
e.ctrlKey = true;
}
element.trigger(e);
}
describe('$datepickerSuppressError', function() {
var $compile,
$log,
$scope;
it('should not suppress log error message for ng-model date error by default', function() {
inject(function(_$log_, _$rootScope_, _$compile_) {
$log = _$log_;
$scope = _$rootScope_.$new();
$compile = _$compile_;
});
spyOn($log, 'error');
element = $compile('<datepicker ng-model="locals.date"></datepicker>')($scope);
$scope.locals = {
date: 'lalala'
};
$scope.$digest();
expect($log.error).toHaveBeenCalled();
});
it('should not suppress log error message for ng-model date error when false', function() {
module(function($provide) {
$provide.value('$datepickerSuppressError', false);
});
inject(function(_$log_, _$rootScope_, _$compile_) {
$log = _$log_;
$scope = _$rootScope_.$new();
$compile = _$compile_;
});
spyOn($log, 'error');
element = $compile('<datepicker ng-model="locals.date"></datepicker>')($scope);
$scope.locals = {
date: 'lalala'
};
$scope.$digest();
expect($log.error).toHaveBeenCalled();
});
it('should suppress log error message for ng-model date error when true', function() {
module(function($provide) {
$provide.value('$datepickerSuppressError', true);
});
inject(function(_$log_, _$rootScope_, _$compile_) {
$log = _$log_;
$scope = _$rootScope_.$new();
$compile = _$compile_;
});
spyOn($log, 'error');
element = $compile('<datepicker ng-model="locals.date"></datepicker>')($scope);
$scope.locals = {
date: 'lalala'
};
$scope.$digest();
expect($log.error).not.toHaveBeenCalled();
});
});
describe('', function () {
beforeEach(inject(function(_$compile_, _$rootScope_, _$templateCache_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
$rootScope.date = new Date('September 30, 2010 15:30:00');
$templateCache = _$templateCache_;
}));
describe('', function () {
beforeEach(function() {
element = $compile('<datepicker ng-model="date"></datepicker>')($rootScope);
$rootScope.$digest();
});
it('is has a `<table>` element', function() {
expect(element.find('table').length).toBe(1);
});
it('shows the correct title', function() {
expect(getTitle()).toBe('September 2010');
});
it('shows the label row & the correct day labels', function() {
expect(getLabelsRow().css('display')).not.toBe('none');
expect(getLabels(true)).toEqual(['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']);
});
it('renders the calendar days correctly', function() {
expect(getOptions(true)).toEqual([
['29', '30', '31', '01', '02', '03', '04'],
['05', '06', '07', '08', '09', '10', '11'],
['12', '13', '14', '15', '16', '17', '18'],
['19', '20', '21', '22', '23', '24', '25'],
['26', '27', '28', '29', '30', '01', '02'],
['03', '04', '05', '06', '07', '08', '09']
]);
});
it('renders the week numbers based on ISO 8601', function() {
expect(getWeeks()).toEqual(['35', '36', '37', '38', '39', '40']);
});
it('value is correct', function() {
expect($rootScope.date).toEqual(new Date('September 30, 2010 15:30:00'));
});
it('has `selected` only the correct day', function() {
expectSelectedElement( 32 );
});
it('has no `selected` day when model is cleared', function() {
$rootScope.date = null;
$rootScope.$digest();
expect($rootScope.date).toBe(null);
expectSelectedElement( null );
});
it('does not change current view when model is cleared', function() {
$rootScope.date = null;
$rootScope.$digest();
expect($rootScope.date).toBe(null);
expect(getTitle()).toBe('September 2010');
});
it('`disables` visible dates from other months', function() {
var buttons = getAllOptionsEl();
angular.forEach(buttons, function( button, index ) {
expect(angular.element(button).find('span').hasClass('text-muted')).toBe( index < 3 || index > 32 );
});
});
it('updates the model when a day is clicked', function() {
clickOption( 17 );
expect($rootScope.date).toEqual(new Date('September 15, 2010 15:30:00'));
});
it('moves to the previous month & renders correctly when `previous` button is clicked', function() {
clickPreviousButton();
expect(getTitle()).toBe('August 2010');
expect(getLabels(true)).toEqual(['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']);
expect(getOptions(true)).toEqual([
['01', '02', '03', '04', '05', '06', '07'],
['08', '09', '10', '11', '12', '13', '14'],
['15', '16', '17', '18', '19', '20', '21'],
['22', '23', '24', '25', '26', '27', '28'],
['29', '30', '31', '01', '02', '03', '04'],
['05', '06', '07', '08', '09', '10', '11']
]);
expectSelectedElement( null, null );
});
it('updates the model only when a day is clicked in the `previous` month', function() {
clickPreviousButton();
expect($rootScope.date).toEqual(new Date('September 30, 2010 15:30:00'));
clickOption( 17 );
expect($rootScope.date).toEqual(new Date('August 18, 2010 15:30:00'));
});
it('moves to the next month & renders correctly when `next` button is clicked', function() {
clickNextButton();
expect(getTitle()).toBe('October 2010');
expect(getLabels(true)).toEqual(['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']);
expect(getOptions(true)).toEqual([
['26', '27', '28', '29', '30', '01', '02'],
['03', '04', '05', '06', '07', '08', '09'],
['10', '11', '12', '13', '14', '15', '16'],
['17', '18', '19', '20', '21', '22', '23'],
['24', '25', '26', '27', '28', '29', '30'],
['31', '01', '02', '03', '04', '05', '06']
]);
expectSelectedElement( 4 );
});
it('updates the model only when a day is clicked in the `next` month', function() {
clickNextButton();
expect($rootScope.date).toEqual(new Date('September 30, 2010 15:30:00'));
clickOption( 17 );
expect($rootScope.date).toEqual(new Date('October 13, 2010 15:30:00'));
});
it('updates the calendar when a day of another month is selected', function() {
clickOption( 33 );
expect($rootScope.date).toEqual(new Date('October 01, 2010 15:30:00'));
expect(getTitle()).toBe('October 2010');
expect(getLabels(true)).toEqual(['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']);
expect(getOptions(true)).toEqual([
['26', '27', '28', '29', '30', '01', '02'],
['03', '04', '05', '06', '07', '08', '09'],
['10', '11', '12', '13', '14', '15', '16'],
['17', '18', '19', '20', '21', '22', '23'],
['24', '25', '26', '27', '28', '29', '30'],
['31', '01', '02', '03', '04', '05', '06']
]);
expectSelectedElement( 5 );
});
// issue #1697
it('should not "jump" months', function() {
$rootScope.date = new Date('January 30, 2014');
$rootScope.$digest();
clickNextButton();
expect(getTitle()).toBe('February 2014');
clickPreviousButton();
expect(getTitle()).toBe('January 2014');
});
it('should support custom templates', function() {
$templateCache.put('foo/bar.html', '<div>baz</div>');
element = $compile('<datepicker ng-model="date" template-url="foo/bar.html"></datepicker>')($rootScope);
$rootScope.$digest();
expect(element.html()).toBe('baz');
});
it('should expose the controller in the template', function() {
$templateCache.put('template/datepicker/datepicker.html', '<div>{{datepicker.text}}</div>');
element = $compile('<datepicker ng-model="date"></datepicker>')($rootScope);
$rootScope.$digest();
var ctrl = element.controller('datepicker');
expect(ctrl).toBeDefined();
expect(element.html()).toBe('');
ctrl.text = 'baz';
$rootScope.$digest();
expect(element.html()).toBe('baz');
});
// issue #3079
describe('time zone bug', function () {
it('should deal with time zone bug', function() {
var ctrl = element.controller('datepicker'),
date = new Date('January 1, 2014');
spyOn(date, 'getHours').and.returnValue(23);
spyOn(date, 'setHours').and.returnValue();
ctrl.fixTimeZone(date);
expect(date.setHours).toHaveBeenCalledWith(25);
});
it('should not change hours if time zone bug does not occur', function() {
var ctrl = element.controller('datepicker'),
date = new Date('January 1, 2014');
spyOn(date, 'getHours').and.returnValue(0);
spyOn(date, 'setHours').and.returnValue();
ctrl.fixTimeZone(date);
expect(date.setHours).toHaveBeenCalledWith(0);
});
});
describe('when `model` changes', function () {
function testCalendar() {
expect(getTitle()).toBe('November 2005');
expect(getOptions(true)).toEqual([
['30', '31', '01', '02', '03', '04', '05'],
['06', '07', '08', '09', '10', '11', '12'],
['13', '14', '15', '16', '17', '18', '19'],
['20', '21', '22', '23', '24', '25', '26'],
['27', '28', '29', '30', '01', '02', '03'],
['04', '05', '06', '07', '08', '09', '10']
]);
expectSelectedElement( 8 );
}
describe('to a Date object', function() {
it('updates', function() {
$rootScope.date = new Date('November 7, 2005 23:30:00');
$rootScope.$digest();
testCalendar();
expect(angular.isDate($rootScope.date)).toBe(true);
});
it('to a date that is invalid, it doesn\`t update', function() {
$rootScope.date = new Date('pizza');
$rootScope.$digest();
expect(getTitle()).toBe('September 2010');
expect(angular.isDate($rootScope.date)).toBe(true);
expect(isNaN($rootScope.date)).toBe(true);
});
});
describe('not to a Date object', function() {
it('to a Number, it updates calendar', function() {
$rootScope.date = parseInt((new Date('November 7, 2005 23:30:00')).getTime(), 10);
$rootScope.$digest();
testCalendar();
expect(angular.isNumber($rootScope.date)).toBe(true);
});
it('to a string that can be parsed by Date, it updates calendar', function() {
$rootScope.date = 'November 7, 2005 23:30:00';
$rootScope.$digest();
testCalendar();
expect(angular.isString($rootScope.date)).toBe(true);
});
it('to a string that cannot be parsed by Date, it doesn\'t update', function() {
$rootScope.date = 'pizza';
$rootScope.$digest();
expect(getTitle()).toBe('September 2010');
expect($rootScope.date).toBe('pizza');
});
});
});
it('does not loop between after max mode', function() {
expect(getTitle()).toBe('September 2010');
clickTitleButton();
expect(getTitle()).toBe('2010');
clickTitleButton();
expect(getTitle()).toBe('2001 - 2020');
clickTitleButton();
expect(getTitle()).toBe('2001 - 2020');
});
describe('month selection mode', function () {
beforeEach(function() {
clickTitleButton();
});
it('shows the year as title', function() {
expect(getTitle()).toBe('2010');
});
it('shows months as options', function() {
expect(getOptions()).toEqual([
['January', 'February', 'March'],
['April', 'May', 'June'],
['July', 'August', 'September'],
['October', 'November', 'December']
]);
});
it('does not change the model', function() {
expect($rootScope.date).toEqual(new Date('September 30, 2010 15:30:00'));
});
it('has `selected` only the correct month', function() {
expectSelectedElement( 8 );
});
it('moves to the previous year when `previous` button is clicked', function() {
clickPreviousButton();
expect(getTitle()).toBe('2009');
expect(getOptions()).toEqual([
['January', 'February', 'March'],
['April', 'May', 'June'],
['July', 'August', 'September'],
['October', 'November', 'December']
]);
expectSelectedElement( null );
});
it('moves to the next year when `next` button is clicked', function() {
clickNextButton();
expect(getTitle()).toBe('2011');
expect(getOptions()).toEqual([
['January', 'February', 'March'],
['April', 'May', 'June'],
['July', 'August', 'September'],
['October', 'November', 'December']
]);
expectSelectedElement( null );
});
it('renders correctly when a month is clicked', function() {
clickPreviousButton(5);
expect(getTitle()).toBe('2005');
clickOption( 10 );
expect($rootScope.date).toEqual(new Date('September 30, 2010 15:30:00'));
expect(getTitle()).toBe('November 2005');
expect(getOptions(true)).toEqual([
['30', '31', '01', '02', '03', '04', '05'],
['06', '07', '08', '09', '10', '11', '12'],
['13', '14', '15', '16', '17', '18', '19'],
['20', '21', '22', '23', '24', '25', '26'],
['27', '28', '29', '30', '01', '02', '03'],
['04', '05', '06', '07', '08', '09', '10']
]);
clickOption( 17 );
expect($rootScope.date).toEqual(new Date('November 16, 2005 15:30:00'));
});
});
describe('year selection mode', function () {
beforeEach(function() {
clickTitleButton();
clickTitleButton();
});
it('shows the year range as title', function() {
expect(getTitle()).toBe('2001 - 2020');
});
it('shows years as options', function() {
expect(getOptions()).toEqual([
['2001', '2002', '2003', '2004', '2005'],
['2006', '2007', '2008', '2009', '2010'],
['2011', '2012', '2013', '2014', '2015'],
['2016', '2017', '2018', '2019', '2020']
]);
});
it('does not change the model', function() {
expect($rootScope.date).toEqual(new Date('September 30, 2010 15:30:00'));
});
it('has `selected` only the selected year', function() {
expectSelectedElement( 9 );
});
it('moves to the previous year set when `previous` button is clicked', function() {
clickPreviousButton();
expect(getTitle()).toBe('1981 - 2000');
expect(getOptions()).toEqual([
['1981', '1982', '1983', '1984', '1985'],
['1986', '1987', '1988', '1989', '1990'],
['1991', '1992', '1993', '1994', '1995'],
['1996', '1997', '1998', '1999', '2000']
]);
expectSelectedElement( null );
});
it('moves to the next year set when `next` button is clicked', function() {
clickNextButton();
expect(getTitle()).toBe('2021 - 2040');
expect(getOptions()).toEqual([
['2021', '2022', '2023', '2024', '2025'],
['2026', '2027', '2028', '2029', '2030'],
['2031', '2032', '2033', '2034', '2035'],
['2036', '2037', '2038', '2039', '2040']
]);
expectSelectedElement( null );
});
});
describe('keyboard navigation', function() {
function getActiveLabel() {
return element.find('.active').eq(0).text();
}
describe('day mode', function() {
it('will be able to activate previous day', function() {
triggerKeyDown(element, 'left');
expect(getActiveLabel()).toBe('29');
});
it('will be able to select with enter', function() {
triggerKeyDown(element, 'left');
triggerKeyDown(element, 'enter');
expect($rootScope.date).toEqual(new Date('September 29, 2010 15:30:00'));
});
it('will be able to select with space', function() {
triggerKeyDown(element, 'left');
triggerKeyDown(element, 'space');
expect($rootScope.date).toEqual(new Date('September 29, 2010 15:30:00'));
});
it('will be able to activate next day', function() {
triggerKeyDown(element, 'right');
expect(getActiveLabel()).toBe('01');
expect(getTitle()).toBe('October 2010');
});
it('will be able to activate same day in previous week', function() {
triggerKeyDown(element, 'up');
expect(getActiveLabel()).toBe('23');
});
it('will be able to activate same day in next week', function() {
triggerKeyDown(element, 'down');
expect(getActiveLabel()).toBe('07');
expect(getTitle()).toBe('October 2010');
});
it('will be able to activate same date in previous month', function() {
triggerKeyDown(element, 'pageup');
expect(getActiveLabel()).toBe('30');
expect(getTitle()).toBe('August 2010');
});
it('will be able to activate same date in next month', function() {
triggerKeyDown(element, 'pagedown');
expect(getActiveLabel()).toBe('30');
expect(getTitle()).toBe('October 2010');
});
it('will be able to activate first day of the month', function() {
triggerKeyDown(element, 'home');
expect(getActiveLabel()).toBe('01');
expect(getTitle()).toBe('September 2010');
});
it('will be able to activate last day of the month', function() {
$rootScope.date = new Date('September 1, 2010 15:30:00');
$rootScope.$digest();
triggerKeyDown(element, 'end');
expect(getActiveLabel()).toBe('30');
expect(getTitle()).toBe('September 2010');
});
it('will be able to move to month mode', function() {
triggerKeyDown(element, 'up', true);
expect(getActiveLabel()).toBe('September');
expect(getTitle()).toBe('2010');
});
it('will not respond when trying to move to lower mode', function() {
triggerKeyDown(element, 'down', true);
expect(getActiveLabel()).toBe('30');
expect(getTitle()).toBe('September 2010');
});
});
describe('month mode', function() {
beforeEach(function() {
triggerKeyDown(element, 'up', true);
});
it('will be able to activate previous month', function() {
triggerKeyDown(element, 'left');
expect(getActiveLabel()).toBe('August');
});
it('will be able to activate next month', function() {
triggerKeyDown(element, 'right');
expect(getActiveLabel()).toBe('October');
});
it('will be able to activate same month in previous row', function() {
triggerKeyDown(element, 'up');
expect(getActiveLabel()).toBe('June');
});
it('will be able to activate same month in next row', function() {
triggerKeyDown(element, 'down');
expect(getActiveLabel()).toBe('December');
});
it('will be able to activate same date in previous year', function() {
triggerKeyDown(element, 'pageup');
expect(getActiveLabel()).toBe('September');
expect(getTitle()).toBe('2009');
});
it('will be able to activate same date in next year', function() {
triggerKeyDown(element, 'pagedown');
expect(getActiveLabel()).toBe('September');
expect(getTitle()).toBe('2011');
});
it('will be able to activate first month of the year', function() {
triggerKeyDown(element, 'home');
expect(getActiveLabel()).toBe('January');
expect(getTitle()).toBe('2010');
});
it('will be able to activate last month of the year', function() {
triggerKeyDown(element, 'end');
expect(getActiveLabel()).toBe('December');
expect(getTitle()).toBe('2010');
});
it('will be able to move to year mode', function() {
triggerKeyDown(element, 'up', true);
expect(getActiveLabel()).toBe('2010');
expect(getTitle()).toBe('2001 - 2020');
});
it('will be able to move to day mode', function() {
triggerKeyDown(element, 'down', true);
expect(getActiveLabel()).toBe('30');
expect(getTitle()).toBe('September 2010');
});
it('will move to day mode when selecting', function() {
triggerKeyDown(element, 'left', true);
triggerKeyDown(element, 'enter', true);
expect(getActiveLabel()).toBe('30');
expect(getTitle()).toBe('August 2010');
expect($rootScope.date).toEqual(new Date('September 30, 2010 15:30:00'));
});
});
describe('year mode', function() {
beforeEach(function() {
triggerKeyDown(element, 'up', true);
triggerKeyDown(element, 'up', true);
});
it('will be able to activate previous year', function() {
triggerKeyDown(element, 'left');
expect(getActiveLabel()).toBe('2009');
});
it('will be able to activate next year', function() {
triggerKeyDown(element, 'right');
expect(getActiveLabel()).toBe('2011');
});
it('will be able to activate same year in previous row', function() {
triggerKeyDown(element, 'up');
expect(getActiveLabel()).toBe('2005');
});
it('will be able to activate same year in next row', function() {
triggerKeyDown(element, 'down');
expect(getActiveLabel()).toBe('2015');
});
it('will be able to activate same date in previous view', function() {
triggerKeyDown(element, 'pageup');
expect(getActiveLabel()).toBe('1990');
});
it('will be able to activate same date in next view', function() {
triggerKeyDown(element, 'pagedown');
expect(getActiveLabel()).toBe('2030');
});
it('will be able to activate first year of the year', function() {
triggerKeyDown(element, 'home');
expect(getActiveLabel()).toBe('2001');
});
it('will be able to activate last year of the year', function() {
triggerKeyDown(element, 'end');
expect(getActiveLabel()).toBe('2020');
});
it('will not respond when trying to move to upper mode', function() {
triggerKeyDown(element, 'up', true);
expect(getTitle()).toBe('2001 - 2020');
});
it('will be able to move to month mode', function() {
triggerKeyDown(element, 'down', true);
expect(getActiveLabel()).toBe('September');
expect(getTitle()).toBe('2010');
});
it('will move to month mode when selecting', function() {
triggerKeyDown(element, 'left', true);
triggerKeyDown(element, 'enter', true);
expect(getActiveLabel()).toBe('September');
expect(getTitle()).toBe('2009');
expect($rootScope.date).toEqual(new Date('September 30, 2010 15:30:00'));
});
});
describe('`aria-activedescendant`', function() {
function checkActivedescendant() {
var activeId = element.find('table').attr('aria-activedescendant');
expect(element.find('#' + activeId + ' > button')).toHaveClass('active');
}
it('updates correctly', function() {
triggerKeyDown(element, 'left');
checkActivedescendant();
triggerKeyDown(element, 'down');
checkActivedescendant();
triggerKeyDown(element, 'up', true);
checkActivedescendant();
triggerKeyDown(element, 'up', true);
checkActivedescendant();
});
});
});
});
describe('attribute `starting-day`', function () {
beforeEach(function() {
$rootScope.startingDay = 1;
element = $compile('<datepicker ng-model="date" starting-day="startingDay"></datepicker>')($rootScope);
$rootScope.$digest();
});
it('shows the day labels rotated', function() {
expect(getLabels(true)).toEqual(['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']);
});
it('renders the calendar days correctly', function() {
expect(getOptions(true)).toEqual([
['30', '31', '01', '02', '03', '04', '05'],
['06', '07', '08', '09', '10', '11', '12'],
['13', '14', '15', '16', '17', '18', '19'],
['20', '21', '22', '23', '24', '25', '26'],
['27', '28', '29', '30', '01', '02', '03'],
['04', '05', '06', '07', '08', '09', '10']
]);
});
it('renders the week numbers correctly', function() {
expect(getWeeks()).toEqual(['35', '36', '37', '38', '39', '40']);
});
});
describe('attribute `show-weeks`', function () {
beforeEach(function() {
$rootScope.showWeeks = false;
element = $compile('<datepicker ng-model="date" show-weeks="showWeeks"></datepicker>')($rootScope);
$rootScope.$digest();
});
it('hides week numbers based on variable', function() {
expect(getLabelsRow().find('th').length).toEqual(7);
var tr = element.find('tbody').find('tr');
for (var i = 0; i < 5; i++) {
expect(tr.eq(i).find('td').length).toEqual(7);
}
});
});
describe('`min-date` attribute', function () {
beforeEach(function() {
$rootScope.mindate = new Date('September 12, 2010');
element = $compile('<datepicker ng-model="date" min-date="mindate"></datepicker>')($rootScope);
$rootScope.$digest();
});
it('disables appropriate days in current month', function() {
var buttons = getAllOptionsEl();
angular.forEach(buttons, function( button, index ) {
expect(angular.element(button).prop('disabled')).toBe( index < 14 );
});
});
it('disables appropriate days when min date changes', function() {
$rootScope.mindate = new Date('September 5, 2010');
$rootScope.$digest();
var buttons = getAllOptionsEl();
angular.forEach(buttons, function( button, index ) {
expect(angular.element(button).prop('disabled')).toBe( index < 7 );
});
});
it('invalidates when model is a disabled date', function() {
$rootScope.mindate = new Date('September 5, 2010');
$rootScope.date = new Date('September 2, 2010');
$rootScope.$digest();
expect(element.hasClass('ng-invalid')).toBeTruthy();
expect(element.hasClass('ng-invalid-date-disabled')).toBeTruthy();
});
it('disables all days in previous month', function() {
clickPreviousButton();
var buttons = getAllOptionsEl();
angular.forEach(buttons, function( button, index ) {
expect(angular.element(button).prop('disabled')).toBe( true );
});
});
it('disables no days in next month', function() {
clickNextButton();
var buttons = getAllOptionsEl();
angular.forEach(buttons, function( button, index ) {
expect(angular.element(button).prop('disabled')).toBe( false );
});
});
it('disables appropriate months in current year', function() {
clickTitleButton();
var buttons = getAllOptionsEl();
angular.forEach(buttons, function( button, index ) {
expect(angular.element(button).prop('disabled')).toBe( index < 8 );
});
});
it('disables all months in previous year', function() {
clickTitleButton();
clickPreviousButton();
var buttons = getAllOptionsEl();
angular.forEach(buttons, function( button, index ) {
expect(angular.element(button).prop('disabled')).toBe( true );
});
});
it('disables no months in next year', function() {
clickTitleButton();
clickNextButton();
var buttons = getAllOptionsEl();
angular.forEach(buttons, function( button, index ) {
expect(angular.element(button).prop('disabled')).toBe( false );
});
});
it('enables everything before if it is cleared', function() {
$rootScope.mindate = null;
$rootScope.date = new Date('December 20, 1949');
$rootScope.$digest();
clickTitleButton();
var buttons = getAllOptionsEl();
angular.forEach(buttons, function( button, index ) {
expect(angular.element(button).prop('disabled')).toBe( false );
});
});
});
describe('`max-date` attribute', function () {
beforeEach(function() {
$rootScope.maxdate = new Date('September 25, 2010');
element = $compile('<datepicker ng-model="date" max-date="maxdate"></datepicker>')($rootScope);
$rootScope.$digest();
});
it('disables appropriate days in current month', function() {
var buttons = getAllOptionsEl();
angular.forEach(buttons, function( button, index ) {
expect(angular.element(button).prop('disabled')).toBe( index > 27 );
});
});
it('disables appropriate days when max date changes', function() {
$rootScope.maxdate = new Date('September 18, 2010');
$rootScope.$digest();
var buttons = getAllOptionsEl();
angular.forEach(buttons, function( button, index ) {
expect(angular.element(button).prop('disabled')).toBe( index > 20 );