-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
chip-list.spec.ts
1929 lines (1551 loc) · 64.3 KB
/
chip-list.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 {animate, style, transition, trigger} from '@angular/animations';
import {FocusKeyManager} from '@angular/cdk/a11y';
import {Direction, Directionality} from '@angular/cdk/bidi';
import {
A,
BACKSPACE,
DELETE,
END,
ENTER,
HOME,
LEFT_ARROW,
RIGHT_ARROW,
SPACE,
TAB,
} from '@angular/cdk/keycodes';
import {
createKeyboardEvent,
dispatchEvent,
dispatchFakeEvent,
dispatchKeyboardEvent,
dispatchMouseEvent,
MockNgZone,
typeInElement,
} from '../../cdk/testing/private';
import {
ChangeDetectionStrategy,
Component,
DebugElement,
NgZone,
Provider,
QueryList,
Type,
ViewChild,
ViewChildren,
} from '@angular/core';
import {ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
import {
FormBuilder,
FormControl,
FormGroup,
FormsModule,
NgForm,
ReactiveFormsModule,
Validators,
} from '@angular/forms';
import {MatLegacyFormFieldModule} from '@angular/material/legacy-form-field';
import {By} from '@angular/platform-browser';
import {BrowserAnimationsModule, NoopAnimationsModule} from '@angular/platform-browser/animations';
import {Subject} from 'rxjs';
import {MatLegacyInputModule} from '../legacy-input/index';
import {MatLegacyChip} from './chip';
import {MatLegacyChipInputEvent} from './chip-input';
import {
MatLegacyChipEvent,
MatLegacyChipList,
MatLegacyChipRemove,
MatLegacyChipsModule,
} from './index';
describe('MatChipList', () => {
let fixture: ComponentFixture<any>;
let chipListDebugElement: DebugElement;
let chipListNativeElement: HTMLElement;
let chipListInstance: MatLegacyChipList;
let testComponent: StandardChipList;
let chips: QueryList<MatLegacyChip>;
let manager: FocusKeyManager<MatLegacyChip>;
let zone: MockNgZone;
let dirChange: Subject<Direction>;
describe('StandardChipList', () => {
describe('basic behaviors', () => {
beforeEach(() => {
setupStandardList();
});
it('should add the `mat-chip-list` class', () => {
expect(chipListNativeElement.classList).toContain('mat-chip-list');
});
it('should not have the aria-selected attribute when is not selectable', () => {
testComponent.selectable = false;
fixture.detectChanges();
const chipsValid = chips
.toArray()
.every(
chip =>
!chip.selectable && !chip._elementRef.nativeElement.hasAttribute('aria-selected'),
);
expect(chipsValid).toBe(true);
});
it('should toggle the chips disabled state based on whether it is disabled', () => {
expect(chips.toArray().every(chip => chip.disabled)).toBe(false);
chipListInstance.disabled = true;
fixture.detectChanges();
expect(chips.toArray().every(chip => chip.disabled)).toBe(true);
chipListInstance.disabled = false;
fixture.detectChanges();
expect(chips.toArray().every(chip => chip.disabled)).toBe(false);
});
it('should disable a chip that is added after the list became disabled', fakeAsync(() => {
expect(chips.toArray().every(chip => chip.disabled)).toBe(false);
chipListInstance.disabled = true;
fixture.detectChanges();
expect(chips.toArray().every(chip => chip.disabled)).toBe(true);
fixture.componentInstance.chips.push(5, 6);
fixture.detectChanges();
tick();
fixture.detectChanges();
expect(chips.toArray().every(chip => chip.disabled)).toBe(true);
}));
it('should preserve the disabled state of a chip if the list gets re-enabled', () => {
const chipArray = chips.toArray();
chipArray[2].disabled = true;
fixture.detectChanges();
expect(chips.toArray().map(chip => chip.disabled)).toEqual([
false,
false,
true,
false,
false,
]);
chipListInstance.disabled = true;
fixture.detectChanges();
expect(chips.toArray().map(chip => chip.disabled)).toEqual([true, true, true, true, true]);
chipListInstance.disabled = false;
fixture.detectChanges();
expect(chips.toArray().map(chip => chip.disabled)).toEqual([
false,
false,
true,
false,
false,
]);
});
});
describe('with selected chips', () => {
beforeEach(() => {
fixture = createComponent(SelectedChipList);
fixture.detectChanges();
chipListDebugElement = fixture.debugElement.query(By.directive(MatLegacyChipList))!;
chipListNativeElement = chipListDebugElement.nativeElement;
});
it('should not override chips selected', () => {
const instanceChips = fixture.componentInstance.chips.toArray();
expect(instanceChips[0].selected)
.withContext('Expected first option to be selected.')
.toBe(true);
expect(instanceChips[1].selected)
.withContext('Expected second option to be not selected.')
.toBe(false);
expect(instanceChips[2].selected)
.withContext('Expected third option to be selected.')
.toBe(true);
});
it('should have role listbox', () => {
expect(chipListNativeElement.getAttribute('role')).toBe('listbox');
});
it('should not have role when empty', () => {
fixture.componentInstance.foods = [];
fixture.detectChanges();
expect(chipListNativeElement.getAttribute('role'))
.withContext('Expect no role attribute')
.toBeNull();
});
it('should not have aria-required when it has no role', () => {
fixture.componentInstance.foods = [];
fixture.detectChanges();
expect(chipListNativeElement.hasAttribute('role')).toBe(false);
expect(chipListNativeElement.hasAttribute('aria-required')).toBe(false);
});
it('should be able to set a custom role', () => {
fixture.componentInstance.chipList.role = 'grid';
fixture.detectChanges();
expect(chipListNativeElement.getAttribute('role')).toBe('grid');
});
});
describe('focus behaviors', () => {
beforeEach(() => {
setupStandardList();
manager = chipListInstance._keyManager;
});
it('should focus the first chip on focus', () => {
chipListInstance.focus();
fixture.detectChanges();
expect(manager.activeItemIndex).toBe(0);
});
it('should watch for chip focus', () => {
const array = chips.toArray();
const lastIndex = array.length - 1;
const lastItem = array[lastIndex];
lastItem.focus();
fixture.detectChanges();
expect(manager.activeItemIndex).toBe(lastIndex);
});
it('should be able to become focused when disabled', () => {
expect(chipListInstance.focused)
.withContext('Expected list to not be focused.')
.toBe(false);
chipListInstance.disabled = true;
fixture.detectChanges();
chipListInstance.focus();
fixture.detectChanges();
expect(chipListInstance.focused)
.withContext('Expected list to continue not to be focused')
.toBe(false);
});
it('should remove the tabindex from the list if it is disabled', () => {
expect(chipListNativeElement.getAttribute('tabindex')).toBeTruthy();
chipListInstance.disabled = true;
fixture.detectChanges();
expect(chipListNativeElement.hasAttribute('tabindex')).toBeFalsy();
});
describe('on chip destroy', () => {
it('should focus the next item', () => {
const array = chips.toArray();
const midItem = array[2];
// Focus the middle item
midItem.focus();
// Destroy the middle item
testComponent.chips.splice(2, 1);
fixture.detectChanges();
// It focuses the 4th item (now at index 2)
expect(manager.activeItemIndex).toEqual(2);
});
it('should focus the previous item', () => {
const array = chips.toArray();
const lastIndex = array.length - 1;
const lastItem = array[lastIndex];
// Focus the last item
lastItem.focus();
// Destroy the last item
testComponent.chips.pop();
fixture.detectChanges();
// It focuses the next-to-last item
expect(manager.activeItemIndex).toEqual(lastIndex - 1);
});
it('should not focus if chip list is not focused', () => {
const array = chips.toArray();
const midItem = array[2];
// Focus and blur the middle item
midItem.focus();
midItem._blur();
zone.simulateZoneExit();
// Destroy the middle item
testComponent.chips.splice(2, 1);
fixture.detectChanges();
// Should not have focus
expect(chipListInstance._keyManager.activeItemIndex).toEqual(-1);
});
it('should focus the list if the last focused item is removed', () => {
testComponent.chips = [0];
spyOn(chipListInstance, 'focus');
chips.last.focus();
testComponent.chips.pop();
fixture.detectChanges();
expect(chipListInstance.focus).toHaveBeenCalled();
});
it(
'should move focus to the last chip when the focused chip was deleted inside a' +
'component with animations',
fakeAsync(() => {
fixture.destroy();
TestBed.resetTestingModule();
fixture = createComponent(StandardChipListWithAnimations, [], BrowserAnimationsModule);
fixture.detectChanges();
chipListDebugElement = fixture.debugElement.query(By.directive(MatLegacyChipList))!;
chipListNativeElement = chipListDebugElement.nativeElement;
chipListInstance = chipListDebugElement.componentInstance;
testComponent = fixture.debugElement.componentInstance;
chips = chipListInstance.chips;
chips.last.focus();
fixture.detectChanges();
expect(chipListInstance._keyManager.activeItemIndex).toBe(chips.length - 1);
dispatchKeyboardEvent(chips.last._elementRef.nativeElement, 'keydown', BACKSPACE);
fixture.detectChanges();
tick(500);
expect(chipListInstance._keyManager.activeItemIndex).toBe(chips.length - 1);
}),
);
});
});
describe('keyboard behavior', () => {
describe('LTR (default)', () => {
beforeEach(() => {
setupStandardList();
manager = chipListInstance._keyManager;
});
it('should focus previous item when press LEFT ARROW', () => {
const nativeChips = chipListNativeElement.querySelectorAll('mat-chip');
const lastNativeChip = nativeChips[nativeChips.length - 1] as HTMLElement;
const array = chips.toArray();
const lastIndex = array.length - 1;
const lastItem = array[lastIndex];
// Focus the last item in the array
lastItem.focus();
expect(manager.activeItemIndex).toEqual(lastIndex);
// Press the LEFT arrow
dispatchKeyboardEvent(lastNativeChip, 'keydown', LEFT_ARROW);
chipListInstance._blur(); // Simulate focus leaving the list and going to the chip.
fixture.detectChanges();
// It focuses the next-to-last item
expect(manager.activeItemIndex).toEqual(lastIndex - 1);
});
it('should focus next item when press RIGHT ARROW', () => {
const nativeChips = chipListNativeElement.querySelectorAll('mat-chip');
const firstNativeChip = nativeChips[0] as HTMLElement;
const array = chips.toArray();
const firstItem = array[0];
// Focus the last item in the array
firstItem.focus();
expect(manager.activeItemIndex).toEqual(0);
// Press the RIGHT arrow
dispatchKeyboardEvent(firstNativeChip, 'keydown', RIGHT_ARROW);
chipListInstance._blur(); // Simulate focus leaving the list and going to the chip.
fixture.detectChanges();
// It focuses the next-to-last item
expect(manager.activeItemIndex).toEqual(1);
});
it('should not handle arrow key events from non-chip elements', () => {
const initialActiveIndex = manager.activeItemIndex;
dispatchKeyboardEvent(chipListNativeElement, 'keydown', RIGHT_ARROW);
fixture.detectChanges();
expect(manager.activeItemIndex)
.withContext('Expected focused item not to have changed.')
.toBe(initialActiveIndex);
});
it('should focus the first item when pressing HOME', () => {
const nativeChips = chipListNativeElement.querySelectorAll('mat-chip');
const lastNativeChip = nativeChips[nativeChips.length - 1] as HTMLElement;
const HOME_EVENT = createKeyboardEvent('keydown', HOME);
const array = chips.toArray();
const lastItem = array[array.length - 1];
lastItem.focus();
expect(manager.activeItemIndex).toBe(array.length - 1);
dispatchEvent(lastNativeChip, HOME_EVENT);
fixture.detectChanges();
expect(manager.activeItemIndex).toBe(0);
expect(HOME_EVENT.defaultPrevented).toBe(true);
});
it('should focus the last item when pressing END', () => {
const nativeChips = chipListNativeElement.querySelectorAll('mat-chip');
const END_EVENT = createKeyboardEvent('keydown', END);
expect(manager.activeItemIndex).toBe(-1);
dispatchEvent(nativeChips[0], END_EVENT);
fixture.detectChanges();
expect(manager.activeItemIndex).toBe(chips.length - 1);
expect(END_EVENT.defaultPrevented).toBe(true);
});
});
describe('RTL', () => {
beforeEach(() => {
setupStandardList('rtl');
manager = chipListInstance._keyManager;
});
it('should focus previous item when press RIGHT ARROW', () => {
const nativeChips = chipListNativeElement.querySelectorAll('mat-chip');
const lastNativeChip = nativeChips[nativeChips.length - 1] as HTMLElement;
const array = chips.toArray();
const lastIndex = array.length - 1;
const lastItem = array[lastIndex];
// Focus the last item in the array
lastItem.focus();
expect(manager.activeItemIndex).toEqual(lastIndex);
// Press the RIGHT arrow
dispatchKeyboardEvent(lastNativeChip, 'keydown', RIGHT_ARROW);
chipListInstance._blur(); // Simulate focus leaving the list and going to the chip.
fixture.detectChanges();
// It focuses the next-to-last item
expect(manager.activeItemIndex).toEqual(lastIndex - 1);
});
it('should focus next item when press LEFT ARROW', () => {
const nativeChips = chipListNativeElement.querySelectorAll('mat-chip');
const firstNativeChip = nativeChips[0] as HTMLElement;
const array = chips.toArray();
const firstItem = array[0];
// Focus the last item in the array
firstItem.focus();
expect(manager.activeItemIndex).toEqual(0);
// Press the LEFT arrow
dispatchKeyboardEvent(firstNativeChip, 'keydown', LEFT_ARROW);
chipListInstance._blur(); // Simulate focus leaving the list and going to the chip.
fixture.detectChanges();
// It focuses the next-to-last item
expect(manager.activeItemIndex).toEqual(1);
});
it('should allow focus to escape when tabbing away', fakeAsync(() => {
chipListInstance._keyManager.onKeydown(createKeyboardEvent('keydown', TAB));
expect(chipListInstance._tabIndex)
.withContext('Expected tabIndex to be set to -1 temporarily.')
.toBe(-1);
tick();
expect(chipListInstance._tabIndex)
.withContext('Expected tabIndex to be reset back to 0')
.toBe(0);
}));
it(`should use user defined tabIndex`, fakeAsync(() => {
chipListInstance.tabIndex = 4;
fixture.detectChanges();
expect(chipListInstance._tabIndex)
.withContext('Expected tabIndex to be set to user defined value 4.')
.toBe(4);
chipListInstance._keyManager.onKeydown(createKeyboardEvent('keydown', TAB));
expect(chipListInstance._tabIndex)
.withContext('Expected tabIndex to be set to -1 temporarily.')
.toBe(-1);
tick();
expect(chipListInstance._tabIndex)
.withContext('Expected tabIndex to be reset back to 4')
.toBe(4);
}));
});
it('should account for the direction changing', () => {
setupStandardList();
manager = chipListInstance._keyManager;
const nativeChips = chipListNativeElement.querySelectorAll('mat-chip');
const firstNativeChip = nativeChips[0] as HTMLElement;
const RIGHT_EVENT = createKeyboardEvent('keydown', RIGHT_ARROW);
const array = chips.toArray();
const firstItem = array[0];
firstItem.focus();
expect(manager.activeItemIndex).toBe(0);
dispatchEvent(firstNativeChip, RIGHT_EVENT);
chipListInstance._blur();
fixture.detectChanges();
expect(manager.activeItemIndex).toBe(1);
dirChange.next('rtl');
fixture.detectChanges();
chipListInstance._keydown(RIGHT_EVENT);
chipListInstance._blur();
fixture.detectChanges();
expect(manager.activeItemIndex).toBe(0);
});
});
});
describe('FormFieldChipList', () => {
beforeEach(() => {
setupInputList();
});
describe('keyboard behavior', () => {
beforeEach(() => {
manager = chipListInstance._keyManager;
});
it('should maintain focus if the active chip is deleted', () => {
const secondChip = fixture.nativeElement.querySelectorAll('.mat-chip')[1];
secondChip.focus();
fixture.detectChanges();
expect(chipListInstance.chips.toArray().findIndex(chip => chip._hasFocus)).toBe(1);
dispatchKeyboardEvent(secondChip, 'keydown', DELETE);
fixture.detectChanges();
expect(chipListInstance.chips.toArray().findIndex(chip => chip._hasFocus)).toBe(1);
});
describe('when the input has focus', () => {
it('should not focus the last chip when press DELETE', () => {
const nativeInput = fixture.nativeElement.querySelector('input');
// Focus the input
nativeInput.focus();
expect(manager.activeItemIndex).toBe(-1);
// Press the DELETE key
dispatchKeyboardEvent(nativeInput, 'keydown', DELETE);
fixture.detectChanges();
// It doesn't focus the last chip
expect(manager.activeItemIndex).toEqual(-1);
});
it('should focus the last chip when press BACKSPACE', () => {
const nativeInput = fixture.nativeElement.querySelector('input');
// Focus the input
nativeInput.focus();
expect(manager.activeItemIndex).toBe(-1);
// Press the BACKSPACE key
dispatchKeyboardEvent(nativeInput, 'keydown', BACKSPACE);
fixture.detectChanges();
// It focuses the last chip
expect(manager.activeItemIndex).toEqual(chips.length - 1);
});
it('should not focus the last chip when pressing BACKSPACE on a non-empty input', () => {
const nativeInput = fixture.nativeElement.querySelector('input');
nativeInput.value = 'hello';
nativeInput.focus();
fixture.detectChanges();
expect(manager.activeItemIndex).toBe(-1);
dispatchKeyboardEvent(nativeInput, 'keydown', BACKSPACE);
fixture.detectChanges();
expect(manager.activeItemIndex).toBe(-1);
});
});
});
it('should complete the stateChanges stream on destroy', () => {
const spy = jasmine.createSpy('stateChanges complete');
const subscription = chipListInstance.stateChanges.subscribe({complete: spy});
fixture.destroy();
expect(spy).toHaveBeenCalled();
subscription.unsubscribe();
});
it('should point the label id to the chip input', () => {
const label = fixture.nativeElement.querySelector('label');
const input = fixture.nativeElement.querySelector('input');
fixture.detectChanges();
expect(label.getAttribute('for')).toBeTruthy();
expect(label.getAttribute('for')).toBe(input.getAttribute('id'));
expect(label.getAttribute('aria-owns')).toBe(input.getAttribute('id'));
});
});
describe('with chip remove', () => {
let chipList: MatLegacyChipList;
let chipRemoveDebugElements: DebugElement[];
beforeEach(() => {
fixture = createComponent(ChipListWithRemove);
fixture.detectChanges();
chipList = fixture.debugElement.query(By.directive(MatLegacyChipList))!.componentInstance;
chipRemoveDebugElements = fixture.debugElement.queryAll(By.directive(MatLegacyChipRemove));
chips = chipList.chips;
});
it('should properly focus next item if chip is removed through click', () => {
chips.toArray()[2].focus();
// Destroy the third focused chip by dispatching a bubbling click event on the
// associated chip remove element.
dispatchMouseEvent(chipRemoveDebugElements[2].nativeElement, 'click');
fixture.detectChanges();
expect(chips.toArray()[2].value).not.toBe(2, 'Expected the third chip to be removed.');
expect(chipList._keyManager.activeItemIndex).toBe(2);
});
});
describe('selection logic', () => {
let formField: HTMLElement;
let nativeChips: HTMLElement[];
beforeEach(() => {
fixture = createComponent(BasicChipList);
fixture.detectChanges();
formField = fixture.debugElement.query(By.css('.mat-form-field'))!.nativeElement;
nativeChips = fixture.debugElement
.queryAll(By.css('mat-chip'))
.map(chip => chip.nativeElement);
chipListDebugElement = fixture.debugElement.query(By.directive(MatLegacyChipList))!;
chipListInstance = chipListDebugElement.componentInstance;
chips = chipListInstance.chips;
});
it('should float placeholder if chip is selected', () => {
expect(formField.classList.contains('mat-form-field-should-float'))
.withContext('placeholder should be floating')
.toBe(true);
});
it('should remove selection if chip has been removed', fakeAsync(() => {
const instanceChips = fixture.componentInstance.chips;
const chipList = fixture.componentInstance.chipList;
const firstChip = nativeChips[0];
dispatchKeyboardEvent(firstChip, 'keydown', SPACE);
fixture.detectChanges();
expect(instanceChips.first.selected)
.withContext('Expected first option to be selected.')
.toBe(true);
expect(chipList.selected)
.withContext('Expected first option to be selected.')
.toBe(chips.first);
fixture.componentInstance.foods = [];
fixture.detectChanges();
tick();
expect(chipList.selected)
.withContext('Expected selection to be removed when option no longer exists.')
.toBe(undefined);
}));
it('should select an option that was added after initialization', () => {
fixture.componentInstance.foods.push({viewValue: 'Potatoes', value: 'potatoes-8'});
fixture.detectChanges();
nativeChips = fixture.debugElement
.queryAll(By.css('mat-chip'))
.map(chip => chip.nativeElement);
const lastChip = nativeChips[8];
dispatchKeyboardEvent(lastChip, 'keydown', SPACE);
fixture.detectChanges();
expect(fixture.componentInstance.chipList.value)
.withContext('Expect value contain the value of the last option')
.toContain('potatoes-8');
expect(fixture.componentInstance.chips.last.selected)
.withContext('Expect last option selected')
.toBeTruthy();
});
it('should not select disabled chips', () => {
const array = chips.toArray();
const disabledChip = nativeChips[2];
dispatchKeyboardEvent(disabledChip, 'keydown', SPACE);
fixture.detectChanges();
expect(fixture.componentInstance.chipList.value)
.withContext('Expect value to be undefined')
.toBeUndefined();
expect(array[2].selected).withContext('Expect disabled chip not selected').toBeFalsy();
expect(fixture.componentInstance.chipList.selected)
.withContext('Expect no selected chips')
.toBeUndefined();
});
it('should not select when is not selectable', fakeAsync(() => {
fixture.destroy();
TestBed.resetTestingModule();
const falsyFixture = createComponent(FalsyBasicChipList);
falsyFixture.detectChanges();
tick();
falsyFixture.detectChanges();
const chipListElement = falsyFixture.debugElement.query(By.directive(MatLegacyChipList))!;
const currentChips = chipListElement.componentInstance.chips;
const currentNativeChips = falsyFixture.debugElement
.queryAll(By.css('mat-chip'))
.map(chip => chip.nativeElement);
expect(currentChips.first.selected)
.withContext('Expected first option not to be selected')
.toBe(false);
dispatchKeyboardEvent(currentNativeChips[0], 'keydown', SPACE);
falsyFixture.detectChanges();
expect(currentChips.first.selected)
.withContext('Expected first option not to be selected.')
.toBe(false);
}));
});
describe('forms integration', () => {
let nativeChips: HTMLElement[];
describe('single selection', () => {
beforeEach(() => {
fixture = createComponent(BasicChipList);
fixture.detectChanges();
nativeChips = fixture.debugElement
.queryAll(By.css('mat-chip'))
.map(chip => chip.nativeElement);
chips = fixture.componentInstance.chips;
});
it('should take an initial view value with reactive forms', () => {
fixture.componentInstance.selectable = true;
fixture.componentInstance.control = new FormControl('pizza-1');
fixture.detectChanges();
const array = chips.toArray();
expect(array[1].selected).withContext('Expect pizza-1 chip to be selected').toBeTruthy();
dispatchKeyboardEvent(nativeChips[1], 'keydown', SPACE);
fixture.detectChanges();
expect(array[1].selected)
.withContext('Expect chip to be not selected after toggle selected')
.toBeFalsy();
});
it('should set the view value from the form', () => {
const chipList = fixture.componentInstance.chipList;
const array = chips.toArray();
expect(chipList.value).withContext('Expect chip list to have no initial value').toBeFalsy();
fixture.componentInstance.control.setValue('pizza-1');
fixture.detectChanges();
expect(array[1].selected).withContext('Expect chip to be selected').toBeTruthy();
});
it('should update the form value when the view changes', () => {
expect(fixture.componentInstance.control.value)
.withContext(`Expected the control's value to be empty initially.`)
.toEqual(null);
dispatchKeyboardEvent(nativeChips[0], 'keydown', SPACE);
fixture.detectChanges();
expect(fixture.componentInstance.control.value)
.withContext(`Expected control's value to be set to the new option.`)
.toEqual('steak-0');
});
it('should clear the selection when a nonexistent option value is selected', () => {
const array = chips.toArray();
fixture.componentInstance.control.setValue('pizza-1');
fixture.detectChanges();
expect(array[1].selected)
.withContext(`Expected chip with the value to be selected.`)
.toBeTruthy();
fixture.componentInstance.control.setValue('gibberish');
fixture.detectChanges();
expect(array[1].selected)
.withContext(`Expected chip with the old value not to be selected.`)
.toBeFalsy();
});
it('should clear the selection when the control is reset', () => {
const array = chips.toArray();
fixture.componentInstance.control.setValue('pizza-1');
fixture.detectChanges();
fixture.componentInstance.control.reset();
fixture.detectChanges();
expect(array[1].selected)
.withContext(`Expected chip with the old value not to be selected.`)
.toBeFalsy();
});
it('should set the control to touched when the chip list is touched', () => {
expect(fixture.componentInstance.control.touched)
.withContext('Expected the control to start off as untouched.')
.toBe(false);
const nativeChipList = fixture.debugElement.query(By.css('.mat-chip-list'))!.nativeElement;
dispatchFakeEvent(nativeChipList, 'blur');
expect(fixture.componentInstance.control.touched)
.withContext('Expected the control to be touched.')
.toBe(true);
});
it('should not set touched when a disabled chip list is touched', () => {
expect(fixture.componentInstance.control.touched)
.withContext('Expected the control to start off as untouched.')
.toBe(false);
fixture.componentInstance.control.disable();
const nativeChipList = fixture.debugElement.query(By.css('.mat-chip-list'))!.nativeElement;
dispatchFakeEvent(nativeChipList, 'blur');
expect(fixture.componentInstance.control.touched)
.withContext('Expected the control to stay untouched.')
.toBe(false);
});
it("should set the control to dirty when the chip list's value changes in the DOM", () => {
expect(fixture.componentInstance.control.dirty)
.withContext(`Expected control to start out pristine.`)
.toEqual(false);
dispatchKeyboardEvent(nativeChips[1], 'keydown', SPACE);
fixture.detectChanges();
expect(fixture.componentInstance.control.dirty)
.withContext(`Expected control to be dirty after value was changed by user.`)
.toEqual(true);
});
it('should not set the control to dirty when the value changes programmatically', () => {
expect(fixture.componentInstance.control.dirty)
.withContext(`Expected control to start out pristine.`)
.toEqual(false);
fixture.componentInstance.control.setValue('pizza-1');
expect(fixture.componentInstance.control.dirty)
.withContext(`Expected control to stay pristine after programmatic change.`)
.toEqual(false);
});
it('should set an asterisk after the placeholder if the control is required', () => {
let requiredMarker = fixture.debugElement.query(By.css('.mat-form-field-required-marker'))!;
expect(requiredMarker)
.withContext(`Expected placeholder not to have an asterisk, as control was not required.`)
.toBeNull();
fixture.componentInstance.chipList.required = true;
fixture.detectChanges();
requiredMarker = fixture.debugElement.query(By.css('.mat-form-field-required-marker'))!;
expect(requiredMarker)
.withContext(`Expected placeholder to have an asterisk, as control was required.`)
.not.toBeNull();
});
it('should mark the component as required if the control has a required validator', () => {
fixture.componentInstance.control = new FormControl(undefined, [Validators.required]);
fixture.detectChanges();
expect(fixture.nativeElement.querySelector('.mat-form-field-required-marker')).toBeTruthy();
});
it('should be able to programmatically select a falsy option', () => {
fixture.destroy();
TestBed.resetTestingModule();
const falsyFixture = createComponent(FalsyValueChipList);
falsyFixture.detectChanges();
falsyFixture.componentInstance.control.setValue([0]);
falsyFixture.detectChanges();
falsyFixture.detectChanges();
expect(falsyFixture.componentInstance.chips.first.selected)
.withContext('Expected first option to be selected')
.toBe(true);
});
it('should not focus the active chip when the value is set programmatically', () => {
const chipArray = fixture.componentInstance.chips.toArray();
spyOn(chipArray[4], 'focus').and.callThrough();
fixture.componentInstance.control.setValue('chips-4');
fixture.detectChanges();
expect(chipArray[4].focus).not.toHaveBeenCalled();
});
it('should blur the form field when the active chip is blurred', fakeAsync(() => {
const formField: HTMLElement = fixture.nativeElement.querySelector('.mat-form-field');
nativeChips[0].focus();
fixture.detectChanges();
expect(formField.classList).toContain('mat-focused');
nativeChips[0].blur();
fixture.detectChanges();
zone.simulateZoneExit();
fixture.detectChanges();
expect(formField.classList).not.toContain('mat-focused');
}));
});
describe('multiple selection', () => {
beforeEach(() => {
fixture = createComponent(MultiSelectionChipList);
fixture.detectChanges();
nativeChips = fixture.debugElement
.queryAll(By.css('mat-chip'))
.map(chip => chip.nativeElement);
chips = fixture.componentInstance.chips;
});
it('should take an initial view value with reactive forms', () => {
fixture.componentInstance.selectable = true;
fixture.componentInstance.control = new FormControl(['pizza-1']);