1
1
import { FocusKeyManager } from '@angular/cdk/a11y' ;
2
- import { Directionality } from '@angular/cdk/bidi' ;
2
+ import { Directionality , Direction } from '@angular/cdk/bidi' ;
3
3
import { BACKSPACE , DELETE , ENTER , LEFT_ARROW , RIGHT_ARROW , SPACE , TAB } from '@angular/cdk/keycodes' ;
4
4
import { createKeyboardEvent , dispatchFakeEvent , dispatchKeyboardEvent } from '@angular/cdk/testing' ;
5
- import { Component , DebugElement , QueryList , ViewChild , ViewChildren } from '@angular/core' ;
6
- import { async , ComponentFixture , fakeAsync , TestBed , tick } from '@angular/core/testing' ;
5
+ import {
6
+ Component ,
7
+ DebugElement ,
8
+ QueryList ,
9
+ ViewChild ,
10
+ ViewChildren ,
11
+ Type ,
12
+ Provider ,
13
+ } from '@angular/core' ;
14
+ import { ComponentFixture , fakeAsync , TestBed , tick } from '@angular/core/testing' ;
7
15
import { FormControl , FormsModule , NgForm , ReactiveFormsModule , Validators } from '@angular/forms' ;
8
16
import { MatFormFieldModule } from '@angular/material/form-field' ;
9
17
import { By } from '@angular/platform-browser' ;
@@ -22,43 +30,12 @@ describe('MatChipList', () => {
22
30
let testComponent : StandardChipList ;
23
31
let chips : QueryList < any > ;
24
32
let manager : FocusKeyManager < MatChip > ;
25
- let dir = 'ltr' ;
26
-
27
- beforeEach ( async ( ( ) => {
28
- TestBed . configureTestingModule ( {
29
- imports : [
30
- FormsModule ,
31
- ReactiveFormsModule ,
32
- MatChipsModule ,
33
- MatFormFieldModule ,
34
- MatInputModule ,
35
- NoopAnimationsModule
36
- ] ,
37
- declarations : [
38
- ChipListWithFormErrorMessages ,
39
- StandardChipList ,
40
- FormFieldChipList ,
41
- BasicChipList ,
42
- InputChipList ,
43
- MultiSelectionChipList ,
44
- FalsyValueChipList ,
45
- SelectedChipList
46
- ] ,
47
- providers : [ {
48
- provide : Directionality , useFactory : ( ) => {
49
- return { value : dir . toLowerCase ( ) } ;
50
- }
51
- } ]
52
- } ) ;
53
-
54
- TestBed . compileComponents ( ) ;
55
- } ) ) ;
56
33
57
34
describe ( 'StandardChipList' , ( ) => {
58
35
describe ( 'basic behaviors' , ( ) => {
59
- beforeEach ( async ( ( ) => {
36
+ beforeEach ( ( ) => {
60
37
setupStandardList ( ) ;
61
- } ) ) ;
38
+ } ) ;
62
39
63
40
it ( 'should add the `mat-chip-list` class' , ( ) => {
64
41
expect ( chipListNativeElement . classList ) . toContain ( 'mat-chip-list' ) ;
@@ -76,12 +53,12 @@ describe('MatChipList', () => {
76
53
} ) ;
77
54
78
55
describe ( 'with selected chips' , ( ) => {
79
- beforeEach ( async ( ( ) => {
80
- fixture = TestBed . createComponent ( SelectedChipList ) ;
56
+ beforeEach ( ( ) => {
57
+ fixture = createComponent ( SelectedChipList ) ;
81
58
fixture . detectChanges ( ) ;
82
59
chipListDebugElement = fixture . debugElement . query ( By . directive ( MatChipList ) ) ;
83
60
chipListNativeElement = chipListDebugElement . nativeElement ;
84
- } ) ) ;
61
+ } ) ;
85
62
86
63
it ( 'should not override chips selected' , ( ) => {
87
64
const instanceChips = fixture . componentInstance . chips . toArray ( ) ;
@@ -104,10 +81,10 @@ describe('MatChipList', () => {
104
81
} ) ;
105
82
106
83
describe ( 'focus behaviors' , ( ) => {
107
- beforeEach ( async ( ( ) => {
84
+ beforeEach ( ( ) => {
108
85
setupStandardList ( ) ;
109
86
manager = chipListInstance . _keyManager ;
110
- } ) ) ;
87
+ } ) ;
111
88
112
89
it ( 'should focus the first chip on focus' , ( ) => {
113
90
chipListInstance . focus ( ) ;
@@ -190,11 +167,10 @@ describe('MatChipList', () => {
190
167
191
168
describe ( 'keyboard behavior' , ( ) => {
192
169
describe ( 'LTR (default)' , ( ) => {
193
- beforeEach ( async ( ( ) => {
194
- dir = 'ltr' ;
170
+ beforeEach ( ( ) => {
195
171
setupStandardList ( ) ;
196
172
manager = chipListInstance . _keyManager ;
197
- } ) ) ;
173
+ } ) ;
198
174
199
175
it ( 'should focus previous item when press LEFT ARROW' , ( ) => {
200
176
let nativeChips = chipListNativeElement . querySelectorAll ( 'mat-chip' ) ;
@@ -253,11 +229,10 @@ describe('MatChipList', () => {
253
229
} ) ;
254
230
255
231
describe ( 'RTL' , ( ) => {
256
- beforeEach ( async ( ( ) => {
257
- dir = 'rtl' ;
258
- setupStandardList ( ) ;
232
+ beforeEach ( ( ) => {
233
+ setupStandardList ( 'rtl' ) ;
259
234
manager = chipListInstance . _keyManager ;
260
- } ) ) ;
235
+ } ) ;
261
236
262
237
it ( 'should focus previous item when press RIGHT ARROW' , ( ) => {
263
238
let nativeChips = chipListNativeElement . querySelectorAll ( 'mat-chip' ) ;
@@ -411,7 +386,7 @@ describe('MatChipList', () => {
411
386
let nativeChips : HTMLElement [ ] ;
412
387
413
388
beforeEach ( ( ) => {
414
- fixture = TestBed . createComponent ( BasicChipList ) ;
389
+ fixture = createComponent ( BasicChipList ) ;
415
390
fixture . detectChanges ( ) ;
416
391
417
392
formField = fixture . debugElement . query ( By . css ( '.mat-form-field' ) ) . nativeElement ;
@@ -430,7 +405,7 @@ describe('MatChipList', () => {
430
405
. toBe ( true , 'placeholder should be floating' ) ;
431
406
} ) ;
432
407
433
- it ( 'should remove selection if chip has been removed' , async ( ( ) => {
408
+ it ( 'should remove selection if chip has been removed' , fakeAsync ( ( ) => {
434
409
const instanceChips = fixture . componentInstance . chips ;
435
410
const chipList = fixture . componentInstance . chipList ;
436
411
const firstChip = nativeChips [ 0 ] ;
@@ -442,11 +417,10 @@ describe('MatChipList', () => {
442
417
443
418
fixture . componentInstance . foods = [ ] ;
444
419
fixture . detectChanges ( ) ;
420
+ tick ( ) ;
445
421
446
- fixture . whenStable ( ) . then ( ( ) => {
447
- expect ( chipList . selected )
448
- . toBe ( undefined , 'Expected selection to be removed when option no longer exists.' ) ;
449
- } ) ;
422
+ expect ( chipList . selected )
423
+ . toBe ( undefined , 'Expected selection to be removed when option no longer exists.' ) ;
450
424
} ) ) ;
451
425
452
426
@@ -486,7 +460,7 @@ describe('MatChipList', () => {
486
460
487
461
describe ( 'single selection' , ( ) => {
488
462
beforeEach ( ( ) => {
489
- fixture = TestBed . createComponent ( BasicChipList ) ;
463
+ fixture = createComponent ( BasicChipList ) ;
490
464
fixture . detectChanges ( ) ;
491
465
492
466
nativeChips = fixture . debugElement . queryAll ( By . css ( 'mat-chip' ) )
@@ -623,8 +597,9 @@ describe('MatChipList', () => {
623
597
624
598
it ( 'should be able to programmatically select a falsy option' , ( ) => {
625
599
fixture . destroy ( ) ;
600
+ TestBed . resetTestingModule ( ) ;
626
601
627
- const falsyFixture = TestBed . createComponent ( FalsyValueChipList ) ;
602
+ const falsyFixture = createComponent ( FalsyValueChipList ) ;
628
603
falsyFixture . detectChanges ( ) ;
629
604
630
605
falsyFixture . componentInstance . control . setValue ( [ 0 ] ) ;
@@ -649,7 +624,7 @@ describe('MatChipList', () => {
649
624
650
625
describe ( 'multiple selection' , ( ) => {
651
626
beforeEach ( ( ) => {
652
- fixture = TestBed . createComponent ( MultiSelectionChipList ) ;
627
+ fixture = createComponent ( MultiSelectionChipList ) ;
653
628
fixture . detectChanges ( ) ;
654
629
655
630
nativeChips = fixture . debugElement . queryAll ( By . css ( 'mat-chip' ) )
@@ -732,7 +707,7 @@ describe('MatChipList', () => {
732
707
let nativeChips : HTMLElement [ ] ;
733
708
734
709
beforeEach ( ( ) => {
735
- fixture = TestBed . createComponent ( InputChipList ) ;
710
+ fixture = createComponent ( InputChipList ) ;
736
711
fixture . detectChanges ( ) ;
737
712
738
713
nativeChips = fixture . debugElement . queryAll ( By . css ( 'mat-chip' ) )
@@ -807,18 +782,17 @@ describe('MatChipList', () => {
807
782
. toBeFalsy ( `Expected chip with the old value not to be selected.` ) ;
808
783
} ) ;
809
784
810
- it ( 'should set the control to touched when the chip list is touched' , async ( ( ) => {
785
+ it ( 'should set the control to touched when the chip list is touched' , fakeAsync ( ( ) => {
811
786
expect ( fixture . componentInstance . control . touched )
812
787
. toBe ( false , 'Expected the control to start off as untouched.' ) ;
813
788
814
789
const nativeChipList = fixture . debugElement . query ( By . css ( '.mat-chip-list' ) ) . nativeElement ;
815
790
816
791
dispatchFakeEvent ( nativeChipList , 'blur' ) ;
792
+ tick ( ) ;
817
793
818
- fixture . whenStable ( ) . then ( ( ) => {
819
- expect ( fixture . componentInstance . control . touched )
820
- . toBe ( true , 'Expected the control to be touched.' ) ;
821
- } ) ;
794
+ expect ( fixture . componentInstance . control . touched )
795
+ . toBe ( true , 'Expected the control to be touched.' ) ;
822
796
} ) ) ;
823
797
824
798
it ( 'should not set touched when a disabled chip list is touched' , ( ) => {
@@ -870,8 +844,6 @@ describe('MatChipList', () => {
870
844
871
845
describe ( 'keyboard behavior' , ( ) => {
872
846
beforeEach ( ( ) => {
873
- fixture = TestBed . createComponent ( InputChipList ) ;
874
- fixture . detectChanges ( ) ;
875
847
chipListDebugElement = fixture . debugElement . query ( By . directive ( MatChipList ) ) ;
876
848
chipListInstance = chipListDebugElement . componentInstance ;
877
849
chips = chipListInstance . chips ;
@@ -924,7 +896,7 @@ describe('MatChipList', () => {
924
896
let chipListEl : HTMLElement ;
925
897
926
898
beforeEach ( ( ) => {
927
- fixture = TestBed . createComponent ( ChipListWithFormErrorMessages ) ;
899
+ fixture = createComponent ( ChipListWithFormErrorMessages ) ;
928
900
fixture . detectChanges ( ) ;
929
901
errorTestComponent = fixture . componentInstance ;
930
902
containerEl = fixture . debugElement . query ( By . css ( 'mat-form-field' ) ) . nativeElement ;
@@ -939,23 +911,22 @@ describe('MatChipList', () => {
939
911
. toBe ( 'false' , 'Expected aria-invalid to be set to "false".' ) ;
940
912
} ) ;
941
913
942
- it ( 'should display an error message when the chip list is touched and invalid' , async ( ( ) => {
914
+ it ( 'should display an error message when the list is touched and invalid' , fakeAsync ( ( ) => {
943
915
expect ( errorTestComponent . formControl . invalid )
944
916
. toBe ( true , 'Expected form control to be invalid' ) ;
945
917
expect ( containerEl . querySelectorAll ( 'mat-error' ) . length )
946
918
. toBe ( 0 , 'Expected no error message' ) ;
947
919
948
920
errorTestComponent . formControl . markAsTouched ( ) ;
949
921
fixture . detectChanges ( ) ;
922
+ tick ( ) ;
950
923
951
- fixture . whenStable ( ) . then ( ( ) => {
952
- expect ( containerEl . classList )
953
- . toContain ( 'mat-form-field-invalid' , 'Expected container to have the invalid CSS class.' ) ;
954
- expect ( containerEl . querySelectorAll ( 'mat-error' ) . length )
955
- . toBe ( 1 , 'Expected one error message to have been rendered.' ) ;
956
- expect ( chipListEl . getAttribute ( 'aria-invalid' ) )
957
- . toBe ( 'true' , 'Expected aria-invalid to be set to "true".' ) ;
958
- } ) ;
924
+ expect ( containerEl . classList )
925
+ . toContain ( 'mat-form-field-invalid' , 'Expected container to have the invalid CSS class.' ) ;
926
+ expect ( containerEl . querySelectorAll ( 'mat-error' ) . length )
927
+ . toBe ( 1 , 'Expected one error message to have been rendered.' ) ;
928
+ expect ( chipListEl . getAttribute ( 'aria-invalid' ) )
929
+ . toBe ( 'true' , 'Expected aria-invalid to be set to "true".' ) ;
959
930
} ) ) ;
960
931
961
932
it ( 'should display an error message when the parent form is submitted' , fakeAsync ( ( ) => {
@@ -1033,8 +1004,27 @@ describe('MatChipList', () => {
1033
1004
} ) ;
1034
1005
} ) ;
1035
1006
1036
- function setupStandardList ( ) {
1037
- fixture = TestBed . createComponent ( StandardChipList ) ;
1007
+ function createComponent < T > ( component : Type < T > , providers : Provider [ ] = [ ] ) : ComponentFixture < T > {
1008
+ TestBed . configureTestingModule ( {
1009
+ imports : [
1010
+ FormsModule ,
1011
+ ReactiveFormsModule ,
1012
+ MatChipsModule ,
1013
+ MatFormFieldModule ,
1014
+ MatInputModule ,
1015
+ NoopAnimationsModule ,
1016
+ ] ,
1017
+ declarations : [ component ] ,
1018
+ providers
1019
+ } ) . compileComponents ( ) ;
1020
+
1021
+ return TestBed . createComponent < T > ( component ) ;
1022
+ }
1023
+
1024
+ function setupStandardList ( direction : Direction = 'ltr' ) {
1025
+ fixture = createComponent ( StandardChipList , [ {
1026
+ provide : Directionality , useFactory : ( ) => ( { value : direction . toLowerCase ( ) } )
1027
+ } ] ) ;
1038
1028
fixture . detectChanges ( ) ;
1039
1029
1040
1030
chipListDebugElement = fixture . debugElement . query ( By . directive ( MatChipList ) ) ;
@@ -1045,7 +1035,7 @@ describe('MatChipList', () => {
1045
1035
}
1046
1036
1047
1037
function setupInputList ( ) {
1048
- fixture = TestBed . createComponent ( FormFieldChipList ) ;
1038
+ fixture = createComponent ( FormFieldChipList ) ;
1049
1039
fixture . detectChanges ( ) ;
1050
1040
1051
1041
chipListDebugElement = fixture . debugElement . query ( By . directive ( MatChipList ) ) ;
0 commit comments