@@ -8,13 +8,21 @@ import {
8
8
dispatchEvent ,
9
9
} from '@angular/cdk/testing/private' ;
10
10
import { ESCAPE , A } from '@angular/cdk/keycodes' ;
11
- import { Overlay , CdkConnectedOverlay , OverlayModule , CdkOverlayOrigin } from './index' ;
11
+ import {
12
+ Overlay ,
13
+ CdkConnectedOverlay ,
14
+ OverlayModule ,
15
+ CdkOverlayOrigin ,
16
+ ScrollDispatcher ,
17
+ ScrollStrategy ,
18
+ } from './index' ;
12
19
import { OverlayContainer } from './overlay-container' ;
13
20
import {
14
21
ConnectedOverlayPositionChange ,
15
22
ConnectionPositionPair ,
16
23
} from './position/connected-position' ;
17
24
import { FlexibleConnectedPositionStrategy } from './position/flexible-connected-position-strategy' ;
25
+ import { Subject } from 'rxjs' ;
18
26
19
27
20
28
describe ( 'Overlay directives' , ( ) => {
@@ -23,12 +31,17 @@ describe('Overlay directives', () => {
23
31
let overlayContainerElement : HTMLElement ;
24
32
let fixture : ComponentFixture < ConnectedOverlayDirectiveTest > ;
25
33
let dir : { value : string } ;
34
+ let scrolledSubject = new Subject ( ) ;
26
35
27
36
beforeEach ( ( ) => {
28
37
TestBed . configureTestingModule ( {
29
38
imports : [ OverlayModule ] ,
30
39
declarations : [ ConnectedOverlayDirectiveTest , ConnectedOverlayPropertyInitOrder ] ,
31
- providers : [ { provide : Directionality , useFactory : ( ) => dir = { value : 'ltr' } } ] ,
40
+ providers : [ { provide : Directionality , useFactory : ( ) => dir = { value : 'ltr' } } ,
41
+ { provide : ScrollDispatcher , useFactory : ( ) => ( {
42
+ scrolled : ( ) => scrolledSubject . asObservable ( )
43
+ } ) }
44
+ ] ,
32
45
} ) ;
33
46
} ) ;
34
47
@@ -529,7 +542,7 @@ describe('Overlay directives', () => {
529
542
} ) ;
530
543
531
544
describe ( 'outputs' , ( ) => {
532
- it ( 'should emit backdropClick appropriately ' , ( ) => {
545
+ it ( 'should emit when the backdrop was clicked ' , ( ) => {
533
546
fixture . componentInstance . hasBackdrop = true ;
534
547
fixture . componentInstance . isOpen = true ;
535
548
fixture . detectChanges ( ) ;
@@ -543,7 +556,7 @@ describe('Overlay directives', () => {
543
556
. toHaveBeenCalledWith ( jasmine . any ( MouseEvent ) ) ;
544
557
} ) ;
545
558
546
- it ( 'should emit positionChange appropriately ' , ( ) => {
559
+ it ( 'should emit when the position has changed ' , ( ) => {
547
560
expect ( fixture . componentInstance . positionChangeHandler ) . not . toHaveBeenCalled ( ) ;
548
561
fixture . componentInstance . isOpen = true ;
549
562
fixture . detectChanges ( ) ;
@@ -556,15 +569,24 @@ describe('Overlay directives', () => {
556
569
. toBe ( true , `Expected directive to emit an instance of ConnectedOverlayPositionChange.` ) ;
557
570
} ) ;
558
571
559
- it ( 'should emit attach and detach appropriately ' , ( ) => {
572
+ it ( 'should emit when attached ' , ( ) => {
560
573
expect ( fixture . componentInstance . attachHandler ) . not . toHaveBeenCalled ( ) ;
561
- expect ( fixture . componentInstance . detachHandler ) . not . toHaveBeenCalled ( ) ;
562
574
fixture . componentInstance . isOpen = true ;
563
575
fixture . detectChanges ( ) ;
564
576
565
577
expect ( fixture . componentInstance . attachHandler ) . toHaveBeenCalled ( ) ;
566
578
expect ( fixture . componentInstance . attachResult instanceof HTMLElement )
567
579
. toBe ( true , `Expected pane to be populated with HTML elements when attach was called.` ) ;
580
+
581
+ fixture . componentInstance . isOpen = false ;
582
+ fixture . detectChanges ( ) ;
583
+ } ) ;
584
+
585
+ it ( 'should emit when detached' , ( ) => {
586
+ expect ( fixture . componentInstance . detachHandler ) . not . toHaveBeenCalled ( ) ;
587
+ fixture . componentInstance . isOpen = true ;
588
+ fixture . detectChanges ( ) ;
589
+
568
590
expect ( fixture . componentInstance . detachHandler ) . not . toHaveBeenCalled ( ) ;
569
591
570
592
fixture . componentInstance . isOpen = false ;
@@ -584,11 +606,40 @@ describe('Overlay directives', () => {
584
606
expect ( fixture . componentInstance . keydownHandler ) . toHaveBeenCalledWith ( event ) ;
585
607
} ) ;
586
608
609
+ it ( 'should emit when detached externally' , ( ) => {
610
+ expect ( fixture . componentInstance . detachHandler ) . not . toHaveBeenCalled ( ) ;
611
+ fixture . componentInstance . scrollStrategy = overlay . scrollStrategies . close ( ) ;
612
+ fixture . componentInstance . isOpen = true ;
613
+ fixture . detectChanges ( ) ;
614
+
615
+ expect ( fixture . componentInstance . detachHandler ) . not . toHaveBeenCalled ( ) ;
616
+
617
+ scrolledSubject . next ( ) ;
618
+ fixture . detectChanges ( ) ;
619
+
620
+ expect ( fixture . componentInstance . detachHandler ) . toHaveBeenCalled ( ) ;
621
+ } ) ;
622
+
623
+ // This is intended as a simplified example of a more complicated bug in g3. Technically
624
+ // these events shouldn't invoke their listeners after destruction anyway, but in some
625
+ // tests it can happen. For more context: https://github.com/crisbeto/material2/pull/10
626
+ it ( 'should not emit after the directive is destroyed' , ( ) => {
627
+ const spy = jasmine . createSpy ( 'detach spy' ) ;
628
+ const subscription =
629
+ fixture . componentInstance . connectedOverlayDirective . detach . subscribe ( spy ) ;
630
+
631
+ fixture . componentInstance . isOpen = true ;
632
+ fixture . detectChanges ( ) ;
633
+ fixture . destroy ( ) ;
634
+
635
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
636
+ subscription . unsubscribe ( ) ;
637
+ } ) ;
638
+
587
639
} ) ;
588
640
589
641
} ) ;
590
642
591
-
592
643
@Component ( {
593
644
template : `
594
645
<button cdk-overlay-origin id="trigger" #trigger="cdkOverlayOrigin">Toggle menu</button>
@@ -605,6 +656,7 @@ describe('Overlay directives', () => {
605
656
[cdkConnectedOverlayFlexibleDimensions]="flexibleDimensions"
606
657
[cdkConnectedOverlayGrowAfterOpen]="growAfterOpen"
607
658
[cdkConnectedOverlayPush]="push"
659
+ [cdkConnectedOverlayScrollStrategy]="scrollStrategy"
608
660
cdkConnectedOverlayBackdropClass="mat-test-class"
609
661
cdkConnectedOverlayPanelClass="cdk-test-panel-class"
610
662
(backdropClick)="backdropClickHandler($event)"
@@ -640,13 +692,14 @@ class ConnectedOverlayDirectiveTest {
640
692
flexibleDimensions : boolean ;
641
693
growAfterOpen : boolean ;
642
694
push : boolean ;
695
+ scrollStrategy : ScrollStrategy ;
643
696
backdropClickHandler = jasmine . createSpy ( 'backdropClick handler' ) ;
644
697
positionChangeHandler = jasmine . createSpy ( 'positionChange handler' ) ;
645
698
keydownHandler = jasmine . createSpy ( 'keydown handler' ) ;
646
699
positionOverrides : ConnectionPositionPair [ ] ;
647
700
attachHandler = jasmine . createSpy ( 'attachHandler' ) . and . callFake ( ( ) => {
648
- this . attachResult =
649
- this . connectedOverlayDirective . overlayRef . overlayElement . querySelector ( 'p' ) as HTMLElement ;
701
+ const overlayElement = this . connectedOverlayDirective . overlayRef . overlayElement ;
702
+ this . attachResult = overlayElement . querySelector ( 'p' ) as HTMLElement ;
650
703
} ) ;
651
704
detachHandler = jasmine . createSpy ( 'detachHandler' ) ;
652
705
attachResult : HTMLElement ;
0 commit comments