@@ -534,6 +534,7 @@ describe('MdAutocomplete', () => {
534
534
let fixture : ComponentFixture < SimpleAutocomplete > ;
535
535
let input : HTMLInputElement ;
536
536
let DOWN_ARROW_EVENT : KeyboardEvent ;
537
+ let UP_ARROW_EVENT : KeyboardEvent ;
537
538
let ENTER_EVENT : KeyboardEvent ;
538
539
539
540
beforeEach ( ( ) => {
@@ -542,6 +543,7 @@ describe('MdAutocomplete', () => {
542
543
543
544
input = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
544
545
DOWN_ARROW_EVENT = createKeyboardEvent ( 'keydown' , DOWN_ARROW ) ;
546
+ UP_ARROW_EVENT = createKeyboardEvent ( 'keydown' , UP_ARROW ) ;
545
547
ENTER_EVENT = createKeyboardEvent ( 'keydown' , ENTER ) ;
546
548
547
549
fixture . componentInstance . trigger . openPanel ( ) ;
@@ -600,7 +602,6 @@ describe('MdAutocomplete', () => {
600
602
const optionEls =
601
603
overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
602
604
603
- const UP_ARROW_EVENT = createKeyboardEvent ( 'keydown' , UP_ARROW ) ;
604
605
fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
605
606
tick ( ) ;
606
607
fixture . detectChanges ( ) ;
@@ -754,7 +755,6 @@ describe('MdAutocomplete', () => {
754
755
const scrollContainer =
755
756
document . querySelector ( '.cdk-overlay-pane .mat-autocomplete-panel' ) ! ;
756
757
757
- const UP_ARROW_EVENT = createKeyboardEvent ( 'keydown' , UP_ARROW ) ;
758
758
fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
759
759
tick ( ) ;
760
760
fixture . detectChanges ( ) ;
@@ -763,6 +763,64 @@ describe('MdAutocomplete', () => {
763
763
expect ( scrollContainer . scrollTop ) . toEqual ( 272 , `Expected panel to reveal last option.` ) ;
764
764
} ) ) ;
765
765
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
+
766
824
it ( 'should close the panel when pressing escape' , async ( ( ) => {
767
825
const trigger = fixture . componentInstance . trigger ;
768
826
const escapeEvent = createKeyboardEvent ( 'keydown' , ESCAPE ) ;
0 commit comments