@@ -548,6 +548,7 @@ describe('MdAutocomplete', () => {
548
548
let fixture : ComponentFixture < SimpleAutocomplete > ;
549
549
let input : HTMLInputElement ;
550
550
let DOWN_ARROW_EVENT : KeyboardEvent ;
551
+ let UP_ARROW_EVENT : KeyboardEvent ;
551
552
let ENTER_EVENT : KeyboardEvent ;
552
553
553
554
beforeEach ( ( ) => {
@@ -556,6 +557,7 @@ describe('MdAutocomplete', () => {
556
557
557
558
input = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
558
559
DOWN_ARROW_EVENT = createKeyboardEvent ( 'keydown' , DOWN_ARROW ) ;
560
+ UP_ARROW_EVENT = createKeyboardEvent ( 'keydown' , UP_ARROW ) ;
559
561
ENTER_EVENT = createKeyboardEvent ( 'keydown' , ENTER ) ;
560
562
561
563
fixture . componentInstance . trigger . openPanel ( ) ;
@@ -614,7 +616,6 @@ describe('MdAutocomplete', () => {
614
616
const optionEls =
615
617
overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
616
618
617
- const UP_ARROW_EVENT = createKeyboardEvent ( 'keydown' , UP_ARROW ) ;
618
619
fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
619
620
tick ( ) ;
620
621
fixture . detectChanges ( ) ;
@@ -768,7 +769,6 @@ describe('MdAutocomplete', () => {
768
769
const scrollContainer =
769
770
document . querySelector ( '.cdk-overlay-pane .mat-autocomplete-panel' ) ! ;
770
771
771
- const UP_ARROW_EVENT = createKeyboardEvent ( 'keydown' , UP_ARROW ) ;
772
772
fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
773
773
tick ( ) ;
774
774
fixture . detectChanges ( ) ;
@@ -777,6 +777,64 @@ describe('MdAutocomplete', () => {
777
777
expect ( scrollContainer . scrollTop ) . toEqual ( 272 , `Expected panel to reveal last option.` ) ;
778
778
} ) ) ;
779
779
780
+ it ( 'should not scroll to active options that are fully in the panel' , fakeAsync ( ( ) => {
781
+ tick ( ) ;
782
+ const scrollContainer =
783
+ document . querySelector ( '.cdk-overlay-pane .mat-autocomplete-panel' ) ! ;
784
+
785
+ fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
786
+ tick ( ) ;
787
+ fixture . detectChanges ( ) ;
788
+ expect ( scrollContainer . scrollTop ) . toEqual ( 0 , `Expected panel not to scroll.` ) ;
789
+
790
+ // These down arrows will set the 6th option active, below the fold.
791
+ [ 1 , 2 , 3 , 4 , 5 ] . forEach ( ( ) => {
792
+ fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
793
+ tick ( ) ;
794
+ } ) ;
795
+
796
+ // Expect option bottom minus the panel height (288 - 256 = 32)
797
+ expect ( scrollContainer . scrollTop )
798
+ . toEqual ( 32 , `Expected panel to reveal the sixth option.` ) ;
799
+
800
+ // These up arrows will set the 2nd option active
801
+ [ 4 , 3 , 2 , 1 ] . forEach ( ( ) => {
802
+ fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
803
+ tick ( ) ;
804
+ } ) ;
805
+
806
+ // Expect no scrolling to have occurred. Still showing bottom of 6th option.
807
+ expect ( scrollContainer . scrollTop )
808
+ . toEqual ( 32 , `Expected panel not to scroll up since sixth option still fully visible.` ) ;
809
+ } ) ) ;
810
+
811
+ it ( 'should scroll to active options that are above the panel' , fakeAsync ( ( ) => {
812
+ tick ( ) ;
813
+ const scrollContainer =
814
+ document . querySelector ( '.cdk-overlay-pane .mat-autocomplete-panel' ) ! ;
815
+
816
+ fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
817
+ tick ( ) ;
818
+ fixture . detectChanges ( ) ;
819
+ expect ( scrollContainer . scrollTop ) . toEqual ( 0 , `Expected panel not to scroll.` ) ;
820
+
821
+ // These down arrows will set the 7th option active, below the fold.
822
+ [ 1 , 2 , 3 , 4 , 5 , 6 ] . forEach ( ( ) => {
823
+ fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
824
+ tick ( ) ;
825
+ } ) ;
826
+
827
+ // These up arrows will set the 2nd option active
828
+ [ 5 , 4 , 3 , 2 , 1 ] . forEach ( ( ) => {
829
+ fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
830
+ tick ( ) ;
831
+ } ) ;
832
+
833
+ // Expect to show the top of the 2nd option at the top of the panel
834
+ expect ( scrollContainer . scrollTop )
835
+ . toEqual ( 48 , `Expected panel to scroll up when option is above panel.` ) ;
836
+ } ) ) ;
837
+
780
838
it ( 'should close the panel when pressing escape' , async ( ( ) => {
781
839
const trigger = fixture . componentInstance . trigger ;
782
840
const escapeEvent = createKeyboardEvent ( 'keydown' , ESCAPE ) ;
0 commit comments