@@ -529,6 +529,7 @@ describe('MdAutocomplete', () => {
529
529
let fixture : ComponentFixture < SimpleAutocomplete > ;
530
530
let input : HTMLInputElement ;
531
531
let DOWN_ARROW_EVENT : KeyboardEvent ;
532
+ let UP_ARROW_EVENT : KeyboardEvent ;
532
533
let ENTER_EVENT : KeyboardEvent ;
533
534
534
535
beforeEach ( ( ) => {
@@ -537,6 +538,7 @@ describe('MdAutocomplete', () => {
537
538
538
539
input = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
539
540
DOWN_ARROW_EVENT = createKeyboardEvent ( 'keydown' , DOWN_ARROW ) ;
541
+ UP_ARROW_EVENT = createKeyboardEvent ( 'keydown' , UP_ARROW ) ;
540
542
ENTER_EVENT = createKeyboardEvent ( 'keydown' , ENTER ) ;
541
543
542
544
fixture . componentInstance . trigger . openPanel ( ) ;
@@ -595,7 +597,6 @@ describe('MdAutocomplete', () => {
595
597
const optionEls =
596
598
overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
597
599
598
- const UP_ARROW_EVENT = createKeyboardEvent ( 'keydown' , UP_ARROW ) ;
599
600
fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
600
601
tick ( ) ;
601
602
fixture . detectChanges ( ) ;
@@ -749,7 +750,6 @@ describe('MdAutocomplete', () => {
749
750
const scrollContainer =
750
751
document . querySelector ( '.cdk-overlay-pane .mat-autocomplete-panel' ) ;
751
752
752
- const UP_ARROW_EVENT = createKeyboardEvent ( 'keydown' , UP_ARROW ) ;
753
753
fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
754
754
tick ( ) ;
755
755
fixture . detectChanges ( ) ;
@@ -758,6 +758,64 @@ describe('MdAutocomplete', () => {
758
758
expect ( scrollContainer . scrollTop ) . toEqual ( 272 , `Expected panel to reveal last option.` ) ;
759
759
} ) ) ;
760
760
761
+ it ( 'should not scroll to active options that are fully in the panel' , fakeAsync ( ( ) => {
762
+ tick ( ) ;
763
+ const scrollContainer =
764
+ document . querySelector ( '.cdk-overlay-pane .mat-autocomplete-panel' ) ;
765
+
766
+ fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
767
+ tick ( ) ;
768
+ fixture . detectChanges ( ) ;
769
+ expect ( scrollContainer . scrollTop ) . toEqual ( 0 , `Expected panel not to scroll.` ) ;
770
+
771
+ // These down arrows will set the 6th option active, below the fold.
772
+ [ 1 , 2 , 3 , 4 , 5 ] . forEach ( ( ) => {
773
+ fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
774
+ tick ( ) ;
775
+ } ) ;
776
+
777
+ // Expect option bottom minus the panel height (288 - 256 = 32)
778
+ expect ( scrollContainer . scrollTop )
779
+ . toEqual ( 32 , `Expected panel to reveal the sixth option.` ) ;
780
+
781
+ // These up arrows will set the 2nd option active
782
+ [ 4 , 3 , 2 , 1 ] . forEach ( ( ) => {
783
+ fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
784
+ tick ( ) ;
785
+ } ) ;
786
+
787
+ // Expect no scrolling to have occurred. Still showing bottom of 6th option.
788
+ expect ( scrollContainer . scrollTop )
789
+ . toEqual ( 32 , `Expected panel to not scroll back.` ) ;
790
+ } ) ) ;
791
+
792
+ it ( 'should scroll to active options that are above the panel' , fakeAsync ( ( ) => {
793
+ tick ( ) ;
794
+ const scrollContainer =
795
+ document . querySelector ( '.cdk-overlay-pane .mat-autocomplete-panel' ) ;
796
+
797
+ fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
798
+ tick ( ) ;
799
+ fixture . detectChanges ( ) ;
800
+ expect ( scrollContainer . scrollTop ) . toEqual ( 0 , `Expected panel not to scroll.` ) ;
801
+
802
+ // These down arrows will set the 7th option active, below the fold.
803
+ [ 1 , 2 , 3 , 4 , 5 , 6 ] . forEach ( ( ) => {
804
+ fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
805
+ tick ( ) ;
806
+ } ) ;
807
+
808
+ // These up arrows will set the 2nd option active
809
+ [ 5 , 4 , 3 , 2 , 1 ] . forEach ( ( ) => {
810
+ fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
811
+ tick ( ) ;
812
+ } ) ;
813
+
814
+ // Expect to show the top of the 2nd option at the top of the panel
815
+ expect ( scrollContainer . scrollTop )
816
+ . toEqual ( 48 , `Expected panel to scroll up when option is above panel.` ) ;
817
+ } ) ) ;
818
+
761
819
it ( 'should close the panel when pressing escape' , async ( ( ) => {
762
820
const trigger = fixture . componentInstance . trigger ;
763
821
const escapeEvent = createKeyboardEvent ( 'keydown' , ESCAPE ) ;
0 commit comments