1
1
import { ArrayDataSource } from '@angular/cdk/collections' ;
2
- import { CdkVirtualForOf , CdkVirtualScrollViewport , ScrollingModule } from '@angular/cdk/scrolling' ;
2
+ import {
3
+ CdkVirtualForOf ,
4
+ CdkVirtualScrollViewport ,
5
+ ScrollDispatcher ,
6
+ ScrollingModule
7
+ } from '@angular/cdk/scrolling' ;
3
8
import { dispatchFakeEvent } from '@angular/cdk/testing' ;
4
9
import {
5
10
Component ,
@@ -9,7 +14,7 @@ import {
9
14
ViewContainerRef ,
10
15
ViewEncapsulation
11
16
} from '@angular/core' ;
12
- import { ComponentFixture , fakeAsync , flush , TestBed } from '@angular/core/testing' ;
17
+ import { ComponentFixture , fakeAsync , flush , inject , TestBed } from '@angular/core/testing' ;
13
18
import { animationFrameScheduler , Subject } from 'rxjs' ;
14
19
15
20
@@ -157,7 +162,7 @@ describe('CdkVirtualScrollViewport', () => {
157
162
fixture . detectChanges ( ) ;
158
163
flush ( ) ;
159
164
160
- expect ( viewport . elementRef . nativeElement . scrollTop ) . toBe ( testComponent . itemSize * 2 ) ;
165
+ expect ( viewport . measureScrollOffset ( ) ) . toBe ( testComponent . itemSize * 2 ) ;
161
166
expect ( viewport . getRenderedRange ( ) ) . toEqual ( { start : 2 , end : 6 } ) ;
162
167
} ) ) ;
163
168
@@ -169,7 +174,7 @@ describe('CdkVirtualScrollViewport', () => {
169
174
fixture . detectChanges ( ) ;
170
175
flush ( ) ;
171
176
172
- expect ( viewport . elementRef . nativeElement . scrollTop ) . toBe ( testComponent . itemSize * 2 ) ;
177
+ expect ( viewport . measureScrollOffset ( ) ) . toBe ( testComponent . itemSize * 2 ) ;
173
178
expect ( viewport . getRenderedRange ( ) ) . toEqual ( { start : 2 , end : 6 } ) ;
174
179
} ) ) ;
175
180
@@ -182,7 +187,7 @@ describe('CdkVirtualScrollViewport', () => {
182
187
fixture . detectChanges ( ) ;
183
188
flush ( ) ;
184
189
185
- expect ( viewport . elementRef . nativeElement . scrollLeft ) . toBe ( testComponent . itemSize * 2 ) ;
190
+ expect ( viewport . measureScrollOffset ( ) ) . toBe ( testComponent . itemSize * 2 ) ;
186
191
expect ( viewport . getRenderedRange ( ) ) . toEqual ( { start : 2 , end : 6 } ) ;
187
192
} ) ) ;
188
193
@@ -195,7 +200,7 @@ describe('CdkVirtualScrollViewport', () => {
195
200
fixture . detectChanges ( ) ;
196
201
flush ( ) ;
197
202
198
- expect ( viewport . elementRef . nativeElement . scrollLeft ) . toBe ( testComponent . itemSize * 2 ) ;
203
+ expect ( viewport . measureScrollOffset ( ) ) . toBe ( testComponent . itemSize * 2 ) ;
199
204
expect ( viewport . getRenderedRange ( ) ) . toEqual ( { start : 2 , end : 6 } ) ;
200
205
} ) ) ;
201
206
@@ -577,6 +582,16 @@ describe('CdkVirtualScrollViewport', () => {
577
582
testComponent . maxBufferPx = 99 ;
578
583
expect ( ( ) => finishInit ( fixture ) ) . toThrow ( ) ;
579
584
} ) ) ;
585
+
586
+ it ( 'should register and degregister with ScrollDispatcher' ,
587
+ fakeAsync ( inject ( [ ScrollDispatcher ] , ( dispatcher : ScrollDispatcher ) => {
588
+ spyOn ( dispatcher , 'register' ) . and . callThrough ( ) ;
589
+ spyOn ( dispatcher , 'deregister' ) . and . callThrough ( ) ;
590
+ finishInit ( fixture ) ;
591
+ expect ( dispatcher . register ) . toHaveBeenCalledWith ( testComponent . viewport ) ;
592
+ fixture . destroy ( ) ;
593
+ expect ( dispatcher . deregister ) . toHaveBeenCalledWith ( testComponent . viewport ) ;
594
+ } ) ) ) ;
580
595
} ) ;
581
596
582
597
describe ( 'with RTL direction' , ( ) => {
@@ -605,8 +620,7 @@ describe('CdkVirtualScrollViewport', () => {
605
620
testComponent . orientation = 'horizontal' ;
606
621
finishInit ( fixture ) ;
607
622
608
- expect ( viewportEl . scrollLeft ) . toBe (
609
- testComponent . itemSize * testComponent . items . length - testComponent . viewportSize ) ;
623
+ expect ( viewport . measureScrollOffset ( ) ) . toBe ( 0 ) ;
610
624
expect ( contentWrapperEl . style . transform ) . toMatch ( / t r a n s l a t e X \( 0 ( p x ) ? \) / ) ;
611
625
expect ( ( contentWrapperEl . children [ 0 ] as HTMLElement ) . innerText ) . toBe ( '0 - 0' ) ;
612
626
} ) ) ;
@@ -616,7 +630,7 @@ describe('CdkVirtualScrollViewport', () => {
616
630
testComponent . orientation = 'horizontal' ;
617
631
finishInit ( fixture ) ;
618
632
619
- triggerScroll ( viewport , 0 ) ;
633
+ triggerScroll ( viewport , 300 ) ;
620
634
fixture . detectChanges ( ) ;
621
635
flush ( ) ;
622
636
@@ -634,8 +648,7 @@ describe('CdkVirtualScrollViewport', () => {
634
648
fixture . detectChanges ( ) ;
635
649
flush ( ) ;
636
650
637
- expect ( viewportEl . scrollLeft ) . toBe ( testComponent . itemSize * testComponent . items . length -
638
- testComponent . viewportSize - 100 ) ;
651
+ expect ( viewport . measureScrollOffset ( 'right' ) ) . toBe ( 100 ) ;
639
652
} ) ) ;
640
653
641
654
it ( 'should scroll to the correct index in horizontal mode' , fakeAsync ( ( ) => {
@@ -686,11 +699,7 @@ function finishInit(fixture: ComponentFixture<any>) {
686
699
/** Trigger a scroll event on the viewport (optionally setting a new scroll offset). */
687
700
function triggerScroll ( viewport : CdkVirtualScrollViewport , offset ?: number ) {
688
701
if ( offset !== undefined ) {
689
- if ( viewport . orientation == 'horizontal' ) {
690
- viewport . elementRef . nativeElement . scrollLeft = offset ;
691
- } else {
692
- viewport . elementRef . nativeElement . scrollTop = offset ;
693
- }
702
+ viewport . scrollToOffset ( offset ) ;
694
703
}
695
704
dispatchFakeEvent ( viewport . elementRef . nativeElement , 'scroll' ) ;
696
705
animationFrameScheduler . flush ( ) ;
@@ -751,9 +760,9 @@ class FixedSizeVirtualScroll {
751
760
@Component ( {
752
761
template : `
753
762
<cdk-virtual-scroll-viewport dir="rtl"
754
- [itemSize]="itemSize" [bufferSize ]="bufferSize " [orientation ]="orientation "
755
- [style.height.px ]="viewportHeight " [style.width .px]="viewportWidth "
756
- (scrolledIndexChange)="scrolledToIndex = $event">
763
+ [itemSize]="itemSize" [minBufferPx ]="minBufferPx " [maxBufferPx ]="maxBufferPx "
764
+ [orientation ]="orientation " [style.height .px]="viewportHeight "
765
+ [style.width.px]="viewportWidth" (scrolledIndexChange)="scrolledToIndex = $event">
757
766
<div class="item"
758
767
*cdkVirtualFor="let item of items; let i = index; trackBy: trackBy; \
759
768
templateCacheSize: templateCacheSize"
@@ -782,7 +791,8 @@ class FixedSizeVirtualScrollWithRtlDirection {
782
791
@Input ( ) viewportSize = 200 ;
783
792
@Input ( ) viewportCrossSize = 100 ;
784
793
@Input ( ) itemSize = 50 ;
785
- @Input ( ) bufferSize = 0 ;
794
+ @Input ( ) minBufferPx = 0 ;
795
+ @Input ( ) maxBufferPx = 0 ;
786
796
@Input ( ) items = Array ( 10 ) . fill ( 0 ) . map ( ( _ , i ) => i ) ;
787
797
@Input ( ) trackBy : TrackByFunction < number > ;
788
798
@Input ( ) templateCacheSize = 20 ;
0 commit comments