Skip to content

Commit 60d3317

Browse files
committed
test: Convert material chips tests to zoneless (#29238)
(cherry picked from commit 799d952)
1 parent 9988ef2 commit 60d3317

17 files changed

+175
-118
lines changed

src/material/chips/chip-grid.spec.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ import {
2020
typeInElement,
2121
} from '@angular/cdk/testing/private';
2222
import {
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';
3234
import {ComponentFixture, TestBed, fakeAsync, flush, tick} from '@angular/core/testing';
3335
import {FormControl, FormsModule, NgForm, ReactiveFormsModule, Validators} from '@angular/forms';
3436
import {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({

src/material/chips/chip-grid.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {Directionality} from '@angular/cdk/bidi';
910
import {hasModifierKey, TAB} from '@angular/cdk/keycodes';
1011
import {
1112
AfterContentInit,
@@ -20,6 +21,7 @@ import {
2021
EventEmitter,
2122
Input,
2223
OnDestroy,
24+
OnInit,
2325
Optional,
2426
Output,
2527
QueryList,
@@ -33,15 +35,14 @@ import {
3335
NgForm,
3436
Validators,
3537
} from '@angular/forms';
36-
import {ErrorStateMatcher, _ErrorStateTracker} from '@angular/material/core';
38+
import {_ErrorStateTracker, ErrorStateMatcher} from '@angular/material/core';
3739
import {MatFormFieldControl} from '@angular/material/form-field';
38-
import {MatChipTextControl} from './chip-text-control';
39-
import {Observable, Subject, merge} from 'rxjs';
40+
import {merge, Observable, Subject} from 'rxjs';
4041
import {takeUntil} from 'rxjs/operators';
4142
import {MatChipEvent} from './chip';
4243
import {MatChipRow} from './chip-row';
4344
import {MatChipSet} from './chip-set';
44-
import {Directionality} from '@angular/cdk/bidi';
45+
import {MatChipTextControl} from './chip-text-control';
4546

4647
/** Change event object that is emitted when the chip grid value has changed. */
4748
export class MatChipGridChange {
@@ -90,7 +91,8 @@ export class MatChipGrid
9091
ControlValueAccessor,
9192
DoCheck,
9293
MatFormFieldControl<any>,
93-
OnDestroy
94+
OnDestroy,
95+
OnInit
9496
{
9597
/**
9698
* Implemented as part of MatFormFieldControl.
@@ -278,6 +280,14 @@ export class MatChipGrid
278280
);
279281
}
280282

283+
ngOnInit() {
284+
if (this.ngControl) {
285+
this.ngControl.control?.events.pipe(takeUntil(this._destroyed)).subscribe(() => {
286+
this._changeDetectorRef.markForCheck();
287+
});
288+
}
289+
}
290+
281291
ngAfterContentInit() {
282292
this.chipBlurChanges.pipe(takeUntil(this._destroyed)).subscribe(() => {
283293
this._blur();

src/material/chips/chip-input.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import {Directionality} from '@angular/cdk/bidi';
22
import {COMMA, ENTER, TAB} from '@angular/cdk/keycodes';
33
import {PlatformModule} from '@angular/cdk/platform';
44
import {dispatchKeyboardEvent} from '@angular/cdk/testing/private';
5-
import {Component, DebugElement, ViewChild, provideZoneChangeDetection} from '@angular/core';
6-
import {waitForAsync, ComponentFixture, fakeAsync, TestBed, flush} from '@angular/core/testing';
5+
import {Component, DebugElement, ViewChild} from '@angular/core';
6+
import {ComponentFixture, TestBed, fakeAsync, flush, waitForAsync} from '@angular/core/testing';
77
import {MatFormFieldModule} from '@angular/material/form-field';
88
import {By} from '@angular/platform-browser';
99
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
@@ -25,12 +25,6 @@ describe('MDC-based MatChipInput', () => {
2525
let chipInputDirective: MatChipInput;
2626
let dir = 'ltr';
2727

28-
beforeEach(() => {
29-
TestBed.configureTestingModule({
30-
providers: [provideZoneChangeDetection()],
31-
});
32-
});
33-
3428
beforeEach(waitForAsync(() => {
3529
TestBed.configureTestingModule({
3630
imports: [PlatformModule, MatChipsModule, MatFormFieldModule, NoopAnimationsModule],
@@ -77,6 +71,7 @@ describe('MDC-based MatChipInput', () => {
7771
expect(inputNativeElement.hasAttribute('placeholder')).toBe(false);
7872

7973
testChipInput.placeholder = 'bound placeholder';
74+
fixture.changeDetectorRef.markForCheck();
8075
fixture.detectChanges();
8176

8277
expect(inputNativeElement.getAttribute('placeholder')).toBe('bound placeholder');
@@ -87,6 +82,7 @@ describe('MDC-based MatChipInput', () => {
8782
expect(chipInputDirective.disabled).toBe(false);
8883

8984
fixture.componentInstance.chipGridInstance.disabled = true;
85+
fixture.changeDetectorRef.markForCheck();
9086
fixture.detectChanges();
9187

9288
expect(inputNativeElement.getAttribute('disabled')).toBe('true');
@@ -97,6 +93,7 @@ describe('MDC-based MatChipInput', () => {
9793
expect(inputNativeElement.hasAttribute('aria-required')).toBe(false);
9894

9995
fixture.componentInstance.required = true;
96+
fixture.changeDetectorRef.markForCheck();
10097
fixture.detectChanges();
10198

10299
expect(inputNativeElement.getAttribute('aria-required')).toBe('true');
@@ -106,6 +103,7 @@ describe('MDC-based MatChipInput', () => {
106103
expect(inputNativeElement.hasAttribute('required')).toBe(false);
107104

108105
fixture.componentInstance.required = true;
106+
fixture.changeDetectorRef.markForCheck();
109107
fixture.detectChanges();
110108

111109
expect(inputNativeElement.getAttribute('required')).toBe('true');
@@ -144,6 +142,7 @@ describe('MDC-based MatChipInput', () => {
144142
spyOn(testChipInput, 'add');
145143

146144
testChipInput.addOnBlur = true;
145+
fixture.changeDetectorRef.markForCheck();
147146
fixture.detectChanges();
148147

149148
chipInputDirective._blur();
@@ -154,6 +153,7 @@ describe('MDC-based MatChipInput', () => {
154153
spyOn(testChipInput, 'add');
155154

156155
testChipInput.addOnBlur = false;
156+
fixture.changeDetectorRef.markForCheck();
157157
fixture.detectChanges();
158158

159159
chipInputDirective._blur();

0 commit comments

Comments
 (0)