@@ -816,27 +816,29 @@ describe('Overlay', () => {
816816 } ) ;
817817
818818 describe ( 'scroll strategy' , ( ) => {
819- let fakeScrollStrategy : FakeScrollStrategy ;
820- let config : OverlayConfig ;
821- let overlayRef : OverlayRef ;
822-
823- beforeEach ( ( ) => {
824- fakeScrollStrategy = new FakeScrollStrategy ( ) ;
825- config = new OverlayConfig ( { scrollStrategy : fakeScrollStrategy } ) ;
826- overlayRef = overlay . create ( config ) ;
827- } ) ;
828-
829819 it ( 'should attach the overlay ref to the scroll strategy' , ( ) => {
820+ const fakeScrollStrategy = new FakeScrollStrategy ( ) ;
821+ const config = new OverlayConfig ( { scrollStrategy : fakeScrollStrategy } ) ;
822+ const overlayRef = overlay . create ( config ) ;
823+
830824 expect ( fakeScrollStrategy . overlayRef ) . toBe ( overlayRef ,
831825 'Expected scroll strategy to have been attached to the current overlay ref.' ) ;
832826 } ) ;
833827
834828 it ( 'should enable the scroll strategy when the overlay is attached' , ( ) => {
829+ const fakeScrollStrategy = new FakeScrollStrategy ( ) ;
830+ const config = new OverlayConfig ( { scrollStrategy : fakeScrollStrategy } ) ;
831+ const overlayRef = overlay . create ( config ) ;
832+
835833 overlayRef . attach ( componentPortal ) ;
836834 expect ( fakeScrollStrategy . isEnabled ) . toBe ( true , 'Expected scroll strategy to be enabled.' ) ;
837835 } ) ;
838836
839837 it ( 'should disable the scroll strategy once the overlay is detached' , ( ) => {
838+ const fakeScrollStrategy = new FakeScrollStrategy ( ) ;
839+ const config = new OverlayConfig ( { scrollStrategy : fakeScrollStrategy } ) ;
840+ const overlayRef = overlay . create ( config ) ;
841+
840842 overlayRef . attach ( componentPortal ) ;
841843 expect ( fakeScrollStrategy . isEnabled ) . toBe ( true , 'Expected scroll strategy to be enabled.' ) ;
842844
@@ -845,9 +847,93 @@ describe('Overlay', () => {
845847 } ) ;
846848
847849 it ( 'should disable the scroll strategy when the overlay is destroyed' , ( ) => {
850+ const fakeScrollStrategy = new FakeScrollStrategy ( ) ;
851+ const config = new OverlayConfig ( { scrollStrategy : fakeScrollStrategy } ) ;
852+ const overlayRef = overlay . create ( config ) ;
853+
848854 overlayRef . dispose ( ) ;
849855 expect ( fakeScrollStrategy . isEnabled ) . toBe ( false , 'Expected scroll strategy to be disabled.' ) ;
850856 } ) ;
857+
858+ it ( 'should detach the scroll strategy when the overlay is destroyed' , ( ) => {
859+ const fakeScrollStrategy = new FakeScrollStrategy ( ) ;
860+ const config = new OverlayConfig ( { scrollStrategy : fakeScrollStrategy } ) ;
861+ const overlayRef = overlay . create ( config ) ;
862+
863+ expect ( fakeScrollStrategy . overlayRef ) . toBe ( overlayRef ) ;
864+
865+ overlayRef . dispose ( ) ;
866+
867+ expect ( fakeScrollStrategy . overlayRef ) . toBeNull ( ) ;
868+ } ) ;
869+
870+ it ( 'should be able to swap scroll strategies' , fakeAsync ( ( ) => {
871+ const firstStrategy = new FakeScrollStrategy ( ) ;
872+ const secondStrategy = new FakeScrollStrategy ( ) ;
873+
874+ [ firstStrategy , secondStrategy ] . forEach ( strategy => {
875+ spyOn ( strategy , 'attach' ) ;
876+ spyOn ( strategy , 'enable' ) ;
877+ spyOn ( strategy , 'disable' ) ;
878+ spyOn ( strategy , 'detach' ) ;
879+ } ) ;
880+
881+ const overlayRef = overlay . create ( { scrollStrategy : firstStrategy } ) ;
882+
883+ overlayRef . attach ( componentPortal ) ;
884+ viewContainerFixture . detectChanges ( ) ;
885+ zone . simulateZoneExit ( ) ;
886+ tick ( ) ;
887+
888+ expect ( firstStrategy . attach ) . toHaveBeenCalledTimes ( 1 ) ;
889+ expect ( firstStrategy . enable ) . toHaveBeenCalledTimes ( 1 ) ;
890+
891+ expect ( secondStrategy . attach ) . not . toHaveBeenCalled ( ) ;
892+ expect ( secondStrategy . enable ) . not . toHaveBeenCalled ( ) ;
893+
894+ overlayRef . updateScrollStrategy ( secondStrategy ) ;
895+ viewContainerFixture . detectChanges ( ) ;
896+ tick ( ) ;
897+
898+ expect ( firstStrategy . attach ) . toHaveBeenCalledTimes ( 1 ) ;
899+ expect ( firstStrategy . enable ) . toHaveBeenCalledTimes ( 1 ) ;
900+ expect ( firstStrategy . disable ) . toHaveBeenCalledTimes ( 1 ) ;
901+ expect ( firstStrategy . detach ) . toHaveBeenCalledTimes ( 1 ) ;
902+
903+ expect ( secondStrategy . attach ) . toHaveBeenCalledTimes ( 1 ) ;
904+ expect ( secondStrategy . enable ) . toHaveBeenCalledTimes ( 1 ) ;
905+ } ) ) ;
906+
907+ it ( 'should not do anything when trying to swap a strategy with itself' , fakeAsync ( ( ) => {
908+ const strategy = new FakeScrollStrategy ( ) ;
909+
910+ spyOn ( strategy , 'attach' ) ;
911+ spyOn ( strategy , 'enable' ) ;
912+ spyOn ( strategy , 'disable' ) ;
913+ spyOn ( strategy , 'detach' ) ;
914+
915+ const overlayRef = overlay . create ( { scrollStrategy : strategy } ) ;
916+
917+ overlayRef . attach ( componentPortal ) ;
918+ viewContainerFixture . detectChanges ( ) ;
919+ zone . simulateZoneExit ( ) ;
920+ tick ( ) ;
921+
922+ expect ( strategy . attach ) . toHaveBeenCalledTimes ( 1 ) ;
923+ expect ( strategy . enable ) . toHaveBeenCalledTimes ( 1 ) ;
924+ expect ( strategy . disable ) . not . toHaveBeenCalled ( ) ;
925+ expect ( strategy . detach ) . not . toHaveBeenCalled ( ) ;
926+
927+ overlayRef . updateScrollStrategy ( strategy ) ;
928+ viewContainerFixture . detectChanges ( ) ;
929+ tick ( ) ;
930+
931+ expect ( strategy . attach ) . toHaveBeenCalledTimes ( 1 ) ;
932+ expect ( strategy . enable ) . toHaveBeenCalledTimes ( 1 ) ;
933+ expect ( strategy . disable ) . not . toHaveBeenCalled ( ) ;
934+ expect ( strategy . detach ) . not . toHaveBeenCalled ( ) ;
935+ } ) ) ;
936+
851937 } ) ;
852938} ) ;
853939
@@ -908,4 +994,8 @@ class FakeScrollStrategy implements ScrollStrategy {
908994 disable ( ) {
909995 this . isEnabled = false ;
910996 }
997+
998+ detach ( ) {
999+ this . overlayRef = null ! ;
1000+ }
9111001}
0 commit comments