This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
autocomplete.spec.ts
1890 lines (1517 loc) · 58.1 KB
/
autocomplete.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
/**
* @license
* Copyright 2019 Dynatrace LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// tslint:disable no-lifecycle-call no-use-before-declare no-magic-numbers
// tslint:disable no-any max-file-line-count no-unbound-method use-component-selector
import {
DOWN_ARROW,
ENTER,
ESCAPE,
SPACE,
TAB,
UP_ARROW,
} from '@angular/cdk/keycodes';
import { OverlayContainer } from '@angular/cdk/overlay';
import {
Component,
NgZone,
OnDestroy,
Provider,
QueryList,
Type,
ViewChild,
ViewChildren,
} from '@angular/core';
import {
ComponentFixture,
TestBed,
async,
fakeAsync,
flush,
inject,
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 { Observable, Subscription } from 'rxjs';
import { map, startWith } from 'rxjs/operators';
import {
DT_AUTOCOMPLETE_DEFAULT_OPTIONS,
DtAutocomplete,
DtAutocompleteModule,
DtAutocompleteTrigger,
getDtAutocompleteMissingPanelError,
} from '@dynatrace/barista-components/autocomplete';
import {
DtOption,
DtOptionSelectionChange,
} from '@dynatrace/barista-components/core';
import {
DtFormField,
DtFormFieldModule,
} from '@dynatrace/barista-components/form-field';
import { DtInputModule } from '@dynatrace/barista-components/input';
import {
dispatchEvent,
dispatchFakeEvent,
dispatchKeyboardEvent,
createKeyboardEvent,
MockNgZone,
typeInElement,
} from '@dynatrace/barista-components/testing';
describe('DtAutocomplete', () => {
let overlayContainer: OverlayContainer;
let overlayContainerElement: HTMLElement;
let zone: MockNgZone;
// Creates a test component fixture.
function createComponent<T>(
component: Type<T>,
providers: Provider[] = [],
): any {
TestBed.configureTestingModule({
imports: [
DtAutocompleteModule,
DtFormFieldModule,
DtInputModule,
FormsModule,
ReactiveFormsModule,
NoopAnimationsModule,
],
declarations: [component],
providers: [
{ provide: NgZone, useFactory: () => (zone = new MockNgZone()) },
...providers,
],
});
TestBed.compileComponents();
inject([OverlayContainer], (oc: OverlayContainer) => {
overlayContainer = oc;
overlayContainerElement = oc.getContainerElement();
})();
return TestBed.createComponent<T>(component);
}
afterEach(inject(
[OverlayContainer],
(currentOverlayContainer: OverlayContainer) => {
// Since we're resetting the testing module in some of the tests,
// we can potentially have multiple overlay containers.
currentOverlayContainer.ngOnDestroy();
overlayContainer.ngOnDestroy();
},
));
describe('panel toggling', () => {
let fixture: ComponentFixture<SimpleAutocomplete>;
let input: HTMLInputElement;
beforeEach(() => {
fixture = createComponent(SimpleAutocomplete);
fixture.detectChanges();
input = fixture.debugElement.query(By.css('input')).nativeElement;
});
it('should open the panel when the input is focused', () => {
// Expected panel state to start out closed.
expect(fixture.componentInstance.trigger.panelOpen).toBe(false);
dispatchFakeEvent(input, 'focusin');
fixture.detectChanges();
expect(fixture.componentInstance.trigger.panelOpen).toBe(true);
// Expected panel to display when input is focused.
expect(overlayContainerElement.textContent).toContain('Alabama');
expect(overlayContainerElement.textContent).toContain('California');
});
it('should not open the panel on focus if the input is readonly', fakeAsync(() => {
const trigger = fixture.componentInstance.trigger;
input.readOnly = true;
fixture.detectChanges();
// Expected panel state to start out closed.
expect(trigger.panelOpen).toBe(false);
dispatchFakeEvent(input, 'focusin');
flush();
fixture.detectChanges();
// Expected panel to stay closed.
expect(trigger.panelOpen).toBe(false);
}));
it('should not open using the arrow keys when the input is readonly', fakeAsync(() => {
const trigger = fixture.componentInstance.trigger;
input.readOnly = true;
fixture.detectChanges();
// Expected panel state to start out closed.
expect(trigger.panelOpen).toBe(false);
dispatchKeyboardEvent(input, 'keydown', DOWN_ARROW);
flush();
fixture.detectChanges();
// Expected panel to stay closed.
expect(trigger.panelOpen).toBe(false);
}));
it('should open the panel programmatically', () => {
// Expected panel state to start out closed.
expect(fixture.componentInstance.trigger.panelOpen).toBe(false);
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
// Expected panel state to read open when opened programmatically.
expect(fixture.componentInstance.trigger.panelOpen).toBe(true);
// Expected panel to display when opened programmatically.
expect(overlayContainerElement.textContent).toContain('Alabama');
// Expected panel to display when opened programmatically.
expect(overlayContainerElement.textContent).toContain('California');
});
it('should show the panel when the first open is after the initial zone stabilization', async(() => {
// Note that we're running outside the Angular zone, in order to be able
// to test properly without the subscription from `_subscribeToClosingActions`
// giving us a false positive.
fixture.ngZone!.runOutsideAngular(() => {
fixture.componentInstance.trigger.openPanel();
Promise.resolve().then(() => {
expect(fixture.componentInstance.panel.showPanel).toBe(true);
});
});
}));
it('should close the panel when the user clicks away', fakeAsync(() => {
dispatchFakeEvent(input, 'focusin');
fixture.detectChanges();
zone.simulateZoneExit();
dispatchFakeEvent(document, 'click');
expect(fixture.componentInstance.trigger.panelOpen).toBe(false);
expect(overlayContainerElement.textContent).toEqual('');
}));
it('should close the panel when the user taps away on a touch device', fakeAsync(() => {
dispatchFakeEvent(input, 'focus');
fixture.detectChanges();
flush();
dispatchFakeEvent(document, 'touchend');
// Expected tapping outside the panel to set its state to closed.
expect(fixture.componentInstance.trigger.panelOpen).toBe(false);
// Expected tapping outside the panel to close the panel.
expect(overlayContainerElement.textContent).toEqual('');
}));
it('should close the panel when an option is clicked', fakeAsync(() => {
dispatchFakeEvent(input, 'focusin');
fixture.detectChanges();
zone.simulateZoneExit();
const option = overlayContainerElement.querySelector(
'dt-option',
) as HTMLElement;
option.click();
fixture.detectChanges();
// Expected clicking an option to set the panel state to closed.
expect(fixture.componentInstance.trigger.panelOpen).toBe(false);
// Expected clicking an option to close the panel.
expect(overlayContainerElement.textContent).toEqual('');
}));
it('should close the panel when a newly created option is clicked', fakeAsync(() => {
dispatchFakeEvent(input, 'focusin');
fixture.detectChanges();
zone.simulateZoneExit();
// Filter down the option list to a subset of original options ('Alabama', 'California')
typeInElement('al', input);
fixture.detectChanges();
tick();
let options = overlayContainerElement.querySelectorAll<HTMLElement>(
'dt-option',
);
options[0].click();
// Changing value from 'Alabama' to 'al' to re-populate the option list,
// ensuring that 'California' is created new.
dispatchFakeEvent(input, 'focusin');
typeInElement('al', input);
fixture.detectChanges();
tick();
options = overlayContainerElement.querySelectorAll<HTMLElement>(
'dt-option',
);
options[1].click();
fixture.detectChanges();
// Expected clicking a new option to set the panel state to closed.
expect(fixture.componentInstance.trigger.panelOpen).toBe(false);
// Expected clicking a new option to close the panel.
expect(overlayContainerElement.textContent).toEqual('');
}));
it('should close the panel programmatically', () => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
fixture.componentInstance.trigger.closePanel();
fixture.detectChanges();
// Expected closing programmatically to set the panel state to closed.
expect(fixture.componentInstance.trigger.panelOpen).toBe(false);
// Expected closing programmatically to close the panel.
expect(overlayContainerElement.textContent).toEqual('');
});
it('should not throw when attempting to close the panel of a destroyed autocomplete', () => {
const trigger = fixture.componentInstance.trigger;
trigger.openPanel();
fixture.detectChanges();
fixture.destroy();
expect(() => {
trigger.closePanel();
}).not.toThrow();
});
it('should hide the panel when the options list is empty', fakeAsync(() => {
dispatchFakeEvent(input, 'focusin');
fixture.detectChanges();
const panel = overlayContainerElement.querySelector(
'.dt-autocomplete-panel',
) as HTMLElement;
// Expected panel to start out visible.
expect(panel.classList).toContain('dt-autocomplete-visible');
// Filter down the option list such that no options match the value
typeInElement('af', input);
fixture.detectChanges();
tick();
fixture.detectChanges();
// Expected panel to hide itself when empty.
expect(panel.classList).toContain('dt-autocomplete-hidden');
}));
it('should not open the panel when the `input` event is invoked on a non-focused input', () => {
// Expected panel state to start out closed.
expect(fixture.componentInstance.trigger.panelOpen).toBe(false);
input.value = 'Alabama';
dispatchFakeEvent(input, 'input');
fixture.detectChanges();
// Expected panel state to stay closed.
expect(fixture.componentInstance.trigger.panelOpen).toBe(false);
});
it('should toggle the visibility when typing and closing the panel', fakeAsync(() => {
fixture.componentInstance.trigger.openPanel();
tick();
fixture.detectChanges();
// Expected panel to be visible.
expect(
overlayContainerElement.querySelector('.dt-autocomplete-panel')!
.classList,
).toContain('dt-autocomplete-visible');
typeInElement('x', input);
fixture.detectChanges();
tick();
fixture.detectChanges();
// Expected panel to be hidden.
expect(
overlayContainerElement.querySelector('.dt-autocomplete-panel')!
.classList,
).toContain('dt-autocomplete-hidden');
fixture.componentInstance.trigger.closePanel();
fixture.detectChanges();
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
typeInElement('al', input);
fixture.detectChanges();
tick();
fixture.detectChanges();
// Expected panel to be visible.
expect(
overlayContainerElement.querySelector('.dt-autocomplete-panel')!
.classList,
).toContain('dt-autocomplete-visible');
}));
it('should provide the open state of the panel', fakeAsync(() => {
// Expected the panel to be unopened initially.
expect(fixture.componentInstance.panel.isOpen).toBeFalsy();
dispatchFakeEvent(input, 'focusin');
fixture.detectChanges();
flush();
// Expected the panel to be opened on focus.
expect(fixture.componentInstance.panel.isOpen).toBeTruthy();
}));
it('should emit an event when the panel is opened', () => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
expect(fixture.componentInstance.openedSpy).toHaveBeenCalled();
});
it('should not emit the `opened` event when no options are being shown', () => {
fixture.componentInstance.filteredStates = fixture.componentInstance.states = [];
fixture.detectChanges();
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
expect(fixture.componentInstance.openedSpy).not.toHaveBeenCalled();
});
it('should not emit the opened event multiple times while typing', fakeAsync(() => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
expect(fixture.componentInstance.openedSpy).toHaveBeenCalledTimes(1);
typeInElement('Alabam', input);
fixture.detectChanges();
tick();
fixture.detectChanges();
expect(fixture.componentInstance.openedSpy).toHaveBeenCalledTimes(1);
}));
it('should emit an event when the panel is closed', () => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
fixture.componentInstance.trigger.closePanel();
fixture.detectChanges();
expect(fixture.componentInstance.closedSpy).toHaveBeenCalled();
});
it('should not emit the `closed` event when no options were shown', () => {
fixture.componentInstance.filteredStates = fixture.componentInstance.states = [];
fixture.detectChanges();
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
fixture.componentInstance.trigger.closePanel();
fixture.detectChanges();
expect(fixture.componentInstance.closedSpy).not.toHaveBeenCalled();
});
it('should not be able to open the panel if the autocomplete is disabled', () => {
expect(fixture.componentInstance.trigger.panelOpen).toBe(false);
fixture.componentInstance.autocompleteDisabled = true;
fixture.detectChanges();
dispatchFakeEvent(input, 'focusin');
fixture.detectChanges();
expect(fixture.componentInstance.trigger.panelOpen).toBe(false);
});
});
it('should be able to set a custom value for the `autocomplete` attribute', () => {
const fixture = createComponent(
AutocompleteWithNativeAutocompleteAttribute,
);
const input = fixture.nativeElement.querySelector('input');
fixture.detectChanges();
expect(input.getAttribute('autocomplete')).toBe('changed');
});
it('should not throw when typing in an element with a null and disabled autocomplete', () => {
const fixture = createComponent(InputWithoutAutocompleteAndDisabled);
fixture.detectChanges();
expect(() => {
dispatchKeyboardEvent(
fixture.nativeElement.querySelector('input'),
'keydown',
SPACE,
);
fixture.detectChanges();
}).not.toThrow();
});
describe('forms integration', () => {
let fixture: ComponentFixture<SimpleAutocomplete>;
let input: HTMLInputElement;
beforeEach(() => {
fixture = createComponent(SimpleAutocomplete);
fixture.detectChanges();
input = fixture.debugElement.query(By.css('input')).nativeElement;
});
it('should update control value as user types with input value', () => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
zone.simulateZoneExit();
typeInElement('a', input);
fixture.detectChanges();
expect(fixture.componentInstance.stateCtrl.value).toEqual('a');
typeInElement('al', input);
fixture.detectChanges();
expect(fixture.componentInstance.stateCtrl.value).toEqual('al');
});
it('should update control value when option is selected with option value', fakeAsync(() => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
zone.simulateZoneExit();
const options = overlayContainerElement.querySelectorAll<HTMLElement>(
'dt-option',
);
options[1].click();
fixture.detectChanges();
expect(fixture.componentInstance.stateCtrl.value).toEqual({
code: 'CA',
name: 'California',
});
}));
it('should update the control back to a string if user types after an option is selected', fakeAsync(() => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
zone.simulateZoneExit();
const options = overlayContainerElement.querySelectorAll<HTMLElement>(
'dt-option',
);
options[1].click();
fixture.detectChanges();
typeInElement('Californi', input);
fixture.detectChanges();
tick();
expect(fixture.componentInstance.stateCtrl.value).toEqual('Californi');
}));
it('should fill the text field with display value when an option is selected', fakeAsync(() => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
zone.simulateZoneExit();
const options = overlayContainerElement.querySelectorAll<HTMLElement>(
'dt-option',
);
options[1].click();
fixture.detectChanges();
expect(input.value).toContain('California');
}));
it('should fill the text field with value if displayWith is not set', fakeAsync(() => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
zone.simulateZoneExit();
fixture.componentInstance.panel.displayWith = null;
fixture.componentInstance.options.toArray()[1].value = 'test value';
fixture.detectChanges();
const options = overlayContainerElement.querySelectorAll<HTMLElement>(
'dt-option',
);
options[1].click();
fixture.detectChanges();
expect(input.value).toContain('test value');
}));
it('should fill the text field correctly if value is set to obj programmatically', fakeAsync(() => {
fixture.componentInstance.stateCtrl.setValue({
code: 'AL',
name: 'Alabama',
});
fixture.detectChanges();
tick();
fixture.detectChanges();
expect(input.value).toContain('Alabama');
}));
it('should clear the text field if value is reset programmatically', fakeAsync(() => {
typeInElement('Alabama', input);
fixture.detectChanges();
tick();
fixture.componentInstance.stateCtrl.reset();
tick();
fixture.detectChanges();
tick();
expect(input.value).toEqual('');
}));
it('should disable input in view when disabled programmatically', () => {
const formFieldElement = fixture.debugElement.query(
By.css('.dt-form-field'),
).nativeElement;
expect(input.disabled).toBe(false);
expect(
formFieldElement.classList.contains('dt-form-field-disabled'),
).toBe(false);
fixture.componentInstance.stateCtrl.disable();
fixture.detectChanges();
expect(input.disabled).toBe(true);
expect(
formFieldElement.classList.contains('dt-form-field-disabled'),
).toBe(true);
});
it('should mark the autocomplete control as dirty as user types', () => {
expect(fixture.componentInstance.stateCtrl.dirty).toBe(false);
typeInElement('a', input);
fixture.detectChanges();
expect(fixture.componentInstance.stateCtrl.dirty).toBe(true);
});
it('should mark the autocomplete control as dirty when an option is selected', fakeAsync(() => {
expect(fixture.componentInstance.stateCtrl.dirty).toBe(false);
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
zone.simulateZoneExit();
const options = overlayContainerElement.querySelectorAll<HTMLElement>(
'dt-option',
);
options[1].click();
fixture.detectChanges();
expect(fixture.componentInstance.stateCtrl.dirty).toBe(true);
}));
it('should not mark the control dirty when the value is set programmatically', () => {
expect(fixture.componentInstance.stateCtrl.dirty).toBe(false);
fixture.componentInstance.stateCtrl.setValue('AL');
fixture.detectChanges();
expect(fixture.componentInstance.stateCtrl.dirty).toBe(false);
});
it('should mark the autocomplete control as touched on blur', () => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
expect(fixture.componentInstance.stateCtrl.touched).toBe(false);
dispatchFakeEvent(input, 'blur');
fixture.detectChanges();
fixture.componentInstance.trigger.closePanel();
fixture.detectChanges();
expect(fixture.componentInstance.stateCtrl.touched).toBe(true);
});
it('should mark the autocomplete control as touched on blur if the panel is closed', () => {
expect(fixture.componentInstance.stateCtrl.touched).toBe(false);
dispatchFakeEvent(input, 'blur');
fixture.detectChanges();
expect(fixture.componentInstance.stateCtrl.touched).toBe(true);
});
it('should not mark the autocomplete control as touched if the input was blurred while the panel is open', () => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
expect(fixture.componentInstance.stateCtrl.touched).toBe(false);
dispatchFakeEvent(input, 'blur');
fixture.detectChanges();
expect(fixture.componentInstance.stateCtrl.touched).toBe(false);
fixture.componentInstance.trigger.closePanel();
fixture.detectChanges();
expect(fixture.componentInstance.stateCtrl.touched).toBe(true);
});
it('should disable the input when used with a value accessor and without `dtInput`', () => {
overlayContainer.ngOnDestroy();
fixture.destroy();
TestBed.resetTestingModule();
const plainFixture = createComponent(
PlainAutocompleteInputWithFormControl,
);
plainFixture.detectChanges();
input = plainFixture.nativeElement.querySelector('input');
expect(input.disabled).toBe(false);
plainFixture.componentInstance.formControl.disable();
plainFixture.detectChanges();
expect(input.disabled).toBe(true);
});
});
describe('keyboard events', () => {
let fixture: ComponentFixture<SimpleAutocomplete>;
let input: HTMLInputElement;
let DOWN_ARROW_EVENT: KeyboardEvent;
let UP_ARROW_EVENT: KeyboardEvent;
let ENTER_EVENT: KeyboardEvent;
beforeEach(fakeAsync(() => {
fixture = createComponent(SimpleAutocomplete);
fixture.detectChanges();
input = fixture.debugElement.query(By.css('input')).nativeElement;
DOWN_ARROW_EVENT = createKeyboardEvent('keydown', DOWN_ARROW);
UP_ARROW_EVENT = createKeyboardEvent('keydown', UP_ARROW);
ENTER_EVENT = createKeyboardEvent('keydown', ENTER);
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
zone.simulateZoneExit();
}));
it('should not focus the option when DOWN key is pressed', () => {
jest
.spyOn(fixture.componentInstance.options.first, 'focus')
.mockImplementation(() => {});
fixture.componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
expect(
fixture.componentInstance.options.first.focus,
).not.toHaveBeenCalled();
});
it('should not close the panel when DOWN key is pressed', () => {
fixture.componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
expect(fixture.componentInstance.trigger.panelOpen).toBe(true);
expect(overlayContainerElement.textContent).toContain('Alabama');
expect(overlayContainerElement.textContent).toContain('California');
});
it('should set the active item to the first option when DOWN key is pressed', () => {
const componentInstance = fixture.componentInstance;
const optionEls = overlayContainerElement.querySelectorAll<HTMLElement>(
'dt-option',
);
expect(componentInstance.trigger.panelOpen).toBe(true);
componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
fixture.detectChanges();
expect(
componentInstance.trigger.activeOption ===
componentInstance.options.first,
).toBe(true);
expect(optionEls[0].classList).toContain('dt-option-active');
expect(optionEls[1].classList).not.toContain('dt-option-active');
componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
fixture.detectChanges();
expect(
componentInstance.trigger.activeOption ===
componentInstance.options.toArray()[1],
).toBe(true);
expect(optionEls[0].classList).not.toContain('dt-option-active');
expect(optionEls[1].classList).toContain('dt-option-active');
});
it('should set the active item to the last option when UP key is pressed', () => {
const componentInstance = fixture.componentInstance;
const optionEls = overlayContainerElement.querySelectorAll<HTMLElement>(
'dt-option',
);
expect(componentInstance.trigger.panelOpen).toBe(true);
componentInstance.trigger._handleKeydown(UP_ARROW_EVENT);
fixture.detectChanges();
expect(
componentInstance.trigger.activeOption ===
componentInstance.options.last,
).toBe(true);
expect(optionEls[10].classList).toContain('dt-option-active');
expect(optionEls[0].classList).not.toContain('dt-option-active');
componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
fixture.detectChanges();
expect(
componentInstance.trigger.activeOption ===
componentInstance.options.first,
).toBe(true);
expect(optionEls[0].classList).toContain('dt-option-active');
});
it('should set the active item properly after filtering', fakeAsync(() => {
const componentInstance = fixture.componentInstance;
componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
tick();
fixture.detectChanges();
}));
it('should set the active item properly after filtering', () => {
const componentInstance = fixture.componentInstance;
typeInElement('o', input);
fixture.detectChanges();
componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
fixture.detectChanges();
const optionEls = overlayContainerElement.querySelectorAll<HTMLElement>(
'dt-option',
);
expect(
componentInstance.trigger.activeOption ===
componentInstance.options.first,
).toBe(true);
expect(optionEls[0].classList).toContain('dt-option-active');
expect(optionEls[1].classList).not.toContain('dt-option-active');
});
it('should fill the text field when an option is selected with ENTER', fakeAsync(() => {
fixture.componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
flush();
fixture.detectChanges();
fixture.componentInstance.trigger._handleKeydown(ENTER_EVENT);
fixture.detectChanges();
expect(input.value).toContain('Alabama');
}));
it('should prevent the default enter key action', fakeAsync(() => {
fixture.componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
flush();
fixture.componentInstance.trigger._handleKeydown(ENTER_EVENT);
expect(ENTER_EVENT.defaultPrevented).toBe(true);
}));
it('should not prevent the default enter action for a closed panel after a user action', () => {
fixture.componentInstance.trigger._handleKeydown(UP_ARROW_EVENT);
fixture.detectChanges();
fixture.componentInstance.trigger.closePanel();
fixture.detectChanges();
fixture.componentInstance.trigger._handleKeydown(ENTER_EVENT);
expect(ENTER_EVENT.defaultPrevented).toBe(false);
});
it('should fill the text field, not select an option, when SPACE is entered', () => {
typeInElement('New', input);
fixture.detectChanges();
const SPACE_EVENT = createKeyboardEvent('keydown', SPACE);
fixture.componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
fixture.detectChanges();
fixture.componentInstance.trigger._handleKeydown(SPACE_EVENT);
fixture.detectChanges();
expect(input.value).not.toContain('New York');
});
it('should mark the control dirty when selecting an option from the keyboard', fakeAsync(() => {
expect(fixture.componentInstance.stateCtrl.dirty).toBe(false);
fixture.componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
flush();
fixture.componentInstance.trigger._handleKeydown(ENTER_EVENT);
fixture.detectChanges();
expect(fixture.componentInstance.stateCtrl.dirty).toBe(true);
}));
it('should open the panel again when typing after making a selection', fakeAsync(() => {
fixture.componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
flush();
fixture.componentInstance.trigger._handleKeydown(ENTER_EVENT);
fixture.detectChanges();
expect(fixture.componentInstance.trigger.panelOpen).toBe(false);
expect(overlayContainerElement.textContent).toEqual('');
dispatchFakeEvent(input, 'focusin');
typeInElement('Alabama', input);
fixture.detectChanges();
tick();
expect(fixture.componentInstance.trigger.panelOpen).toBe(true);
expect(overlayContainerElement.textContent).toContain('Alabama');
}));
it('should not open the panel if the `input` event was dispatched with changing the value', fakeAsync(() => {
const trigger = fixture.componentInstance.trigger;
dispatchFakeEvent(input, 'focusin');
typeInElement('A', input);
fixture.detectChanges();
tick();
expect(trigger.panelOpen).toBe(true);
trigger.closePanel();
fixture.detectChanges();
expect(trigger.panelOpen).toBe(false);
// Dispatch the event without actually changing the value
// to simulate what happen in some cases on IE.
dispatchFakeEvent(input, 'input');
fixture.detectChanges();
tick();
expect(trigger.panelOpen).toBe(false);
}));
it('should close the panel when pressing escape', fakeAsync(() => {
const trigger = fixture.componentInstance.trigger;
input.focus();
flush();
fixture.detectChanges();
expect(document.activeElement).toBe(input);
expect(trigger.panelOpen).toBe(true);
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
fixture.detectChanges();
expect(document.activeElement).toBe(input);
expect(trigger.panelOpen).toBe(false);
}));
it('should prevent the default action when pressing escape', fakeAsync(() => {
const escapeEvent = dispatchKeyboardEvent(input, 'keydown', ESCAPE);
fixture.detectChanges();
expect(escapeEvent.defaultPrevented).toBe(true);
}));
it('should close the panel when pressing ALT + UP_ARROW', fakeAsync(() => {
const trigger = fixture.componentInstance.trigger;
const upArrowEvent = createKeyboardEvent('keydown', UP_ARROW);
Object.defineProperty(upArrowEvent, 'altKey', { get: () => true });
input.focus();
flush();
fixture.detectChanges();
expect(document.activeElement).toBe(input);
expect(trigger.panelOpen).toBe(true);
dispatchEvent(document.body, upArrowEvent);
fixture.detectChanges();
expect(document.activeElement).toBe(input);
expect(trigger.panelOpen).toBe(false);
}));
it('should close the panel when tabbing away from a trigger without results', fakeAsync(() => {
fixture.componentInstance.states = [];
fixture.componentInstance.filteredStates = [];
fixture.detectChanges();
input.focus();
flush();
expect(
overlayContainerElement.querySelector('.dt-autocomplete-panel'),
).toBeTruthy();
dispatchKeyboardEvent(input, 'keydown', TAB);
fixture.detectChanges();
expect(
overlayContainerElement.querySelector('.dt-autocomplete-panel'),
).toBeFalsy();
}));
it('should reset the active option when closing with the escape key', fakeAsync(() => {
const trigger = fixture.componentInstance.trigger;