1
1
import { inject , TestBed , async , ComponentFixture } from '@angular/core/testing' ;
2
- import { NgModule , Component , ViewChild , ElementRef } from '@angular/core' ;
2
+ import { NgModule , Component , ViewChild , ElementRef , QueryList , ViewChildren } from '@angular/core' ;
3
3
import { ScrollDispatcher } from './scroll-dispatcher' ;
4
4
import { OverlayModule } from '../overlay-directives' ;
5
5
import { Scrollable } from './scrollable' ;
6
6
7
7
describe ( 'Scroll Dispatcher' , ( ) => {
8
- let scroll : ScrollDispatcher ;
9
- let fixture : ComponentFixture < ScrollingComponent > ;
10
8
11
9
beforeEach ( async ( ( ) => {
12
10
TestBed . configureTestingModule ( {
@@ -16,45 +14,71 @@ describe('Scroll Dispatcher', () => {
16
14
TestBed . compileComponents ( ) ;
17
15
} ) ) ;
18
16
19
- beforeEach ( inject ( [ ScrollDispatcher ] , ( s : ScrollDispatcher ) => {
20
- scroll = s ;
17
+ describe ( 'Basic usage' , ( ) => {
18
+ let scroll : ScrollDispatcher ;
19
+ let fixture : ComponentFixture < ScrollingComponent > ;
21
20
22
- fixture = TestBed . createComponent ( ScrollingComponent ) ;
23
- fixture . detectChanges ( ) ;
24
- } ) ) ;
21
+ beforeEach ( inject ( [ ScrollDispatcher ] , ( s : ScrollDispatcher ) => {
22
+ scroll = s ;
25
23
26
- it ( 'should be registered with the scrollable directive with the scroll service' , ( ) => {
27
- const componentScrollable = fixture . componentInstance . scrollable ;
28
- expect ( scroll . scrollableReferences . has ( componentScrollable ) ) . toBe ( true ) ;
29
- } ) ;
24
+ fixture = TestBed . createComponent ( ScrollingComponent ) ;
25
+ fixture . detectChanges ( ) ;
26
+ } ) ) ;
27
+
28
+ it ( 'should be registered with the scrollable directive with the scroll service' , ( ) => {
29
+ const componentScrollable = fixture . componentInstance . scrollable ;
30
+ expect ( scroll . scrollableReferences . has ( componentScrollable ) ) . toBe ( true ) ;
31
+ } ) ;
32
+
33
+ it ( 'should have the scrollable directive deregistered when the component is destroyed' , ( ) => {
34
+ const componentScrollable = fixture . componentInstance . scrollable ;
35
+ expect ( scroll . scrollableReferences . has ( componentScrollable ) ) . toBe ( true ) ;
30
36
31
- it ( 'should have the scrollable directive deregistered when the component is destroyed' , ( ) => {
32
- const componentScrollable = fixture . componentInstance . scrollable ;
33
- expect ( scroll . scrollableReferences . has ( componentScrollable ) ) . toBe ( true ) ;
37
+ fixture . destroy ( ) ;
38
+ expect ( scroll . scrollableReferences . has ( componentScrollable ) ) . toBe ( false ) ;
39
+ } ) ;
40
+
41
+ it ( 'should notify through the directive and service that a scroll event occurred' , ( ) => {
42
+ let hasDirectiveScrollNotified = false ;
43
+ // Listen for notifications from scroll directive
44
+ let scrollable = fixture . componentInstance . scrollable ;
45
+ scrollable . elementScrolled ( ) . subscribe ( ( ) => { hasDirectiveScrollNotified = true ; } ) ;
46
+
47
+ // Listen for notifications from scroll service
48
+ let hasServiceScrollNotified = false ;
49
+ scroll . scrolled ( ) . subscribe ( ( ) => { hasServiceScrollNotified = true ; } ) ;
34
50
35
- fixture . destroy ( ) ;
36
- expect ( scroll . scrollableReferences . has ( componentScrollable ) ) . toBe ( false ) ;
51
+ // Emit a scroll event from the scrolling element in our component.
52
+ // This event should be picked up by the scrollable directive and notify.
53
+ // The notification should be picked up by the service.
54
+ const scrollEvent = document . createEvent ( 'UIEvents' ) ;
55
+ scrollEvent . initUIEvent ( 'scroll' , true , true , window , 0 ) ;
56
+ fixture . componentInstance . scrollingElement . nativeElement . dispatchEvent ( scrollEvent ) ;
57
+
58
+ expect ( hasDirectiveScrollNotified ) . toBe ( true ) ;
59
+ expect ( hasServiceScrollNotified ) . toBe ( true ) ;
60
+ } ) ;
37
61
} ) ;
38
62
39
- it ( 'should notify through the directive and service that a scroll event occurred ', ( ) => {
40
- let hasDirectiveScrollNotified = false ;
41
- // Listen for notifications from scroll directive
42
- let scrollable = fixture . componentInstance . scrollable ;
43
- scrollable . elementScrolled ( ) . subscribe ( ( ) => { hasDirectiveScrollNotified = true ; } ) ;
44
-
45
- // Listen for notifications from scroll service
46
- let hasServiceScrollNotified = false ;
47
- scroll . scrolled ( ) . subscribe ( ( ) => { hasServiceScrollNotified = true ; } ) ;
48
-
49
- // Emit a scroll event from the scrolling element in our component.
50
- // This event should be picked up by the scrollable directive and notify.
51
- // The notification should be picked up by the service.
52
- const scrollEvent = document . createEvent ( 'UIEvents' ) ;
53
- scrollEvent . initUIEvent ( 'scroll' , true , true , window , 0 ) ;
54
- fixture . componentInstance . scrollingElement . nativeElement . dispatchEvent ( scrollEvent ) ;
55
-
56
- expect ( hasDirectiveScrollNotified ) . toBe ( true ) ;
57
- expect ( hasServiceScrollNotified ) . toBe ( true ) ;
63
+ fdescribe ( 'Nested scrollables ', ( ) => {
64
+ let scroll : ScrollDispatcher ;
65
+ let fixture : ComponentFixture < NestedScrollingComponent > ;
66
+
67
+ beforeEach ( inject ( [ ScrollDispatcher ] , ( s : ScrollDispatcher ) => {
68
+ scroll = s ;
69
+
70
+ fixture = TestBed . createComponent ( NestedScrollingComponent ) ;
71
+ fixture . detectChanges ( ) ;
72
+ } ) ) ;
73
+
74
+ it ( ' should be able to identify the containing scrollables of an element' , ( ) => {
75
+ const interestingElement = fixture . componentInstance . interestingElement ;
76
+ const scrollContainers = scroll . getScrollContainers ( interestingElement ) ;
77
+ const scrollableElementIds =
78
+ scrollContainers . map ( scrollable => scrollable . getElementRef ( ) . nativeElement . id ) ;
79
+
80
+ expect ( scrollableElementIds ) . toEqual ( [ 'scrollable-1' , 'scrollable-1a' ] ) ;
81
+ } ) ;
58
82
} ) ;
59
83
} ) ;
60
84
@@ -68,7 +92,25 @@ class ScrollingComponent {
68
92
@ViewChild ( 'scrollingElement' ) scrollingElement : ElementRef ;
69
93
}
70
94
71
- const TEST_COMPONENTS = [ ScrollingComponent ] ;
95
+
96
+ /** Component containing nested scrollables. */
97
+ @Component ( {
98
+ template : `
99
+ <div id="scrollable-1" cdk-scrollable>
100
+ <div id="scrollable-1a" cdk-scrollable>
101
+ <div #interestingElement></div>
102
+ </div>
103
+ <div id="scrollable-1b" cdk-scrollable></div>
104
+ </div>
105
+ <div id="scrollable-2" cdk-scrollable></div>
106
+ `
107
+ } )
108
+ class NestedScrollingComponent {
109
+ @ViewChild ( 'interestingElement' ) interestingElement : ElementRef ;
110
+ @ViewChildren ( Scrollable ) scrollables : QueryList < Scrollable > ;
111
+ }
112
+
113
+ const TEST_COMPONENTS = [ ScrollingComponent , NestedScrollingComponent ] ;
72
114
@NgModule ( {
73
115
imports : [ OverlayModule ] ,
74
116
providers : [ ScrollDispatcher ] ,
0 commit comments