@@ -4,26 +4,39 @@ import {ComponentFixture, TestBed, async, inject} from '@angular/core/testing';
44import { Directionality } from '@angular/cdk/bidi' ;
55import { dispatchKeyboardEvent } from '@angular/cdk/testing' ;
66import { ESCAPE } from '@angular/cdk/keycodes' ;
7- import { CdkConnectedOverlay , OverlayModule , CdkOverlayOrigin } from './index' ;
7+ import {
8+ CdkConnectedOverlay ,
9+ OverlayModule ,
10+ CdkOverlayOrigin ,
11+ ScrollDispatcher ,
12+ Overlay ,
13+ ScrollStrategy ,
14+ } from './index' ;
815import { OverlayContainer } from './overlay-container' ;
916import { ConnectedPositionStrategy } from './position/connected-position-strategy' ;
1017import {
1118 ConnectedOverlayPositionChange ,
1219 ConnectionPositionPair ,
1320} from './position/connected-position' ;
21+ import { Subject } from 'rxjs/Subject' ;
1422
1523
1624describe ( 'Overlay directives' , ( ) => {
1725 let overlayContainer : OverlayContainer ;
1826 let overlayContainerElement : HTMLElement ;
1927 let fixture : ComponentFixture < ConnectedOverlayDirectiveTest > ;
2028 let dir : { value : string } ;
29+ let scrolledSubject = new Subject ( ) ;
2130
2231 beforeEach ( ( ) => {
2332 TestBed . configureTestingModule ( {
2433 imports : [ OverlayModule ] ,
2534 declarations : [ ConnectedOverlayDirectiveTest , ConnectedOverlayPropertyInitOrder ] ,
26- providers : [ { provide : Directionality , useFactory : ( ) => dir = { value : 'ltr' } } ] ,
35+ providers : [ { provide : Directionality , useFactory : ( ) => dir = { value : 'ltr' } } ,
36+ { provide : ScrollDispatcher , useFactory : ( ) => ( {
37+ scrolled : ( ) => scrolledSubject . asObservable ( )
38+ } ) }
39+ ] ,
2740 } ) ;
2841 } ) ;
2942
@@ -310,7 +323,7 @@ describe('Overlay directives', () => {
310323 } ) ;
311324
312325 describe ( 'outputs' , ( ) => {
313- it ( 'should emit backdropClick appropriately ' , ( ) => {
326+ it ( 'should emit when the backdrop was clicked ' , ( ) => {
314327 fixture . componentInstance . hasBackdrop = true ;
315328 fixture . componentInstance . isOpen = true ;
316329 fixture . detectChanges ( ) ;
@@ -323,7 +336,7 @@ describe('Overlay directives', () => {
323336 expect ( fixture . componentInstance . backdropClicked ) . toBe ( true ) ;
324337 } ) ;
325338
326- it ( 'should emit positionChange appropriately ' , ( ) => {
339+ it ( 'should emit when the position has changed ' , ( ) => {
327340 expect ( fixture . componentInstance . positionChangeHandler ) . not . toHaveBeenCalled ( ) ;
328341 fixture . componentInstance . isOpen = true ;
329342 fixture . detectChanges ( ) ;
@@ -336,34 +349,57 @@ describe('Overlay directives', () => {
336349 . toBe ( true , `Expected directive to emit an instance of ConnectedOverlayPositionChange.` ) ;
337350 } ) ;
338351
339- it ( 'should emit attach and detach appropriately ' , ( ) => {
352+ it ( 'should emit when attached ' , ( ) => {
340353 expect ( fixture . componentInstance . attachHandler ) . not . toHaveBeenCalled ( ) ;
341- expect ( fixture . componentInstance . detachHandler ) . not . toHaveBeenCalled ( ) ;
342354 fixture . componentInstance . isOpen = true ;
343355 fixture . detectChanges ( ) ;
344356
345357 expect ( fixture . componentInstance . attachHandler ) . toHaveBeenCalled ( ) ;
346358 expect ( fixture . componentInstance . attachResult instanceof HTMLElement )
347359 . toBe ( true , `Expected pane to be populated with HTML elements when attach was called.` ) ;
360+
361+ fixture . componentInstance . isOpen = false ;
362+ fixture . detectChanges ( ) ;
363+ } ) ;
364+
365+ it ( 'should emit when detached' , ( ) => {
366+ expect ( fixture . componentInstance . detachHandler ) . not . toHaveBeenCalled ( ) ;
367+ fixture . componentInstance . isOpen = true ;
368+ fixture . detectChanges ( ) ;
369+
348370 expect ( fixture . componentInstance . detachHandler ) . not . toHaveBeenCalled ( ) ;
349371
350372 fixture . componentInstance . isOpen = false ;
351373 fixture . detectChanges ( ) ;
352374 expect ( fixture . componentInstance . detachHandler ) . toHaveBeenCalled ( ) ;
353375 } ) ;
354376
377+ it ( 'should emit when detached externally' , inject ( [ Overlay ] , ( overlay : Overlay ) => {
378+ expect ( fixture . componentInstance . detachHandler ) . not . toHaveBeenCalled ( ) ;
379+ fixture . componentInstance . scrollStrategy = overlay . scrollStrategies . close ( ) ;
380+ fixture . componentInstance . isOpen = true ;
381+ fixture . detectChanges ( ) ;
382+
383+ expect ( fixture . componentInstance . detachHandler ) . not . toHaveBeenCalled ( ) ;
384+
385+ scrolledSubject . next ( ) ;
386+ fixture . detectChanges ( ) ;
387+
388+ expect ( fixture . componentInstance . detachHandler ) . toHaveBeenCalled ( ) ;
389+ } ) ) ;
390+
355391 } ) ;
356392
357393} ) ;
358394
359-
360395@Component ( {
361396 template : `
362397 <button cdk-overlay-origin id="trigger" #trigger="cdkOverlayOrigin">Toggle menu</button>
363398 <button cdk-overlay-origin id="otherTrigger" #otherTrigger="cdkOverlayOrigin">Toggle menu</button>
364399
365400 <ng-template cdk-connected-overlay [open]="isOpen" [width]="width" [height]="height"
366401 [cdkConnectedOverlayOrigin]="triggerOverride || trigger"
402+ [scrollStrategy]="scrollStrategy"
367403 [hasBackdrop]="hasBackdrop" backdropClass="mat-test-class"
368404 (backdropClick)="backdropClicked=true" [offsetX]="offsetX" [offsetY]="offsetY"
369405 (positionChange)="positionChangeHandler($event)" (attach)="attachHandler()"
@@ -387,11 +423,12 @@ class ConnectedOverlayDirectiveTest {
387423 triggerOverride : CdkOverlayOrigin ;
388424 hasBackdrop : boolean ;
389425 backdropClicked = false ;
426+ scrollStrategy : ScrollStrategy ;
390427 positionChangeHandler = jasmine . createSpy ( 'positionChangeHandler' ) ;
391428 positionOverrides : ConnectionPositionPair [ ] ;
392429 attachHandler = jasmine . createSpy ( 'attachHandler' ) . and . callFake ( ( ) => {
393- this . attachResult =
394- this . connectedOverlayDirective . overlayRef . overlayElement . querySelector ( 'p' ) as HTMLElement ;
430+ const overlayElement = this . connectedOverlayDirective . overlayRef . overlayElement ;
431+ this . attachResult = overlayElement . querySelector ( 'p' ) as HTMLElement ;
395432 } ) ;
396433 detachHandler = jasmine . createSpy ( 'detachHandler' ) ;
397434 attachResult : HTMLElement ;
0 commit comments