Skip to content

Commit f429971

Browse files
committed
test(cdk/overlay): change how renders are counted in test
1 parent d4cccb7 commit f429971

File tree

2 files changed

+59
-42
lines changed

2 files changed

+59
-42
lines changed

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

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
import {ApplicationRef, Component} from '@angular/core';
12
import {TestBed, inject} from '@angular/core/testing';
2-
import {ApplicationRef, Component, afterRender} from '@angular/core';
3+
import {ComponentPortal} from '../../portal';
34
import {dispatchFakeEvent, dispatchMouseEvent} from '../../testing/private';
4-
import {OverlayModule, Overlay} from '../index';
5+
import {Overlay, OverlayModule} from '../index';
56
import {OverlayOutsideClickDispatcher} from './overlay-outside-click-dispatcher';
6-
import {ComponentPortal} from '../../portal';
7-
import {filter, take} from 'rxjs/operators';
87

98
describe('OverlayOutsideClickDispatcher', () => {
109
let appRef: ApplicationRef;
@@ -385,44 +384,6 @@ describe('OverlayOutsideClickDispatcher', () => {
385384

386385
expect(appRef.tick).toHaveBeenCalledTimes(0);
387386
});
388-
389-
it('should run change detection if the click was made outside the overlay and there are `outsidePointerEvents` observers', async () => {
390-
let renders = 0;
391-
TestBed.runInInjectionContext(() => {
392-
afterRender(() => {
393-
renders++;
394-
});
395-
});
396-
function stablePromise() {
397-
return TestBed.inject(ApplicationRef)
398-
.isStable.pipe(
399-
filter(stable => stable),
400-
take(1),
401-
)
402-
.toPromise();
403-
}
404-
await stablePromise();
405-
expect(renders).toEqual(1);
406-
const portal = new ComponentPortal(TestComponent);
407-
const overlayRef = overlay.create();
408-
overlayRef.attach(portal);
409-
outsideClickDispatcher.add(overlayRef);
410-
411-
const context = document.createElement('div');
412-
document.body.appendChild(context);
413-
414-
await stablePromise();
415-
expect(renders).toEqual(2);
416-
dispatchMouseEvent(context, 'click');
417-
await stablePromise();
418-
expect(renders).toEqual(2);
419-
420-
overlayRef.outsidePointerEvents().subscribe();
421-
422-
dispatchMouseEvent(context, 'click');
423-
await stablePromise();
424-
expect(renders).toEqual(2);
425-
});
426387
});
427388
});
428389

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import {ComponentPortal} from '@angular/cdk/portal';
2+
import {dispatchMouseEvent} from '@angular/cdk/testing/private';
3+
import {ApplicationRef, Component, NgZone, provideZoneChangeDetection} from '@angular/core';
4+
import {inject, TestBed} from '@angular/core/testing';
5+
import {Overlay} from '../overlay';
6+
import {OverlayModule} from '../overlay-module';
7+
import {OverlayOutsideClickDispatcher} from './overlay-outside-click-dispatcher';
8+
9+
describe('OverlayOutsideClickDispatcher', () => {
10+
let appRef: ApplicationRef;
11+
let outsideClickDispatcher: OverlayOutsideClickDispatcher;
12+
let overlay: Overlay;
13+
14+
beforeEach(() => {
15+
TestBed.configureTestingModule({
16+
imports: [OverlayModule, TestComponent],
17+
providers: [provideZoneChangeDetection()],
18+
});
19+
20+
inject(
21+
[ApplicationRef, OverlayOutsideClickDispatcher, Overlay],
22+
(ar: ApplicationRef, ocd: OverlayOutsideClickDispatcher, o: Overlay) => {
23+
appRef = ar;
24+
outsideClickDispatcher = ocd;
25+
overlay = o;
26+
},
27+
)();
28+
});
29+
30+
it('should emit `outsidePointerEvents` outside NgZone', async () => {
31+
const calls: boolean[] = [];
32+
const spy = () => {
33+
calls.push(NgZone.isInAngularZone());
34+
};
35+
36+
const portal = new ComponentPortal(TestComponent);
37+
const overlayRef = overlay.create();
38+
overlayRef.attach(portal);
39+
outsideClickDispatcher.add(overlayRef);
40+
41+
const context = document.createElement('div');
42+
document.body.appendChild(context);
43+
44+
overlayRef.outsidePointerEvents().subscribe(spy);
45+
46+
dispatchMouseEvent(context, 'click');
47+
await TestBed.inject(ApplicationRef).whenStable();
48+
expect(calls).toEqual([false]);
49+
});
50+
});
51+
52+
@Component({
53+
template: 'Hello',
54+
imports: [OverlayModule],
55+
})
56+
class TestComponent {}

0 commit comments

Comments
 (0)