Skip to content

Commit 25f15bf

Browse files
authored
test: move aria tests away from fakeAsync (#32214)
`fakeAsync` can be problematic on the framework side. These changes switch tha `aria` tests away from it since it's new code.
1 parent 943e1e3 commit 25f15bf

File tree

6 files changed

+61
-68
lines changed

6 files changed

+61
-68
lines changed

src/aria/combobox/combobox.spec.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Component, computed, DebugElement, signal} from '@angular/core';
2-
import {ComponentFixture, TestBed, fakeAsync, tick} from '@angular/core/testing';
2+
import {ComponentFixture, TestBed} from '@angular/core/testing';
33
import {By} from '@angular/platform-browser';
44
import {Combobox, ComboboxInput, ComboboxPopup, ComboboxPopupContainer} from '../combobox';
55
import {Listbox, Option} from '../listbox';
@@ -421,37 +421,33 @@ describe('Combobox', () => {
421421
expect(fixture.componentInstance.value()).toEqual(['California']);
422422
});
423423

424-
it('should insert a highlighted completion string on input', fakeAsync(() => {
424+
it('should insert a highlighted completion string on input', () => {
425425
focus();
426426
input('A');
427-
tick();
428427

429428
expect(inputElement.value).toBe('Alabama');
430429
expect(inputElement.selectionStart).toBe(1);
431430
expect(inputElement.selectionEnd).toBe(7);
432-
}));
431+
});
433432

434-
it('should not insert a completion string on backspace', fakeAsync(() => {
433+
it('should not insert a completion string on backspace', () => {
435434
focus();
436435
input('New');
437-
tick();
438436

439437
expect(inputElement.value).toBe('New Hampshire');
440438
expect(inputElement.selectionStart).toBe(3);
441439
expect(inputElement.selectionEnd).toBe(13);
442-
}));
440+
});
443441

444-
it('should insert a completion string even if the items are not changed', fakeAsync(() => {
442+
it('should insert a completion string even if the items are not changed', () => {
445443
focus();
446444
input('New');
447-
tick();
448445

449446
input('New ');
450-
tick();
451447
expect(inputElement.value).toBe('New Hampshire');
452448
expect(inputElement.selectionStart).toBe(4);
453449
expect(inputElement.selectionEnd).toBe(13);
454-
}));
450+
});
455451

