1
1
import { TestBed , inject , fakeAsync } from '@angular/core/testing' ;
2
- import { Component } from '@angular/core' ;
2
+ import { ApplicationRef , Component } from '@angular/core' ;
3
3
import { dispatchFakeEvent , dispatchMouseEvent } from '../../testing/private' ;
4
4
import { OverlayModule , Overlay } from '../index' ;
5
5
import { OverlayOutsideClickDispatcher } from './overlay-outside-click-dispatcher' ;
6
6
import { ComponentPortal } from '@angular/cdk/portal' ;
7
7
8
8
describe ( 'OverlayOutsideClickDispatcher' , ( ) => {
9
+ let appRef : ApplicationRef ;
9
10
let outsideClickDispatcher : OverlayOutsideClickDispatcher ;
10
11
let overlay : Overlay ;
11
12
@@ -16,8 +17,9 @@ describe('OverlayOutsideClickDispatcher', () => {
16
17
} ) ;
17
18
18
19
inject (
19
- [ OverlayOutsideClickDispatcher , Overlay ] ,
20
- ( ocd : OverlayOutsideClickDispatcher , o : Overlay ) => {
20
+ [ ApplicationRef , OverlayOutsideClickDispatcher , Overlay ] ,
21
+ ( ar : ApplicationRef , ocd : OverlayOutsideClickDispatcher , o : Overlay ) => {
22
+ appRef = ar ;
21
23
outsideClickDispatcher = ocd ;
22
24
overlay = o ;
23
25
} ,
@@ -336,6 +338,70 @@ describe('OverlayOutsideClickDispatcher', () => {
336
338
thirdOverlayRef . dispose ( ) ;
337
339
} ) ,
338
340
) ;
341
+
342
+ describe ( 'change detection behavior' , ( ) => {
343
+ it ( 'should not run change detection if there is no portal attached to the overlay' , ( ) => {
344
+ spyOn ( appRef , 'tick' ) ;
345
+ const overlayRef = overlay . create ( ) ;
346
+ outsideClickDispatcher . add ( overlayRef ) ;
347
+
348
+ const context = document . createElement ( 'div' ) ;
349
+ document . body . appendChild ( context ) ;
350
+
351
+ overlayRef . outsidePointerEvents ( ) . subscribe ( ) ;
352
+ dispatchMouseEvent ( context , 'click' ) ;
353
+
354
+ expect ( appRef . tick ) . toHaveBeenCalledTimes ( 0 ) ;
355
+ } ) ;
356
+
357
+ it ( 'should not run change detection if the click was made outside the overlay but there are no `outsidePointerEvents` observers' , ( ) => {
358
+ spyOn ( appRef , 'tick' ) ;
359
+ const portal = new ComponentPortal ( TestComponent ) ;
360
+ const overlayRef = overlay . create ( ) ;
361
+ overlayRef . attach ( portal ) ;
362
+ outsideClickDispatcher . add ( overlayRef ) ;
363
+
364
+ const context = document . createElement ( 'div' ) ;
365
+ document . body . appendChild ( context ) ;
366
+
367
+ dispatchMouseEvent ( context , 'click' ) ;
368
+
369
+ expect ( appRef . tick ) . toHaveBeenCalledTimes ( 0 ) ;
370
+ } ) ;
371
+
372
+ it ( 'should not run change detection if the click was made inside the overlay and there are `outsidePointerEvents` observers' , ( ) => {
373
+ spyOn ( appRef , 'tick' ) ;
374
+ const portal = new ComponentPortal ( TestComponent ) ;
375
+ const overlayRef = overlay . create ( ) ;
376
+ overlayRef . attach ( portal ) ;
377
+ outsideClickDispatcher . add ( overlayRef ) ;
378
+
379
+ overlayRef . outsidePointerEvents ( ) . subscribe ( ) ;
380
+ dispatchMouseEvent ( overlayRef . overlayElement , 'click' ) ;
381
+
382
+ expect ( appRef . tick ) . toHaveBeenCalledTimes ( 0 ) ;
383
+ } ) ;
384
+
385
+ it ( 'should run change detection if the click was made outside the overlay and there are `outsidePointerEvents` observers' , ( ) => {
386
+ spyOn ( appRef , 'tick' ) ;
387
+ const portal = new ComponentPortal ( TestComponent ) ;
388
+ const overlayRef = overlay . create ( ) ;
389
+ overlayRef . attach ( portal ) ;
390
+ outsideClickDispatcher . add ( overlayRef ) ;
391
+
392
+ const context = document . createElement ( 'div' ) ;
393
+ document . body . appendChild ( context ) ;
394
+
395
+ expect ( appRef . tick ) . toHaveBeenCalledTimes ( 0 ) ;
396
+ dispatchMouseEvent ( context , 'click' ) ;
397
+ expect ( appRef . tick ) . toHaveBeenCalledTimes ( 0 ) ;
398
+
399
+ overlayRef . outsidePointerEvents ( ) . subscribe ( ) ;
400
+
401
+ dispatchMouseEvent ( context , 'click' ) ;
402
+ expect ( appRef . tick ) . toHaveBeenCalledTimes ( 1 ) ;
403
+ } ) ;
404
+ } ) ;
339
405
} ) ;
340
406
341
407
@Component ( {
0 commit comments