@@ -142,20 +142,6 @@ describe('CdkVirtualScrollViewport', () => {
142
142
expect ( viewport . getOffsetToRenderedContentStart ( ) ) . toBe ( 10 ) ;
143
143
} ) ) ;
144
144
145
- it ( 'should set scroll offset' , fakeAsync ( ( ) => {
146
- finishInit ( fixture ) ;
147
- viewport . setScrollOffset ( testComponent . itemSize * 2 ) ;
148
- fixture . detectChanges ( ) ;
149
- flush ( ) ;
150
-
151
- triggerScroll ( viewport ) ;
152
- fixture . detectChanges ( ) ;
153
- flush ( ) ;
154
-
155
- expect ( viewport . elementRef . nativeElement . scrollTop ) . toBe ( testComponent . itemSize * 2 ) ;
156
- expect ( viewport . getRenderedRange ( ) ) . toEqual ( { start : 2 , end : 6 } ) ;
157
- } ) ) ;
158
-
159
145
it ( 'should scroll to offset' , fakeAsync ( ( ) => {
160
146
finishInit ( fixture ) ;
161
147
viewport . scrollToOffset ( testComponent . itemSize * 2 ) ;
@@ -552,6 +538,92 @@ describe('CdkVirtualScrollViewport', () => {
552
538
expect ( testComponent . virtualForViewContainer . createEmbeddedView ) . toHaveBeenCalledTimes ( 5 ) ;
553
539
} ) ) ;
554
540
} ) ;
541
+
542
+ describe ( 'with RTL direction' , ( ) => {
543
+ let fixture : ComponentFixture < FixedSizeVirtualScrollWithRtlDirection > ;
544
+ let testComponent : FixedSizeVirtualScrollWithRtlDirection ;
545
+ let viewport : CdkVirtualScrollViewport ;
546
+ let viewportEl : HTMLElement ;
547
+ let contentWrapperEl : HTMLElement ;
548
+
549
+ beforeEach ( ( ) => {
550
+ TestBed . configureTestingModule ( {
551
+ imports : [ ScrollingModule ] ,
552
+ declarations : [ FixedSizeVirtualScrollWithRtlDirection ] ,
553
+ } ) . compileComponents ( ) ;
554
+
555
+ fixture = TestBed . createComponent ( FixedSizeVirtualScrollWithRtlDirection ) ;
556
+ testComponent = fixture . componentInstance ;
557
+ viewport = testComponent . viewport ;
558
+ viewportEl = viewport . elementRef . nativeElement ;
559
+ contentWrapperEl =
560
+ viewportEl . querySelector ( '.cdk-virtual-scroll-content-wrapper' ) as HTMLElement ;
561
+ } ) ;
562
+
563
+ it ( 'should initially be scrolled all the way right and showing the first item in horizontal' +
564
+ ' mode' , fakeAsync ( ( ) => {
565
+ testComponent . orientation = 'horizontal' ;
566
+ finishInit ( fixture ) ;
567
+
568
+ expect ( viewportEl . scrollLeft ) . toBe (
569
+ testComponent . itemSize * testComponent . items . length - testComponent . viewportSize ) ;
570
+ expect ( contentWrapperEl . style . transform ) . toMatch ( / t r a n s l a t e X \( 0 ( p x ) ? \) / ) ;
571
+ expect ( ( contentWrapperEl . children [ 0 ] as HTMLElement ) . innerText ) . toBe ( '0 - 0' ) ;
572
+ } ) ) ;
573
+
574
+ it ( 'should scroll through items as user scrolls to the left in horizontal mode' ,
575
+ fakeAsync ( ( ) => {
576
+ testComponent . orientation = 'horizontal' ;
577
+ finishInit ( fixture ) ;
578
+
579
+ triggerScroll ( viewport , 0 ) ;
580
+ fixture . detectChanges ( ) ;
581
+ flush ( ) ;
582
+
583
+ expect ( contentWrapperEl . style . transform ) . toBe ( 'translateX(-300px)' ) ;
584
+ expect ( ( contentWrapperEl . children [ 0 ] as HTMLElement ) . innerText ) . toBe ( '6 - 6' ) ;
585
+ } ) ) ;
586
+
587
+ it ( 'should interpret scrollToOffset amount as an offset from the right in horizontal mode' ,
588
+ fakeAsync ( ( ) => {
589
+ testComponent . orientation = 'horizontal' ;
590
+ finishInit ( fixture ) ;
591
+
592
+ viewport . scrollToOffset ( 100 ) ;
593
+ triggerScroll ( viewport ) ;
594
+ fixture . detectChanges ( ) ;
595
+ flush ( ) ;
596
+
597
+ expect ( viewportEl . scrollLeft ) . toBe ( testComponent . itemSize * testComponent . items . length -
598
+ testComponent . viewportSize - 100 ) ;
599
+ } ) ) ;
600
+
601
+ it ( 'should scroll to the correct index in horizontal mode' , fakeAsync ( ( ) => {
602
+ testComponent . orientation = 'horizontal' ;
603
+ finishInit ( fixture ) ;
604
+
605
+ viewport . scrollToIndex ( 2 ) ;
606
+ triggerScroll ( viewport ) ;
607
+ fixture . detectChanges ( ) ;
608
+ flush ( ) ;
609
+
610
+ expect ( ( contentWrapperEl . children [ 0 ] as HTMLElement ) . innerText ) . toBe ( '2 - 2' ) ;
611
+ } ) ) ;
612
+
613
+ it ( 'should emit the scrolled to index in horizontal mode' , fakeAsync ( ( ) => {
614
+ testComponent . orientation = 'horizontal' ;
615
+ finishInit ( fixture ) ;
616
+
617
+ expect ( testComponent . scrolledToIndex ) . toBe ( 0 ) ;
618
+
619
+ viewport . scrollToIndex ( 2 ) ;
620
+ triggerScroll ( viewport ) ;
621
+ fixture . detectChanges ( ) ;
622
+ flush ( ) ;
623
+
624
+ expect ( testComponent . scrolledToIndex ) . toBe ( 2 ) ;
625
+ } ) ) ;
626
+ } ) ;
555
627
} ) ;
556
628
557
629
@@ -564,6 +636,11 @@ function finishInit(fixture: ComponentFixture<any>) {
564
636
// On the second cycle we render the items.
565
637
fixture . detectChanges ( ) ;
566
638
flush ( ) ;
639
+
640
+ // Flush the initial fake scroll event.
641
+ animationFrameScheduler . flush ( ) ;
642
+ flush ( ) ;
643
+ fixture . detectChanges ( ) ;
567
644
}
568
645
569
646
/** Trigger a scroll event on the viewport (optionally setting a new scroll offset). */
@@ -629,3 +706,53 @@ class FixedSizeVirtualScroll {
629
706
return this . orientation == 'horizontal' ? this . viewportCrossSize : this . viewportSize ;
630
707
}
631
708
}
709
+
710
+ @Component ( {
711
+ template : `
712
+ <cdk-virtual-scroll-viewport dir="rtl"
713
+ [itemSize]="itemSize" [bufferSize]="bufferSize" [orientation]="orientation"
714
+ [style.height.px]="viewportHeight" [style.width.px]="viewportWidth"
715
+ (scrolledIndexChange)="scrolledToIndex = $event">
716
+ <div class="item"
717
+ *cdkVirtualFor="let item of items; let i = index; trackBy: trackBy; \
718
+ templateCacheSize: templateCacheSize"
719
+ [style.height.px]="itemSize" [style.width.px]="itemSize">
720
+ {{i}} - {{item}}
721
+ </div>
722
+ </cdk-virtual-scroll-viewport>
723
+ ` ,
724
+ styles : [ `
725
+ .cdk-virtual-scroll-content-wrapper {
726
+ display: flex;
727
+ flex-direction: column;
728
+ }
729
+
730
+ .cdk-virtual-scroll-orientation-horizontal .cdk-virtual-scroll-content-wrapper {
731
+ flex-direction: row;
732
+ }
733
+ ` ] ,
734
+ encapsulation : ViewEncapsulation . None ,
735
+ } )
736
+ class FixedSizeVirtualScrollWithRtlDirection {
737
+ @ViewChild ( CdkVirtualScrollViewport ) viewport : CdkVirtualScrollViewport ;
738
+ @ViewChild ( CdkVirtualForOf , { read : ViewContainerRef } ) virtualForViewContainer : ViewContainerRef ;
739
+
740
+ @Input ( ) orientation = 'vertical' ;
741
+ @Input ( ) viewportSize = 200 ;
742
+ @Input ( ) viewportCrossSize = 100 ;
743
+ @Input ( ) itemSize = 50 ;
744
+ @Input ( ) bufferSize = 0 ;
745
+ @Input ( ) items = Array ( 10 ) . fill ( 0 ) . map ( ( _ , i ) => i ) ;
746
+ @Input ( ) trackBy ;
747
+ @Input ( ) templateCacheSize = 20 ;
748
+
749
+ scrolledToIndex = 0 ;
750
+
751
+ get viewportWidth ( ) {
752
+ return this . orientation == 'horizontal' ? this . viewportSize : this . viewportCrossSize ;
753
+ }
754
+
755
+ get viewportHeight ( ) {
756
+ return this . orientation == 'horizontal' ? this . viewportCrossSize : this . viewportSize ;
757
+ }
758
+ }
0 commit comments