Skip to content

Commit 88b24e0

Browse files
authored
test(material/chips): fix flakes and re-enable test (#33426)
Fixes a flaky test in the chip listbox and enables a test from #33329 that had to be disabled.
1 parent 8925fc9 commit 88b24e0

2 files changed

Lines changed: 44 additions & 33 deletions

File tree

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

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -180,23 +180,23 @@ describe('MatChipGrid', () => {
180180
expect(chipGridNativeElement.getAttribute('tabindex')).toBe('-1');
181181
});
182182

183-
// TODO: re-enable this test.
184-
// it('should clear the active item in key manager when the last focused chip is destroyed', fakeAsync(() => {
185-
// const fixture = createComponent(StandardChipGrid);
186-
// fixture.detectChanges();
183+
it('should clear the active item in key manager when the last focused chip is destroyed', async () => {
184+
const fixture = createComponent(InputChipGrid);
185+
patchElementFocus(primaryActions[0]);
186+
fixture.detectChanges();
187187

188-
// // Focus a chip
189-
// chips.first.focus();
190-
// fixture.detectChanges();
188+
// Focus a chip
189+
chips.first.focus();
190+
fixture.detectChanges();
191191

192-
// // Remove ALL chips
193-
// fixture.componentInstance.foods = []; // Clear the bound data array
194-
// fixture.detectChanges();
195-
// flush();
192+
// Remove ALL chips
193+
fixture.componentInstance.foods.set([]); // Clear the bound data array
194+
fixture.detectChanges();
195+
await fixture.whenStable();
196196

197-
// // Verify key manager is reset to prevent stale references
198-
// expect(chipGridInstance._keyManager.activeItemIndex).toBe(-1);
199-
// }));
197+
// Verify key manager is reset to prevent stale references
198+
expect((chipGridInstance as any)._keyManager.activeItemIndex).toBe(-1);
199+
});
200200

201201
describe('on chip destroy', () => {
202202
it('should focus the next item', () => {
@@ -872,7 +872,7 @@ describe('MatChipGrid', () => {
872872
}
873873

874874
nativeInput.focus();
875-
expect(fixture.componentInstance.foods)
875+
expect(fixture.componentInstance.foods())
876876
.withContext('Expected all chips to be removed.')
877877
.toEqual([]);
878878
expect(document.activeElement).withContext('Expected input to be focused.').toBe(nativeInput);
@@ -1172,7 +1172,7 @@ class FormFieldChipGrid {
11721172
<mat-form-field>
11731173
<mat-label>New food...</mat-label>
11741174
<mat-chip-grid #chipGrid placeholder="Food" [formControl]="control">
1175-
@for (food of foods; track food) {
1175+
@for (food of foods(); track food) {
11761176
<mat-chip-row [value]="food.value" (removed)="remove(food)">
11771177
{{ food.viewValue }}
11781178
</mat-chip-row>
@@ -1189,7 +1189,7 @@ class FormFieldChipGrid {
11891189
changeDetection: ChangeDetectionStrategy.Eager,
11901190
})
11911191
class InputChipGrid {
1192-
foods: any[] = [
1192+
readonly foods = signal([
11931193
{value: 'steak-0', viewValue: 'Steak'},
11941194
{value: 'pizza-1', viewValue: 'Pizza'},
11951195
{value: 'tacos-2', viewValue: 'Tacos', disabled: true},
@@ -1198,7 +1198,7 @@ class InputChipGrid {
11981198
{value: 'eggs-5', viewValue: 'Eggs'},
11991199
{value: 'pasta-6', viewValue: 'Pasta'},
12001200
{value: 'sushi-7', viewValue: 'Sushi'},
1201-
];
1201+
]);
12021202
control = new FormControl<string | null>(null);
12031203

12041204
separatorKeyCodes = [ENTER, SPACE];
@@ -1209,21 +1209,28 @@ class InputChipGrid {
12091209

12101210
// Add our foods
12111211
if (value) {
1212-
this.foods.push({
1213-
value: `${value.toLowerCase()}-${this.foods.length}`,
1214-
viewValue: value,
1215-
});
1212+
this.foods.update(current => [
1213+
...current,
1214+
{
1215+
value: `${value.toLowerCase()}-${current.length}`,
1216+
viewValue: value,
1217+
},
1218+
]);
12161219
}
12171220

12181221
// Reset the input value
12191222
event.chipInput!.clear();
12201223
}
12211224

12221225
remove(food: any): void {
1223-
const index = this.foods.indexOf(food);
1226+
const index = this.foods().indexOf(food);
12241227

12251228
if (index > -1) {
1226-
this.foods.splice(index, 1);
1229+
this.foods.update(current => {
1230+
const newValue = current.slice();
1231+
newValue.splice(index, 1);
1232+
return newValue;
1233+
});
12271234
}
12281235
}
12291236

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ describe('MatChipListbox', () => {
3535
let directionality: WritableSignal<Direction>;
3636
let primaryActions: NodeListOf<HTMLElement>;
3737

38+
function wait(milliseconds: number) {
39+
return new Promise(resolve => setTimeout(resolve, milliseconds));
40+
}
41+
3842
describe('StandardChipList', () => {
3943
describe('basic behaviors', () => {
4044
beforeEach(() => {
@@ -384,7 +388,7 @@ describe('MatChipListbox', () => {
384388
.withContext('Expected tabIndex to be set to -1 temporarily.')
385389
.toBe(-1);
386390

387-
await new Promise(r => setTimeout(r, 0));
391+
await wait(0);
388392

389393
expect(chipListboxNativeElement.tabIndex)
390394
.withContext('Expected tabIndex to be reset back to 0')
@@ -407,7 +411,7 @@ describe('MatChipListbox', () => {
407411
.withContext('Expected tabIndex to be set to -1 temporarily.')
408412
.toBe(-1);
409413

410-
await new Promise(r => setTimeout(r, 0));
414+
await wait(0);
411415

412416
expect(chipListboxNativeElement.tabIndex)
413417
.withContext('Expected tabIndex to be reset back to 4')
@@ -690,7 +694,7 @@ describe('MatChipListbox', () => {
690694
By.css('mat-chip-listbox'),
691695
)!.nativeElement;
692696
dispatchFakeEvent(nativeChipListbox, 'blur');
693-
await new Promise(r => setTimeout(r, 0));
697+
await wait(0);
694698

695699
expect(fixture.componentInstance.control.touched)
696700
.withContext('Expected the control to be touched.')
@@ -707,7 +711,7 @@ describe('MatChipListbox', () => {
707711
By.css('mat-chip-listbox'),
708712
)!.nativeElement;
709713
dispatchFakeEvent(nativeChipListbox, 'blur');
710-
await new Promise(r => setTimeout(r, 0));
714+
await wait(0);
711715

712716
expect(fixture.componentInstance.control.touched)
713717
.withContext('Expected the control to stay untouched.')
@@ -863,16 +867,16 @@ describe('MatChipListbox', () => {
863867
initFixture.componentInstance.control = new FormControl(['tutorial-1', 'tutorial-2']);
864868
});
865869
fixture.detectChanges();
866-
867-
await new Promise(r => setTimeout(r, 400));
870+
await wait(400);
868871
fixture.detectChanges();
869872

870873
let array = fixture.componentInstance.chips.toArray();
871874

872875
expect(array.length).withContext('Expect chips not to be rendered yet').toBe(0);
873876

874-
await new Promise(r => setTimeout(r, 100));
877+
await wait(100);
875878
fixture.detectChanges();
879+
await fixture.whenStable();
876880

877881
array = fixture.componentInstance.chips.toArray();
878882

@@ -891,13 +895,13 @@ describe('MatChipListbox', () => {
891895
fixture.detectChanges();
892896
await fixture.whenStable();
893897

894-
await new Promise(r => setTimeout(r, 500));
898+
await wait(500);
895899
fixture.detectChanges();
896900

897901
fixture.componentInstance.control.setValue(['tutorial-4']);
898902
fixture.componentInstance.updateChips(['tutorial-3', 'tutorial-4']);
899903

900-
await new Promise(r => setTimeout(r, 500));
904+
await wait(500);
901905
fixture.detectChanges();
902906
await fixture.whenStable();
903907

0 commit comments

Comments
 (0)