Skip to content

Commit c6505d8

Browse files
authored
test(material/expansion): switch tests away from fakeAsync (#33617)
Reworks the expansion panel tests not to depend on `fakeAsync`.
1 parent 3e1e879 commit c6505d8

1 file changed

Lines changed: 23 additions & 26 deletions

File tree

src/material/expansion/expansion.spec.ts

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,7 @@ import {
55
dispatchKeyboardEvent,
66
} from '@angular/cdk/testing/private';
77
import {Component, ViewChild, ChangeDetectionStrategy} from '@angular/core';
8-
import {
9-
ComponentFixture,
10-
TestBed,
11-
fakeAsync,
12-
flush,
13-
tick,
14-
waitForAsync,
15-
} from '@angular/core/testing';
8+
import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing';
169
import {By} from '@angular/platform-browser';
1710
import {
1811
MAT_EXPANSION_PANEL_DEFAULT_OPTIONS,
@@ -34,7 +27,7 @@ describe('MatExpansionPanel', () => {
3427
});
3528
}));
3629

37-
it('should expand and collapse the panel', fakeAsync(() => {
30+
it('should expand and collapse the panel', async () => {
3831
const fixture = TestBed.createComponent(PanelWithContent);
3932
const headerEl = fixture.nativeElement.querySelector('.mat-expansion-panel-header');
4033
fixture.detectChanges();
@@ -44,10 +37,10 @@ describe('MatExpansionPanel', () => {
4437
fixture.componentInstance.expanded = true;
4538
fixture.changeDetectorRef.markForCheck();
4639
fixture.detectChanges();
47-
flush();
40+
await fixture.whenStable();
4841

4942
expect(headerEl.classList).toContain('mat-expanded');
50-
}));
43+
});
5144

5245
it('should add strong focus indication', () => {
5346
const fixture = TestBed.createComponent(PanelWithContent);
@@ -234,12 +227,12 @@ describe('MatExpansionPanel', () => {
234227
expect(wrapper.hasAttribute('inert')).toBe(true);
235228
});
236229

237-
it('should restore focus to header if focused element is inside panel on close', fakeAsync(() => {
230+
it('should restore focus to header if focused element is inside panel on close', async () => {
238231
const fixture = TestBed.createComponent(PanelWithContent);
239232
fixture.componentInstance.expanded = true;
240233
fixture.changeDetectorRef.markForCheck();
241234
fixture.detectChanges();
242-
tick(250);
235+
await wait(300);
243236

244237
const button = fixture.debugElement.query(By.css('button'))!.nativeElement;
245238
const header = fixture.debugElement.query(By.css('mat-expansion-panel-header'))!.nativeElement;
@@ -252,31 +245,31 @@ describe('MatExpansionPanel', () => {
252245
fixture.componentInstance.expanded = false;
253246
fixture.changeDetectorRef.markForCheck();
254247
fixture.detectChanges();
255-
tick(250);
248+
await wait(300);
256249

257250
expect(document.activeElement).withContext('Expected header to be focused.').toBe(header);
258-
}));
251+
});
259252

260-
it('should not change focus origin if origin not specified', fakeAsync(() => {
253+
it('should not change focus origin if origin not specified', async () => {
261254
const fixture = TestBed.createComponent(PanelWithContent);
262255
fixture.componentInstance.expanded = true;
263256
fixture.changeDetectorRef.markForCheck();
264257
fixture.detectChanges();
265-
tick(250);
258+
await wait(300);
266259

267260
const header = fixture.debugElement.query(By.css('mat-expansion-panel-header'))!;
268261
const headerInstance = header.componentInstance;
269262

270263
headerInstance.focus('mouse');
271264
headerInstance.focus();
272265
fixture.detectChanges();
273-
tick(250);
266+
await wait(300);
274267

275268
expect(header.nativeElement.classList).toContain('cdk-focused');
276269
expect(header.nativeElement.classList).toContain('cdk-mouse-focused');
277-
}));
270+
});
278271

279-
it('should not override the panel margin if it is not inside an accordion', fakeAsync(() => {
272+
it('should not override the panel margin if it is not inside an accordion', async () => {
280273
const fixture = TestBed.createComponent(PanelWithCustomMargin);
281274
fixture.detectChanges();
282275

@@ -292,7 +285,7 @@ describe('MatExpansionPanel', () => {
292285
fixture.componentInstance.expanded = true;
293286
fixture.changeDetectorRef.markForCheck();
294287
fixture.detectChanges();
295-
tick(250);
288+
await wait(300);
296289

297290
styles = getComputedStyle(panel.nativeElement);
298291

@@ -301,7 +294,7 @@ describe('MatExpansionPanel', () => {
301294
expect(styles.marginBottom).toBe('13px');
302295
expect(styles.marginLeft).toBe('37px');
303296
expect(styles.marginRight).toBe('37px');
304-
}));
297+
});
305298

306299
it('should be able to hide the toggle', () => {
307300
const fixture = TestBed.createComponent(PanelWithContent);
@@ -352,7 +345,7 @@ describe('MatExpansionPanel', () => {
352345
expect(fixture.componentInstance.expanded).toBe(false);
353346
});
354347

355-
it('should emit events for body expanding and collapsing animations', fakeAsync(() => {
348+
it('should emit events for body expanding and collapsing animations', async () => {
356349
const fixture = TestBed.createComponent(PanelWithContent);
357350
fixture.detectChanges();
358351
let afterExpand = 0;
@@ -363,17 +356,17 @@ describe('MatExpansionPanel', () => {
363356
fixture.componentInstance.expanded = true;
364357
fixture.changeDetectorRef.markForCheck();
365358
fixture.detectChanges();
366-
flush();
359+
await fixture.whenStable();
367360
expect(afterExpand).toBe(1);
368361
expect(afterCollapse).toBe(0);
369362

370363
fixture.componentInstance.expanded = false;
371364
fixture.changeDetectorRef.markForCheck();
372365
fixture.detectChanges();
373-
flush();
366+
await fixture.whenStable();
374367
expect(afterExpand).toBe(1);
375368
expect(afterCollapse).toBe(1);
376-
}));
369+
});
377370

378371
it('should be able to set the default options through the injection token', () => {
379372
TestBed.resetTestingModule().configureTestingModule({
@@ -544,6 +537,10 @@ describe('MatExpansionPanel', () => {
544537
});
545538
});
546539

540+
function wait(milliseconds: number) {
541+
return new Promise(resolve => setTimeout(resolve, milliseconds));
542+
}
543+
547544
@Component({
548545
template: `
549546
<mat-expansion-panel [expanded]="expanded"

0 commit comments

Comments
 (0)