Skip to content

Commit

Permalink
fixup! test: move some CDK tests to zoneless
Browse files Browse the repository at this point in the history
.
  • Loading branch information
mmalerba committed May 24, 2024
1 parent 0912b2e commit 5bafb89
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 43 deletions.
3 changes: 0 additions & 3 deletions src/cdk/overlay/overlay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -973,10 +973,7 @@ describe('Overlay', () => {
.toContain('custom-panel-class');
});

// TODO: verify
it('should wait for the overlay to be detached before removing the panelClass', async () => {
viewContainerFixture.autoDetectChanges();

const config = new OverlayConfig({panelClass: 'custom-panel-class'});
const overlayRef = overlay.create(config);

Expand Down
2 changes: 1 addition & 1 deletion src/cdk/overlay/scroll/close-scroll-strategy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ComponentPortal, PortalModule} from '@angular/cdk/portal';
import {CdkScrollable, ScrollDispatcher, ViewportRuler} from '@angular/cdk/scrolling';
import {Component, ElementRef, NgZone} from '@angular/core';
import {Component, ElementRef} from '@angular/core';
import {TestBed, fakeAsync, inject} from '@angular/core/testing';
import {Subject} from 'rxjs';
import {Overlay, OverlayConfig, OverlayContainer, OverlayModule, OverlayRef} from '../index';
Expand Down
12 changes: 5 additions & 7 deletions src/cdk/portal/portal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,15 @@ describe('Portals', () => {
});

it('should throw when trying to load an element without a parent into a DOM portal', () => {
const errorSpy = spyOn(console, 'error');
const testAppComponent = fixture.componentInstance;
const element = document.createElement('div');
const domPortal = new DomPortal(element);

testAppComponent.selectedPortal = domPortal;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(errorSpy.calls.first().args[1]).toMatch(
/DOM portal content must be attached to a parent node./,
);
expect(() => {
testAppComponent.selectedPortal = domPortal;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
}).toThrowError('DOM portal content must be attached to a parent node.');
});

it('should not throw when restoring if the outlet element was cleared', () => {
Expand Down
35 changes: 8 additions & 27 deletions src/cdk/scrolling/virtual-scroll-viewport.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,7 @@ describe('CdkVirtualScrollViewport', () => {
expect(viewport.getOffsetToRenderedContentStart()).toBe(10);
}));

// TODO: verify
fit('should set content offset to bottom of content', fakeAsync(async () => {
fixture.autoDetectChanges();
it('should set content offset to bottom of content', fakeAsync(async () => {
finishInit(fixture);
const contentSize = viewport.measureRenderedContentSize();

Expand Down Expand Up @@ -809,11 +807,13 @@ describe('CdkVirtualScrollViewport', () => {
}));

it('should throw if maxBufferPx is less than minBufferPx', fakeAsync(() => {
const errorSpy = spyOn(console, 'error');
testComponent.minBufferPx = 100;
testComponent.maxBufferPx = 99;
finishInit(fixture);
expect(errorSpy).toHaveBeenCalled();
expect(() => {
testComponent.minBufferPx = 100;
testComponent.maxBufferPx = 99;
finishInit(fixture);
}).toThrowError(
'CDK virtual scroll: maxBufferPx must be greater than or equal to minBufferPx',
);
}));

it('should register and degregister with ScrollDispatcher', fakeAsync(
Expand Down Expand Up @@ -867,23 +867,6 @@ describe('CdkVirtualScrollViewport', () => {

expect(appRef.tick).not.toHaveBeenCalled();
}));

// TODO: fix
xit('should run change detection if there are any viewChange listeners', fakeAsync(() => {
testComponent.virtualForOf.viewChange.subscribe();
finishInit(fixture);
testComponent.items = Array(10).fill(0);
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
flush();

spyOn(appRef, 'tick');

viewport.scrollToIndex(5);
triggerScroll(viewport);

expect(appRef.tick).toHaveBeenCalledTimes(1);
}));
});
});

Expand Down Expand Up @@ -1125,9 +1108,7 @@ describe('CdkVirtualScrollViewport', () => {
.toBe(0);
}));

// TODO: verify
it('should set content offset to bottom of content', fakeAsync(async () => {
fixture.autoDetectChanges();
finishInit(fixture);
const contentSize = viewport.measureRenderedContentSize();

Expand Down
54 changes: 49 additions & 5 deletions src/cdk/scrolling/virtual-scroll-viewport.zone.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import {
ApplicationRef,
Component,
NgZone,
TrackByFunction,
ViewChild,
ViewEncapsulation,
provideZoneChangeDetection,
} from '@angular/core';
import {ComponentFixture, TestBed, fakeAsync, flush, waitForAsync} from '@angular/core/testing';
import {
ComponentFixture,
TestBed,
fakeAsync,
flush,
inject,
waitForAsync,
} from '@angular/core/testing';
import {animationFrameScheduler} from 'rxjs';
import {dispatchFakeEvent} from '../testing/private';
import {ScrollingModule} from './scrolling-module';
import {CdkVirtualForOf} from './virtual-for-of';
import {CdkVirtualScrollViewport} from './virtual-scroll-viewport';
Expand All @@ -16,6 +25,7 @@ describe('CdkVirtualScrollViewport Zone.js intergation', () => {
describe('with FixedSizeVirtualScrollStrategy', () => {
let fixture: ComponentFixture<FixedSizeVirtualScroll>;
let testComponent: FixedSizeVirtualScroll;
let viewport: CdkVirtualScrollViewport;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
Expand All @@ -27,6 +37,7 @@ describe('CdkVirtualScrollViewport Zone.js intergation', () => {
beforeEach(() => {
fixture = TestBed.createComponent(FixedSizeVirtualScroll);
testComponent = fixture.componentInstance;
viewport = testComponent.viewport;
});

it('should emit on viewChange inside the Angular zone', fakeAsync(() => {
Expand All @@ -35,6 +46,30 @@ describe('CdkVirtualScrollViewport Zone.js intergation', () => {
finishInit(fixture);
expect(zoneTest).toHaveBeenCalledWith(true);
}));

describe('viewChange change detection behavior', () => {
let appRef: ApplicationRef;

beforeEach(inject([ApplicationRef], (ar: ApplicationRef) => {
appRef = ar;
}));

it('should run change detection if there are any viewChange listeners', fakeAsync(() => {
testComponent.virtualForOf.viewChange.subscribe();
finishInit(fixture);
testComponent.items = Array(10).fill(0);
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
flush();

spyOn(appRef, 'tick');

viewport.scrollToIndex(5);
triggerScroll(viewport);

expect(appRef.tick).toHaveBeenCalledTimes(1);
}));
});
});
});

Expand All @@ -58,20 +93,20 @@ describe('CdkVirtualScrollViewport Zone.js intergation', () => {
display: flex;
flex-direction: column;
}
.cdk-virtual-scroll-orientation-horizontal .cdk-virtual-scroll-content-wrapper {
flex-direction: row;
}
.cdk-virtual-scroll-viewport {
background-color: #f5f5f5;
}
.item {
box-sizing: border-box;
border: 1px dashed #ccc;
}
.has-margin .item {
margin-bottom: 10px;
}
Expand Down Expand Up @@ -127,3 +162,12 @@ function finishInit(fixture: ComponentFixture<any>) {
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
}

/** Trigger a scroll event on the viewport (optionally setting a new scroll offset). */
function triggerScroll(viewport: CdkVirtualScrollViewport, offset?: number) {
if (offset !== undefined) {
viewport.scrollToOffset(offset);
}
dispatchFakeEvent(viewport.scrollable.getElementRef().nativeElement, 'scroll');
animationFrameScheduler.flush();
}

0 comments on commit 5bafb89

Please sign in to comment.