-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdate-range-picker-single.spec.ts
More file actions
1313 lines (1086 loc) · 41.9 KB
/
Copy pathdate-range-picker-single.spec.ts
File metadata and controls
1313 lines (1086 loc) · 41.9 KB
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 {
elementUpdated,
expect,
fixture,
html,
waitUntil,
} from '@open-wc/testing';
import sinon, { spy } from 'sinon';
import {
altKey,
arrowDown,
arrowLeft,
arrowRight,
arrowUp,
ctrlKey,
escapeKey,
} from '#internals/controllers/key-bindings.js';
import { CalendarDay } from '#internals/date/model.js';
import { defineComponents } from '#internals/definitions/defineComponents.js';
import {
runAriaProjectionTests,
runExternalLabelAssociationTests,
} from '#internals/testing/form-testbed.spec.js';
import { isFocused } from '#internals/testing/helpers.spec.js';
import {
simulateClick,
simulateInput,
simulateKeyboard,
simulateScroll,
} from '#internals/testing/simulate.spec.js';
import IgcCalendarComponent from '../calendar/calendar.js';
import type IgcDialogComponent from '../dialog/dialog.js';
import IgcDateRangeInputComponent from './date-range-input.js';
import IgcDateRangePickerComponent from './date-range-picker.js';
import {
checkSelectedRange,
getIcon,
getInput,
selectDates,
} from './date-range-picker.utils.spec.js';
describe('Date range picker - single input', () => {
before(() => defineComponents(IgcDateRangePickerComponent));
runExternalLabelAssociationTests({
tagName: IgcDateRangePickerComponent.tagName,
getNativeInput: (host) =>
(host as IgcDateRangePickerComponent).renderRoot
.querySelector<IgcDateRangeInputComponent>(
IgcDateRangeInputComponent.tagName
)!
.renderRoot.querySelector('input')!,
});
runAriaProjectionTests({
tagName: IgcDateRangePickerComponent.tagName,
getNativeInput: (host) =>
(host as IgcDateRangePickerComponent).renderRoot
.querySelector<IgcDateRangeInputComponent>(
IgcDateRangeInputComponent.tagName
)!
.renderRoot.querySelector('input')!,
expected: { hasPopup: 'dialog' },
getDescription: (host) => [
(host as IgcDateRangePickerComponent).renderRoot.querySelector(
'#helper-text'
)!,
],
});
let picker: IgcDateRangePickerComponent;
let rangeInput: IgcDateRangeInputComponent;
let input: HTMLInputElement;
let calendar: IgcCalendarComponent;
const clearIcon = 'input_clear';
const today = CalendarDay.today;
const tomorrow = today.add('day', 1);
beforeEach(async () => {
picker = await fixture<IgcDateRangePickerComponent>(
html`<igc-date-range-picker></igc-date-range-picker>`
);
rangeInput = picker.renderRoot.querySelector(
IgcDateRangeInputComponent.tagName
)!;
input = rangeInput.renderRoot.querySelector('input')!;
calendar = picker.renderRoot.querySelector(IgcCalendarComponent.tagName)!;
});
afterEach(() => {
sinon.restore();
});
describe('Rendering and initialization', () => {
it('is defined', async () => {
expect(picker).is.not.undefined;
});
it('is accessible (closed state)', async () => {
await expect(picker).shadowDom.to.be.accessible();
await expect(picker).lightDom.to.be.accessible();
});
it('is accessible (open state) - default dropdown mode', async () => {
picker.open = true;
await elementUpdated(picker);
await expect(picker).shadowDom.to.be.accessible();
await expect(picker).lightDom.to.be.accessible();
});
it('is accessible (open state) - dialog mode', async () => {
picker.open = true;
picker.mode = 'dialog';
await elementUpdated(picker);
await expect(picker).shadowDom.to.be.accessible();
await expect(picker).lightDom.to.be.accessible();
});
it('should not render title slot elements in dropdown mode', async () => {
picker = await fixture<IgcDateRangePickerComponent>(
html`<igc-date-range-picker mode="dropdown">
<p slot="title">Custom title</p>
</igc-date-range-picker>`
);
await elementUpdated(picker);
const slot = picker.renderRoot.querySelector(
`slot[name="title"]`
) as HTMLSlotElement;
expect(slot).to.be.null;
expect(picker.useTwoInputs).to.be.false;
});
it('should render title slot elements in dialog mode', async () => {
picker = await fixture<IgcDateRangePickerComponent>(
html`<igc-date-range-picker mode="dialog">
<p slot="title">Custom title</p>
</igc-date-range-picker>`
);
await elementUpdated(picker);
const slot = picker.renderRoot.querySelector(
`slot[name="title"]`
) as HTMLSlotElement;
expect(slot).to.not.be.null;
});
});
describe('Properties', () => {
it('should set value through attribute correctly in case the date values are valid ISO 8601 strings', async () => {
const expectedValue = { start: today.native, end: tomorrow.native };
const attributeValue = { start: today.native, end: tomorrow.native };
picker.setAttribute('value', JSON.stringify(attributeValue));
await elementUpdated(picker);
checkSelectedRange(picker, expectedValue, false);
});
it('should keep the calendar selection and input values on changing the mode', async () => {
const expectedValue = { start: today.native, end: tomorrow.native };
picker = await fixture<IgcDateRangePickerComponent>(
html`<igc-date-range-picker
.value=${expectedValue}
></igc-date-range-picker>`
);
checkSelectedRange(picker, expectedValue, false);
picker.mode = 'dialog';
await elementUpdated(picker);
checkSelectedRange(picker, expectedValue, false);
});
it('should modify value only through calendar selection and not input', async () => {
const eventSpy = spy(picker, 'emitEvent');
// current implementation of DRP single input is not editable;
// to refactor when the input is made editable
picker.nonEditable = true;
await elementUpdated(picker);
input = getInput(picker);
input.focus();
simulateKeyboard(input, arrowDown);
await elementUpdated(picker);
expect(isFocused(input)).to.be.true;
expect(input.value).to.equal('');
expect(picker.value).to.deep.equal({ start: null, end: null });
expect(calendar.values).to.deep.equal([]);
expect(eventSpy).not.to.be.called;
await picker.show();
await selectDates(today, tomorrow, calendar);
input.blur();
await elementUpdated(input);
checkSelectedRange(picker, picker.value, false);
expect(eventSpy).calledWith('igcChange');
});
it('should set properties of the input correctly', async () => {
const propsSingle = {
required: true,
disabled: true,
placeholder: 'Sample placeholder',
outlined: true,
};
Object.assign(picker, propsSingle);
await elementUpdated(picker);
const input = picker.renderRoot.querySelector(
IgcDateRangeInputComponent.tagName
)!;
for (const [prop, value] of Object.entries(propsSingle)) {
expect((input as any)[prop], `Fail for ${prop}`).to.equal(value);
}
});
});
describe('Localization', () => {
it('should properly set displayFormat to the set of predefined formats - single input', async () => {
const predefinedFormats = [
{ format: 'short', formattedValue: '4/14/25' },
{ format: 'medium', formattedValue: 'Apr 14, 2025' },
{ format: 'long', formattedValue: 'April 14, 2025' },
{ format: 'full', formattedValue: 'Monday, April 14, 2025' },
];
picker.value = {
start: CalendarDay.from(new Date(2025, 3, 14)).native,
end: CalendarDay.from(new Date(2025, 3, 14)).native,
};
picker.useTwoInputs = false;
await elementUpdated(picker);
for (const obj of predefinedFormats) {
picker.displayFormat = obj.format;
await elementUpdated(picker);
input = getInput(picker);
expect(input.value).to.equal(
`${obj.formattedValue} - ${obj.formattedValue}`
);
}
});
it('should update the masked value with the locale', async () => {
expect(picker.displayFormat).to.equal('M/d/yyyy');
picker.value = {
start: CalendarDay.from(new Date(2025, 3, 9)).native,
end: CalendarDay.from(new Date(2025, 3, 10)).native,
};
await elementUpdated(picker);
const rangeInput = picker.renderRoot.querySelector(
IgcDateRangeInputComponent.tagName
)!;
const input = rangeInput.renderRoot.querySelector('input')!;
expect(input.value).to.equal('4/9/2025 - 4/10/2025');
picker.locale = 'bg';
await elementUpdated(picker);
expect(input.value.normalize('NFKC')).to.equal(
'9.04.2025 г. - 10.04.2025 г.'
);
});
it('should set the default placeholder of the single input to the input format (like dd/MM/yyyy - dd/MM/yyyy)', async () => {
picker.useTwoInputs = false;
await elementUpdated(picker);
input = getInput(picker);
expect(input.placeholder).to.equal(
`${picker.inputFormat} - ${picker.inputFormat}`
);
picker.inputFormat = 'yyyy-MM-dd';
await elementUpdated(picker);
expect(input.placeholder).to.equal('yyyy-MM-dd - yyyy-MM-dd');
});
it('should set the mask of the single input per the display format (like dd/MM/yyyy - dd/MM/yyyy)', async () => {
picker.useTwoInputs = false;
await elementUpdated(picker);
expect(picker.displayFormat).to.equal('M/d/yyyy');
picker.value = {
start: CalendarDay.from(new Date(2025, 3, 9)).native,
end: CalendarDay.from(new Date(2025, 3, 10)).native,
};
await elementUpdated(picker);
const rangeInput = picker.renderRoot.querySelector(
IgcDateRangeInputComponent.tagName
)!;
const input = rangeInput.renderRoot.querySelector('input')!;
expect(input.value).to.equal('4/9/2025 - 4/10/2025');
picker.displayFormat = 'yyyy-MM-dd';
await elementUpdated(picker);
expect(input.value).to.equal('2025-04-09 - 2025-04-10');
});
it('should use inputFormat for display when displayFormat is not set', async () => {
picker.useTwoInputs = false;
picker.inputFormat = 'yyyy-MM-dd';
await elementUpdated(picker);
input = getInput(picker);
expect(input.placeholder).to.equal('yyyy-MM-dd - yyyy-MM-dd');
picker.value = {
start: CalendarDay.from(new Date(2025, 3, 9)).native,
end: CalendarDay.from(new Date(2025, 3, 10)).native,
};
await elementUpdated(picker);
expect(input.value).to.equal('2025-04-09 - 2025-04-10');
input.focus();
await elementUpdated(input);
expect(input.value).to.equal('2025-04-09 - 2025-04-10');
input.blur();
await elementUpdated(input);
expect(input.value).to.equal('2025-04-09 - 2025-04-10');
});
it('should apply inputFormat to empty mask and display value when set initially', async () => {
picker = await fixture<IgcDateRangePickerComponent>(
html`<igc-date-range-picker
display-format="yyyy-MM-dd hh:mm tt"
></igc-date-range-picker>`
);
picker.useTwoInputs = false;
await elementUpdated(picker);
const rangeInput = picker.renderRoot.querySelector(
IgcDateRangeInputComponent.tagName
)!;
input = rangeInput.renderRoot.querySelector('input')!;
// Placeholder uses inputFormat (the default locale format)
expect(input.placeholder).to.equal('MM/dd/yyyy - MM/dd/yyyy');
input.focus();
await elementUpdated(input);
// When focused, the mask uses inputFormat, not displayFormat
expect(input.value).to.contain('__/__/____');
picker.value = {
start: new Date(2025, 3, 9, 14, 30, 0),
end: new Date(2025, 3, 10, 9, 15, 0),
};
await elementUpdated(picker);
// When focused with a value, still uses inputFormat for editing
expect(input.value).to.equal('04/09/2025 - 04/10/2025');
input.blur();
await elementUpdated(input);
// When blurred (not focused), uses displayFormat for display
expect(input.value).to.equal('2025-04-09 02:30 PM - 2025-04-10 09:15 AM');
});
it('should initialize default mask based on locale when no inputFormat is set', async () => {
picker = await fixture<IgcDateRangePickerComponent>(
html`<igc-date-range-picker></igc-date-range-picker>`
);
picker.useTwoInputs = false;
await elementUpdated(picker);
const rangeInput = picker.renderRoot.querySelector(
IgcDateRangeInputComponent.tagName
)!;
input = rangeInput.renderRoot.querySelector('input')!;
expect(input.placeholder).to.equal('MM/dd/yyyy - MM/dd/yyyy');
picker.locale = 'de';
await elementUpdated(picker);
expect(input.placeholder).to.equal('dd.MM.yyyy - dd.MM.yyyy');
});
it('should target the last end position date part when the input receives focus by default', async () => {
picker.useTwoInputs = false;
await elementUpdated(picker);
input = getInput(picker);
input.focus();
await elementUpdated(input);
// Press arrow up without selecting a specific part
// Should increment the last end position part (year)
const initialDate = new Date(2025, 0, 15); // Jan 15, 2025
picker.value = { start: initialDate, end: initialDate };
await elementUpdated(picker);
expect(input.value).to.equal('01/15/2025 - 01/15/2025');
// Blur and refocus to reset cursor
input.blur();
await elementUpdated(input);
input.focus();
await elementUpdated(input);
// Arrow up should increment the year (last date part)
simulateKeyboard(input, arrowUp);
await elementUpdated(input);
expect(input.value).to.equal('01/15/2025 - 01/15/2026');
});
});
describe('Scroll strategy', () => {
// Inherited from the picker base class - one end-to-end smoke test.
it('`close` behavior', async () => {
const container = await fixture<HTMLDivElement>(html`
<div style="height: 1200px">
<igc-date-range-picker
scroll-strategy="close"
></igc-date-range-picker>
</div>
`);
picker = container.querySelector(IgcDateRangePickerComponent.tagName)!;
const eventSpy = spy(picker, 'emitEvent');
await picker.show();
await simulateScroll(container, { top: 200 });
expect(picker.open).to.be.false;
expect(eventSpy.firstCall).calledWith('igcClosing');
expect(eventSpy.lastCall).calledWith('igcClosed');
});
});
describe('Methods', () => {
it('should clear the input on invoking clear()', async () => {
const eventSpy = spy(picker, 'emitEvent');
picker.value = {
start: CalendarDay.from(new Date(2025, 3, 9)).native,
end: CalendarDay.from(new Date(2025, 3, 10)).native,
};
await elementUpdated(picker);
expect(input.value).to.equal('4/9/2025 - 4/10/2025');
picker.clear();
await elementUpdated(picker);
expect(eventSpy).not.called;
expect(picker.value).to.deep.equal(null);
expect(input.value).to.equal('');
});
it('should select a date range on invoking select', async () => {
const eventSpy = spy(picker, 'emitEvent');
expect(picker.value).to.deep.equal({ start: null, end: null });
const start = CalendarDay.from(new Date(2025, 3, 9)).native;
const end = CalendarDay.from(new Date(2025, 3, 10)).native;
picker.select({
start,
end,
});
await elementUpdated(picker);
expect(picker.value).to.deep.equal({
start,
end,
});
expect(input.value).to.equal('4/9/2025 - 4/10/2025');
expect(eventSpy).not.called;
});
it('should stepUp/stepDown when input is not focused', async () => {
picker.useTwoInputs = false;
picker.value = {
start: new Date(2025, 0, 15), // Jan 15, 2025
end: new Date(2025, 0, 16), // Jan 16, 2025
};
await elementUpdated(picker);
input = getInput(picker);
expect(isFocused(input)).to.be.false;
expect(input.value).to.equal('1/15/2025 - 1/16/2025');
// stepUp should increment the default date part (first start position part - Month)
rangeInput.stepUp();
await elementUpdated(rangeInput);
expect(rangeInput.value?.start?.getMonth()).to.equal(1);
expect(rangeInput.value?.start?.getDate()).to.equal(15);
expect(input.value).to.equal('2/15/2025 - 1/16/2025');
// stepDown should decrement the default date part (Month)
rangeInput.stepDown();
await elementUpdated(rangeInput);
expect(rangeInput.value?.start?.getMonth()).to.equal(0);
expect(rangeInput.value?.start?.getDate()).to.equal(15);
expect(input.value).to.equal('1/15/2025 - 1/16/2025');
});
});
describe('Interactions', () => {
describe('Selection via the calendar', () => {
it('should select a single date in dropdown mode and emit igcChange', async () => {
const eventSpy = spy(picker, 'emitEvent');
picker.open = true;
await elementUpdated(picker);
await selectDates(today, null, calendar);
expect(eventSpy).calledWith('igcChange');
checkSelectedRange(
picker,
{ start: today.native, end: today.native },
false
);
const popover = picker.renderRoot.querySelector('igc-popover');
// when selecting a single date, the calendar won't close
expect(popover?.hasAttribute('open')).to.equal(true);
});
it('should select a range of dates in dropdown mode and emit igcChange', async () => {
const eventSpy = spy(picker, 'emitEvent');
picker.open = true;
await elementUpdated(picker);
await selectDates(today, tomorrow, calendar);
expect(eventSpy).calledWith('igcChange');
checkSelectedRange(
picker,
{
start: today.native,
end: tomorrow.native,
},
false
);
const popover = picker.renderRoot.querySelector('igc-popover');
// with the second click, the calendar closes
expect(popover?.hasAttribute('open')).to.equal(false);
});
it('should select a range of dates in dialog mode and emit igcChange when done is clicked', async () => {
const eventSpy = spy(picker, 'emitEvent');
picker.mode = 'dialog';
await elementUpdated(picker);
picker.open = true;
await elementUpdated(picker);
await selectDates(today, tomorrow, calendar);
expect(eventSpy).not.to.be.calledWith('igcChange');
let dialog = picker.renderRoot.querySelector('igc-dialog');
expect(dialog?.hasAttribute('open')).to.equal(true);
const doneBtn = picker.shadowRoot!.querySelector(
'igc-button[slot="footer"]:last-of-type'
) as HTMLButtonElement;
doneBtn?.click();
await elementUpdated(picker);
expect(eventSpy).calledWith('igcChange');
checkSelectedRange(
picker,
{
start: today.native,
end: tomorrow.native,
},
false
);
dialog = picker.renderRoot.querySelector('igc-dialog');
expect(dialog?.hasAttribute('open')).to.equal(false);
});
it('should select a range of dates in dialog mode and emit igcChange when clicked outside of dialog', async () => {
const eventSpy = spy(picker, 'emitEvent');
picker.mode = 'dialog';
await elementUpdated(picker);
picker.open = true;
await elementUpdated(picker);
await selectDates(today, tomorrow, calendar);
expect(eventSpy).not.to.be.calledWith('igcChange');
let dialog = picker.renderRoot.querySelector(
'igc-dialog'
) as IgcDialogComponent;
const nativeDialog = dialog.renderRoot.querySelector('dialog')!;
expect(dialog?.hasAttribute('open')).to.equal(true);
const { x, y } = dialog.getBoundingClientRect();
simulateClick(nativeDialog, { clientX: x + 1, clientY: y - 1 });
await elementUpdated(dialog);
await waitUntil(() => eventSpy.calledWith('igcClosed'));
await elementUpdated(picker);
expect(eventSpy).calledWith('igcChange');
checkSelectedRange(
picker,
{
start: today.native,
end: tomorrow.native,
},
false
);
dialog = picker.renderRoot.querySelector(
'igc-dialog'
) as IgcDialogComponent;
expect(dialog?.hasAttribute('open')).to.equal(false);
});
it('should not emit igcChange when cancel is clicked and the value should be the initial value', async () => {
const eventSpy = spy(picker, 'emitEvent');
picker.mode = 'dialog';
const date1 = new CalendarDay({
year: today.year,
month: today.month,
date: 10,
});
const date2 = new CalendarDay({
year: today.year,
month: today.month,
date: 14,
});
picker.value = { start: today.native, end: tomorrow.native };
await elementUpdated(picker);
picker.open = true;
await elementUpdated(picker);
await selectDates(date1, date2, calendar);
expect(eventSpy).not.to.be.calledWith('igcChange');
let dialog = picker.renderRoot.querySelector('igc-dialog');
expect(dialog?.hasAttribute('open')).to.equal(true);
checkSelectedRange(
picker,
{ start: date1.native, end: date2.native },
false
);
const cancelBtn = picker.shadowRoot!.querySelector(
'igc-button[slot="footer"]'
) as HTMLButtonElement;
cancelBtn?.click();
await elementUpdated(picker);
expect(eventSpy).not.to.be.calledWith('igcChange');
dialog = picker.renderRoot.querySelector('igc-dialog');
expect(dialog?.hasAttribute('open')).to.equal(false);
checkSelectedRange(
picker,
{
start: today.native,
end: tomorrow.native,
},
false
);
});
it('should revert to no value when cancel is clicked and initial value is null', async () => {
const eventSpy = spy(picker, 'emitEvent');
picker.mode = 'dialog';
const date1 = new CalendarDay({
year: today.year,
month: today.month,
date: 10,
});
const date2 = new CalendarDay({
year: today.year,
month: today.month,
date: 14,
});
picker.value = null;
await elementUpdated(picker);
picker.open = true;
await elementUpdated(picker);
await selectDates(date1, date2, calendar);
expect(eventSpy).not.to.be.calledWith('igcChange');
let dialog = picker.renderRoot.querySelector('igc-dialog');
expect(dialog?.hasAttribute('open')).to.equal(true);
checkSelectedRange(
picker,
{ start: date1.native, end: date2.native },
false
);
const cancelBtn = picker.shadowRoot!.querySelector(
'igc-button[slot="footer"]'
) as HTMLButtonElement;
cancelBtn?.click();
await elementUpdated(picker);
expect(eventSpy).not.to.be.calledWith('igcChange');
dialog = picker.renderRoot.querySelector('igc-dialog');
expect(dialog?.hasAttribute('open')).to.equal(false);
input = getInput(picker);
calendar = picker.renderRoot.querySelector(
IgcCalendarComponent.tagName
)!;
expect(input.value).to.equal('');
expect(calendar.values).to.deep.equal([]);
});
it('should not emit igcChange when escape is pressed and the value should be the initial value', async () => {
const eventSpy = spy(picker, 'emitEvent');
picker.mode = 'dialog';
const date1 = new CalendarDay({
year: today.year,
month: today.month,
date: 10,
});
const date2 = new CalendarDay({
year: today.year,
month: today.month,
date: 14,
});
picker.value = { start: today.native, end: tomorrow.native };
await elementUpdated(picker);
picker.open = true;
await elementUpdated(picker);
await selectDates(date1, date2, calendar);
expect(eventSpy).not.to.be.calledWith('igcChange');
let dialog = picker.renderRoot.querySelector('igc-dialog');
expect(dialog?.hasAttribute('open')).to.equal(true);
checkSelectedRange(
picker,
{ start: date1.native, end: date2.native },
false
);
simulateKeyboard(picker, escapeKey);
await elementUpdated(picker);
await elementUpdated(input);
expect(eventSpy).not.to.be.calledWith('igcChange');
dialog = picker.renderRoot.querySelector('igc-dialog');
expect(dialog?.hasAttribute('open')).to.equal(false);
input.blur();
await elementUpdated(input);
checkSelectedRange(
picker,
{
start: today.native,
end: tomorrow.native,
},
false
);
});
});
describe('Interactions with the inputs and the open and clear buttons', () => {
it('should not open the picker when clicking the input in dropdown mode', async () => {
const input = picker.renderRoot.querySelector(
IgcDateRangeInputComponent.tagName
)!;
simulateClick(input);
await elementUpdated(picker);
expect(picker.open).to.be.false;
});
it('should open the picker when clicking the input in dialog mode', async () => {
picker.mode = 'dialog';
await elementUpdated(picker);
const rangeInput = picker.renderRoot.querySelector(
IgcDateRangeInputComponent.tagName
)!;
const input = rangeInput.renderRoot.querySelector('input')!;
input.focus();
simulateClick(input);
await elementUpdated(picker);
expect(picker.open).to.be.true;
});
it('should clear the inputs on clicking the clear icon', async () => {
const eventSpy = spy(picker, 'emitEvent');
picker.useTwoInputs = false;
picker.value = { start: today.native, end: tomorrow.native };
await elementUpdated(picker);
input = getInput(picker);
input.focus();
await elementUpdated(input);
simulateClick(getIcon(picker, clearIcon));
await elementUpdated(picker);
input.blur();
await elementUpdated(input);
expect(isFocused(input)).to.be.false;
expect(eventSpy).to.be.calledWith('igcChange', {
detail: { start: null, end: null },
});
expect(picker.open).to.be.false;
expect(picker.value).to.deep.equal({ start: null, end: null });
expect(input.value).to.equal('');
});
it('should support date-range typing for single input', async () => {
const eventSpy = spy(picker, 'emitEvent');
picker.useTwoInputs = false;
await elementUpdated(picker);
const expectedStart = CalendarDay.today.set({
year: 2025,
month: 3,
date: 22,
});
const expectedEnd = expectedStart.add('day', 1);
input = getInput(picker);
input.focus();
input.setSelectionRange(0, 1);
await elementUpdated(picker);
let value = '04/22/2025 - 04/23/2025';
simulateInput(input as unknown as HTMLInputElement, {
value,
inputType: 'insertText',
});
input.setSelectionRange(value.length - 1, value.length);
await elementUpdated(picker);
expect(eventSpy.lastCall).calledWith('igcInput', {
detail: { start: expectedStart.native, end: expectedEnd.native },
});
eventSpy.resetHistory();
picker.clear();
await elementUpdated(picker);
input.focus();
input.setSelectionRange(0, 1);
await elementUpdated(picker);
value = '04/22/202504/23/2025'; //not typing the separator characters
simulateInput(input as unknown as HTMLInputElement, {
value,
inputType: 'insertText',
});
input.setSelectionRange(value.length - 1, value.length);
await elementUpdated(picker);
expect(eventSpy.lastCall).calledWith('igcInput', {
detail: { start: expectedStart.native, end: expectedEnd.native },
});
eventSpy.resetHistory();
input.blur();
await elementUpdated(picker);
expect(eventSpy).calledWith('igcChange', {
detail: { start: expectedStart.native, end: expectedEnd.native },
});
});
it('should in/decrement the different date parts with arrow up/down', async () => {
const expectedStart = CalendarDay.today.set({
year: 2025,
month: 3,
date: 22,
});
const expectedEnd = expectedStart.add('day', 1);
const value = { start: expectedStart.native, end: expectedEnd.native };
picker.useTwoInputs = false;
picker.value = value;
await elementUpdated(picker);
input = getInput(picker);
input.focus();
await elementUpdated(input);
input.setSelectionRange(2, 2);
await elementUpdated(input);
await elementUpdated(picker);
expect(isFocused(input)).to.be.true;
//Move selection to the end of 'day' part.
simulateKeyboard(input, [ctrlKey, arrowRight]);
await elementUpdated(rangeInput);
await elementUpdated(input);
expect(input.selectionStart).to.equal(5);
expect(input.selectionEnd).to.equal(5);
simulateKeyboard(input, arrowUp);
await elementUpdated(picker);
await elementUpdated(input);
expect(input.value).to.equal('04/23/2025 - 04/23/2025');
//Move selection to the end of 'year' part.
simulateKeyboard(input, [ctrlKey, arrowRight]);
await elementUpdated(picker);
await elementUpdated(input);
expect(input.selectionStart).to.equal(10);
expect(input.selectionEnd).to.equal(10);
simulateKeyboard(input, arrowUp);
await elementUpdated(picker);
await elementUpdated(input);
expect(input.value).to.equal('04/23/2026 - 04/23/2025');
//Move selection to the end of 'month' part of the end date.
// skips the separator parts when navigating (right direction)
simulateKeyboard(input, [ctrlKey, arrowRight]);
await elementUpdated(input);
expect(input.selectionStart).to.equal(15);
expect(input.selectionEnd).to.equal(15);
simulateKeyboard(input, arrowUp);
await elementUpdated(picker);
expect(input.value).to.equal('04/23/2026 - 05/23/2025');
// skips the separator parts when navigating (left direction)
input.setSelectionRange(13, 13); // set selection to the start of the month part of the end date
simulateKeyboard(input, [ctrlKey, arrowLeft]);
await elementUpdated(rangeInput);
await elementUpdated(input);
expect(input.selectionStart).to.equal(6);
expect(input.selectionEnd).to.equal(6);
simulateKeyboard(input, arrowUp);
await elementUpdated(picker);
expect(input.value).to.equal('04/23/2027 - 05/23/2025');
});
it('should set the range to the current date (start-end) if no value and arrow up/down pressed', async () => {
picker.useTwoInputs = false;
picker.value = null;
await elementUpdated(picker);
input = getInput(picker);
input.focus();
input.setSelectionRange(0, 1);
expect(isFocused(input)).to.be.true;
simulateKeyboard(input, arrowUp);
await elementUpdated(picker);
input.blur();
await elementUpdated(input);
checkSelectedRange(
picker,
{ start: today.native, end: today.native },
false
);
});
it('should set the range to current date with Ctrl+; keyboard combination', async () => {
const eventSpy = spy(picker, 'emitEvent');
picker.useTwoInputs = false;
picker.value = {
start: new Date(2025, 0, 1),
end: new Date(2025, 0, 2),
};
await elementUpdated(picker);
input = getInput(picker);
input.focus();
await elementUpdated(input);
expect(input.value).to.equal('01/01/2025 - 01/02/2025');
simulateKeyboard(input, [ctrlKey, ';']);
await elementUpdated(picker);