Skip to content

Commit 205c0a4

Browse files
authored
test: move some CDK tests to zoneless (#29103)
* test: move some CDK tests to zoneless * test: Fix flakiness in virtual scroll tests * test: remove overzealous markForCheck calls
1 parent 32c5308 commit 205c0a4

File tree

7 files changed

+344
-111
lines changed

7 files changed

+344
-111
lines changed

src/cdk/overlay/overlay.spec.ts

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,35 @@
1-
import {
2-
waitForAsync,
3-
fakeAsync,
4-
tick,
5-
ComponentFixture,
6-
TestBed,
7-
flush,
8-
} from '@angular/core/testing';
1+
import {Direction, Directionality} from '@angular/cdk/bidi';
2+
import {CdkPortal, ComponentPortal, TemplatePortal} from '@angular/cdk/portal';
3+
import {Location} from '@angular/common';
4+
import {SpyLocation} from '@angular/common/testing';
95
import {
106
Component,
11-
ViewChild,
12-
ViewContainerRef,
137
ErrorHandler,
14-
Injectable,
158
EventEmitter,
16-
NgZone,
9+
Injectable,
1710
Type,
18-
provideZoneChangeDetection,
11+
ViewChild,
12+
ViewContainerRef,
1913
} from '@angular/core';
20-
import {Direction, Directionality} from '@angular/cdk/bidi';
14+
import {
15+
ComponentFixture,
16+
TestBed,
17+
fakeAsync,
18+
flush,
19+
tick,
20+
waitForAsync,
21+
} from '@angular/core/testing';
22+
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
2123
import {dispatchFakeEvent} from '../testing/private';
22-
import {ComponentPortal, TemplatePortal, CdkPortal} from '@angular/cdk/portal';
23-
import {Location} from '@angular/common';
24-
import {SpyLocation} from '@angular/common/testing';
2524
import {
2625
Overlay,
26+
OverlayConfig,
2727
OverlayContainer,
2828
OverlayModule,
2929
OverlayRef,
30-
OverlayConfig,
3130
PositionStrategy,
3231
ScrollStrategy,
3332
} from './index';
34-
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
3533

3634
describe('Overlay', () => {
3735
let overlay: Overlay;
@@ -48,8 +46,6 @@ describe('Overlay', () => {
4846
TestBed.configureTestingModule({
4947
imports: [OverlayModule, ...imports],
5048
providers: [
51-
provideZoneChangeDetection(),
52-
provideZoneChangeDetection(),
5349
{
5450
provide: Directionality,
5551
useFactory: () => {
@@ -949,26 +945,23 @@ describe('Overlay', () => {
949945
.toContain('custom-panel-class');
950946
});
951947

952-
it('should wait for the overlay to be detached before removing the panelClass', () => {
948+
it('should wait for the overlay to be detached before removing the panelClass', async () => {
953949
const config = new OverlayConfig({panelClass: 'custom-panel-class'});
954950
const overlayRef = overlay.create(config);
955951

956952
overlayRef.attach(componentPortal);
957-
viewContainerFixture.detectChanges();
953+
await viewContainerFixture.whenStable();
958954

959955
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
960956
expect(pane.classList)
961957
.withContext('Expected class to be added')
962958
.toContain('custom-panel-class');
963959

964960
overlayRef.detach();
965-
// Stable emits after zone.run
966-
TestBed.inject(NgZone).run(() => {
967-
viewContainerFixture.detectChanges();
968-
expect(pane.classList)
969-
.withContext('Expected class not to be removed immediately')
970-
.toContain('custom-panel-class');
971-
});
961+
expect(pane.classList)
962+
.withContext('Expected class not to be removed immediately')
963+
.toContain('custom-panel-class');
964+
await viewContainerFixture.whenStable();
972965

973966
expect(pane.classList)
974967
.not.withContext('Expected class to be removed on stable')

src/cdk/overlay/scroll/close-scroll-strategy.spec.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import {inject, TestBed, fakeAsync} from '@angular/core/testing';
2-
import {Component, ElementRef, NgZone, provideZoneChangeDetection} from '@angular/core';
3-
import {Subject} from 'rxjs';
41
import {ComponentPortal, PortalModule} from '@angular/cdk/portal';
52
import {CdkScrollable, ScrollDispatcher, ViewportRuler} from '@angular/cdk/scrolling';
6-
import {Overlay, OverlayConfig, OverlayRef, OverlayModule, OverlayContainer} from '../index';
3+
import {Component, ElementRef} from '@angular/core';
4+
import {TestBed, fakeAsync, inject} from '@angular/core/testing';
5+
import {Subject} from 'rxjs';
6+
import {Overlay, OverlayConfig, OverlayContainer, OverlayModule, OverlayRef} from '../index';
77

88
describe('CloseScrollStrategy', () => {
99
let overlayRef: OverlayRef;
@@ -17,7 +17,6 @@ describe('CloseScrollStrategy', () => {
1717
TestBed.configureTestingModule({
1818
imports: [OverlayModule, PortalModule, MozarellaMsg],
1919
providers: [
20-
provideZoneChangeDetection(),
2120
{
2221
provide: ScrollDispatcher,
2322
useFactory: () => ({
@@ -75,17 +74,6 @@ describe('CloseScrollStrategy', () => {
7574
expect(overlayRef.detach).not.toHaveBeenCalled();
7675
});
7776

78-
it('should detach inside the NgZone', () => {
79-
const spy = jasmine.createSpy('detachment spy');
80-
const subscription = overlayRef.detachments().subscribe(() => spy(NgZone.isInAngularZone()));
81-
82-
overlayRef.attach(componentPortal);
83-
scrolledSubject.next();
84-
85-
expect(spy).toHaveBeenCalledWith(true);
86-
subscription.unsubscribe();
87-
});
88-
8977
it('should be able to reposition the overlay up to a certain threshold before closing', inject(
9078
[Overlay],
9179
(overlay: Overlay) => {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import {ComponentPortal, PortalModule} from '@angular/cdk/portal';
2+
import {Component, NgZone, provideZoneChangeDetection} from '@angular/core';
3+
import {TestBed, fakeAsync, inject} from '@angular/core/testing';
4+
import {Subject} from 'rxjs';
5+
import {Overlay} from '../overlay';
6+
import {OverlayConfig} from '../overlay-config';
7+
import {OverlayContainer} from '../overlay-container';
8+
import {OverlayModule} from '../overlay-module';
9+
import {OverlayRef} from '../overlay-ref';
10+
import {CdkScrollable, ScrollDispatcher, ViewportRuler} from '../public-api';
11+
12+
describe('CloseScrollStrategy Zone.js integration', () => {
13+
let overlayRef: OverlayRef;
14+
let componentPortal: ComponentPortal<MozarellaMsg>;
15+
let scrolledSubject = new Subject<CdkScrollable | undefined>();
16+
let scrollPosition: number;
17+
18+
beforeEach(fakeAsync(() => {
19+
scrollPosition = 0;
20+
21+
TestBed.configureTestingModule({
22+
imports: [OverlayModule, PortalModule, MozarellaMsg],
23+
providers: [
24+
provideZoneChangeDetection(),
25+
{
26+
provide: ScrollDispatcher,
27+
useFactory: () => ({
28+
scrolled: () => scrolledSubject,
29+
}),
30+
},
31+
{
32+
provide: ViewportRuler,
33+
useFactory: () => ({
34+
getViewportScrollPosition: () => ({top: scrollPosition}),
35+
}),
36+
},
37+
],
38+
});
39+
40+
TestBed.compileComponents();
41+
}));
42+
43+
beforeEach(inject([Overlay], (overlay: Overlay) => {
44+
let overlayConfig = new OverlayConfig({scrollStrategy: overlay.scrollStrategies.close()});
45+
overlayRef = overlay.create(overlayConfig);
46+
componentPortal = new ComponentPortal(MozarellaMsg);
47+
}));
48+
49+
afterEach(inject([OverlayContainer], (container: OverlayContainer) => {
50+
overlayRef.dispose();
51+
container.getContainerElement().remove();
52+
}));
53+
54+
it('should detach inside the NgZone', () => {
55+
const spy = jasmine.createSpy('detachment spy');
56+
const subscription = overlayRef.detachments().subscribe(() => spy(NgZone.isInAngularZone()));
57+
58+
overlayRef.attach(componentPortal);
59+
scrolledSubject.next();
60+
61+
expect(spy).toHaveBeenCalledWith(true);
62+
subscription.unsubscribe();
63+
});
64+
});
65+
66+
/** Simple component that we can attach to the overlay. */
67+
@Component({
68+
template: '<p>Mozarella</p>',
69+
standalone: true,
70+
imports: [OverlayModule, PortalModule],
71+
})
72+
class MozarellaMsg {}

0 commit comments

Comments
 (0)