Skip to content

Commit 2600e0d

Browse files
committed
test(multiple): remove unnecessary fakeAsync and add flush
Fixes various tests that were using `fakeAsync` unnecessarily or were missing `flush`. They showed up after the update to v19.
1 parent 9194f61 commit 2600e0d

File tree

24 files changed

+562
-493
lines changed

24 files changed

+562
-493
lines changed

src/cdk-experimental/selection/selection.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,12 @@ describe('cdkSelectionColumn', () => {
308308
});
309309
}));
310310

311-
beforeEach(() => {
311+
beforeEach(fakeAsync(() => {
312312
fixture = TestBed.createComponent(MultiSelectTableWithSelectionColumn);
313313
component = fixture.componentInstance;
314314
fixture.detectChanges();
315-
});
315+
flush();
316+
}));
316317

317318
it('should show check boxes', () => {
318319
const checkboxes =
@@ -585,6 +586,7 @@ class MultiSelectTableWithSelectionColumn {
585586
this.getSelectAll().click();
586587
flush();
587588
this._cdr.detectChanges();
589+
flush();
588590
}
589591

590592
clickSelectionToggle(index: number) {
@@ -596,6 +598,7 @@ class MultiSelectTableWithSelectionColumn {
596598
toggle.click();
597599
flush();
598600
this._cdr.detectChanges();
601+
flush();
599602
}
600603

601604
constructor(
@@ -648,6 +651,7 @@ class SingleSelectTableWithSelectionColumn {
648651
toggle.click();
649652
flush();
650653
this._cdr.detectChanges();
654+
flush();
651655
}
652656

653657
constructor(

src/cdk/bidi/directionality.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {waitForAsync, fakeAsync, TestBed} from '@angular/core/testing';
1+
import {waitForAsync, fakeAsync, TestBed, flush} from '@angular/core/testing';
22
import {Component, ViewChild, signal} from '@angular/core';
33
import {By} from '@angular/platform-browser';
44
import {BidiModule, Directionality, Dir, Direction, DIR_DOCUMENT} from './index';
@@ -121,6 +121,7 @@ describe('Directionality', () => {
121121
fixture.destroy();
122122
expect(spy).toHaveBeenCalled();
123123
subscription.unsubscribe();
124+
flush();
124125
}));
125126

126127
it('should default to ltr if an invalid value is passed in', () => {

src/cdk/overlay/dispatchers/overlay-outside-click-dispatcher.spec.ts

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {TestBed, inject, fakeAsync} from '@angular/core/testing';
1+
import {TestBed, inject} from '@angular/core/testing';
22
import {ApplicationRef, Component, afterRender} from '@angular/core';
33
import {dispatchFakeEvent, dispatchMouseEvent} from '../../testing/private';
44
import {OverlayModule, Overlay} from '../index';
@@ -305,39 +305,35 @@ describe('OverlayOutsideClickDispatcher', () => {
305305
overlayRef.dispose();
306306
});
307307

308-
it(
309-
'should not throw an error when closing out related components via the ' +
310-
'outsidePointerEvents emitter on background click',
311-
fakeAsync(() => {
312-
const firstOverlayRef = overlay.create();
313-
firstOverlayRef.attach(new ComponentPortal(TestComponent));
314-
const secondOverlayRef = overlay.create();
315-
secondOverlayRef.attach(new ComponentPortal(TestComponent));
316-
const thirdOverlayRef = overlay.create();
317-
thirdOverlayRef.attach(new ComponentPortal(TestComponent));
318-
319-
const spy = jasmine.createSpy('background click handler spy').and.callFake(() => {
320-
// we close out both overlays from a single outside click event
321-
firstOverlayRef.detach();
322-
thirdOverlayRef.detach();
323-
});
324-
firstOverlayRef.outsidePointerEvents().subscribe(spy);
325-
secondOverlayRef.outsidePointerEvents().subscribe(spy);
326-
thirdOverlayRef.outsidePointerEvents().subscribe(spy);
308+
it('should not throw an error when closing out related components via the outsidePointerEvents emitter on background click', () => {
309+
const firstOverlayRef = overlay.create();
310+
firstOverlayRef.attach(new ComponentPortal(TestComponent));
311+
const secondOverlayRef = overlay.create();
312+
secondOverlayRef.attach(new ComponentPortal(TestComponent));
313+
const thirdOverlayRef = overlay.create();
314+
thirdOverlayRef.attach(new ComponentPortal(TestComponent));
315+
316+
const spy = jasmine.createSpy('background click handler spy').and.callFake(() => {
317+
// we close out both overlays from a single outside click event
318+
firstOverlayRef.detach();
319+
thirdOverlayRef.detach();
320+
});
321+
firstOverlayRef.outsidePointerEvents().subscribe(spy);
322+
secondOverlayRef.outsidePointerEvents().subscribe(spy);
323+
thirdOverlayRef.outsidePointerEvents().subscribe(spy);
327324

328-
const backgroundElement = document.createElement('div');
329-
document.body.appendChild(backgroundElement);
325+
const backgroundElement = document.createElement('div');
326+
document.body.appendChild(backgroundElement);
330327

331-
expect(() => backgroundElement.click()).not.toThrowError();
328+
expect(() => backgroundElement.click()).not.toThrowError();
332329

333-
expect(spy).toHaveBeenCalled();
330+
expect(spy).toHaveBeenCalled();
334331

335-
backgroundElement.remove();
336-
firstOverlayRef.dispose();
337-
secondOverlayRef.dispose();
338-
thirdOverlayRef.dispose();
339-
}),
340-
);
332+
backgroundElement.remove();
333+
firstOverlayRef.dispose();
334+
secondOverlayRef.dispose();
335+
thirdOverlayRef.dispose();
336+
});
341337

342338
describe('change detection behavior', () => {
343339
it('should not run change detection if there is no portal attached to the overlay', () => {

src/cdk/overlay/overlay.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ describe('Overlay', () => {
528528
tick();
529529

530530
overlayRef.attach(componentPortal);
531+
tick();
531532

532533
expect(overlayPresentInDom)
533534
.withContext('Expected host element to be attached to the DOM.')

src/cdk/scrolling/virtual-scroll-viewport.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ describe('CdkVirtualScrollViewport', () => {
8383
flush();
8484
viewport.checkViewportSize();
8585
expect(viewport.getViewportSize()).toBe(500);
86+
flush();
8687
}));
8788

8889
it('should update the viewport size when the page viewport changes', fakeAsync(() => {
@@ -112,12 +113,14 @@ describe('CdkVirtualScrollViewport', () => {
112113
fixture.componentInstance.items = [0, 1];
113114
fixture.changeDetectorRef.markForCheck();
114115
fixture.detectChanges();
116+
flush();
115117

116118
expect(viewport.getRenderedRange()).toEqual({start: 0, end: 2});
117119

118120
fixture.componentInstance.items = [];
119121
fixture.changeDetectorRef.markForCheck();
120122
fixture.detectChanges();
123+
flush();
121124

122125
expect(viewport.getRenderedRange()).toEqual({start: 0, end: 0});
123126
}));

src/material-experimental/column-resize/column-resize.spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {BidiModule} from '@angular/cdk/bidi';
22
import {DataSource} from '@angular/cdk/collections';
33
import {ESCAPE} from '@angular/cdk/keycodes';
44
import {ChangeDetectionStrategy, Component, Directive, ElementRef, ViewChild} from '@angular/core';
5-
import {ComponentFixture, TestBed, fakeAsync, flushMicrotasks} from '@angular/core/testing';
5+
import {ComponentFixture, TestBed, fakeAsync, flush} from '@angular/core/testing';
66
import {MatTableModule} from '@angular/material/table';
77
import {BehaviorSubject} from 'rxjs';
88
import {dispatchKeyboardEvent} from '../../cdk/testing/private';
@@ -379,7 +379,7 @@ describe('Material Popover Edit', () => {
379379
fixture = TestBed.createComponent(componentClass);
380380
component = fixture.componentInstance;
381381
fixture.detectChanges();
382-
flushMicrotasks();
382+
flush();
383383
}));
384384

385385
it('shows resize handle overlays on header row hover and while a resize handle is in use', fakeAsync(() => {
@@ -427,7 +427,7 @@ describe('Material Popover Edit', () => {
427427
component.completeResizeWithMouseInProgress(0);
428428
component.endHoverState();
429429
fixture.detectChanges();
430-
flushMicrotasks();
430+
flush();
431431

432432
expect(component.getOverlayThumbElement(0)).toBeUndefined();
433433
}));
@@ -445,7 +445,7 @@ describe('Material Popover Edit', () => {
445445
const initialThumbPosition = component.getOverlayThumbPosition(1);
446446
component.updateResizeWithMouseInProgress(5);
447447
fixture.detectChanges();
448-
flushMicrotasks();
448+
flush();
449449

450450
let thumbPositionDelta = component.getOverlayThumbPosition(1) - initialThumbPosition;
451451
let columnPositionDelta = component.getColumnOriginPosition(1) - initialColumnPosition;
@@ -463,7 +463,7 @@ describe('Material Popover Edit', () => {
463463

464464
component.updateResizeWithMouseInProgress(1);
465465
fixture.detectChanges();
466-
flushMicrotasks();
466+
flush();
467467

468468
thumbPositionDelta = component.getOverlayThumbPosition(1) - initialThumbPosition;
469469
columnPositionDelta = component.getColumnOriginPosition(1) - initialColumnPosition;
@@ -473,7 +473,7 @@ describe('Material Popover Edit', () => {
473473
(expect(component.getColumnWidth(1)) as any).isApproximately(initialColumnWidth + 1);
474474

475475
component.completeResizeWithMouseInProgress(1);
476-
flushMicrotasks();
476+
flush();
477477

478478
(expect(component.getColumnWidth(1)) as any).isApproximately(initialColumnWidth + 1);
479479

@@ -509,7 +509,7 @@ describe('Material Popover Edit', () => {
509509

510510
component.updateResizeWithMouseInProgress(5);
511511
fixture.detectChanges();
512-
flushMicrotasks();
512+
flush();
513513

514514
let thumbPositionDelta = component.getOverlayThumbPosition(1) - initialThumbPosition;
515515
let columnPositionDelta = component.getColumnOriginPosition(1) - initialColumnPosition;
@@ -521,7 +521,7 @@ describe('Material Popover Edit', () => {
521521
// (expect(component.getTableWidth()) as any).isApproximately(initialTableWidth + 5);
522522

523523
dispatchKeyboardEvent(document, 'keyup', ESCAPE);
524-
flushMicrotasks();
524+
flush();
525525

526526
(expect(component.getColumnWidth(1)) as any).isApproximately(initialColumnWidth);
527527
(expect(component.getTableWidth()) as any).isApproximately(initialTableWidth);
@@ -545,7 +545,7 @@ describe('Material Popover Edit', () => {
545545

546546
component.resizeColumnWithMouse(1, 5);
547547
fixture.detectChanges();
548-
flushMicrotasks();
548+
flush();
549549

550550
expect(resize).toEqual({columnId: 'name', size: initialColumnWidth + 5} as any);
551551

@@ -564,10 +564,10 @@ describe('Material Popover Edit', () => {
564564
component.beginColumnResizeWithMouse(0);
565565

566566
component.updateResizeWithMouseInProgress(5);
567-
flushMicrotasks();
567+
flush();
568568

569569
dispatchKeyboardEvent(document, 'keyup', ESCAPE);
570-
flushMicrotasks();
570+
flush();
571571

572572
component.endHoverState();
573573
fixture.detectChanges();
@@ -580,7 +580,7 @@ describe('Material Popover Edit', () => {
580580
(expect(component.getColumnWidth(1)) as any).not.isApproximately(173);
581581

582582
component.columnResize.columnResizeNotifier.resize('name', 173);
583-
flushMicrotasks();
583+
flush();
584584

585585
(expect(component.getColumnWidth(1)) as any).isApproximately(173);
586586
}));

0 commit comments

Comments
 (0)