@@ -772,9 +772,9 @@ describe('CdkVirtualScrollViewport', () => {
772
772
spyOn ( dispatcher , 'register' ) . and . callThrough ( ) ;
773
773
spyOn ( dispatcher , 'deregister' ) . and . callThrough ( ) ;
774
774
finishInit ( fixture ) ;
775
- expect ( dispatcher . register ) . toHaveBeenCalledWith ( testComponent . viewport ) ;
775
+ expect ( dispatcher . register ) . toHaveBeenCalledWith ( testComponent . viewport . scrollable ) ;
776
776
fixture . destroy ( ) ;
777
- expect ( dispatcher . deregister ) . toHaveBeenCalledWith ( testComponent . viewport ) ;
777
+ expect ( dispatcher . deregister ) . toHaveBeenCalledWith ( testComponent . viewport . scrollable ) ;
778
778
} ) ,
779
779
) ) ;
780
780
@@ -1086,6 +1086,76 @@ describe('CdkVirtualScrollViewport', () => {
1086
1086
expect ( viewport . getOffsetToRenderedContentStart ( ) ) . toBe ( 0 ) ;
1087
1087
} ) ) ;
1088
1088
} ) ;
1089
+
1090
+ describe ( 'with custom scrolling element' , ( ) => {
1091
+ let fixture : ComponentFixture < VirtualScrollWithCustomScrollingElement > ;
1092
+ let testComponent : VirtualScrollWithCustomScrollingElement ;
1093
+ let viewport : CdkVirtualScrollViewport ;
1094
+
1095
+ beforeEach ( waitForAsync ( ( ) => {
1096
+ TestBed . configureTestingModule ( {
1097
+ imports : [ ScrollingModule ] ,
1098
+ declarations : [ VirtualScrollWithCustomScrollingElement ] ,
1099
+ } ) . compileComponents ( ) ;
1100
+ } ) ) ;
1101
+
1102
+ beforeEach ( ( ) => {
1103
+ fixture = TestBed . createComponent ( VirtualScrollWithCustomScrollingElement ) ;
1104
+ testComponent = fixture . componentInstance ;
1105
+ viewport = testComponent . viewport ;
1106
+ } ) ;
1107
+
1108
+ it ( 'should measure viewport offset' , fakeAsync ( ( ) => {
1109
+ finishInit ( fixture ) ;
1110
+
1111
+ expect ( viewport . measureViewportOffset ( 'top' ) )
1112
+ . withContext ( 'with scrolling-element padding-top: 50 offset should be 50' )
1113
+ . toBe ( 50 ) ;
1114
+ } ) ) ;
1115
+
1116
+ it ( 'should measure scroll offset' , fakeAsync ( ( ) => {
1117
+ finishInit ( fixture ) ;
1118
+ triggerScroll ( viewport , 100 ) ;
1119
+ fixture . detectChanges ( ) ;
1120
+ flush ( ) ;
1121
+
1122
+ expect ( viewport . measureScrollOffset ( 'top' ) )
1123
+ . withContext ( 'should be 50 (actual scroll offset - viewport offset)' )
1124
+ . toBe ( 50 ) ;
1125
+ } ) ) ;
1126
+ } ) ;
1127
+
1128
+ describe ( 'with scrollable window' , ( ) => {
1129
+ let fixture : ComponentFixture < VirtualScrollWithScrollableWindow > ;
1130
+ let testComponent : VirtualScrollWithScrollableWindow ;
1131
+ let viewport : CdkVirtualScrollViewport ;
1132
+
1133
+ beforeEach ( waitForAsync ( ( ) => {
1134
+ TestBed . configureTestingModule ( {
1135
+ imports : [ ScrollingModule ] ,
1136
+ declarations : [ VirtualScrollWithScrollableWindow ] ,
1137
+ } ) . compileComponents ( ) ;
1138
+ } ) ) ;
1139
+
1140
+ beforeEach ( ( ) => {
1141
+ fixture = TestBed . createComponent ( VirtualScrollWithScrollableWindow ) ;
1142
+ testComponent = fixture . componentInstance ;
1143
+ viewport = testComponent . viewport ;
1144
+ } ) ;
1145
+
1146
+ it ( 'should measure scroll offset' , fakeAsync ( ( ) => {
1147
+ finishInit ( fixture ) ;
1148
+ viewport . scrollToOffset ( 100 + 8 ) ; // the +8 is due to a horizontal scrollbar
1149
+ dispatchFakeEvent ( window , 'scroll' , true ) ;
1150
+ tick ( ) ;
1151
+ fixture . detectChanges ( ) ;
1152
+ flush ( ) ;
1153
+
1154
+ expect ( viewport . measureScrollOffset ( 'top' ) )
1155
+ . withContext ( 'should be 50 (actual scroll offset - viewport offset)' )
1156
+ . toBe ( 50 ) ;
1157
+ } ) ) ;
1158
+ } ) ;
1089
1159
} ) ;
1090
1160
1091
1161
/** Finish initializing the virtual scroll component at the beginning of a test. */
@@ -1109,7 +1179,7 @@ function triggerScroll(viewport: CdkVirtualScrollViewport, offset?: number) {
1109
1179
if ( offset !== undefined ) {
1110
1180
viewport . scrollToOffset ( offset ) ;
1111
1181
}
1112
- dispatchFakeEvent ( viewport . elementRef . nativeElement , 'scroll' ) ;
1182
+ dispatchFakeEvent ( viewport . scrollable . getElementRef ( ) . nativeElement , 'scroll' ) ;
1113
1183
animationFrameScheduler . flush ( ) ;
1114
1184
}
1115
1185
@@ -1391,3 +1461,88 @@ class VirtualScrollWithAppendOnly {
1391
1461
. fill ( 0 )
1392
1462
. map ( ( _ , i ) => i ) ;
1393
1463
}
1464
+
1465
+ @Component ( {
1466
+ template : `
1467
+ <div cdkVirtualScrollingElement class="scrolling-element">
1468
+ <cdk-virtual-scroll-viewport itemSize="50">
1469
+ <div class="item" *cdkVirtualFor="let item of items">{{item}}</div>
1470
+ </cdk-virtual-scroll-viewport>
1471
+ </div>
1472
+ ` ,
1473
+ styles : [
1474
+ `
1475
+ .cdk-virtual-scroll-content-wrapper {
1476
+ display: flex;
1477
+ flex-direction: column;
1478
+ }
1479
+
1480
+ .cdk-virtual-scroll-viewport {
1481
+ width: 200px;
1482
+ height: 200px;
1483
+ background-color: #f5f5f5;
1484
+ }
1485
+
1486
+ .item {
1487
+ width: 100%;
1488
+ height: 50px;
1489
+ box-sizing: border-box;
1490
+ border: 1px dashed #ccc;
1491
+ }
1492
+
1493
+ .scrolling-element {
1494
+ padding-top: 50px;
1495
+ }
1496
+ ` ,
1497
+ ] ,
1498
+ encapsulation : ViewEncapsulation . None ,
1499
+ } )
1500
+ class VirtualScrollWithCustomScrollingElement {
1501
+ @ViewChild ( CdkVirtualScrollViewport , { static : true } ) viewport : CdkVirtualScrollViewport ;
1502
+ itemSize = 50 ;
1503
+ items = Array ( 20000 )
1504
+ . fill ( 0 )
1505
+ . map ( ( _ , i ) => i ) ;
1506
+ }
1507
+
1508
+ @Component ( {
1509
+ template : `
1510
+ <div class="before-virtual-viewport"></div>
1511
+ <cdk-virtual-scroll-viewport scrollWindow itemSize="50">
1512
+ <div class="item" *cdkVirtualFor="let item of items">{{item}}</div>
1513
+ </cdk-virtual-scroll-viewport>
1514
+ ` ,
1515
+ styles : [
1516
+ `
1517
+ .cdk-virtual-scroll-content-wrapper {
1518
+ display: flex;
1519
+ flex-direction: column;
1520
+ }
1521
+
1522
+ .cdk-virtual-scroll-viewport {
1523
+ width: 200px;
1524
+ height: 200px;
1525
+ background-color: #f5f5f5;
1526
+ }
1527
+
1528
+ .item {
1529
+ width: 100%;
1530
+ height: 50px;
1531
+ box-sizing: border-box;
1532
+ border: 1px dashed #ccc;
1533
+ }
1534
+
1535
+ .before-virtual-viewport {
1536
+ height: 50px;
1537
+ }
1538
+ ` ,
1539
+ ] ,
1540
+ encapsulation : ViewEncapsulation . None ,
1541
+ } )
1542
+ class VirtualScrollWithScrollableWindow {
1543
+ @ViewChild ( CdkVirtualScrollViewport , { static : true } ) viewport : CdkVirtualScrollViewport ;
1544
+ itemSize = 50 ;
1545
+ items = Array ( 20000 )
1546
+ . fill ( 0 )
1547
+ . map ( ( _ , i ) => i ) ;
1548
+ }
0 commit comments