@@ -28,6 +28,8 @@ import { IgxForOfSyncService, IgxForOfScrollSyncService } from './for_of.sync.se
2828
2929describe ( 'IgxForOf directive -' , ( ) => {
3030 const INACTIVE_VIRT_CONTAINER = 'igx-display-container--inactive' ;
31+ const DISPLAY_CONTAINER = 'igx-display-container' ;
32+ const VERTICAL_SCROLLER = 'igx-virtual-helper' ;
3133 let displayContainer : HTMLElement ;
3234 let verticalScroller : HTMLElement ;
3335 let horizontalScroller : HTMLElement ;
@@ -202,34 +204,6 @@ describe('IgxForOf directive -', () => {
202204 horizontalScroller = fix . nativeElement . querySelector ( 'igx-horizontal-virtual-helper' ) ;
203205 } ) ;
204206
205- it ( 'should reset scroll position if component is destroyed and recreated.' , async ( ) => {
206- let scrollComponent = fix . debugElement . query ( By . css ( VERTICAL_SCROLLER ) ) . componentInstance ;
207- expect ( scrollComponent . scrollAmount ) . toBe ( 0 ) ;
208- expect ( scrollComponent . destroyed ) . toBeFalsy ( ) ;
209-
210- fix . componentInstance . scrollTop ( 500 ) ;
211- fix . detectChanges ( ) ;
212- await wait ( ) ;
213- expect ( scrollComponent . scrollAmount ) . toBe ( 500 ) ;
214-
215- fix . componentInstance . exists = false ;
216- fix . detectChanges ( ) ;
217- await wait ( ) ;
218-
219- expect ( scrollComponent . destroyed ) . toBeTruthy ( ) ;
220-
221- fix . componentInstance . exists = true ;
222- fix . detectChanges ( ) ;
223- await wait ( ) ;
224-
225- scrollComponent = fix . debugElement . query ( By . css ( VERTICAL_SCROLLER ) ) . componentInstance ;
226- expect ( scrollComponent . scrollAmount ) . toBe ( 0 ) ;
227- expect ( scrollComponent . destroyed ) . toBeFalsy ( ) ;
228-
229- displayContainer = fix . nativeElement . querySelector ( DISPLAY_CONTAINER ) ;
230- const firstInnerDisplayContainer = displayContainer . children [ 0 ] ;
231- expect ( firstInnerDisplayContainer . children [ 0 ] . textContent ) . toBe ( '0' ) ;
232- } ) ;
233207
234208 it ( 'should initialize directive with vertical virtualization' , async ( ) => {
235209 expect ( displayContainer ) . not . toBeNull ( ) ;
@@ -1169,6 +1143,55 @@ describe('IgxForOf directive -', () => {
11691143
11701144 } ) ;
11711145
1146+ describe ( 'on destroy' , ( ) => {
1147+ let fix : ComponentFixture < VerticalVirtualDestroyComponent > ;
1148+
1149+ configureTestSuite ( ) ;
1150+ beforeAll ( async ( ( ) => {
1151+ TestBed . configureTestingModule ( {
1152+ declarations : [
1153+ TestIgxForOfDirective ,
1154+ VerticalVirtualDestroyComponent
1155+ ] ,
1156+ imports : [ IgxForOfModule ]
1157+ } ) . compileComponents ( ) ;
1158+ } ) ) ;
1159+
1160+ beforeEach ( ( ) => {
1161+ fix = TestBed . createComponent ( VerticalVirtualDestroyComponent ) ;
1162+ fix . componentInstance . data = dg . generateVerticalData ( fix . componentInstance . cols ) ;
1163+ fix . componentRef . hostView . detectChanges ( ) ;
1164+ fix . detectChanges ( ) ;
1165+ } ) ;
1166+ it ( 'should reset scroll position if component is destroyed and recreated.' , async ( ) => {
1167+ let scrollComponent = fix . debugElement . query ( By . css ( VERTICAL_SCROLLER ) ) . componentInstance ;
1168+ expect ( scrollComponent . scrollAmount ) . toBe ( 0 ) ;
1169+ expect ( scrollComponent . destroyed ) . toBeFalsy ( ) ;
1170+
1171+ scrollComponent . nativeElement . scrollTop = 500 ;
1172+ fix . detectChanges ( ) ;
1173+ await wait ( ) ;
1174+ expect ( scrollComponent . scrollAmount ) . toBe ( 500 ) ;
1175+
1176+ fix . componentInstance . exists = false ;
1177+ fix . detectChanges ( ) ;
1178+ await wait ( ) ;
1179+
1180+ expect ( scrollComponent . destroyed ) . toBeTruthy ( ) ;
1181+
1182+ fix . componentInstance . exists = true ;
1183+ fix . detectChanges ( ) ;
1184+ await wait ( ) ;
1185+
1186+ scrollComponent = fix . debugElement . query ( By . css ( VERTICAL_SCROLLER ) ) . componentInstance ;
1187+ expect ( scrollComponent . scrollAmount ) . toBe ( 0 ) ;
1188+ expect ( scrollComponent . destroyed ) . toBeFalsy ( ) ;
1189+
1190+ displayContainer = fix . nativeElement . querySelector ( DISPLAY_CONTAINER ) ;
1191+ const firstInnerDisplayContainer = displayContainer . children [ 0 ] ;
1192+ expect ( firstInnerDisplayContainer . children [ 0 ] . textContent ) . toBe ( '0' ) ;
1193+ } ) ;
1194+ } ) ;
11721195} ) ;
11731196
11741197class DataGenerator {
@@ -1442,10 +1465,10 @@ export class VirtualComponent {
14421465 public cols = [ ] ;
14431466 public data = [ ] ;
14441467
1445- @ViewChild ( 'container' , { read : ViewContainerRef , static : false } )
1468+ @ViewChild ( 'container' , { read : ViewContainerRef , static : true } )
14461469 public container : ViewContainerRef ;
14471470
1448- @ViewChild ( 'scrollContainer' , { read : TestIgxForOfDirective , static : false } )
1471+ @ViewChild ( 'scrollContainer' , { read : TestIgxForOfDirective , static : true } )
14491472 public parentVirtDir : TestIgxForOfDirective < any > ;
14501473
14511474 @ViewChildren ( 'childContainer' , { read : TestIgxForOfDirective } )
@@ -1484,10 +1507,42 @@ export class VirtualComponent {
14841507 */
14851508 return horizontalScrollbar . offsetHeight >= 16 ;
14861509 }
1510+ }
14871511
1488-
1512+ @Component ( {
1513+ template : `
1514+ <div *ngIf='exists' #container [style.width]='width' [style.height]='height'>
1515+ <ng-template #scrollContainer igxForTest let-rowData [igxForOf]="data"
1516+ [igxForScrollOrientation]="'vertical'"
1517+ [igxForContainerSize]='height'
1518+ [igxForItemSize]='itemSize'>
1519+ <div [style.display]="'flex'" [style.height]="rowData.height || itemSize || '50px'">
1520+ <div [style.min-width]=cols[0].width>{{rowData['1']}}</div>
1521+ <div [style.min-width]=cols[1].width>{{rowData['2']}}</div>
1522+ <div [style.min-width]=cols[2].width>{{rowData['3']}}</div>
1523+ <div [style.min-width]=cols[3].width>{{rowData['4']}}</div>
1524+ <div [style.min-width]=cols[4].width>{{rowData['5']}}</div>
1525+ </div>
1526+ </ng-template>
1527+ </div>
1528+ `
1529+ } )
1530+ export class VerticalVirtualDestroyComponent {
1531+ public width = '450px' ;
1532+ public height = '300px' ;
1533+ public cols = [
1534+ { field : '1' , width : '150px' } ,
1535+ { field : '2' , width : '70px' } ,
1536+ { field : '3' , width : '50px' } ,
1537+ { field : '4' , width : '80px' } ,
1538+ { field : '5' , width : '100px' }
1539+ ] ;
1540+ public data = [ ] ;
1541+ public itemSize = '50px' ;
1542+ public exists = true ;
14891543}
14901544
1545+
14911546/** Only vertically virtualized component */
14921547@Component ( {
14931548 template : `
0 commit comments