1
1
import { inject , TestBed , async , fakeAsync , ComponentFixture , tick } from '@angular/core/testing' ;
2
2
import { NgModule , Component , ViewChild , ElementRef } from '@angular/core' ;
3
- import { Scrollable , ScrollDispatcher , ScrollDispatchModule } from './public-api' ;
3
+ import { CdkScrollable , ScrollDispatcher , ScrollDispatchModule } from './public-api' ;
4
4
import { dispatchFakeEvent } from '@angular/cdk/testing' ;
5
5
6
6
describe ( 'Scroll Dispatcher' , ( ) => {
@@ -26,15 +26,15 @@ describe('Scroll Dispatcher', () => {
26
26
27
27
it ( 'should be registered with the scrollable directive with the scroll service' , ( ) => {
28
28
const componentScrollable = fixture . componentInstance . scrollable ;
29
- expect ( scroll . scrollableReferences . has ( componentScrollable ) ) . toBe ( true ) ;
29
+ expect ( scroll . scrollContainers . has ( componentScrollable ) ) . toBe ( true ) ;
30
30
} ) ;
31
31
32
32
it ( 'should have the scrollable directive deregistered when the component is destroyed' , ( ) => {
33
33
const componentScrollable = fixture . componentInstance . scrollable ;
34
- expect ( scroll . scrollableReferences . has ( componentScrollable ) ) . toBe ( true ) ;
34
+ expect ( scroll . scrollContainers . has ( componentScrollable ) ) . toBe ( true ) ;
35
35
36
36
fixture . destroy ( ) ;
37
- expect ( scroll . scrollableReferences . has ( componentScrollable ) ) . toBe ( false ) ;
37
+ expect ( scroll . scrollContainers . has ( componentScrollable ) ) . toBe ( false ) ;
38
38
} ) ;
39
39
40
40
it ( 'should notify through the directive and service that a scroll event occurred' ,
@@ -52,7 +52,7 @@ describe('Scroll Dispatcher', () => {
52
52
// Emit a scroll event from the scrolling element in our component.
53
53
// This event should be picked up by the scrollable directive and notify.
54
54
// The notification should be picked up by the service.
55
- dispatchFakeEvent ( fixture . componentInstance . scrollingElement . nativeElement , 'scroll' ) ;
55
+ dispatchFakeEvent ( fixture . componentInstance . scrollingElement . nativeElement , 'scroll' , false ) ;
56
56
57
57
// The scrollable directive should have notified the service immediately.
58
58
expect ( directiveSpy ) . toHaveBeenCalled ( ) ;
@@ -71,7 +71,7 @@ describe('Scroll Dispatcher', () => {
71
71
const subscription = fixture . ngZone ! . onUnstable . subscribe ( spy ) ;
72
72
73
73
scroll . scrolled ( 0 ) . subscribe ( ( ) => { } ) ;
74
- dispatchFakeEvent ( document , 'scroll' ) ;
74
+ dispatchFakeEvent ( document , 'scroll' , false ) ;
75
75
76
76
expect ( spy ) . not . toHaveBeenCalled ( ) ;
77
77
subscription . unsubscribe ( ) ;
@@ -81,7 +81,7 @@ describe('Scroll Dispatcher', () => {
81
81
const spy = jasmine . createSpy ( 'zone unstable callback' ) ;
82
82
const subscription = fixture . ngZone ! . onUnstable . subscribe ( spy ) ;
83
83
84
- dispatchFakeEvent ( fixture . componentInstance . scrollingElement . nativeElement , 'scroll' ) ;
84
+ dispatchFakeEvent ( fixture . componentInstance . scrollingElement . nativeElement , 'scroll' , false ) ;
85
85
86
86
expect ( spy ) . not . toHaveBeenCalled ( ) ;
87
87
subscription . unsubscribe ( ) ;
@@ -91,11 +91,11 @@ describe('Scroll Dispatcher', () => {
91
91
const spy = jasmine . createSpy ( 'global scroll callback' ) ;
92
92
const subscription = scroll . scrolled ( 0 ) . subscribe ( spy ) ;
93
93
94
- dispatchFakeEvent ( document , 'scroll' ) ;
94
+ dispatchFakeEvent ( document , 'scroll' , false ) ;
95
95
expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
96
96
97
97
subscription . unsubscribe ( ) ;
98
- dispatchFakeEvent ( document , 'scroll' ) ;
98
+ dispatchFakeEvent ( document , 'scroll' , false ) ;
99
99
100
100
expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
101
101
} ) ;
@@ -104,22 +104,48 @@ describe('Scroll Dispatcher', () => {
104
104
describe ( 'Nested scrollables' , ( ) => {
105
105
let scroll : ScrollDispatcher ;
106
106
let fixture : ComponentFixture < NestedScrollingComponent > ;
107
+ let element : ElementRef ;
107
108
108
109
beforeEach ( inject ( [ ScrollDispatcher ] , ( s : ScrollDispatcher ) => {
109
110
scroll = s ;
110
111
111
112
fixture = TestBed . createComponent ( NestedScrollingComponent ) ;
112
113
fixture . detectChanges ( ) ;
114
+ element = fixture . componentInstance . interestingElement ;
113
115
} ) ) ;
114
116
115
117
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 ) ;
118
119
const scrollableElementIds =
119
120
scrollContainers . map ( scrollable => scrollable . getElementRef ( ) . nativeElement . id ) ;
120
121
121
122
expect ( scrollableElementIds ) . toEqual ( [ 'scrollable-1' , 'scrollable-1a' ] ) ;
122
123
} ) ;
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
+ } ) ;
123
149
} ) ;
124
150
125
151
describe ( 'lazy subscription' , ( ) => {
@@ -172,7 +198,7 @@ describe('Scroll Dispatcher', () => {
172
198
template : `<div #scrollingElement cdk-scrollable style="height: 9999px"></div>`
173
199
} )
174
200
class ScrollingComponent {
175
- @ViewChild ( Scrollable ) scrollable : Scrollable ;
201
+ @ViewChild ( CdkScrollable ) scrollable : CdkScrollable ;
176
202
@ViewChild ( 'scrollingElement' ) scrollingElement : ElementRef ;
177
203
}
178
204
0 commit comments