@@ -534,6 +534,7 @@ describe('MdAutocomplete', () => {
534534 let fixture : ComponentFixture < SimpleAutocomplete > ;
535535 let input : HTMLInputElement ;
536536 let DOWN_ARROW_EVENT : KeyboardEvent ;
537+ let UP_ARROW_EVENT : KeyboardEvent ;
537538 let ENTER_EVENT : KeyboardEvent ;
538539
539540 beforeEach ( ( ) => {
@@ -542,6 +543,7 @@ describe('MdAutocomplete', () => {
542543
543544 input = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
544545 DOWN_ARROW_EVENT = createKeyboardEvent ( 'keydown' , DOWN_ARROW ) ;
546+ UP_ARROW_EVENT = createKeyboardEvent ( 'keydown' , UP_ARROW ) ;
545547 ENTER_EVENT = createKeyboardEvent ( 'keydown' , ENTER ) ;
546548
547549 fixture . componentInstance . trigger . openPanel ( ) ;
@@ -600,7 +602,6 @@ describe('MdAutocomplete', () => {
600602 const optionEls =
601603 overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
602604
603- const UP_ARROW_EVENT = createKeyboardEvent ( 'keydown' , UP_ARROW ) ;
604605 fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
605606 tick ( ) ;
606607 fixture . detectChanges ( ) ;
@@ -754,7 +755,6 @@ describe('MdAutocomplete', () => {
754755 const scrollContainer =
755756 document . querySelector ( '.cdk-overlay-pane .mat-autocomplete-panel' ) ! ;
756757
757- const UP_ARROW_EVENT = createKeyboardEvent ( 'keydown' , UP_ARROW ) ;
758758 fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
759759 tick ( ) ;
760760 fixture . detectChanges ( ) ;
@@ -763,6 +763,64 @@ describe('MdAutocomplete', () => {
763763 expect ( scrollContainer . scrollTop ) . toEqual ( 272 , `Expected panel to reveal last option.` ) ;
764764 } ) ) ;
765765
766+ it ( 'should not scroll to active options that are fully in the panel' , fakeAsync ( ( ) => {
767+ tick ( ) ;
768+ const scrollContainer =
769+ document . querySelector ( '.cdk-overlay-pane .mat-autocomplete-panel' ) ! ;
770+
771+ fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
772+ tick ( ) ;
773+ fixture . detectChanges ( ) ;
774+ expect ( scrollContainer . scrollTop ) . toEqual ( 0 , `Expected panel not to scroll.` ) ;
775+
776+ // These down arrows will set the 6th option active, below the fold.
777+ [ 1 , 2 , 3 , 4 , 5 ] . forEach ( ( ) => {
778+ fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
779+ tick ( ) ;
780+ } ) ;
781+
782+ // Expect option bottom minus the panel height (288 - 256 = 32)
783+ expect ( scrollContainer . scrollTop )
784+ . toEqual ( 32 , `Expected panel to reveal the sixth option.` ) ;
785+
786+ // These up arrows will set the 2nd option active
787+ [ 4 , 3 , 2 , 1 ] . forEach ( ( ) => {
788+ fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
789+ tick ( ) ;
790+ } ) ;
791+
792+ // Expect no scrolling to have occurred. Still showing bottom of 6th option.
793+ expect ( scrollContainer . scrollTop )
794+ . toEqual ( 32 , `Expected panel to not scroll back.` ) ;
795+ } ) ) ;
796+
797+ it ( 'should scroll to active options that are above the panel' , fakeAsync ( ( ) => {
798+ tick ( ) ;
799+ const scrollContainer =
800+ document . querySelector ( '.cdk-overlay-pane .mat-autocomplete-panel' ) ! ;
801+
802+ fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
803+ tick ( ) ;
804+ fixture . detectChanges ( ) ;
805+ expect ( scrollContainer . scrollTop ) . toEqual ( 0 , `Expected panel not to scroll.` ) ;
806+
807+ // These down arrows will set the 7th option active, below the fold.
808+ [ 1 , 2 , 3 , 4 , 5 , 6 ] . forEach ( ( ) => {
809+ fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
810+ tick ( ) ;
811+ } ) ;
812+
813+ // These up arrows will set the 2nd option active
814+ [ 5 , 4 , 3 , 2 , 1 ] . forEach ( ( ) => {
815+ fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
816+ tick ( ) ;
817+ } ) ;
818+
819+ // Expect to show the top of the 2nd option at the top of the panel
820+ expect ( scrollContainer . scrollTop )
821+ . toEqual ( 48 , `Expected panel to scroll up when option is above panel.` ) ;
822+ } ) ) ;
823+
766824 it ( 'should close the panel when pressing escape' , async ( ( ) => {
767825 const trigger = fixture . componentInstance . trigger ;
768826 const escapeEvent = createKeyboardEvent ( 'keydown' , ESCAPE ) ;
0 commit comments