@@ -5,10 +5,10 @@ import {
5
5
ScrollDispatcher ,
6
6
ScrollingModule
7
7
} from '@angular/cdk/scrolling' ;
8
+ import { CommonModule } from '@angular/common' ;
8
9
import { dispatchFakeEvent } from '@angular/cdk/testing/private' ;
9
10
import {
10
11
Component ,
11
- Input ,
12
12
NgZone ,
13
13
TrackByFunction ,
14
14
ViewChild ,
@@ -29,7 +29,7 @@ import {animationFrameScheduler, Subject} from 'rxjs';
29
29
30
30
31
31
describe ( 'CdkVirtualScrollViewport' , ( ) => {
32
- describe ( 'with FixedSizeVirtualScrollStrategy' , ( ) => {
32
+ describe ( 'with FixedSizeVirtualScrollStrategy' , ( ) => {
33
33
let fixture : ComponentFixture < FixedSizeVirtualScroll > ;
34
34
let testComponent : FixedSizeVirtualScroll ;
35
35
let viewport : CdkVirtualScrollViewport ;
@@ -899,6 +899,35 @@ describe('CdkVirtualScrollViewport', () => {
899
899
. toEqual ( [ '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' ] ) ;
900
900
} ) ) ;
901
901
} ) ;
902
+
903
+ describe ( 'with delayed initialization' , ( ) => {
904
+ let fixture : ComponentFixture < DelayedInitializationVirtualScroll > ;
905
+ let testComponent : DelayedInitializationVirtualScroll ;
906
+ let viewport : CdkVirtualScrollViewport ;
907
+
908
+ beforeEach ( waitForAsync ( ( ) => {
909
+ TestBed . configureTestingModule ( {
910
+ imports : [ ScrollingModule , CommonModule ] ,
911
+ declarations : [ DelayedInitializationVirtualScroll ] ,
912
+ } ) . compileComponents ( ) ;
913
+ fixture = TestBed . createComponent ( DelayedInitializationVirtualScroll ) ;
914
+ testComponent = fixture . componentInstance ;
915
+ viewport = testComponent . viewport ;
916
+ } ) ) ;
917
+
918
+ it ( 'should call custom trackBy when virtual for is added after init' , fakeAsync ( ( ) => {
919
+ finishInit ( fixture ) ;
920
+ expect ( testComponent . trackBy ) . not . toHaveBeenCalled ( ) ;
921
+
922
+ testComponent . renderVirtualFor = true ;
923
+ fixture . detectChanges ( ) ;
924
+ triggerScroll ( viewport , testComponent . itemSize * 5 ) ;
925
+ fixture . detectChanges ( ) ;
926
+ flush ( ) ;
927
+
928
+ expect ( testComponent . trackBy ) . toHaveBeenCalled ( ) ;
929
+ } ) ) ;
930
+ } ) ;
902
931
} ) ;
903
932
904
933
@@ -973,15 +1002,15 @@ class FixedSizeVirtualScroll {
973
1002
// Casting virtualForOf as any so we can spy on private methods
974
1003
@ViewChild ( CdkVirtualForOf , { static : true } ) virtualForOf : any ;
975
1004
976
- @ Input ( ) orientation = 'vertical' ;
977
- @ Input ( ) viewportSize = 200 ;
978
- @ Input ( ) viewportCrossSize = 100 ;
979
- @ Input ( ) itemSize = 50 ;
980
- @ Input ( ) minBufferPx = 0 ;
981
- @ Input ( ) maxBufferPx = 0 ;
982
- @ Input ( ) items = Array ( 10 ) . fill ( 0 ) . map ( ( _ , i ) => i ) ;
983
- @ Input ( ) trackBy : TrackByFunction < number > ;
984
- @ Input ( ) templateCacheSize = 20 ;
1005
+ orientation = 'vertical' ;
1006
+ viewportSize = 200 ;
1007
+ viewportCrossSize = 100 ;
1008
+ itemSize = 50 ;
1009
+ minBufferPx = 0 ;
1010
+ maxBufferPx = 0 ;
1011
+ items = Array ( 10 ) . fill ( 0 ) . map ( ( _ , i ) => i ) ;
1012
+ trackBy : TrackByFunction < number > ;
1013
+ templateCacheSize = 20 ;
985
1014
986
1015
scrolledToIndex = 0 ;
987
1016
hasMargin = false ;
@@ -1033,15 +1062,15 @@ class FixedSizeVirtualScroll {
1033
1062
class FixedSizeVirtualScrollWithRtlDirection {
1034
1063
@ViewChild ( CdkVirtualScrollViewport , { static : true } ) viewport : CdkVirtualScrollViewport ;
1035
1064
1036
- @ Input ( ) orientation = 'vertical' ;
1037
- @ Input ( ) viewportSize = 200 ;
1038
- @ Input ( ) viewportCrossSize = 100 ;
1039
- @ Input ( ) itemSize = 50 ;
1040
- @ Input ( ) minBufferPx = 0 ;
1041
- @ Input ( ) maxBufferPx = 0 ;
1042
- @ Input ( ) items = Array ( 10 ) . fill ( 0 ) . map ( ( _ , i ) => i ) ;
1043
- @ Input ( ) trackBy : TrackByFunction < number > ;
1044
- @ Input ( ) templateCacheSize = 20 ;
1065
+ orientation = 'vertical' ;
1066
+ viewportSize = 200 ;
1067
+ viewportCrossSize = 100 ;
1068
+ itemSize = 50 ;
1069
+ minBufferPx = 0 ;
1070
+ maxBufferPx = 0 ;
1071
+ items = Array ( 10 ) . fill ( 0 ) . map ( ( _ , i ) => i ) ;
1072
+ trackBy : TrackByFunction < number > ;
1073
+ templateCacheSize = 20 ;
1045
1074
1046
1075
scrolledToIndex = 0 ;
1047
1076
@@ -1116,3 +1145,40 @@ class VirtualScrollWithItemInjectingViewContainer {
1116
1145
items = Array ( 20000 ) . fill ( 0 ) . map ( ( _ , i ) => i ) ;
1117
1146
}
1118
1147
1148
+
1149
+ @Component ( {
1150
+ template : `
1151
+ <cdk-virtual-scroll-viewport [itemSize]="itemSize">
1152
+ <ng-container *ngIf="renderVirtualFor">
1153
+ <div class="item" *cdkVirtualFor="let item of items; trackBy: trackBy">{{item}}</div>
1154
+ </ng-container>
1155
+ </cdk-virtual-scroll-viewport>
1156
+ ` ,
1157
+ styles : [ `
1158
+ .cdk-virtual-scroll-content-wrapper {
1159
+ display: flex;
1160
+ flex-direction: column;
1161
+ }
1162
+
1163
+ .cdk-virtual-scroll-viewport {
1164
+ width: 200px;
1165
+ height: 200px;
1166
+ background-color: #f5f5f5;
1167
+ }
1168
+
1169
+ .item {
1170
+ width: 100%;
1171
+ height: 50px;
1172
+ box-sizing: border-box;
1173
+ border: 1px dashed #ccc;
1174
+ }
1175
+ ` ] ,
1176
+ encapsulation : ViewEncapsulation . None ,
1177
+ } )
1178
+ class DelayedInitializationVirtualScroll {
1179
+ @ViewChild ( CdkVirtualScrollViewport , { static : true } ) viewport : CdkVirtualScrollViewport ;
1180
+ itemSize = 50 ;
1181
+ items = Array ( 20000 ) . fill ( 0 ) . map ( ( _ , i ) => i ) ;
1182
+ trackBy = jasmine . createSpy ( 'trackBy' ) . and . callFake ( ( item : unknown ) => item ) ;
1183
+ renderVirtualFor = false ;
1184
+ }
0 commit comments