@@ -529,6 +529,7 @@ describe('MdAutocomplete', () => {
529529 let fixture : ComponentFixture < SimpleAutocomplete > ;
530530 let input : HTMLInputElement ;
531531 let DOWN_ARROW_EVENT : KeyboardEvent ;
532+ let UP_ARROW_EVENT : KeyboardEvent ;
532533 let ENTER_EVENT : KeyboardEvent ;
533534
534535 beforeEach ( ( ) => {
@@ -537,6 +538,7 @@ describe('MdAutocomplete', () => {
537538
538539 input = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
539540 DOWN_ARROW_EVENT = createKeyboardEvent ( 'keydown' , DOWN_ARROW ) ;
541+ UP_ARROW_EVENT = createKeyboardEvent ( 'keydown' , UP_ARROW ) ;
540542 ENTER_EVENT = createKeyboardEvent ( 'keydown' , ENTER ) ;
541543
542544 fixture . componentInstance . trigger . openPanel ( ) ;
@@ -595,7 +597,6 @@ describe('MdAutocomplete', () => {
595597 const optionEls =
596598 overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
597599
598- const UP_ARROW_EVENT = createKeyboardEvent ( 'keydown' , UP_ARROW ) ;
599600 fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
600601 tick ( ) ;
601602 fixture . detectChanges ( ) ;
@@ -749,7 +750,6 @@ describe('MdAutocomplete', () => {
749750 const scrollContainer =
750751 document . querySelector ( '.cdk-overlay-pane .mat-autocomplete-panel' ) ;
751752
752- const UP_ARROW_EVENT = createKeyboardEvent ( 'keydown' , UP_ARROW ) ;
753753 fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
754754 tick ( ) ;
755755 fixture . detectChanges ( ) ;
@@ -758,6 +758,64 @@ describe('MdAutocomplete', () => {
758758 expect ( scrollContainer . scrollTop ) . toEqual ( 272 , `Expected panel to reveal last option.` ) ;
759759 } ) ) ;
760760
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+
761819 it ( 'should close the panel when pressing escape' , async ( ( ) => {
762820 const trigger = fixture . componentInstance . trigger ;
763821 const escapeEvent = createKeyboardEvent ( 'keydown' , ESCAPE ) ;
0 commit comments