456452
it('should commit the selected option on focusout', () => {
457453
focus();
@@ -937,15 +933,14 @@ describe('Combobox', () => {
937933
expect(fixture.componentInstance.value()).toEqual(['September']);
938934
});
939935

940-
it('should insert a highlighted completion string on input', fakeAsync(() => {
936+
it('should insert a highlighted completion string on input', () => {
941937
focus();
942938
input('Feb');
943-
tick();
944939

945940
expect(inputElement.value).toBe('February');
946941
expect(inputElement.selectionStart).toBe(3);
947942
expect(inputElement.selectionEnd).toBe(8);
948-
}));
943+
});
949944

950945
it('should commit the selected option on focusout', () => {
951946
focus();

src/aria/listbox/listbox.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Component, DebugElement, signal} from '@angular/core';
22
import {Listbox, Option} from './listbox';
3-
import {ComponentFixture, TestBed, fakeAsync, tick} from '@angular/core/testing';
3+
import {ComponentFixture, TestBed} from '@angular/core/testing';
44
import {By} from '@angular/platform-browser';
55
import {Direction} from '@angular/cdk/bidi';
66
import {provideFakeDirectionality, runAccessibilityChecks} from '@angular/cdk/testing/private';
@@ -686,15 +686,16 @@ describe('Listbox', () => {
686686
expect(optionElements[4].getAttribute('aria-selected')).toBe('false');
687687
});
688688

689-
it('should reset search term after typeaheadDelay', fakeAsync(() => {
689+
it('should reset search term after typeaheadDelay', async () => {
690690
setupListbox({options: getOptions(), focusMode, typeaheadDelay: 0.1});
691691

692692
type('A');
693693
expect(isFocused(1)).toBe(true);
694-
tick(100);
694+
await new Promise(resolve => setTimeout(resolve, 100));
695+
695696
type('A');
696697
expect(isFocused(0)).toBe(true);
697-
}));
698+
});
698699

699700
it('should skip disabled options with typeahead (softDisabled=false)', () => {
700701
setupListbox({options: getOptions(), focusMode, disabledOptions: [2], softDisabled: false});

src/aria/menu/menu.spec.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Component, DebugElement} from '@angular/core';
2-
import {ComponentFixture, TestBed, fakeAsync, tick} from '@angular/core/testing';
2+
import {ComponentFixture, TestBed} from '@angular/core/testing';
33
import {By} from '@angular/platform-browser';
44
import {Menu, MenuBar, MenuItem, MenuTrigger} from './menu';
55

@@ -102,29 +102,27 @@ describe('Standalone Menu Pattern', () => {
102102
});
103103

104104
describe('Typeahead', () => {
105-
it('should move the active item to the next item that starts with the typed character', fakeAsync(() => {
105+
it('should move the active item to the next item that starts with the typed character', () => {
106106
const apple = getItem('Apple');
107107
const banana = getItem('Banana');
108108

109109
keydown(apple!, 'b');
110110
expect(document.activeElement).toBe(banana);
111-
}));
111+
});
112112

113-
it('should support multi-character typeahead', fakeAsync(() => {
113+
it('should support multi-character typeahead', () => {
114114
const apple = getItem('Apple');
115115
const banana = getItem('Banana');
116116
const berries = getItem('Berries');
117117

118118
keydown(apple!, 'b');
119119
expect(document.activeElement).toBe(banana);
120120

121-
tick(100);
122121
keydown(document.activeElement!, 'e');
123-
124122
expect(document.activeElement).toBe(berries);
125-
}));
123+
});
126124

127-
it('should wrap when reaching the end of the list during typeahead', fakeAsync(() => {
125+
it('should wrap when reaching the end of the list during typeahead', () => {
128126
const apple = getItem('Apple');
129127
const cherry = getItem('Cherry');
130128

@@ -135,14 +133,14 @@ describe('Standalone Menu Pattern', () => {
135133
// Type 'a', which should wrap to 'Apple'
136134
keydown(document.activeElement!, 'a');
137135
expect(document.activeElement).toBe(apple);
138-
}));
136+
});
139137

140-
it('should not move the active item if no item matches the typed character', fakeAsync(() => {
138+
it('should not move the active item if no item matches the typed character', () => {
141139
const apple = getItem('Apple');
142140

143141
keydown(apple!, 'z');
144142
expect(document.activeElement).toBe(apple);
145-
}));
143+
});
146144
});
147145
});
148146

@@ -308,12 +306,11 @@ describe('Standalone Menu Pattern', () => {
308306
expect(document.activeElement).toBe(berries);
309307
});
310308

311-
it('should open submenu on mouseover', fakeAsync(() => {
309+
it('should open submenu on mouseover', () => {
312310
const berries = getItem('Berries');
313311
mouseover(berries!);
314-
tick();
315312
expect(isSubmenuExpanded()).toBe(true);
316-
}));
313+
});
317314

318315
it('should close on selecting an item on click', () => {
319316
spyOn(fixture.componentInstance, 'onSelect');
@@ -385,34 +382,30 @@ describe('Standalone Menu Pattern', () => {
385382
externalElement.remove();
386383
});
387384

388-
it('should close an unfocused submenu on mouse out', fakeAsync(() => {
385+
it('should close an unfocused submenu on mouse out', () => {
389386
const berries = getItem('Berries');
390387
const submenu = getSubmenu();
391388

392389
mouseover(berries!);
393-
tick();
394390
expect(isSubmenuExpanded()).toBe(true);
395391

396392
mouseout(berries!);
397393
mouseout(submenu!);
398-
tick(500);
399394

400395
expect(isSubmenuExpanded()).toBe(false);
401-
}));
396+
});
402397

403-
it('should not close an unfocused submenu on mouse out if the parent menu is hovered', fakeAsync(() => {
398+
it('should not close an unfocused submenu on mouse out if the parent menu is hovered', () => {
404399
const berries = getItem('Berries');
405400
const submenu = getSubmenu();
406401

407402
mouseover(berries!);
408-
tick();
409403
expect(isSubmenuExpanded()).toBe(true);
410404

411405
mouseout(berries!);
412406
mouseover(submenu!);
413-
tick(500);
414407
expect(isSubmenuExpanded()).toBe(true);
415-
}));
408+
});
416409
});
417410
});
418411

src/aria/private/behaviors/list-typeahead/list-typeahead.spec.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import {Signal, signal, WritableSignal} from '@angular/core';
1010
import {ListTypeaheadItem, ListTypeahead, ListTypeaheadInputs} from './list-typeahead';
11-
import {fakeAsync, tick} from '@angular/core/testing';
1211
import {getListFocus} from '../list-focus/list-focus.spec';
1312
import {ListFocus} from '../list-focus/list-focus';
1413

@@ -68,15 +67,15 @@ describe('List Typeahead', () => {
6867
expect(typeahead.inputs.focusManager.activeIndex()).toBe(3);
6968
});
7069

71-
it('should reset after a delay', fakeAsync(() => {
70+
it('should reset after a delay', async () => {
7271
typeahead.search('i');
7372
expect(typeahead.inputs.focusManager.activeIndex()).toBe(1);
7473

75-
tick(500);
74+
await new Promise(resolve => setTimeout(resolve, 500));
7675

7776
typeahead.search('i');
7877
expect(typeahead.inputs.focusManager.activeIndex()).toBe(2);
79-
}));
78+
});
8079

8180
it('should skip disabled items', () => {
8281
items[1].disabled.set(true);

src/aria/private/behaviors/list/list.spec.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import {signal, WritableSignal} from '@angular/core';
1010
import {List, ListItem, ListInputs} from './list';
11-
import {fakeAsync, tick} from '@angular/core/testing';
1211

1312
type TestItem<V> = ListItem<V> & {
1413
disabled: WritableSignal<boolean>;
@@ -341,7 +340,11 @@ describe('List Behavior', () => {
341340
});
342341

343342
describe('Typeahead', () => {
344-
it('should navigate to an item via typeahead', fakeAsync(() => {
343+
function delay(amount: number) {
344+
return new Promise(resolve => setTimeout(resolve, amount));
345+
}
346+
347+
it('should navigate to an item via typeahead', async () => {
345348
const {list} = getDefaultPatterns();
346349
expect(list.inputs.activeItem()).toBe(list.inputs.items()[0]);
347350
list.search('b');
@@ -350,24 +353,23 @@ describe('List Behavior', () => {
350353
expect(list.inputs.activeItem()).toBe(list.inputs.items()[3]); // Blackberry
351354
list.search('u');
352355
expect(list.inputs.activeItem()).toBe(list.inputs.items()[4]); // Blueberry
353-
354-
tick(500); // Default delay
356+
await delay(500);
355357

356358
list.search('c');
357359
expect(list.inputs.activeItem()).toBe(list.inputs.items()[5]); // Cantaloupe
358-
}));
360+
});
359361

360-
it('should respect typeaheadDelay', fakeAsync(() => {
362+
it('should respect typeaheadDelay', async () => {
361363
const {list} = getDefaultPatterns({typeaheadDelay: signal(0.1)});
362364
list.search('b');
363365
expect(list.inputs.activeItem()).toBe(list.inputs.items()[2]); // Banana
364-
tick(50); // Less than delay
366+
await delay(50); // Less than delay
365367
list.search('l');
366368
expect(list.inputs.activeItem()).toBe(list.inputs.items()[3]); // Blackberry
367-
tick(101); // More than delay
369+
await delay(101); // More than delay
368370
list.search('c');
369371
expect(list.inputs.activeItem()).toBe(list.inputs.items()[5]); // Cantaloupe
370-
}));
372+
});
371373

372374
it('should select an item via typeahead', () => {
373375
const {list} = getDefaultPatterns({multi: signal(false)});

src/aria/private/menu/menu.spec.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {signal, WritableSignal} from '@angular/core';
1010
import {MenuPattern, MenuBarPattern, MenuItemPattern, MenuTriggerPattern} from './menu';
1111
import {createKeyboardEvent} from '@angular/cdk/testing/private';
1212
import {ModifierKeys} from '@angular/cdk/testing';
13-
import {fakeAsync, tick} from '@angular/core/testing';
1413

1514
// Test types
1615
type TestMenuItem = MenuItemPattern<string> & {
@@ -177,22 +176,26 @@ describe('Standalone Menu Pattern', () => {
177176
});
178177

179178
describe('Typeahead', () => {
180-
it('should move the active item to the next item that starts with the typed character', fakeAsync(() => {
179+
function delay(amount: number) {
180+
return new Promise(resolve => setTimeout(resolve, amount));
181+
}
182+
183+
it('should move the active item to the next item that starts with the typed character', async () => {
181184
const menu = getMenuPattern(undefined, ['Apple', 'Banana', 'Cherry']);
182185
const items = menu.inputs.items();
183186

184187
const b = createKeyboardEvent('keydown', 66, 'b');
185188
menu.onKeydown(b);
186-
tick(500);
189+
await delay(500);
187190
expect(menu.inputs.activeItem()).toBe(items[1]);
188191

189192
const c = createKeyboardEvent('keydown', 67, 'c');
190193
menu.onKeydown(c);
191-
tick(500);
194+
await delay(500);
192195
expect(menu.inputs.activeItem()).toBe(items[2]);
193-
}));
196+
});
194197

195-
it('should support multi-character typeahead', fakeAsync(() => {
198+
it('should support multi-character typeahead', async () => {
196199
const menu = getMenuPattern(undefined, ['Cabbage', 'Chard', 'Cherry', 'Cilantro']);
197200

198201
const c = createKeyboardEvent('keydown', 67, 'c');
@@ -208,36 +211,36 @@ describe('Standalone Menu Pattern', () => {
208211
menu.onKeydown(e);
209212
expect(menu.inputs.activeItem()?.value()).toBe('Cherry');
210213

211-
tick(500);
214+
await delay(500);
212215
menu.onKeydown(c);
213216
expect(menu.inputs.activeItem()?.value()).toBe('Cilantro');
214-
}));
217+
});
215218

216-
it('should wrap when reaching the end of the list during typeahead', fakeAsync(() => {
219+
it('should wrap when reaching the end of the list during typeahead', async () => {
217220
const menu = getMenuPattern(undefined, ['Apple', 'Banana', 'Avocado']);
218221
const items = menu.inputs.items();
219222
menu.inputs.activeItem.set(items[1]);
220223

221224
const a = createKeyboardEvent('keydown', 65, 'a');
222225
menu.onKeydown(a);
223-
tick(500);
226+
await delay(500);
224227
expect(menu.inputs.activeItem()).toBe(items[2]);
225228

226229
menu.onKeydown(a);
227-
tick(500);
230+
await delay(500);
228231
expect(menu.inputs.activeItem()).toBe(items[0]);
229-
}));
232+
});
230233

231-
it('should not move the active item if no item matches the typed character', fakeAsync(() => {
234+
it('should not move the active item if no item matches the typed character', async () => {
232235
const menu = getMenuPattern(undefined, ['Apple', 'Banana', 'Cherry']);
233236
const items = menu.inputs.items();
234237
menu.inputs.activeItem.set(items[0]);
235238

236239
const z = createKeyboardEvent('keydown', 90, 'z');
237240
menu.onKeydown(z);
238-
tick(500);
241+
await delay(500);
239242
expect(menu.inputs.activeItem()).toBe(items[0]);
240-
}));
243+
});
241244
});
242245
});
243246

0 commit comments

Comments
 (0)