-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
date-picker.component.spec.ts
1260 lines (1102 loc) · 46.3 KB
/
date-picker.component.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 { ESCAPE } from '@angular/cdk/keycodes';
import { OverlayContainer } from '@angular/cdk/overlay';
import { registerLocaleData } from '@angular/common';
import zh from '@angular/common/locales/zh';
import { ApplicationRef, Component, DebugElement, TemplateRef, ViewChild } from '@angular/core';
import { ComponentFixture, fakeAsync, flush, inject, TestBed, tick } from '@angular/core/testing';
import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import isEqual from 'date-fns/isEqual';
import isSameDay from 'date-fns/isSameDay';
import { enUS } from 'date-fns/locale';
import {
dispatchFakeEvent,
dispatchKeyboardEvent,
dispatchMouseEvent,
typeInElement
} from 'ng-zorro-antd/core/testing';
import { NgStyleInterface } from 'ng-zorro-antd/core/types';
import { NzI18nModule, NzI18nService, NZ_DATE_LOCALE } from 'ng-zorro-antd/i18n';
import en_US from '../i18n/languages/en_US';
import { NzDatePickerComponent } from './date-picker.component';
import { NzDatePickerModule } from './date-picker.module';
import { ENTER_EVENT, getPickerAbstract, getPickerInput } from './testing/util';
import { PREFIX_CLASS } from './util';
registerLocaleData(zh);
describe('NzDatePickerComponent', () => {
let fixture: ComponentFixture<NzTestDatePickerComponent>;
let fixtureInstance: NzTestDatePickerComponent;
let debugElement: DebugElement;
let overlayContainer: OverlayContainer;
let overlayContainerElement: HTMLElement;
let i18nService: NzI18nService;
beforeEach(fakeAsync(() => {
const dir = 'ltr';
TestBed.configureTestingModule({
imports: [FormsModule, NoopAnimationsModule, NzDatePickerModule, NzI18nModule, ReactiveFormsModule],
providers: [
{ provide: Directionality, useFactory: () => ({ value: dir }) }
// { provide: NZ_DATE_CONFIG, useValue: { firstDayOfWeek: 1 } }
],
declarations: [NzTestDatePickerComponent]
});
TestBed.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(NzTestDatePickerComponent);
fixtureInstance = fixture.componentInstance;
debugElement = fixture.debugElement;
});
beforeEach(inject([OverlayContainer, NzI18nService], (oc: OverlayContainer, i18n: NzI18nService) => {
overlayContainer = oc;
overlayContainerElement = oc.getContainerElement();
i18nService = i18n;
}));
afterEach(() => {
overlayContainer.ngOnDestroy();
});
describe('general api testing', () => {
beforeEach(() => (fixtureInstance.useSuite = 1));
it('should open by click and close by click at outside', fakeAsync(() => {
fixture.detectChanges();
openPickerByClickTrigger();
expect(getPickerContainer()).not.toBeNull();
triggerInputBlur();
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerContainer()).toBeNull();
}));
it('should open and close method work', fakeAsync(() => {
fixture.detectChanges();
fixtureInstance.datePicker.open();
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerContainer()).not.toBeNull();
fixtureInstance.datePicker.close();
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerContainer()).toBeNull();
}));
it('should focus on the trigger after a click outside', fakeAsync(() => {
fixture.detectChanges();
openPickerByClickTrigger();
triggerInputBlur();
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(document.activeElement).toEqual(getPickerInput(fixture.debugElement));
}));
it('should open on enter', fakeAsync(() => {
fixture.detectChanges();
getPickerInput(fixture.debugElement).dispatchEvent(ENTER_EVENT);
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerContainer()).toBeNull();
getPickerInput(fixture.debugElement).focus();
getPickerInput(fixture.debugElement).dispatchEvent(ENTER_EVENT);
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerContainer()).not.toBeNull();
}));
it('should open by click and focus on inner calendar input', fakeAsync(() => {
fixture.detectChanges();
openPickerByClickTrigger();
expect(document.activeElement).toEqual(getPickerInput(fixture.debugElement));
}));
it('should open by click, focus on inner calendar input, and submit on enter', fakeAsync(() => {
fixtureInstance.nzValue = new Date();
fixture.detectChanges();
openPickerByClickTrigger();
expect(document.activeElement).toEqual(getPickerInput(fixture.debugElement));
getPickerInput(fixture.debugElement).dispatchEvent(ENTER_EVENT);
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerContainer()).toBeNull();
}));
it('should not submit with invalid input', fakeAsync(() => {
fixture.detectChanges();
openPickerByClickTrigger();
const input = getPickerInput(fixture.debugElement) as HTMLInputElement;
input.value = 'invalid input';
fixture.detectChanges();
input.dispatchEvent(ENTER_EVENT);
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerContainer()).not.toBeNull();
}));
it('should open by click and close by tab', fakeAsync(() => {
const nzOnChange = spyOn(fixtureInstance, 'nzOnChange');
fixtureInstance.useSuite = 5;
fixture.detectChanges();
openPickerByClickTrigger();
expect(getPickerContainer()).not.toBeNull();
typeInElement('2021-04-12', getPickerInput(fixture.debugElement));
fixture.detectChanges();
triggerInputBlur();
fixture.detectChanges();
tick(500);
fixture.detectChanges();
const result = (nzOnChange.calls.allArgs()[0] as Date[])[0];
expect(isSameDay(new Date('2021-04-12'), result)).toBeTruthy();
expect(getPickerContainer()).toBeNull();
}));
it('should support changing language at runtime', fakeAsync(() => {
fixture.detectChanges();
expect(getPickerInput(fixture.debugElement).placeholder).toBe('请选择日期');
i18nService.setLocale(en_US);
// i18nService.setDateLocale(en);
fixture.detectChanges();
expect(getPickerInput(fixture.debugElement).placeholder).toBe('Select date');
openPickerByClickTrigger();
expect(queryFromOverlay(`.${PREFIX_CLASS}-content th`).textContent).toContain('Su');
}));
/* Issue https://github.com/NG-ZORRO/ng-zorro-antd/issues/1539 */
it('should be openable after closed by "Escape" key', fakeAsync(() => {
fixture.detectChanges();
openPickerByClickTrigger();
expect(getPickerContainer()).not.toBeNull();
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerContainer()).toBeNull();
openPickerByClickTrigger();
expect(getPickerContainer()).not.toBeNull();
}));
it('should support nzAllowClear and work properly', fakeAsync(() => {
const clearBtnSelector = By.css(`.${PREFIX_CLASS}-clear`);
const initial = (fixtureInstance.nzValue = new Date());
fixtureInstance.nzAllowClear = false;
fixture.detectChanges();
expect(debugElement.query(clearBtnSelector)).toBeFalsy();
fixtureInstance.nzAllowClear = true;
tick(500);
fixture.detectChanges();
expect(fixtureInstance.nzValue).toBe(initial);
expect(debugElement.query(clearBtnSelector)).toBeDefined();
const nzOnChange = spyOn(fixtureInstance, 'nzOnChange');
debugElement.query(clearBtnSelector).nativeElement.click();
fixture.detectChanges();
expect(fixtureInstance.nzValue).toBe(initial);
expect(nzOnChange).toHaveBeenCalledWith(null);
expect(debugElement.query(clearBtnSelector)).toBeFalsy();
}));
it('should support nzAutoFocus', () => {
fixtureInstance.nzAutoFocus = true;
fixture.detectChanges();
expect(getPickerInput(fixture.debugElement) === document.activeElement).toBeTruthy();
});
it('should support nzDisabled', fakeAsync(() => {
// Make sure picker clear button shown up
fixtureInstance.nzAllowClear = true;
fixtureInstance.nzValue = new Date();
fixtureInstance.nzDisabled = true;
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(debugElement.query(By.css('.ant-input-disabled'))).not.toBeNull();
expect(debugElement.query(By.css('.ant-picker-clear'))).toBeNull();
fixtureInstance.nzDisabled = false;
tick(500);
fixture.detectChanges();
expect(debugElement.query(By.css('.ant-input-disabled'))).toBeNull();
expect(debugElement.query(By.css('.ant-picker-clear'))).not.toBeNull();
}));
it('should support nzInputReadOnly', fakeAsync(() => {
fixtureInstance.nzInputReadOnly = true;
fixture.detectChanges();
expect(getPickerInput(fixture.debugElement).readOnly).toBeTruthy();
fixtureInstance.nzInputReadOnly = false;
fixture.detectChanges();
expect(getPickerInput(fixture.debugElement).readOnly).not.toBeTruthy();
}));
it('should support nzOpen if assigned', fakeAsync(() => {
fixtureInstance.useSuite = 2;
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerContainer()).toBeNull();
fixtureInstance.nzOpen = true;
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerContainer()).not.toBeNull();
fixtureInstance.nzOpen = false;
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerContainer()).toBeNull();
}));
it('should support nzFormat', fakeAsync(() => {
fixtureInstance.nzFormat = 'dd.MM.yyyy';
fixtureInstance.nzValue = new Date('2020-03-04');
fixture.detectChanges();
openPickerByClickTrigger();
const input = getPickerInput(fixture.debugElement);
expect(input.value).toBe('04.03.2020');
dispatchMouseEvent(queryFromOverlay('.ant-picker-cell')!, 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(input.value).toBe('24.02.2020');
}));
it('should support nzDisabledDate', fakeAsync(() => {
fixture.detectChanges();
const compareDate = new Date('2018-11-15 00:00:00');
fixtureInstance.nzValue = new Date('2018-11-11 12:12:12');
fixtureInstance.nzDisabledDate = (current: Date) => isSameDay(current, compareDate);
fixture.detectChanges();
flush();
fixture.detectChanges();
openPickerByClickTrigger();
const disabledCell = queryFromOverlay(`tbody tr td.${PREFIX_CLASS}-cell-disabled div`);
expect(disabledCell.textContent!.trim()).toBe('15');
const input = getPickerInput(fixture.debugElement);
const submit = (date: string): void => {
input.value = date;
fixture.detectChanges();
input.dispatchEvent(ENTER_EVENT);
fixture.detectChanges();
tick(500);
fixture.detectChanges();
};
// Should fail to submit a disabled date
submit('2018-11-15');
expect(getPickerContainer()).not.toBeNull();
// But it should be fine to submit an enabled date
submit('2018-11-11');
expect(getPickerContainer()).toBeNull();
}));
// #5633
it('should support disable year and month right', fakeAsync(() => {
fixture.detectChanges();
fixtureInstance.nzValue = new Date(2020, 0, 1);
fixtureInstance.nzDisabledDate = (date: Date): boolean =>
date >= new Date(2019, 0, 1) && date < new Date(2019, 0, 2);
fixture.detectChanges();
flush();
fixture.detectChanges();
openPickerByClickTrigger();
dispatchMouseEvent(queryFromOverlay('.ant-picker-header-year-btn'), 'click');
fixture.detectChanges();
const year2019 = getFirstCell();
expect(year2019.textContent!.trim()).toBe('2019');
expect(year2019.classList).not.toContain('ant-picker-cell-disabled');
dispatchMouseEvent(year2019, 'click');
fixture.detectChanges();
dispatchMouseEvent(queryFromOverlay('.ant-picker-header-month-btn'), 'click');
fixture.detectChanges();
const january = getFirstCell();
expect(january.textContent!.trim()).toContain('1');
expect(january.classList).not.toContain('ant-picker-cell-disabled');
}));
it('should support nzLocale', () => {
const featureKey = 'TEST_PLACEHOLDER';
fixtureInstance.nzLocale = { lang: { placeholder: featureKey } };
fixture.detectChanges();
expect(getPickerInput(fixture.debugElement).getAttribute('placeholder')).toBe(featureKey);
});
it('should support nzPlaceHolder', () => {
const featureKey = (fixtureInstance.nzPlaceHolder = 'TEST_PLACEHOLDER');
fixture.detectChanges();
expect(getPickerInput(fixture.debugElement).getAttribute('placeholder')).toBe(featureKey);
});
it('should support nzPopupStyle', fakeAsync(() => {
fixtureInstance.nzPopupStyle = { color: 'red' };
fixture.detectChanges();
openPickerByClickTrigger();
expect(queryFromOverlay(`.${PREFIX_CLASS}-dropdown`).style.color).toBe('red');
}));
it('should support nzDropdownClassName', fakeAsync(() => {
const keyCls = (fixtureInstance.nzDropdownClassName = 'my-test-class');
fixture.detectChanges();
openPickerByClickTrigger();
expect(queryFromOverlay(`.${PREFIX_CLASS}-dropdown`).classList.contains(keyCls)).toBeTruthy();
}));
it('should support nzSize', () => {
fixtureInstance.nzSize = 'large';
fixture.detectChanges();
expect(getPickerAbstract(fixture.debugElement).classList.contains(`${PREFIX_CLASS}-large`)).toBeTruthy();
fixtureInstance.nzSize = 'small';
fixture.detectChanges();
expect(getPickerAbstract(fixture.debugElement).classList.contains(`${PREFIX_CLASS}-small`)).toBeTruthy();
});
it('should support nzOnOpenChange', fakeAsync(() => {
const nzOnOpenChange = spyOn(fixtureInstance, 'nzOnOpenChange');
fixture.detectChanges();
openPickerByClickTrigger();
expect(nzOnOpenChange).toHaveBeenCalledWith(true);
triggerInputBlur();
fixture.detectChanges();
flush();
expect(nzOnOpenChange).toHaveBeenCalledWith(false);
expect(nzOnOpenChange).toHaveBeenCalledTimes(2);
}));
it('should not emit nzOnOpenChange second time when input clicked twice', fakeAsync(() => {
const nzOnOpenChange = spyOn(fixtureInstance, 'nzOnOpenChange');
fixture.detectChanges();
openPickerByClickTrigger();
expect(nzOnOpenChange).toHaveBeenCalledWith(true);
expect(nzOnOpenChange).toHaveBeenCalledTimes(1);
}));
it('should not emit nzOnOpenChange when nzOpen is false and input is clicked', fakeAsync(() => {
const nzOnOpenChange = spyOn(fixtureInstance, 'nzOnOpenChange');
fixtureInstance.useSuite = 2;
fixtureInstance.nzOpen = false;
fixture.detectChanges();
openPickerByClickTrigger();
expect(nzOnOpenChange).not.toHaveBeenCalledWith(true);
}));
it('should support nzValue', fakeAsync(() => {
fixtureInstance.nzDefaultPickerValue = new Date('2015-09-17');
fixtureInstance.nzValue = new Date('2018-11-11');
fixture.detectChanges();
flush();
fixture.detectChanges();
openPickerByClickTrigger();
expect(getSelectedDayCell().textContent!.trim()).toBe('11');
}));
it('should support nzOnChange', fakeAsync(() => {
fixtureInstance.nzValue = new Date('2018-11-11');
const nzOnChange = spyOn(fixtureInstance, 'nzOnChange');
const nzOnCalendarChange = spyOn(fixtureInstance, 'nzOnCalendarChange');
fixture.detectChanges();
openPickerByClickTrigger();
const cell = getFirstCell(); // Use the first cell
const cellText = cell.textContent!.trim();
dispatchMouseEvent(cell, 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(nzOnChange).toHaveBeenCalled();
expect(nzOnCalendarChange).not.toHaveBeenCalled();
const result = (nzOnChange.calls.allArgs()[0] as Date[])[0];
expect(result.getDate()).toBe(+cellText);
}));
it('should support nzDefaultPickerValue', fakeAsync(() => {
fixture.detectChanges();
fixtureInstance.nzDefaultPickerValue = new Date('2015-09-17');
fixture.detectChanges();
flush();
fixture.detectChanges();
openPickerByClickTrigger();
expect(queryFromOverlay('.ant-picker-header-year-btn').textContent!.indexOf('2015') > -1).toBeTruthy();
expect(queryFromOverlay('.ant-picker-header-month-btn').textContent!.indexOf('9') > -1).toBeTruthy();
}));
it('should support custom suffixIcon', fakeAsync(() => {
fixtureInstance.nzSuffixIcon = 'clock-circle';
fixture.detectChanges();
expect(debugElement.query(By.css(`.anticon-clock-circle`))).toBeDefined();
}));
it('should support nzBorderless', fakeAsync(() => {
fixtureInstance.nzBorderless = true;
fixture.detectChanges();
expect(debugElement.query(By.css(`.ant-picker-borderless`))).toBeDefined();
}));
it('should support nzInline', fakeAsync(() => {
const nzOnChange = spyOn(fixtureInstance, 'nzOnChange');
fixtureInstance.nzInline = true;
fixture.detectChanges();
overlayContainerElement = debugElement.nativeElement as HTMLLIElement;
const cell = getFirstCell(); // Use the first cell
const cellText = cell.textContent!.trim();
dispatchMouseEvent(cell, 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(nzOnChange).toHaveBeenCalled();
const result = (nzOnChange.calls.allArgs()[0] as Date[])[0];
expect(result.getDate()).toBe(+cellText);
}));
it('should not run change detection when inline mode is enabled and the `date-range-popup` is clicked', () => {
fixtureInstance.nzInline = true;
fixture.detectChanges();
const appRef = TestBed.inject(ApplicationRef);
const event = new MouseEvent('click');
spyOn(appRef, 'tick');
spyOn(event, 'preventDefault').and.callThrough();
debugElement.nativeElement.querySelector('date-range-popup').dispatchEvent(event);
expect(appRef.tick).not.toHaveBeenCalled();
expect(event.preventDefault).toHaveBeenCalled();
});
it('should support nzBackdrop', fakeAsync(() => {
fixtureInstance.nzBackdrop = true;
fixture.detectChanges();
openPickerByClickTrigger();
expect(overlayContainerElement.children[0].classList).toContain('cdk-overlay-backdrop');
}));
});
describe('panel switch and move forward/afterward', () => {
beforeEach(() => (fixtureInstance.useSuite = 1));
it('should support date panel changes on click month', fakeAsync(() => {
fixtureInstance.nzValue = new Date('2018-11-11');
fixture.detectChanges();
flush();
fixture.detectChanges();
openPickerByClickTrigger();
// Click month
dispatchMouseEvent(queryFromOverlay('.ant-picker-header-month-btn'), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
dispatchMouseEvent(getFirstCell(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
// click 2018-01-01
dispatchMouseEvent(getFirstCell(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerInput(fixture.debugElement).value).toBe('2018-01-01');
}));
it('should support date panel changes', fakeAsync(() => {
fixtureInstance.nzValue = new Date('2018-11-11');
fixture.detectChanges();
flush();
fixture.detectChanges();
openPickerByClickTrigger();
// Click previous year button
dispatchMouseEvent(getSuperPreBtn(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(queryFromOverlay('.ant-picker-header-year-btn').textContent!.indexOf('2017') > -1).toBeTruthy();
// Click next year button * 2
dispatchMouseEvent(getSuperNextBtn(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
dispatchMouseEvent(getSuperNextBtn(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(queryFromOverlay('.ant-picker-header-year-btn').textContent!.indexOf('2019') > -1).toBeTruthy();
// Click previous month button
dispatchMouseEvent(getPreBtn(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(queryFromOverlay('.ant-picker-header-month-btn').textContent!.indexOf('10') > -1).toBeTruthy();
// Click next month button * 2
dispatchMouseEvent(getNextBtn(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
dispatchMouseEvent(getNextBtn(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(queryFromOverlay('.ant-picker-header-month-btn').textContent!.indexOf('12') > -1).toBeTruthy();
}));
it('should support month panel changes', fakeAsync(() => {
fixtureInstance.nzValue = new Date('2018-11-11');
fixture.detectChanges();
flush();
fixture.detectChanges();
openPickerByClickTrigger();
// Click month select to show month panel
dispatchMouseEvent(queryFromOverlay('.ant-picker-header-month-btn'), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(queryFromOverlay('.ant-picker-month-panel')).toBeDefined();
expect(queryFromOverlay('.ant-picker-header-month-btn').textContent!.indexOf('2018') > -1).toBeTruthy();
// Goto previous year
dispatchMouseEvent(getSuperPreBtn(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(queryFromOverlay('.ant-picker-header-month-btn').textContent!.indexOf('2017') > -1).toBeTruthy();
// Goto next year * 2
dispatchMouseEvent(getSuperNextBtn(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
dispatchMouseEvent(getSuperNextBtn(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(queryFromOverlay('.ant-picker-header-month-btn').textContent!.indexOf('2019') > -1).toBeTruthy();
// Click to choose a year to change panel
dispatchMouseEvent(queryFromOverlay('td.ant-picker-cell'), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(queryFromOverlay('.ant-picker-date-panel')).toBeTruthy();
}));
it('should support year panel changes', fakeAsync(() => {
fixtureInstance.nzValue = new Date('2018-11-11');
fixture.detectChanges();
flush();
fixture.detectChanges();
openPickerByClickTrigger();
// Click year select to show year panel
dispatchMouseEvent(queryFromOverlay('.ant-picker-header-year-btn'), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(queryFromOverlay('.ant-picker-year-panel')).toBeDefined();
expect(queryFromOverlay('.ant-picker-header-year-btn').textContent!.indexOf('2010') > -1).toBeTruthy();
expect(queryFromOverlay('.ant-picker-header-year-btn').textContent!.indexOf('2019') > -1).toBeTruthy();
// Coverage for last/next cell
dispatchMouseEvent(getSuperPreBtn(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
dispatchMouseEvent(getSuperNextBtn(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
// Goto previous decade
dispatchMouseEvent(getSuperPreBtn(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(queryFromOverlay('.ant-picker-header-year-btn').textContent!.indexOf('2000') > -1).toBeTruthy();
expect(queryFromOverlay('.ant-picker-header-year-btn').textContent!.indexOf('2009') > -1).toBeTruthy();
// Goto next decade * 2
dispatchMouseEvent(getSuperNextBtn(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
dispatchMouseEvent(getSuperNextBtn(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(queryFromOverlay('.ant-picker-header-year-btn').textContent!.indexOf('2020') > -1).toBeTruthy();
expect(queryFromOverlay('.ant-picker-header-year-btn').textContent!.indexOf('2029') > -1).toBeTruthy();
// Click to choose a year to change panel
dispatchMouseEvent(queryFromOverlay('td.ant-picker-cell'), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(queryFromOverlay('.ant-picker-header .ant-picker-year-panel')).toBeFalsy();
}));
it('should support decade panel changes', fakeAsync(() => {
fixtureInstance.nzValue = new Date('2018-11-11');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
openPickerByClickTrigger();
// Click to show decade panel
dispatchMouseEvent(queryFromOverlay('.ant-picker-header-year-btn'), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
dispatchMouseEvent(queryFromOverlay('.ant-picker-header-year-btn'), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(queryFromOverlay('.ant-picker-decade-panel')).toBeDefined();
// Coverage for last/next cell
dispatchMouseEvent(getSuperNextBtn(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
dispatchMouseEvent(getSuperPreBtn(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
// Goto previous century
dispatchMouseEvent(getSuperPreBtn(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(queryFromOverlay('.ant-picker-header-decade-btn').textContent!.indexOf('1900') > -1).toBeTruthy();
expect(queryFromOverlay('.ant-picker-header-decade-btn').textContent!.indexOf('1999') > -1).toBeTruthy();
// Goto next century * 2
dispatchMouseEvent(getSuperNextBtn(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
dispatchMouseEvent(getSuperNextBtn(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(queryFromOverlay('.ant-picker-header-decade-btn').textContent!.indexOf('2100') > -1).toBeTruthy();
expect(queryFromOverlay('.ant-picker-header-decade-btn').textContent!.indexOf('2199') > -1).toBeTruthy();
// Click to choose a decade to change panel
dispatchMouseEvent(queryFromOverlay('td.ant-picker-cell'), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(queryFromOverlay('.ant-picker-year-panel')).toBeDefined();
}));
}); // /panel switch and move forward/afterward
describe('specified date picker testing', () => {
beforeEach(() => (fixtureInstance.useSuite = 1));
it('should support nzDateRender', fakeAsync(() => {
fixtureInstance.nzDateRender = fixtureInstance.tplDateRender;
fixture.detectChanges();
openPickerByClickTrigger();
expect(queryFromOverlay('.test-first-day').textContent!.trim()).toBe('1');
}));
it('should support nzDateRender with typeof function', fakeAsync(() => {
const featureKey = 'TEST_FIRST_DAY';
fixtureInstance.nzDateRender = (d: Date) => (d.getDate() === 1 ? featureKey : d.getDate());
fixture.detectChanges();
openPickerByClickTrigger();
expect(overlayContainerElement.textContent!.indexOf(featureKey) > -1).toBeTruthy();
}));
it('should support nzShowTime', fakeAsync(() => {
fixtureInstance.nzValue = new Date('2018-11-11 11:22:33');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fixtureInstance.nzShowTime = '' as any;
fixture.detectChanges();
openPickerByClickTrigger();
flush();
fixture.detectChanges();
expect(queryFromOverlay('.ant-picker-ok')).toBeDefined();
expect(queryFromOverlay('.ant-picker-time-panel')).toBeDefined();
expect(
queryFromOverlay('.ant-picker-time-panel-cell-selected .ant-picker-time-panel-cell-inner').textContent!.trim()
).toBe('11');
// Click to choose a hour
dispatchMouseEvent(queryFromOverlay('.ant-picker-time-panel-cell:first-child'), 'click');
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(getPickerInput(fixture.debugElement).value).toBe('2018-11-11 00:22:33');
}));
it('should support nzShowTime.nzDefaultOpenValue', fakeAsync(() => {
fixtureInstance.nzValue = null;
fixtureInstance.nzShowTime = { nzDefaultOpenValue: new Date(0, 0, 0, 0, 1, 2) };
fixture.detectChanges();
openPickerByClickTrigger();
dispatchMouseEvent(getFirstCell(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerInput(fixture.debugElement).value).toContain('00:01:02');
const listOfSelectedLi = overlayContainerElement.querySelectorAll(
'.ant-picker-time-panel-cell-selected .ant-picker-time-panel-cell-inner'
);
expect(listOfSelectedLi[0].textContent!.trim()).toBe('00');
expect(listOfSelectedLi[1].textContent!.trim()).toBe('01');
expect(listOfSelectedLi[2].textContent!.trim()).toBe('02');
}));
it('should not reset time', fakeAsync(() => {
fixtureInstance.nzValue = new Date('2019-08-02 13:03:33');
fixtureInstance.nzShowTime = true;
fixture.detectChanges();
flush();
fixture.detectChanges();
openPickerByClickTrigger();
dispatchMouseEvent(getFirstCell(), 'click');
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(getPickerInput(fixture.debugElement).value).toBe('2019-07-29 13:03:33');
}));
it('should support nzShowTime.nzFormat', fakeAsync(() => {
fixtureInstance.nzShowTime = { nzFormat: 'HH:mm' };
fixture.detectChanges();
openPickerByClickTrigger();
expect(overlayContainerElement.querySelectorAll('.ant-picker-time-panel-column').length).toBe(2);
}));
it('should support nzDisabledTime and nzShowTime.nzHideDisabledOptions', fakeAsync(() => {
fixtureInstance.nzShowTime = true;
fixtureInstance.nzDisabledTime = () => ({
nzDisabledHours: () => [0, 1, 2],
nzDisabledMinutes: () => [0, 1],
nzDisabledSeconds: () => [0]
});
fixture.detectChanges();
openPickerByClickTrigger();
expect(
queryFromOverlay('.ant-picker-time-panel-column li:nth-child(3)').classList.contains(
'ant-picker-time-panel-cell-disabled'
)
).toBeTruthy();
expect(
queryFromOverlay('.ant-picker-time-panel-column:nth-child(2) li:nth-child(2)').classList.contains(
'ant-picker-time-panel-cell-disabled'
)
).toBeTruthy();
expect(
queryFromOverlay('.ant-picker-time-panel-column:nth-child(3) li:nth-child(1)').classList.contains(
'ant-picker-time-panel-cell-disabled'
)
).toBeTruthy();
// Use nzHideDisabledOptions to hide disabled times
fixtureInstance.nzShowTime = { nzHideDisabledOptions: true };
fixture.detectChanges();
expect(+queryFromOverlay('.ant-picker-time-panel-column:nth-child(1) li:first-child').textContent!.trim()).toBe(
3
);
expect(+queryFromOverlay('.ant-picker-time-panel-column:nth-child(2) li:first-child').textContent!.trim()).toBe(
2
);
expect(+queryFromOverlay('.ant-picker-time-panel-column:nth-child(3) li:first-child').textContent!.trim()).toBe(
1
);
}));
it('should nzDisabledTime invalid input not emit', fakeAsync(() => {
fixtureInstance.nzShowTime = true;
fixtureInstance.nzDisabledTime = () => ({
nzDisabledHours: () => [0, 1, 2],
nzDisabledMinutes: () => [0, 1],
nzDisabledSeconds: () => [0]
});
fixture.detectChanges();
openPickerByClickTrigger();
// input disabled value
const input = getPickerInput(fixture.debugElement);
typeInElement('2020-03-14 00:00:00', input);
fixture.detectChanges();
input.dispatchEvent(ENTER_EVENT);
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerContainer()).not.toBeNull();
triggerInputBlur();
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerInput(fixture.debugElement).value).toBe('');
}));
it('should support nzRenderExtraFooter', fakeAsync(() => {
fixtureInstance.nzRenderExtraFooter = () => fixtureInstance.tplExtraFooter;
fixture.detectChanges();
openPickerByClickTrigger();
expect(overlayContainerElement.textContent!.indexOf('TEST_EXTRA_FOOTER') > -1).toBeTruthy();
fixtureInstance.nzRenderExtraFooter = 'TEST_EXTRA_FOOTER_STRING';
fixture.detectChanges();
expect(overlayContainerElement.textContent!.indexOf(fixtureInstance.nzRenderExtraFooter) > -1).toBeTruthy();
}));
it('should support nzShowToday', fakeAsync(() => {
fixture.detectChanges();
openPickerByClickTrigger();
expect(overlayContainerElement.querySelector('.ant-picker-footer')).toBeDefined();
fixtureInstance.nzShowToday = true;
fixture.detectChanges();
expect(overlayContainerElement.querySelector('.ant-picker-today-btn')).toBeDefined();
// Click today button
const nzOnChange = spyOn(fixtureInstance, 'nzOnChange');
dispatchMouseEvent(queryFromOverlay('.ant-picker-today-btn'), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
const result = (nzOnChange.calls.allArgs()[0] as Date[])[0];
expect(isSameDay(new Date(), result)).toBeTruthy();
expect(queryFromOverlay('.ant-picker-container')).toBeFalsy(); // Should closed
}));
it('should support nzShowNow', fakeAsync(() => {
fixture.detectChanges();
openPickerByClickTrigger();
expect(overlayContainerElement.querySelector('.ant-picker-footer')).toBeDefined();
fixtureInstance.nzShowTime = true;
fixtureInstance.nzShowNow = false;
fixture.detectChanges();
expect(overlayContainerElement.querySelector('.ant-picker-now-btn')).toBeNull();
fixtureInstance.nzShowNow = true;
fixture.detectChanges();
expect(overlayContainerElement.querySelector('.ant-picker-now-btn')).toBeDefined();
// Click now button
const nzOnChange = spyOn(fixtureInstance, 'nzOnChange');
dispatchMouseEvent(queryFromOverlay('.ant-picker-now-btn'), 'click');
fixture.detectChanges();
// Click ok button
dispatchMouseEvent(overlayContainerElement.querySelector('.ant-picker-ok > button')!, 'click');
const result = (nzOnChange.calls.allArgs()[0] as Date[])[0];
expect(isEqual(new Date(), result)).toBeTruthy();
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(queryFromOverlay('.ant-picker-container')).toBeFalsy(); // Should closed
}));
it('should support nzMode', fakeAsync(() => {
fixtureInstance.nzValue = new Date('2020-12-01');
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(getPickerInput(fixture.debugElement).placeholder).toEqual('请选择日期');
fixtureInstance.nzMode = 'month';
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(getPickerInput(fixture.debugElement).placeholder).toEqual('请选择月份');
expect(getPickerInput(fixture.debugElement).value).toEqual('2020-12');
openPickerByClickTrigger();
expect(overlayContainerElement.querySelector('.ant-picker-month-panel')).toBeDefined();
}));
it('should support nzOnPanelChange', fakeAsync(() => {
spyOn(fixtureInstance, 'nzOnPanelChange');
fixture.detectChanges();
openPickerByClickTrigger();
// Click header to month panel
dispatchMouseEvent(overlayContainerElement.querySelector('.ant-picker-header-month-btn')!, 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(fixtureInstance.nzOnPanelChange).toHaveBeenCalledWith('month');
}));
it('should support nzOnOk', fakeAsync(() => {
spyOn(fixtureInstance, 'nzOnOk');
fixtureInstance.nzValue = new Date('2018-11-11 11:22:33');
fixtureInstance.nzShowTime = true;
fixture.detectChanges();
flush();
fixture.detectChanges();
openPickerByClickTrigger();
// Click ok button
dispatchMouseEvent(overlayContainerElement.querySelector('.ant-picker-ok > button')!, 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(fixtureInstance.nzOnOk).toHaveBeenCalledWith(fixtureInstance.nzValue);
}));
it('should custom input date', fakeAsync(() => {
const nzOnChange = spyOn(fixtureInstance, 'nzOnChange');
fixture.detectChanges();
openPickerByClickTrigger();
const input = getPickerInput(fixture.debugElement);
// Wrong inputing support
typeInElement('wrong', input);
fixture.detectChanges();
flush();
fixture.detectChanges();
// expect(input.classList.contains('ant-calendar-input-invalid')).toBeTruthy();
// Correct inputing
input.value = '2018-11-22';
input.dispatchEvent(ENTER_EVENT);
// dispatchKeyboardEvent(input, 'keyup', ENTER); // Not working?
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(nzOnChange).toHaveBeenCalled();
const result = (nzOnChange.calls.allArgs()[0] as Date[])[0];
expect(result.getDate()).toBe(22);