/
datepicker.spec.ts
2683 lines (2115 loc) · 89.4 KB
/
datepicker.spec.ts
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
import {Directionality} from '@angular/cdk/bidi';
import {DOWN_ARROW, ENTER, ESCAPE, RIGHT_ARROW, UP_ARROW} from '@angular/cdk/keycodes';
import {Overlay} from '@angular/cdk/overlay';
import {ScrollDispatcher} from '@angular/cdk/scrolling';
import {
createKeyboardEvent,
dispatchEvent,
dispatchFakeEvent,
dispatchKeyboardEvent,
dispatchMouseEvent,
} from '../../cdk/testing/private';
import {Component, Type, ViewChild, Provider, Directive, ViewEncapsulation} from '@angular/core';
import {ComponentFixture, fakeAsync, flush, inject, TestBed, tick} from '@angular/core/testing';
import {
FormControl,
FormsModule,
NgModel,
ReactiveFormsModule,
Validator,
NG_VALIDATORS,
} from '@angular/forms';
import {MAT_DATE_LOCALE, MatNativeDateModule, NativeDateModule} from '@angular/material/core';
import {MatFormField, MatFormFieldModule} from '@angular/material/form-field';
import {DEC, JAN, JUL, JUN, SEP} from '../testing';
import {By} from '@angular/platform-browser';
import {_supportsShadowDom} from '@angular/cdk/platform';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {Subject} from 'rxjs';
import {MatInputModule} from '../input/index';
import {MatDatepicker} from './datepicker';
import {MatDatepickerInput} from './datepicker-input';
import {MatDatepickerToggle} from './datepicker-toggle';
import {
MAT_DATEPICKER_SCROLL_STRATEGY,
MatDatepickerIntl,
MatDatepickerModule,
MatDateSelectionModel,
} from './index';
import {DatepickerDropdownPositionX, DatepickerDropdownPositionY} from './datepicker-base';
describe('MatDatepicker', () => {
const SUPPORTS_INTL = typeof Intl != 'undefined';
// Creates a test component fixture.
function createComponent<T>(
component: Type<T>,
imports: Type<any>[] = [],
providers: Provider[] = [],
declarations: Type<any>[] = [],
): ComponentFixture<T> {
TestBed.configureTestingModule({
imports: [
FormsModule,
MatDatepickerModule,
MatFormFieldModule,
MatInputModule,
NoopAnimationsModule,
ReactiveFormsModule,
...imports,
],
providers,
declarations: [component, ...declarations],
});
return TestBed.createComponent(component);
}
describe('with MatNativeDateModule', () => {
describe('standard datepicker', () => {
let fixture: ComponentFixture<StandardDatepicker>;
let testComponent: StandardDatepicker;
let model: MatDateSelectionModel<Date | null, Date>;
beforeEach(fakeAsync(() => {
fixture = createComponent(StandardDatepicker, [MatNativeDateModule]);
fixture.detectChanges();
testComponent = fixture.componentInstance;
model = fixture.debugElement
.query(By.directive(MatDatepicker))
.injector.get(MatDateSelectionModel);
}));
afterEach(fakeAsync(() => {
testComponent.datepicker.close();
fixture.detectChanges();
flush();
}));
it('should initialize with correct value shown in input', () => {
if (SUPPORTS_INTL) {
expect(fixture.nativeElement.querySelector('input').value).toBe('1/1/2020');
}
});
it('open non-touch should open popup', () => {
expect(document.querySelector('.cdk-overlay-pane.mat-datepicker-popup')).toBeNull();
testComponent.datepicker.open();
fixture.detectChanges();
expect(document.querySelector('.cdk-overlay-pane.mat-datepicker-popup')).not.toBeNull();
});
it('touch should open dialog', () => {
testComponent.touch = true;
fixture.detectChanges();
expect(document.querySelector('.mat-datepicker-dialog')).toBeNull();
testComponent.datepicker.open();
fixture.detectChanges();
expect(document.querySelector('.mat-datepicker-dialog')).not.toBeNull();
});
it('should not be able to open more than one dialog', fakeAsync(() => {
testComponent.touch = true;
fixture.detectChanges();
expect(document.querySelectorAll('.mat-datepicker-dialog').length).toBe(0);
testComponent.datepicker.open();
fixture.detectChanges();
tick(500);
fixture.detectChanges();
dispatchKeyboardEvent(document.querySelector('.mat-calendar-body')!, 'keydown', ENTER);
fixture.detectChanges();
tick(100);
testComponent.datepicker.open();
tick(500);
fixture.detectChanges();
expect(document.querySelectorAll('.mat-datepicker-dialog').length).toBe(1);
}));
it('should open datepicker if opened input is set to true', fakeAsync(() => {
testComponent.opened = true;
fixture.detectChanges();
flush();
expect(document.querySelector('.mat-datepicker-content')).not.toBeNull();
testComponent.opened = false;
fixture.detectChanges();
flush();
expect(document.querySelector('.mat-datepicker-content')).toBeNull();
}));
it('open in disabled mode should not open the calendar', () => {
testComponent.disabled = true;
fixture.detectChanges();
expect(document.querySelector('.cdk-overlay-pane')).toBeNull();
expect(document.querySelector('.mat-datepicker-dialog')).toBeNull();
testComponent.datepicker.open();
fixture.detectChanges();
expect(document.querySelector('.cdk-overlay-pane')).toBeNull();
expect(document.querySelector('.mat-datepicker-dialog')).toBeNull();
});
it('disabled datepicker input should open the calendar if datepicker is enabled', () => {
testComponent.datepicker.disabled = false;
testComponent.datepickerInput.disabled = true;
fixture.detectChanges();
expect(document.querySelector('.cdk-overlay-pane')).toBeNull();
testComponent.datepicker.open();
fixture.detectChanges();
expect(document.querySelector('.cdk-overlay-pane')).not.toBeNull();
});
it('close should close popup', fakeAsync(() => {
testComponent.datepicker.open();
fixture.detectChanges();
flush();
const popup = document.querySelector('.cdk-overlay-pane')!;
expect(popup).not.toBeNull();
expect(popup.getBoundingClientRect().height).toBeGreaterThan(0);
testComponent.datepicker.close();
fixture.detectChanges();
flush();
expect(popup.getBoundingClientRect().height).toBe(0);
}));
it('should close the popup when pressing ESCAPE', fakeAsync(() => {
testComponent.datepicker.open();
fixture.detectChanges();
expect(testComponent.datepicker.opened)
.withContext('Expected datepicker to be open.')
.toBe(true);
const event = dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
fixture.detectChanges();
flush();
expect(testComponent.datepicker.opened)
.withContext('Expected datepicker to be closed.')
.toBe(false);
expect(event.defaultPrevented).toBe(true);
}));
it('should not close the popup when pressing ESCAPE with a modifier key', fakeAsync(() => {
testComponent.datepicker.open();
fixture.detectChanges();
expect(testComponent.datepicker.opened)
.withContext('Expected datepicker to be open.')
.toBe(true);
const event = dispatchKeyboardEvent(document.body, 'keydown', ESCAPE, undefined, {
alt: true,
});
fixture.detectChanges();
flush();
expect(testComponent.datepicker.opened)
.withContext('Expected datepicker to stay open.')
.toBe(true);
expect(event.defaultPrevented).toBe(false);
}));
it('should set the proper role on the popup', fakeAsync(() => {
testComponent.datepicker.open();
fixture.detectChanges();
flush();
const popup = document.querySelector('.mat-datepicker-content-container')!;
expect(popup).toBeTruthy();
expect(popup.getAttribute('role')).toBe('dialog');
}));
it(
'should set aria-labelledby to the one from the input, if not placed inside ' +
'a mat-form-field',
fakeAsync(() => {
expect(fixture.nativeElement.querySelector('mat-form-field')).toBeFalsy();
const input: HTMLInputElement = fixture.nativeElement.querySelector('input');
input.setAttribute('aria-labelledby', 'test-label');
testComponent.datepicker.open();
fixture.detectChanges();
flush();
const popup = document.querySelector(
'.cdk-overlay-pane .mat-datepicker-content-container',
)!;
expect(popup).toBeTruthy();
expect(popup.getAttribute('aria-labelledby')).toBe('test-label');
}),
);
it('close should close dialog', fakeAsync(() => {
testComponent.touch = true;
fixture.detectChanges();
testComponent.datepicker.open();
fixture.detectChanges();
expect(document.querySelector('.mat-datepicker-dialog')).not.toBeNull();
testComponent.datepicker.close();
fixture.detectChanges();
flush();
expect(document.querySelector('.mat-datepicker-dialog')).toBeNull();
}));
it('setting selected via click should update input and close calendar', fakeAsync(() => {
testComponent.touch = true;
fixture.detectChanges();
testComponent.datepicker.open();
fixture.detectChanges();
flush();
expect(document.querySelector('.mat-datepicker-dialog')).not.toBeNull();
expect(testComponent.datepickerInput.value).toEqual(new Date(2020, JAN, 1));
let cells = document.querySelectorAll('.mat-calendar-body-cell');
dispatchMouseEvent(cells[1], 'click');
fixture.detectChanges();
flush();
expect(document.querySelector('.mat-datepicker-dialog')).toBeNull();
expect(testComponent.datepickerInput.value).toEqual(new Date(2020, JAN, 2));
}));
it('setting selected via enter press should update input and close calendar', fakeAsync(() => {
testComponent.touch = true;
fixture.detectChanges();
testComponent.datepicker.open();
fixture.detectChanges();
flush();
expect(document.querySelector('.mat-datepicker-dialog')).not.toBeNull();
expect(testComponent.datepickerInput.value).toEqual(new Date(2020, JAN, 1));
let calendarBodyEl = document.querySelector('.mat-calendar-body') as HTMLElement;
dispatchKeyboardEvent(calendarBodyEl, 'keydown', RIGHT_ARROW);
fixture.detectChanges();
flush();
dispatchKeyboardEvent(calendarBodyEl, 'keydown', ENTER);
fixture.detectChanges();
dispatchKeyboardEvent(calendarBodyEl, 'keyup', ENTER);
fixture.detectChanges();
flush();
expect(document.querySelector('.mat-datepicker-dialog')).toBeNull();
expect(testComponent.datepickerInput.value).toEqual(new Date(2020, JAN, 2));
}));
it(
'clicking the currently selected date should close the calendar ' +
'without firing selectedChanged',
fakeAsync(() => {
const spy = jasmine.createSpy('selectionChanged spy');
const selectedSubscription = model.selectionChanged.subscribe(spy);
for (let changeCount = 1; changeCount < 3; changeCount++) {
const currentDay = changeCount;
testComponent.datepicker.open();
fixture.detectChanges();
expect(document.querySelector('mat-datepicker-content')).not.toBeNull();
expect(testComponent.datepickerInput.value).toEqual(new Date(2020, JAN, currentDay));
let cells = document.querySelectorAll('.mat-calendar-body-cell');
dispatchMouseEvent(cells[1], 'click');
fixture.detectChanges();
flush();
}
expect(spy).toHaveBeenCalledTimes(1);
expect(document.querySelector('.mat-datepicker-dialog')).toBeNull();
expect(testComponent.datepickerInput.value).toEqual(new Date(2020, JAN, 2));
selectedSubscription.unsubscribe();
}),
);
it(
'pressing enter on the currently selected date should close the calendar without ' +
'firing selectedChanged',
() => {
const spy = jasmine.createSpy('selectionChanged spy');
const selectedSubscription = model.selectionChanged.subscribe(spy);
testComponent.datepicker.open();
fixture.detectChanges();
let calendarBodyEl = document.querySelector('.mat-calendar-body') as HTMLElement;
expect(calendarBodyEl).not.toBeNull();
expect(testComponent.datepickerInput.value).toEqual(new Date(2020, JAN, 1));
dispatchKeyboardEvent(calendarBodyEl, 'keydown', ENTER);
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(spy).not.toHaveBeenCalled();
expect(document.querySelector('.mat-datepicker-dialog')).toBeNull();
expect(testComponent.datepickerInput.value).toEqual(new Date(2020, JAN, 1));
selectedSubscription.unsubscribe();
});
},
);
it('startAt should fallback to input value', () => {
expect(testComponent.datepicker.startAt).toEqual(new Date(2020, JAN, 1));
});
it('should attach popup to native input', () => {
let attachToRef = testComponent.datepickerInput.getConnectedOverlayOrigin();
expect(attachToRef.nativeElement.tagName.toLowerCase())
.withContext('popup should be attached to native input')
.toBe('input');
});
it('input should aria-owns calendar after opened in non-touch mode', fakeAsync(() => {
let inputEl = fixture.debugElement.query(By.css('input'))!.nativeElement;
expect(inputEl.getAttribute('aria-owns')).toBeNull();
testComponent.datepicker.open();
fixture.detectChanges();
flush();
let ownedElementId = inputEl.getAttribute('aria-owns');
expect(ownedElementId).not.toBeNull();
let ownedElement = document.getElementById(ownedElementId);
expect(ownedElement).not.toBeNull();
expect((ownedElement as Element).tagName.toLowerCase()).toBe('mat-calendar');
}));
it('input should aria-owns calendar after opened in touch mode', () => {
testComponent.touch = true;
fixture.detectChanges();
let inputEl = fixture.debugElement.query(By.css('input'))!.nativeElement;
expect(inputEl.getAttribute('aria-owns')).toBeNull();
testComponent.datepicker.open();
fixture.detectChanges();
let ownedElementId = inputEl.getAttribute('aria-owns');
expect(ownedElementId).not.toBeNull();
let ownedElement = document.getElementById(ownedElementId);
expect(ownedElement).not.toBeNull();
expect((ownedElement as Element).tagName.toLowerCase()).toBe('mat-calendar');
});
it('should not throw when given wrong data type', () => {
testComponent.date = '1/1/2017' as any;
expect(() => fixture.detectChanges()).not.toThrow();
});
it('should clear out the backdrop subscriptions on close', fakeAsync(() => {
for (let i = 0; i < 3; i++) {
testComponent.datepicker.open();
fixture.detectChanges();
testComponent.datepicker.close();
fixture.detectChanges();
}
testComponent.datepicker.open();
fixture.detectChanges();
const spy = jasmine.createSpy('close event spy');
const subscription = testComponent.datepicker.closedStream.subscribe(spy);
const backdrop = document.querySelector('.cdk-overlay-backdrop')! as HTMLElement;
backdrop.click();
fixture.detectChanges();
flush();
expect(spy).toHaveBeenCalledTimes(1);
expect(testComponent.datepicker.opened).toBe(false);
subscription.unsubscribe();
}));
it('should reset the datepicker when it is closed externally', fakeAsync(() => {
TestBed.resetTestingModule();
const scrolledSubject = new Subject();
// Stub out a `CloseScrollStrategy` so we can trigger a detachment via the `OverlayRef`.
fixture = createComponent(
StandardDatepicker,
[MatNativeDateModule],
[
{
provide: ScrollDispatcher,
useValue: {scrolled: () => scrolledSubject},
},
{
provide: MAT_DATEPICKER_SCROLL_STRATEGY,
deps: [Overlay],
useFactory: (overlay: Overlay) => () => overlay.scrollStrategies.close(),
},
],
);
fixture.detectChanges();
testComponent = fixture.componentInstance;
testComponent.datepicker.open();
fixture.detectChanges();
expect(testComponent.datepicker.opened).toBe(true);
scrolledSubject.next();
flush();
fixture.detectChanges();
expect(testComponent.datepicker.opened).toBe(false);
}));
it('should close the datepicker using ALT + UP_ARROW', fakeAsync(() => {
testComponent.datepicker.open();
fixture.detectChanges();
flush();
expect(testComponent.datepicker.opened).toBe(true);
const event = createKeyboardEvent('keydown', UP_ARROW, undefined, {alt: true});
dispatchEvent(document.body, event);
fixture.detectChanges();
flush();
expect(testComponent.datepicker.opened).toBe(false);
}));
it('should open the datepicker using ALT + DOWN_ARROW', fakeAsync(() => {
expect(testComponent.datepicker.opened).toBe(false);
const event = createKeyboardEvent('keydown', DOWN_ARROW, undefined, {alt: true});
dispatchEvent(fixture.nativeElement.querySelector('input'), event);
fixture.detectChanges();
flush();
expect(testComponent.datepicker.opened).toBe(true);
expect(event.defaultPrevented).toBe(true);
}));
it('should not open for ALT + DOWN_ARROW on readonly input', fakeAsync(() => {
const input = fixture.nativeElement.querySelector('input');
expect(testComponent.datepicker.opened).toBe(false);
input.setAttribute('readonly', 'true');
const event = createKeyboardEvent('keydown', DOWN_ARROW, undefined, {alt: true});
dispatchEvent(input, event);
fixture.detectChanges();
flush();
expect(testComponent.datepicker.opened).toBe(false);
expect(event.defaultPrevented).toBe(false);
}));
it('should show the invisible close button on focus', fakeAsync(() => {
testComponent.opened = true;
fixture.detectChanges();
flush();
const button = document.querySelector('.mat-datepicker-close-button') as HTMLButtonElement;
expect(button.classList).toContain('cdk-visually-hidden');
dispatchFakeEvent(button, 'focus');
fixture.detectChanges();
expect(button.classList).not.toContain('cdk-visually-hidden');
dispatchFakeEvent(button, 'blur');
fixture.detectChanges();
expect(button.classList).toContain('cdk-visually-hidden');
}));
it('should close the overlay when clicking on the invisible close button', fakeAsync(() => {
testComponent.opened = true;
fixture.detectChanges();
flush();
const button = document.querySelector('.mat-datepicker-close-button') as HTMLButtonElement;
expect(document.querySelector('.mat-datepicker-content')).not.toBeNull();
button.click();
fixture.detectChanges();
flush();
expect(document.querySelector('.mat-datepicker-content')).toBeNull();
}));
});
describe('datepicker with too many inputs', () => {
it('should throw when multiple inputs registered', fakeAsync(() => {
const fixture = createComponent(MultiInputDatepicker, [MatNativeDateModule]);
expect(() => fixture.detectChanges()).toThrow();
}));
});
describe('datepicker that is assigned to input at a later point', () => {
it('should not throw on ALT + DOWN_ARROW for input without datepicker', fakeAsync(() => {
const fixture = createComponent(DelayedDatepicker, [MatNativeDateModule]);
fixture.detectChanges();
expect(() => {
const event = createKeyboardEvent('keydown', DOWN_ARROW, undefined, {alt: true});
dispatchEvent(fixture.nativeElement.querySelector('input'), event);
fixture.detectChanges();
flush();
}).not.toThrow();
}));
it('should handle value changes when a datepicker is assigned after init', fakeAsync(() => {
const fixture = createComponent(DelayedDatepicker, [MatNativeDateModule]);
const testComponent: DelayedDatepicker = fixture.componentInstance;
const toSelect = new Date(2017, JAN, 1);
fixture.detectChanges();
const model = fixture.debugElement
.query(By.directive(MatDatepicker))
.injector.get(MatDateSelectionModel);
expect(testComponent.datepickerInput.value).toBeNull();
expect(model.selection).toBeNull();
testComponent.assignedDatepicker = testComponent.datepicker;
fixture.detectChanges();
testComponent.assignedDatepicker.select(toSelect);
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(testComponent.datepickerInput.value).toEqual(toSelect);
expect(model.selection).toEqual(toSelect);
}));
});
describe('datepicker with no inputs', () => {
let fixture: ComponentFixture<NoInputDatepicker>;
let testComponent: NoInputDatepicker;
beforeEach(fakeAsync(() => {
fixture = createComponent(NoInputDatepicker, [MatNativeDateModule]);
fixture.detectChanges();
testComponent = fixture.componentInstance;
}));
afterEach(fakeAsync(() => {
testComponent.datepicker.close();
fixture.detectChanges();
}));
it('should not throw when accessing disabled property', () => {
expect(() => testComponent.datepicker.disabled).not.toThrow();
});
it('should throw when opened with no registered inputs', fakeAsync(() => {
expect(() => testComponent.datepicker.open()).toThrow();
}));
});
describe('datepicker with startAt', () => {
let fixture: ComponentFixture<DatepickerWithStartAt>;
let testComponent: DatepickerWithStartAt;
beforeEach(fakeAsync(() => {
fixture = createComponent(DatepickerWithStartAt, [MatNativeDateModule]);
fixture.detectChanges();
testComponent = fixture.componentInstance;
}));
afterEach(fakeAsync(() => {
testComponent.datepicker.close();
fixture.detectChanges();
}));
it('explicit startAt should override input value', () => {
expect(testComponent.datepicker.startAt).toEqual(new Date(2010, JAN, 1));
});
});
describe('datepicker with startView set to year', () => {
let fixture: ComponentFixture<DatepickerWithStartViewYear>;
let testComponent: DatepickerWithStartViewYear;
beforeEach(fakeAsync(() => {
fixture = createComponent(DatepickerWithStartViewYear, [MatNativeDateModule]);
fixture.detectChanges();
testComponent = fixture.componentInstance;
}));
afterEach(fakeAsync(() => {
testComponent.datepicker.close();
fixture.detectChanges();
flush();
}));
it('should start at the specified view', () => {
testComponent.datepicker.open();
fixture.detectChanges();
const firstCalendarCell = document.querySelector('.mat-calendar-body-cell')!;
// When the calendar is in year view, the first cell should be for a month rather than
// for a date.
// When the calendar is in year view, the first cell should be for a month rather than
// for a date.
expect(firstCalendarCell.textContent!.trim())
.withContext('Expected the calendar to be in year-view')
.toBe('JAN');
});
it('should fire yearSelected when user selects calendar year in year view', fakeAsync(() => {
spyOn(testComponent, 'onYearSelection');
expect(testComponent.onYearSelection).not.toHaveBeenCalled();
testComponent.datepicker.open();
fixture.detectChanges();
const cells = document.querySelectorAll('.mat-calendar-body-cell');
dispatchMouseEvent(cells[0], 'click');
fixture.detectChanges();
flush();
expect(testComponent.onYearSelection).toHaveBeenCalled();
}));
});
describe('datepicker with startView set to multiyear', () => {
let fixture: ComponentFixture<DatepickerWithStartViewMultiYear>;
let testComponent: DatepickerWithStartViewMultiYear;
beforeEach(fakeAsync(() => {
fixture = createComponent(DatepickerWithStartViewMultiYear, [MatNativeDateModule]);
fixture.detectChanges();
testComponent = fixture.componentInstance;
spyOn(testComponent, 'onMultiYearSelection');
}));
afterEach(fakeAsync(() => {
testComponent.datepicker.close();
fixture.detectChanges();
flush();
}));
it('should start at the specified view', () => {
testComponent.datepicker.open();
fixture.detectChanges();
const firstCalendarCell = document.querySelector('.mat-calendar-body-cell')!;
// When the calendar is in year view, the first cell should be for a month rather than
// for a date.
// When the calendar is in year view, the first cell should be for a month rather than
// for a date.
expect(firstCalendarCell.textContent!.trim())
.withContext('Expected the calendar to be in multi-year-view')
.toBe('2016');
});
it('should fire yearSelected when user selects calendar year in multiyear view', fakeAsync(() => {
expect(testComponent.onMultiYearSelection).not.toHaveBeenCalled();
testComponent.datepicker.open();
fixture.detectChanges();
const cells = document.querySelectorAll('.mat-calendar-body-cell');
dispatchMouseEvent(cells[0], 'click');
fixture.detectChanges();
flush();
expect(testComponent.onMultiYearSelection).toHaveBeenCalled();
}));
});
describe('datepicker with ngModel', () => {
let fixture: ComponentFixture<DatepickerWithNgModel>;
let testComponent: DatepickerWithNgModel;
let model: MatDateSelectionModel<Date | null, Date>;
beforeEach(fakeAsync(() => {
fixture = createComponent(DatepickerWithNgModel, [MatNativeDateModule]);
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
testComponent = fixture.componentInstance;
model = fixture.debugElement
.query(By.directive(MatDatepicker))
.injector.get(MatDateSelectionModel);
});
}));
afterEach(fakeAsync(() => {
testComponent.datepicker.close();
fixture.detectChanges();
}));
it('should update datepicker when model changes', fakeAsync(() => {
expect(testComponent.datepickerInput.value).toBeNull();
expect(model.selection).toBeNull();
let selected = new Date(2017, JAN, 1);
testComponent.selected = selected;
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(testComponent.datepickerInput.value).toEqual(selected);
expect(model.selection).toEqual(selected);
}));
it('should update model when date is selected', fakeAsync(() => {
expect(testComponent.selected).toBeNull();
expect(testComponent.datepickerInput.value).toBeNull();
let selected = new Date(2017, JAN, 1);
testComponent.datepicker.select(selected);
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(testComponent.selected).toEqual(selected);
expect(testComponent.datepickerInput.value).toEqual(selected);
}));
it('should mark input dirty after input event', () => {
let inputEl = fixture.debugElement.query(By.css('input'))!.nativeElement;
expect(inputEl.classList).toContain('ng-pristine');
inputEl.value = '2001-01-01';
dispatchFakeEvent(inputEl, 'input');
fixture.detectChanges();
expect(inputEl.classList).toContain('ng-dirty');
});
it('should mark input dirty after date selected', fakeAsync(() => {
let inputEl = fixture.debugElement.query(By.css('input'))!.nativeElement;
expect(inputEl.classList).toContain('ng-pristine');
testComponent.datepicker.select(new Date(2017, JAN, 1));
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(inputEl.classList).toContain('ng-dirty');
}));
it('should mark input dirty after invalid value is typed in', () => {
let inputEl = fixture.debugElement.query(By.css('input'))!.nativeElement;
expect(inputEl.classList).toContain('ng-pristine');
inputEl.value = 'hello there';
dispatchFakeEvent(inputEl, 'input');
fixture.detectChanges();
expect(inputEl.classList).toContain('ng-dirty');
});
it('should not mark dirty after model change', fakeAsync(() => {
let inputEl = fixture.debugElement.query(By.css('input'))!.nativeElement;
expect(inputEl.classList).toContain('ng-pristine');
testComponent.selected = new Date(2017, JAN, 1);
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(inputEl.classList).toContain('ng-pristine');
}));
it('should mark input touched on blur', () => {
let inputEl = fixture.debugElement.query(By.css('input'))!.nativeElement;
expect(inputEl.classList).toContain('ng-untouched');
dispatchFakeEvent(inputEl, 'focus');
fixture.detectChanges();
expect(inputEl.classList).toContain('ng-untouched');
dispatchFakeEvent(inputEl, 'blur');
fixture.detectChanges();
expect(inputEl.classList).toContain('ng-touched');
});
it('should mark input as touched when the datepicker is closed', fakeAsync(() => {
let inputEl = fixture.debugElement.query(By.css('input'))!.nativeElement;
expect(inputEl.classList).toContain('ng-untouched');
fixture.componentInstance.datepicker.open();
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(inputEl.classList).toContain('ng-untouched');
fixture.componentInstance.datepicker.close();
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(inputEl.classList).toContain('ng-touched');
}));
it('should reformat the input value on blur', () => {
if (SUPPORTS_INTL) {
// Skip this test if the internationalization API is not supported in the current
// browser. Browsers like Safari 9 do not support the "Intl" API.
return;
}
const inputEl = fixture.debugElement.query(By.css('input'))!.nativeElement;
inputEl.value = '2001-01-01';
dispatchFakeEvent(inputEl, 'input');
fixture.detectChanges();
dispatchFakeEvent(inputEl, 'blur');
fixture.detectChanges();
expect(inputEl.value).toBe('1/1/2001');
});
it('should not reformat invalid dates on blur', () => {
const inputEl = fixture.debugElement.query(By.css('input'))!.nativeElement;
inputEl.value = 'very-valid-date';
dispatchFakeEvent(inputEl, 'input');
fixture.detectChanges();
dispatchFakeEvent(inputEl, 'blur');
fixture.detectChanges();
expect(inputEl.value).toBe('very-valid-date');
});
it('should mark input touched on calendar selection', fakeAsync(() => {
let inputEl = fixture.debugElement.query(By.css('input'))!.nativeElement;
expect(inputEl.classList).toContain('ng-untouched');
testComponent.datepicker.select(new Date(2017, JAN, 1));
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(inputEl.classList).toContain('ng-touched');
}));
});
describe('datepicker with formControl', () => {
let fixture: ComponentFixture<DatepickerWithFormControl>;
let testComponent: DatepickerWithFormControl;
let model: MatDateSelectionModel<Date | null, Date>;
beforeEach(fakeAsync(() => {
fixture = createComponent(DatepickerWithFormControl, [MatNativeDateModule]);
fixture.detectChanges();
testComponent = fixture.componentInstance;
model = fixture.debugElement
.query(By.directive(MatDatepicker))
.injector.get(MatDateSelectionModel);
}));
afterEach(fakeAsync(() => {
testComponent.datepicker.close();
fixture.detectChanges();
}));
it('should update datepicker when formControl changes', () => {
expect(testComponent.datepickerInput.value).toBeNull();
expect(model.selection).toBeNull();
let selected = new Date(2017, JAN, 1);
testComponent.formControl.setValue(selected);
fixture.detectChanges();
expect(testComponent.datepickerInput.value).toEqual(selected);
expect(model.selection).toEqual(selected);
});
it('should update formControl when date is selected', () => {
expect(testComponent.formControl.value).toBeNull();
expect(testComponent.datepickerInput.value).toBeNull();
let selected = new Date(2017, JAN, 1);
testComponent.datepicker.select(selected);
fixture.detectChanges();
expect(testComponent.formControl.value).toEqual(selected);
expect(testComponent.datepickerInput.value).toEqual(selected);
});
it('should disable input when form control disabled', () => {
let inputEl = fixture.debugElement.query(By.css('input'))!.nativeElement;
expect(inputEl.disabled).toBe(false);
testComponent.formControl.disable();
fixture.detectChanges();
expect(inputEl.disabled).toBe(true);