Skip to content

Commit

Permalink
test(multiple): remove unnecessary fakeAsync and add flush
Browse files Browse the repository at this point in the history
Fixes various tests that were using `fakeAsync` unnecessarily or were missing `flush`. They showed up after the update to v19.
  • Loading branch information
crisbeto committed Aug 30, 2024
1 parent 9194f61 commit 2600e0d
Show file tree
Hide file tree
Showing 24 changed files with 562 additions and 493 deletions.
8 changes: 6 additions & 2 deletions src/cdk-experimental/selection/selection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,12 @@ describe('cdkSelectionColumn', () => {
});
}));

beforeEach(() => {
beforeEach(fakeAsync(() => {
fixture = TestBed.createComponent(MultiSelectTableWithSelectionColumn);
component = fixture.componentInstance;
fixture.detectChanges();
});
flush();
}));

it('should show check boxes', () => {
const checkboxes =
Expand Down Expand Up @@ -585,6 +586,7 @@ class MultiSelectTableWithSelectionColumn {
this.getSelectAll().click();
flush();
this._cdr.detectChanges();
flush();
}

clickSelectionToggle(index: number) {
Expand All @@ -596,6 +598,7 @@ class MultiSelectTableWithSelectionColumn {
toggle.click();
flush();
this._cdr.detectChanges();
flush();
}

constructor(
Expand Down Expand Up @@ -648,6 +651,7 @@ class SingleSelectTableWithSelectionColumn {
toggle.click();
flush();
this._cdr.detectChanges();
flush();
}

constructor(
Expand Down
3 changes: 2 additions & 1 deletion src/cdk/bidi/directionality.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {waitForAsync, fakeAsync, TestBed} from '@angular/core/testing';
import {waitForAsync, fakeAsync, TestBed, flush} from '@angular/core/testing';
import {Component, ViewChild, signal} from '@angular/core';
import {By} from '@angular/platform-browser';
import {BidiModule, Directionality, Dir, Direction, DIR_DOCUMENT} from './index';
Expand Down Expand Up @@ -121,6 +121,7 @@ describe('Directionality', () => {
fixture.destroy();
expect(spy).toHaveBeenCalled();
subscription.unsubscribe();
flush();
}));

it('should default to ltr if an invalid value is passed in', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {TestBed, inject, fakeAsync} from '@angular/core/testing';
import {TestBed, inject} from '@angular/core/testing';
import {ApplicationRef, Component, afterRender} from '@angular/core';
import {dispatchFakeEvent, dispatchMouseEvent} from '../../testing/private';
import {OverlayModule, Overlay} from '../index';
Expand Down Expand Up @@ -305,39 +305,35 @@ describe('OverlayOutsideClickDispatcher', () => {
overlayRef.dispose();
});

it(
'should not throw an error when closing out related components via the ' +
'outsidePointerEvents emitter on background click',
fakeAsync(() => {
const firstOverlayRef = overlay.create();
firstOverlayRef.attach(new ComponentPortal(TestComponent));
const secondOverlayRef = overlay.create();
secondOverlayRef.attach(new ComponentPortal(TestComponent));
const thirdOverlayRef = overlay.create();
thirdOverlayRef.attach(new ComponentPortal(TestComponent));

const spy = jasmine.createSpy('background click handler spy').and.callFake(() => {
// we close out both overlays from a single outside click event
firstOverlayRef.detach();
thirdOverlayRef.detach();
});
firstOverlayRef.outsidePointerEvents().subscribe(spy);
secondOverlayRef.outsidePointerEvents().subscribe(spy);
thirdOverlayRef.outsidePointerEvents().subscribe(spy);
it('should not throw an error when closing out related components via the outsidePointerEvents emitter on background click', () => {
const firstOverlayRef = overlay.create();
firstOverlayRef.attach(new ComponentPortal(TestComponent));
const secondOverlayRef = overlay.create();
secondOverlayRef.attach(new ComponentPortal(TestComponent));
const thirdOverlayRef = overlay.create();
thirdOverlayRef.attach(new ComponentPortal(TestComponent));

const spy = jasmine.createSpy('background click handler spy').and.callFake(() => {
// we close out both overlays from a single outside click event
firstOverlayRef.detach();
thirdOverlayRef.detach();
});
firstOverlayRef.outsidePointerEvents().subscribe(spy);
secondOverlayRef.outsidePointerEvents().subscribe(spy);
thirdOverlayRef.outsidePointerEvents().subscribe(spy);

const backgroundElement = document.createElement('div');
document.body.appendChild(backgroundElement);
const backgroundElement = document.createElement('div');
document.body.appendChild(backgroundElement);

expect(() => backgroundElement.click()).not.toThrowError();
expect(() => backgroundElement.click()).not.toThrowError();

expect(spy).toHaveBeenCalled();
expect(spy).toHaveBeenCalled();

backgroundElement.remove();
firstOverlayRef.dispose();
secondOverlayRef.dispose();
thirdOverlayRef.dispose();
}),
);
backgroundElement.remove();
firstOverlayRef.dispose();
secondOverlayRef.dispose();
thirdOverlayRef.dispose();
});

describe('change detection behavior', () => {
it('should not run change detection if there is no portal attached to the overlay', () => {
Expand Down
1 change: 1 addition & 0 deletions src/cdk/overlay/overlay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ describe('Overlay', () => {
tick();

overlayRef.attach(componentPortal);
tick();

expect(overlayPresentInDom)
.withContext('Expected host element to be attached to the DOM.')
Expand Down
3 changes: 3 additions & 0 deletions src/cdk/scrolling/virtual-scroll-viewport.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ describe('CdkVirtualScrollViewport', () => {
flush();
viewport.checkViewportSize();
expect(viewport.getViewportSize()).toBe(500);
flush();
}));

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

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

fixture.componentInstance.items = [];
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
flush();

expect(viewport.getRenderedRange()).toEqual({start: 0, end: 0});
}));
Expand Down
24 changes: 12 additions & 12 deletions src/material-experimental/column-resize/column-resize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {BidiModule} from '@angular/cdk/bidi';
import {DataSource} from '@angular/cdk/collections';
import {ESCAPE} from '@angular/cdk/keycodes';
import {ChangeDetectionStrategy, Component, Directive, ElementRef, ViewChild} from '@angular/core';
import {ComponentFixture, TestBed, fakeAsync, flushMicrotasks} from '@angular/core/testing';
import {ComponentFixture, TestBed, fakeAsync, flush} from '@angular/core/testing';
import {MatTableModule} from '@angular/material/table';
import {BehaviorSubject} from 'rxjs';
import {dispatchKeyboardEvent} from '../../cdk/testing/private';
Expand Down Expand Up @@ -379,7 +379,7 @@ describe('Material Popover Edit', () => {
fixture = TestBed.createComponent(componentClass);
component = fixture.componentInstance;
fixture.detectChanges();
flushMicrotasks();
flush();
}));

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

expect(component.getOverlayThumbElement(0)).toBeUndefined();
}));
Expand All @@ -445,7 +445,7 @@ describe('Material Popover Edit', () => {
const initialThumbPosition = component.getOverlayThumbPosition(1);
component.updateResizeWithMouseInProgress(5);
fixture.detectChanges();
flushMicrotasks();
flush();

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

component.updateResizeWithMouseInProgress(1);
fixture.detectChanges();
flushMicrotasks();
flush();

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

component.completeResizeWithMouseInProgress(1);
flushMicrotasks();
flush();

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

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

component.updateResizeWithMouseInProgress(5);
fixture.detectChanges();
flushMicrotasks();
flush();

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

dispatchKeyboardEvent(document, 'keyup', ESCAPE);
flushMicrotasks();
flush();

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

component.resizeColumnWithMouse(1, 5);
fixture.detectChanges();
flushMicrotasks();
flush();

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

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

component.updateResizeWithMouseInProgress(5);
flushMicrotasks();
flush();

dispatchKeyboardEvent(document, 'keyup', ESCAPE);
flushMicrotasks();
flush();

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

component.columnResize.columnResizeNotifier.resize('name', 173);
flushMicrotasks();
flush();

(expect(component.getColumnWidth(1)) as any).isApproximately(173);
}));
Expand Down
Loading

0 comments on commit 2600e0d

Please sign in to comment.