@@ -7,9 +7,11 @@ import {MdInputModule} from '../input/index';
77import { Dir , LayoutDirection } from '../core/rtl/dir' ;
88import { FormControl , ReactiveFormsModule } from '@angular/forms' ;
99import { Subscription } from 'rxjs/Subscription' ;
10- import { ENTER , DOWN_ARROW , SPACE } from '../core/keyboard/keycodes' ;
10+ import { ENTER , DOWN_ARROW , SPACE , UP_ARROW } from '../core/keyboard/keycodes' ;
1111import { MdOption } from '../core/option/option' ;
1212import { ViewportRuler } from '../core/overlay/position/viewport-ruler' ;
13+ import { FakeViewportRuler } from '../core/overlay/position/fake-viewport-ruler' ;
14+
1315
1416describe ( 'MdAutocomplete' , ( ) => {
1517 let overlayContainerElement : HTMLElement ;
@@ -294,6 +296,36 @@ describe('MdAutocomplete', () => {
294296 } ) ;
295297 } ) ) ;
296298
299+ it ( 'should set the active item to the last option when UP key is pressed' , async ( ( ) => {
300+ fixture . componentInstance . trigger . openPanel ( ) ;
301+ fixture . detectChanges ( ) ;
302+
303+ const optionEls =
304+ overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
305+
306+ const UP_ARROW_EVENT = new FakeKeyboardEvent ( UP_ARROW ) as KeyboardEvent ;
307+ fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
308+
309+ fixture . whenStable ( ) . then ( ( ) => {
310+ fixture . detectChanges ( ) ;
311+ expect ( fixture . componentInstance . trigger . activeOption )
312+ . toBe ( fixture . componentInstance . options . last , 'Expected last option to be active.' ) ;
313+ expect ( optionEls [ 10 ] . classList ) . toContain ( 'md-active' ) ;
314+ expect ( optionEls [ 0 ] . classList ) . not . toContain ( 'md-active' ) ;
315+
316+ fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
317+
318+ fixture . whenStable ( ) . then ( ( ) => {
319+ fixture . detectChanges ( ) ;
320+ expect ( fixture . componentInstance . trigger . activeOption )
321+ . toBe ( fixture . componentInstance . options . first ,
322+ 'Expected first option to be active.' ) ;
323+ expect ( optionEls [ 0 ] . classList ) . toContain ( 'md-active' ) ;
324+ expect ( optionEls [ 10 ] . classList ) . not . toContain ( 'md-active' ) ;
325+ } ) ;
326+ } ) ;
327+ } ) ) ;
328+
297329 it ( 'should set the active item properly after filtering' , async ( ( ) => {
298330 fixture . componentInstance . trigger . openPanel ( ) ;
299331 fixture . detectChanges ( ) ;
@@ -532,7 +564,7 @@ describe('MdAutocomplete', () => {
532564
533565 it ( 'should fall back to above position if panel cannot fit below' , ( ) => {
534566 // Push the autocomplete trigger down so it won't have room to open "below"
535- input . style . top = '400px ' ;
567+ input . style . top = '600px ' ;
536568 input . style . position = 'relative' ;
537569
538570 fixture . componentInstance . trigger . openPanel ( ) ;
@@ -551,7 +583,7 @@ describe('MdAutocomplete', () => {
551583
552584 it ( 'should align panel properly when filtering in "above" position' , ( ) => {
553585 // Push the autocomplete trigger down so it won't have room to open "below"
554- input . style . top = '400px ' ;
586+ input . style . top = '600px ' ;
555587 input . style . position = 'relative' ;
556588
557589 fixture . componentInstance . trigger . openPanel ( ) ;
@@ -645,16 +677,3 @@ class FakeKeyboardEvent {
645677 constructor ( public keyCode : number ) { }
646678 preventDefault ( ) { }
647679}
648-
649- class FakeViewportRuler {
650- getViewportRect ( ) {
651- return {
652- left : 0 , top : 0 , width : 500 , height : 500 , bottom : 500 , right : 500
653- } ;
654- }
655-
656- getViewportScrollPosition ( ) {
657- return { top : 0 , left : 0 } ;
658- }
659- }
660-
0 commit comments