@@ -20,15 +20,17 @@ import {
20
20
typeInElement ,
21
21
} from '@angular/cdk/testing/private' ;
22
22
import {
23
+ ChangeDetectorRef ,
23
24
Component ,
24
25
DebugElement ,
25
26
EventEmitter ,
26
27
QueryList ,
27
28
Type ,
28
29
ViewChild ,
29
30
ViewChildren ,
30
- provideZoneChangeDetection ,
31
+ inject ,
31
32
} from '@angular/core' ;
33
+ import { takeUntilDestroyed } from '@angular/core/rxjs-interop' ;
32
34
import { ComponentFixture , TestBed , fakeAsync , flush , tick } from '@angular/core/testing' ;
33
35
import { FormControl , FormsModule , NgForm , ReactiveFormsModule , Validators } from '@angular/forms' ;
34
36
import { MatFormFieldModule } from '@angular/material/form-field' ;
@@ -70,11 +72,13 @@ describe('MDC-based MatChipGrid', () => {
70
72
expect ( chips . toArray ( ) . every ( chip => chip . disabled ) ) . toBe ( false ) ;
71
73
72
74
chipGridInstance . disabled = true ;
75
+ fixture . changeDetectorRef . markForCheck ( ) ;
73
76
fixture . detectChanges ( ) ;
74
77
75
78
expect ( chips . toArray ( ) . every ( chip => chip . disabled ) ) . toBe ( true ) ;
76
79
77
80
chipGridInstance . disabled = false ;
81
+ fixture . changeDetectorRef . markForCheck ( ) ;
78
82
fixture . detectChanges ( ) ;
79
83
80
84
expect ( chips . toArray ( ) . every ( chip => chip . disabled ) ) . toBe ( false ) ;
@@ -84,11 +88,13 @@ describe('MDC-based MatChipGrid', () => {
84
88
expect ( chips . toArray ( ) . every ( chip => chip . disabled ) ) . toBe ( false ) ;
85
89
86
90
chipGridInstance . disabled = true ;
91
+ fixture . changeDetectorRef . markForCheck ( ) ;
87
92
fixture . detectChanges ( ) ;
88
93
89
94
expect ( chips . toArray ( ) . every ( chip => chip . disabled ) ) . toBe ( true ) ;
90
95
91
96
fixture . componentInstance . chips . push ( 5 , 6 ) ;
97
+ fixture . changeDetectorRef . markForCheck ( ) ;
92
98
fixture . detectChanges ( ) ;
93
99
tick ( ) ;
94
100
fixture . detectChanges ( ) ;
@@ -98,13 +104,15 @@ describe('MDC-based MatChipGrid', () => {
98
104
99
105
it ( 'should not set a role on the grid when the list is empty' , ( ) => {
100
106
testComponent . chips = [ ] ;
107
+ fixture . changeDetectorRef . markForCheck ( ) ;
101
108
fixture . detectChanges ( ) ;
102
109
103
110
expect ( chipGridNativeElement . hasAttribute ( 'role' ) ) . toBe ( false ) ;
104
111
} ) ;
105
112
106
113
it ( 'should be able to set a custom role' , ( ) => {
107
114
testComponent . role = 'listbox' ;
115
+ fixture . changeDetectorRef . markForCheck ( ) ;
108
116
fixture . detectChanges ( ) ;
109
117
110
118
expect ( chipGridNativeElement . getAttribute ( 'role' ) ) . toBe ( 'listbox' ) ;
@@ -140,6 +148,7 @@ describe('MDC-based MatChipGrid', () => {
140
148
. toBe ( false ) ;
141
149
142
150
chipGridInstance . disabled = true ;
151
+ fixture . changeDetectorRef . markForCheck ( ) ;
143
152
fixture . detectChanges ( ) ;
144
153
145
154
chipGridInstance . focus ( ) ;
@@ -154,6 +163,7 @@ describe('MDC-based MatChipGrid', () => {
154
163
expect ( chipGridNativeElement . getAttribute ( 'tabindex' ) ) . toBe ( '0' ) ;
155
164
156
165
chipGridInstance . disabled = true ;
166
+ fixture . changeDetectorRef . markForCheck ( ) ;
157
167
fixture . detectChanges ( ) ;
158
168
159
169
expect ( chipGridNativeElement . getAttribute ( 'tabindex' ) ) . toBe ( '-1' ) ;
@@ -168,6 +178,7 @@ describe('MDC-based MatChipGrid', () => {
168
178
169
179
// Destroy the middle item
170
180
testComponent . chips . splice ( 2 , 1 ) ;
181
+ fixture . changeDetectorRef . markForCheck ( ) ;
171
182
fixture . detectChanges ( ) ;
172
183
173
184
// It focuses the 4th item
@@ -180,6 +191,7 @@ describe('MDC-based MatChipGrid', () => {
180
191
181
192
// Destroy the last item
182
193
testComponent . chips . pop ( ) ;
194
+ fixture . changeDetectorRef . markForCheck ( ) ;
183
195
fixture . detectChanges ( ) ;
184
196
185
197
// It focuses the next-to-last item
@@ -196,6 +208,7 @@ describe('MDC-based MatChipGrid', () => {
196
208
197
209
// Destroy the middle item
198
210
testComponent . chips . splice ( 2 , 1 ) ;
211
+ fixture . changeDetectorRef . markForCheck ( ) ;
199
212
fixture . detectChanges ( ) ;
200
213
flush ( ) ;
201
214
@@ -205,12 +218,14 @@ describe('MDC-based MatChipGrid', () => {
205
218
206
219
it ( 'should focus the grid if the last focused item is removed' , ( ) => {
207
220
testComponent . chips = [ 0 ] ;
221
+ fixture . changeDetectorRef . markForCheck ( ) ;
208
222
209
223
spyOn ( chipGridInstance , 'focus' ) ;
210
224
patchElementFocus ( chips . last . primaryAction ! . _elementRef . nativeElement ) ;
211
225
chips . last . focus ( ) ;
212
226
213
227
testComponent . chips . pop ( ) ;
228
+ fixture . changeDetectorRef . markForCheck ( ) ;
214
229
fixture . detectChanges ( ) ;
215
230
216
231
expect ( chipGridInstance . focus ) . toHaveBeenCalled ( ) ;
@@ -350,6 +365,7 @@ describe('MDC-based MatChipGrid', () => {
350
365
351
366
it ( `should use user defined tabIndex` , fakeAsync ( ( ) => {
352
367
chipGridInstance . tabIndex = 4 ;
368
+ fixture . changeDetectorRef . markForCheck ( ) ;
353
369
fixture . detectChanges ( ) ;
354
370
355
371
expect ( chipGridInstance . tabIndex )
@@ -422,6 +438,7 @@ describe('MDC-based MatChipGrid', () => {
422
438
423
439
it ( 'should ignore all non-tab navigation keyboard events from an editing chip' , fakeAsync ( ( ) => {
424
440
testComponent . editable = true ;
441
+ fixture . changeDetectorRef . markForCheck ( ) ;
425
442
fixture . detectChanges ( ) ;
426
443
427
444
chips . first . focus ( ) ;
@@ -572,6 +589,7 @@ describe('MDC-based MatChipGrid', () => {
572
589
573
590
it ( 'should take an initial view value with reactive forms' , ( ) => {
574
591
fixture . componentInstance . control = new FormControl ( '[pizza-1]' ) ;
592
+ fixture . changeDetectorRef . markForCheck ( ) ;
575
593
fixture . detectChanges ( ) ;
576
594
577
595
expect ( fixture . componentInstance . chipGrid . value ) . toEqual ( '[pizza-1]' ) ;
@@ -756,6 +774,7 @@ describe('MDC-based MatChipGrid', () => {
756
774
757
775
it ( 'should set aria-invalid if the form field is invalid' , fakeAsync ( ( ) => {
758
776
fixture . componentInstance . control = new FormControl ( '' , [ Validators . required ] ) ;
777
+ fixture . changeDetectorRef . markForCheck ( ) ;
759
778
fixture . detectChanges ( ) ;
760
779
761
780
const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
@@ -991,10 +1010,7 @@ describe('MDC-based MatChipGrid', () => {
991
1010
MatInputModule ,
992
1011
animationsModule ,
993
1012
] ,
994
- providers : [
995
- { provide : Directionality , useValue : directionality } ,
996
- provideZoneChangeDetection ( ) ,
997
- ] ,
1013
+ providers : [ { provide : Directionality , useValue : directionality } ] ,
998
1014
declarations : [ component ] ,
999
1015
} ) . compileComponents ( ) ;
1000
1016
@@ -1149,6 +1165,14 @@ class ChipGridWithFormErrorMessages {
1149
1165
1150
1166
@ViewChild ( 'form' ) form : NgForm ;
1151
1167
formControl = new FormControl ( '' , Validators . required ) ;
1168
+
1169
+ private readonly _changeDetectorRef = inject ( ChangeDetectorRef ) ;
1170
+
1171
+ constructor ( ) {
1172
+ this . formControl . events . pipe ( takeUntilDestroyed ( ) ) . subscribe ( ( ) => {
1173
+ this . _changeDetectorRef . markForCheck ( ) ;
1174
+ } ) ;
1175
+ }
1152
1176
}
1153
1177
1154
1178
@Component ( {
0 commit comments