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