11import { inject , TestBed , async , fakeAsync , ComponentFixture , tick } from '@angular/core/testing' ;
22import { NgModule , Component , ViewChild , ElementRef } from '@angular/core' ;
3- import { Scrollable , ScrollDispatcher , ScrollDispatchModule } from './public-api' ;
3+ import { CdkScrollable , ScrollDispatcher , ScrollDispatchModule } from './public-api' ;
44import { dispatchFakeEvent } from '@angular/cdk/testing' ;
55
66describe ( 'Scroll Dispatcher' , ( ) => {
@@ -26,15 +26,15 @@ describe('Scroll Dispatcher', () => {
2626
2727 it ( 'should be registered with the scrollable directive with the scroll service' , ( ) => {
2828 const componentScrollable = fixture . componentInstance . scrollable ;
29- expect ( scroll . scrollableReferences . has ( componentScrollable ) ) . toBe ( true ) ;
29+ expect ( scroll . scrollContainers . has ( componentScrollable ) ) . toBe ( true ) ;
3030 } ) ;
3131
3232 it ( 'should have the scrollable directive deregistered when the component is destroyed' , ( ) => {
3333 const componentScrollable = fixture . componentInstance . scrollable ;
34- expect ( scroll . scrollableReferences . has ( componentScrollable ) ) . toBe ( true ) ;
34+ expect ( scroll . scrollContainers . has ( componentScrollable ) ) . toBe ( true ) ;
3535
3636 fixture . destroy ( ) ;
37- expect ( scroll . scrollableReferences . has ( componentScrollable ) ) . toBe ( false ) ;
37+ expect ( scroll . scrollContainers . has ( componentScrollable ) ) . toBe ( false ) ;
3838 } ) ;
3939
4040 it ( 'should notify through the directive and service that a scroll event occurred' ,
@@ -52,7 +52,7 @@ describe('Scroll Dispatcher', () => {
5252 // Emit a scroll event from the scrolling element in our component.
5353 // This event should be picked up by the scrollable directive and notify.
5454 // The notification should be picked up by the service.
55- dispatchFakeEvent ( fixture . componentInstance . scrollingElement . nativeElement , 'scroll' ) ;
55+ dispatchFakeEvent ( fixture . componentInstance . scrollingElement . nativeElement , 'scroll' , false ) ;
5656
5757 // The scrollable directive should have notified the service immediately.
5858 expect ( directiveSpy ) . toHaveBeenCalled ( ) ;
@@ -71,7 +71,7 @@ describe('Scroll Dispatcher', () => {
7171 const subscription = fixture . ngZone ! . onUnstable . subscribe ( spy ) ;
7272
7373 scroll . scrolled ( 0 ) . subscribe ( ( ) => { } ) ;
74- dispatchFakeEvent ( document , 'scroll' ) ;
74+ dispatchFakeEvent ( document , 'scroll' , false ) ;
7575
7676 expect ( spy ) . not . toHaveBeenCalled ( ) ;
7777 subscription . unsubscribe ( ) ;
@@ -81,7 +81,7 @@ describe('Scroll Dispatcher', () => {
8181 const spy = jasmine . createSpy ( 'zone unstable callback' ) ;
8282 const subscription = fixture . ngZone ! . onUnstable . subscribe ( spy ) ;
8383
84- dispatchFakeEvent ( fixture . componentInstance . scrollingElement . nativeElement , 'scroll' ) ;
84+ dispatchFakeEvent ( fixture . componentInstance . scrollingElement . nativeElement , 'scroll' , false ) ;
8585
8686 expect ( spy ) . not . toHaveBeenCalled ( ) ;
8787 subscription . unsubscribe ( ) ;
@@ -91,11 +91,11 @@ describe('Scroll Dispatcher', () => {
9191 const spy = jasmine . createSpy ( 'global scroll callback' ) ;
9292 const subscription = scroll . scrolled ( 0 ) . subscribe ( spy ) ;
9393
94- dispatchFakeEvent ( document , 'scroll' ) ;
94+ dispatchFakeEvent ( document , 'scroll' , false ) ;
9595 expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
9696
9797 subscription . unsubscribe ( ) ;
98- dispatchFakeEvent ( document , 'scroll' ) ;
98+ dispatchFakeEvent ( document , 'scroll' , false ) ;
9999
100100 expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
101101 } ) ;
@@ -104,22 +104,48 @@ describe('Scroll Dispatcher', () => {
104104 describe ( 'Nested scrollables' , ( ) => {
105105 let scroll : ScrollDispatcher ;
106106 let fixture : ComponentFixture < NestedScrollingComponent > ;
107+ let element : ElementRef ;
107108
108109 beforeEach ( inject ( [ ScrollDispatcher ] , ( s : ScrollDispatcher ) => {
109110 scroll = s ;
110111
111112 fixture = TestBed . createComponent ( NestedScrollingComponent ) ;
112113 fixture . detectChanges ( ) ;
114+ element = fixture . componentInstance . interestingElement ;
113115 } ) ) ;
114116
115117 it ( 'should be able to identify the containing scrollables of an element' , ( ) => {
116- const interestingElement = fixture . componentInstance . interestingElement ;
117- const scrollContainers = scroll . getScrollContainers ( interestingElement ) ;
118+ const scrollContainers = scroll . getAncestorScrollContainers ( element ) ;
118119 const scrollableElementIds =
119120 scrollContainers . map ( scrollable => scrollable . getElementRef ( ) . nativeElement . id ) ;
120121
121122 expect ( scrollableElementIds ) . toEqual ( [ 'scrollable-1' , 'scrollable-1a' ] ) ;
122123 } ) ;
124+
125+ it ( 'should emit when one of the ancestor scrollable containers is scrolled' , ( ) => {
126+ const spy = jasmine . createSpy ( 'scroll spy' ) ;
127+ const subscription = scroll . ancestorScrolled ( element , 0 ) . subscribe ( spy ) ;
128+ const grandparent = fixture . debugElement . nativeElement . querySelector ( '#scrollable-1' ) ;
129+
130+ dispatchFakeEvent ( grandparent , 'scroll' , false ) ;
131+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
132+
133+ dispatchFakeEvent ( window . document , 'scroll' , false ) ;
134+ expect ( spy ) . toHaveBeenCalledTimes ( 2 ) ;
135+
136+ subscription . unsubscribe ( ) ;
137+ } ) ;
138+
139+ it ( 'should not emit when a non-ancestor is scrolled' , ( ) => {
140+ const spy = jasmine . createSpy ( 'scroll spy' ) ;
141+ const subscription = scroll . ancestorScrolled ( element , 0 ) . subscribe ( spy ) ;
142+ const stranger = fixture . debugElement . nativeElement . querySelector ( '#scrollable-2' ) ;
143+
144+ dispatchFakeEvent ( stranger , 'scroll' , false ) ;
145+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
146+
147+ subscription . unsubscribe ( ) ;
148+ } ) ;
123149 } ) ;
124150
125151 describe ( 'lazy subscription' , ( ) => {
@@ -172,7 +198,7 @@ describe('Scroll Dispatcher', () => {
172198 template : `<div #scrollingElement cdk-scrollable style="height: 9999px"></div>`
173199} )
174200class ScrollingComponent {
175- @ViewChild ( Scrollable ) scrollable : Scrollable ;
201+ @ViewChild ( CdkScrollable ) scrollable : CdkScrollable ;
176202 @ViewChild ( 'scrollingElement' ) scrollingElement : ElementRef ;
177203}
178204
0 commit comments