@@ -20,15 +20,17 @@ import {
2020 typeInElement ,
2121} from '@angular/cdk/testing/private' ;
2222import {
23+ ChangeDetectorRef ,
2324 Component ,
2425 DebugElement ,
2526 EventEmitter ,
2627 QueryList ,
2728 Type ,
2829 ViewChild ,
2930 ViewChildren ,
30- provideZoneChangeDetection ,
31+ inject ,
3132} from '@angular/core' ;
33+ import { takeUntilDestroyed } from '@angular/core/rxjs-interop' ;
3234import { ComponentFixture , TestBed , fakeAsync , flush , tick } from '@angular/core/testing' ;
3335import { FormControl , FormsModule , NgForm , ReactiveFormsModule , Validators } from '@angular/forms' ;
3436import { MatFormFieldModule } from '@angular/material/form-field' ;
@@ -70,11 +72,13 @@ describe('MDC-based MatChipGrid', () => {
7072 expect ( chips . toArray ( ) . every ( chip => chip . disabled ) ) . toBe ( false ) ;
7173
7274 chipGridInstance . disabled = true ;
75+ fixture . changeDetectorRef . markForCheck ( ) ;
7376 fixture . detectChanges ( ) ;
7477
7578 expect ( chips . toArray ( ) . every ( chip => chip . disabled ) ) . toBe ( true ) ;
7679
7780 chipGridInstance . disabled = false ;
81+ fixture . changeDetectorRef . markForCheck ( ) ;
7882 fixture . detectChanges ( ) ;
7983
8084 expect ( chips . toArray ( ) . every ( chip => chip . disabled ) ) . toBe ( false ) ;
@@ -84,11 +88,13 @@ describe('MDC-based MatChipGrid', () => {
8488 expect ( chips . toArray ( ) . every ( chip => chip . disabled ) ) . toBe ( false ) ;
8589
8690 chipGridInstance . disabled = true ;
91+ fixture . changeDetectorRef . markForCheck ( ) ;
8792 fixture . detectChanges ( ) ;
8893
8994 expect ( chips . toArray ( ) . every ( chip => chip . disabled ) ) . toBe ( true ) ;
9095
9196 fixture . componentInstance . chips . push ( 5 , 6 ) ;
97+ fixture . changeDetectorRef . markForCheck ( ) ;
9298 fixture . detectChanges ( ) ;
9399 tick ( ) ;
94100 fixture . detectChanges ( ) ;
@@ -98,13 +104,15 @@ describe('MDC-based MatChipGrid', () => {
98104
99105 it ( 'should not set a role on the grid when the list is empty' , ( ) => {
100106 testComponent . chips = [ ] ;
107+ fixture . changeDetectorRef . markForCheck ( ) ;
101108 fixture . detectChanges ( ) ;
102109
103110 expect ( chipGridNativeElement . hasAttribute ( 'role' ) ) . toBe ( false ) ;
104111 } ) ;
105112
106113 it ( 'should be able to set a custom role' , ( ) => {
107114 testComponent . role = 'listbox' ;
115+ fixture . changeDetectorRef . markForCheck ( ) ;
108116 fixture . detectChanges ( ) ;
109117
110118 expect ( chipGridNativeElement . getAttribute ( 'role' ) ) . toBe ( 'listbox' ) ;
@@ -140,6 +148,7 @@ describe('MDC-based MatChipGrid', () => {
140148 . toBe ( false ) ;
141149
142150 chipGridInstance . disabled = true ;
151+ fixture . changeDetectorRef . markForCheck ( ) ;
143152 fixture . detectChanges ( ) ;
144153
145154 chipGridInstance . focus ( ) ;
@@ -154,6 +163,7 @@ describe('MDC-based MatChipGrid', () => {
154163 expect ( chipGridNativeElement . getAttribute ( 'tabindex' ) ) . toBe ( '0' ) ;
155164
156165 chipGridInstance . disabled = true ;
166+ fixture . changeDetectorRef . markForCheck ( ) ;
157167 fixture . detectChanges ( ) ;
158168
159169 expect ( chipGridNativeElement . getAttribute ( 'tabindex' ) ) . toBe ( '-1' ) ;
@@ -168,6 +178,7 @@ describe('MDC-based MatChipGrid', () => {
168178
169179 // Destroy the middle item
170180 testComponent . chips . splice ( 2 , 1 ) ;
181+ fixture . changeDetectorRef . markForCheck ( ) ;
171182 fixture . detectChanges ( ) ;
172183
173184 // It focuses the 4th item
@@ -180,6 +191,7 @@ describe('MDC-based MatChipGrid', () => {
180191
181192 // Destroy the last item
182193 testComponent . chips . pop ( ) ;
194+ fixture . changeDetectorRef . markForCheck ( ) ;
183195 fixture . detectChanges ( ) ;
184196
185197 // It focuses the next-to-last item
@@ -196,6 +208,7 @@ describe('MDC-based MatChipGrid', () => {
196208
197209 // Destroy the middle item
198210 testComponent . chips . splice ( 2 , 1 ) ;
211+ fixture . changeDetectorRef . markForCheck ( ) ;
199212 fixture . detectChanges ( ) ;
200213 flush ( ) ;
201214
@@ -205,12 +218,14 @@ describe('MDC-based MatChipGrid', () => {
205218
206219 it ( 'should focus the grid if the last focused item is removed' , ( ) => {
207220 testComponent . chips = [ 0 ] ;
221+ fixture . changeDetectorRef . markForCheck ( ) ;
208222
209223 spyOn ( chipGridInstance , 'focus' ) ;
210224 patchElementFocus ( chips . last . primaryAction ! . _elementRef . nativeElement ) ;
211225 chips . last . focus ( ) ;
212226
213227 testComponent . chips . pop ( ) ;
228+ fixture . changeDetectorRef . markForCheck ( ) ;
214229 fixture . detectChanges ( ) ;
215230
216231 expect ( chipGridInstance . focus ) . toHaveBeenCalled ( ) ;
@@ -350,6 +365,7 @@ describe('MDC-based MatChipGrid', () => {
350365
351366 it ( `should use user defined tabIndex` , fakeAsync ( ( ) => {
352367 chipGridInstance . tabIndex = 4 ;
368+ fixture . changeDetectorRef . markForCheck ( ) ;
353369 fixture . detectChanges ( ) ;
354370
355371 expect ( chipGridInstance . tabIndex )
@@ -422,6 +438,7 @@ describe('MDC-based MatChipGrid', () => {
422438
423439 it ( 'should ignore all non-tab navigation keyboard events from an editing chip' , fakeAsync ( ( ) => {
424440 testComponent . editable = true ;
441+ fixture . changeDetectorRef . markForCheck ( ) ;
425442 fixture . detectChanges ( ) ;
426443
427444 chips . first . focus ( ) ;
@@ -572,6 +589,7 @@ describe('MDC-based MatChipGrid', () => {
572589
573590 it ( 'should take an initial view value with reactive forms' , ( ) => {
574591 fixture . componentInstance . control = new FormControl ( '[pizza-1]' ) ;
592+ fixture . changeDetectorRef . markForCheck ( ) ;
575593 fixture . detectChanges ( ) ;
576594
577595 expect ( fixture . componentInstance . chipGrid . value ) . toEqual ( '[pizza-1]' ) ;
@@ -756,6 +774,7 @@ describe('MDC-based MatChipGrid', () => {
756774
757775 it ( 'should set aria-invalid if the form field is invalid' , fakeAsync ( ( ) => {
758776 fixture . componentInstance . control = new FormControl ( '' , [ Validators . required ] ) ;
777+ fixture . changeDetectorRef . markForCheck ( ) ;
759778 fixture . detectChanges ( ) ;
760779
761780 const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
@@ -991,10 +1010,7 @@ describe('MDC-based MatChipGrid', () => {
9911010 MatInputModule ,
9921011 animationsModule ,
9931012 ] ,
994- providers : [
995- { provide : Directionality , useValue : directionality } ,
996- provideZoneChangeDetection ( ) ,
997- ] ,
1013+ providers : [ { provide : Directionality , useValue : directionality } ] ,
9981014 declarations : [ component ] ,
9991015 } ) . compileComponents ( ) ;
10001016
@@ -1149,6 +1165,14 @@ class ChipGridWithFormErrorMessages {
11491165
11501166 @ViewChild ( 'form' ) form : NgForm ;
11511167 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+ }
11521176}
11531177
11541178@Component ( {
0 commit comments