From 5bafb89ac4943eefbc8d31ea7f9d1f6177796a33 Mon Sep 17 00:00:00 2001 From: Miles Malerba Date: Thu, 23 May 2024 15:30:26 -0700 Subject: [PATCH] fixup! test: move some CDK tests to zoneless . --- src/cdk/overlay/overlay.spec.ts | 3 -- .../scroll/close-scroll-strategy.spec.ts | 2 +- src/cdk/portal/portal.spec.ts | 12 ++--- .../scrolling/virtual-scroll-viewport.spec.ts | 35 +++--------- .../virtual-scroll-viewport.zone.spec.ts | 54 +++++++++++++++++-- 5 files changed, 63 insertions(+), 43 deletions(-) diff --git a/src/cdk/overlay/overlay.spec.ts b/src/cdk/overlay/overlay.spec.ts index c530576077ef..6b9130a3bc7b 100644 --- a/src/cdk/overlay/overlay.spec.ts +++ b/src/cdk/overlay/overlay.spec.ts @@ -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); diff --git a/src/cdk/overlay/scroll/close-scroll-strategy.spec.ts b/src/cdk/overlay/scroll/close-scroll-strategy.spec.ts index 37b0f55b3d01..54c404ff8f11 100644 --- a/src/cdk/overlay/scroll/close-scroll-strategy.spec.ts +++ b/src/cdk/overlay/scroll/close-scroll-strategy.spec.ts @@ -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'; diff --git a/src/cdk/portal/portal.spec.ts b/src/cdk/portal/portal.spec.ts index 7a455822b2b5..f358ee44bfc8 100644 --- a/src/cdk/portal/portal.spec.ts +++ b/src/cdk/portal/portal.spec.ts @@ -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', () => { diff --git a/src/cdk/scrolling/virtual-scroll-viewport.spec.ts b/src/cdk/scrolling/virtual-scroll-viewport.spec.ts index dd6f90b5e5be..d8638121f1f9 100644 --- a/src/cdk/scrolling/virtual-scroll-viewport.spec.ts +++ b/src/cdk/scrolling/virtual-scroll-viewport.spec.ts @@ -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(); @@ -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( @@ -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); - })); }); }); @@ -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(); diff --git a/src/cdk/scrolling/virtual-scroll-viewport.zone.spec.ts b/src/cdk/scrolling/virtual-scroll-viewport.zone.spec.ts index c2dec79fd4b1..a1d9dfbb055d 100644 --- a/src/cdk/scrolling/virtual-scroll-viewport.zone.spec.ts +++ b/src/cdk/scrolling/virtual-scroll-viewport.zone.spec.ts @@ -1,4 +1,5 @@ import { + ApplicationRef, Component, NgZone, TrackByFunction, @@ -6,8 +7,16 @@ import { 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'; @@ -16,6 +25,7 @@ describe('CdkVirtualScrollViewport Zone.js intergation', () => { describe('with FixedSizeVirtualScrollStrategy', () => { let fixture: ComponentFixture; let testComponent: FixedSizeVirtualScroll; + let viewport: CdkVirtualScrollViewport; beforeEach(waitForAsync(() => { TestBed.configureTestingModule({ @@ -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(() => { @@ -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); + })); + }); }); }); @@ -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; } @@ -127,3 +162,12 @@ function finishInit(fixture: ComponentFixture) { 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(); +}