@@ -10,17 +10,17 @@ import {CdkVirtualScrollViewport} from './virtual-scroll-viewport';
10
10
11
11
describe ( 'CdkVirtualScrollViewport' , ( ) => {
12
12
describe ( 'with FixedSizeVirtualScrollStrategy' , ( ) => {
13
- let fixture : ComponentFixture < FixedVirtualScroll > ;
14
- let testComponent : FixedVirtualScroll ;
13
+ let fixture : ComponentFixture < FixedSizeVirtualScroll > ;
14
+ let testComponent : FixedSizeVirtualScroll ;
15
15
let viewport : CdkVirtualScrollViewport ;
16
16
17
17
beforeEach ( ( ) => {
18
18
TestBed . configureTestingModule ( {
19
19
imports : [ ScrollingModule ] ,
20
- declarations : [ FixedVirtualScroll ] ,
20
+ declarations : [ FixedSizeVirtualScroll ] ,
21
21
} ) . compileComponents ( ) ;
22
22
23
- fixture = TestBed . createComponent ( FixedVirtualScroll ) ;
23
+ fixture = TestBed . createComponent ( FixedSizeVirtualScroll ) ;
24
24
testComponent = fixture . componentInstance ;
25
25
viewport = testComponent . viewport ;
26
26
} ) ;
@@ -454,6 +454,45 @@ describe('CdkVirtualScrollViewport', () => {
454
454
expect ( testComponent . virtualForViewContainer . createEmbeddedView ) . toHaveBeenCalledTimes ( 5 ) ;
455
455
} ) ) ;
456
456
} ) ;
457
+
458
+ describe ( 'with AutoSizeVirtualScrollStrategy' , ( ) => {
459
+ let fixture : ComponentFixture < AutoSizeVirtualScroll > ;
460
+ let testComponent : AutoSizeVirtualScroll ;
461
+ let viewport : CdkVirtualScrollViewport ;
462
+
463
+ beforeEach ( ( ) => {
464
+ TestBed . configureTestingModule ( {
465
+ imports : [ ScrollingModule ] ,
466
+ declarations : [ AutoSizeVirtualScroll ] ,
467
+ } ) . compileComponents ( ) ;
468
+
469
+ fixture = TestBed . createComponent ( AutoSizeVirtualScroll ) ;
470
+ testComponent = fixture . componentInstance ;
471
+ viewport = testComponent . viewport ;
472
+ } ) ;
473
+
474
+ it ( 'should render initial state for uniform items' , fakeAsync ( ( ) => {
475
+ finishInit ( fixture ) ;
476
+
477
+ const contentWrapper =
478
+ viewport . elementRef . nativeElement . querySelector ( '.cdk-virtual-scroll-content-wrapper' ) ;
479
+ expect ( contentWrapper . children . length )
480
+ . toBe ( 4 , 'should render 4 50px items to fill 200px space' ) ;
481
+ } ) ) ;
482
+
483
+ it ( 'should render extra content if first item is smaller than average' , fakeAsync ( ( ) => {
484
+ testComponent . items = [ 50 , 200 , 200 , 200 , 200 , 200 ] ;
485
+ finishInit ( fixture ) ;
486
+
487
+ const contentWrapper =
488
+ viewport . elementRef . nativeElement . querySelector ( '.cdk-virtual-scroll-content-wrapper' ) ;
489
+ expect ( contentWrapper . children . length ) . toBe ( 4 ,
490
+ 'should render 4 items to fill 200px space based on 50px estimate from first item' ) ;
491
+ } ) ) ;
492
+
493
+ // TODO(mmalerba): Add test that it corrects the initial render if it didn't render enough,
494
+ // once it actually does that.
495
+ } ) ;
457
496
} ) ;
458
497
459
498
@@ -506,7 +545,7 @@ function triggerScroll(viewport: CdkVirtualScrollViewport, offset?: number) {
506
545
` ] ,
507
546
encapsulation : ViewEncapsulation . None ,
508
547
} )
509
- class FixedVirtualScroll {
548
+ class FixedSizeVirtualScroll {
510
549
@ViewChild ( CdkVirtualScrollViewport ) viewport : CdkVirtualScrollViewport ;
511
550
@ViewChild ( CdkVirtualForOf , { read : ViewContainerRef } ) virtualForViewContainer : ViewContainerRef ;
512
551
@@ -527,3 +566,46 @@ class FixedVirtualScroll {
527
566
return this . orientation == 'horizontal' ? this . viewportCrossSize : this . viewportSize ;
528
567
}
529
568
}
569
+
570
+ @Component ( {
571
+ template : `
572
+ <cdk-virtual-scroll-viewport
573
+ autosize [minBufferPx]="minBufferSize" [addBufferPx]="addBufferSize"
574
+ [orientation]="orientation" [style.height.px]="viewportHeight"
575
+ [style.width.px]="viewportWidth">
576
+ <div class="item" *cdkVirtualFor="let size of items; let i = index" [style.height.px]="size"
577
+ [style.width.px]="size">
578
+ {{i}} - {{size}}
579
+ </div>
580
+ </cdk-virtual-scroll-viewport>
581
+ ` ,
582
+ styles : [ `
583
+ .cdk-virtual-scroll-content-wrapper {
584
+ display: flex;
585
+ flex-direction: column;
586
+ }
587
+
588
+ .cdk-virtual-scroll-orientation-horizontal .cdk-virtual-scroll-content-wrapper {
589
+ flex-direction: row;
590
+ }
591
+ ` ] ,
592
+ encapsulation : ViewEncapsulation . None ,
593
+ } )
594
+ class AutoSizeVirtualScroll {
595
+ @ViewChild ( CdkVirtualScrollViewport ) viewport : CdkVirtualScrollViewport ;
596
+
597
+ @Input ( ) orientation = 'vertical' ;
598
+ @Input ( ) viewportSize = 200 ;
599
+ @Input ( ) viewportCrossSize = 100 ;
600
+ @Input ( ) minBufferSize = 0 ;
601
+ @Input ( ) addBufferSize = 0 ;
602
+ @Input ( ) items = Array ( 10 ) . fill ( 50 ) ;
603
+
604
+ get viewportWidth ( ) {
605
+ return this . orientation == 'horizontal' ? this . viewportSize : this . viewportCrossSize ;
606
+ }
607
+
608
+ get viewportHeight ( ) {
609
+ return this . orientation == 'horizontal' ? this . viewportCrossSize : this . viewportSize ;
610
+ }
611
+ }
0 commit comments