@@ -4,7 +4,15 @@ import {By} from '@angular/platform-browser';
44import { MdChip , MdChipList , MdChipsModule } from './index' ;
55import { ListKeyManager } from '../core/a11y/list-key-manager' ;
66import { FakeEvent } from '../core/a11y/list-key-manager.spec' ;
7- import { SPACE } from '../core/keyboard/keycodes' ;
7+ import { SPACE , LEFT_ARROW , RIGHT_ARROW } from '../core/keyboard/keycodes' ;
8+
9+ class FakeKeyboardEvent extends FakeEvent {
10+ constructor ( keyCode : number , protected target : HTMLElement ) {
11+ super ( keyCode ) ;
12+
13+ this . target = target ;
14+ }
15+ }
816
917describe ( 'MdChipList' , ( ) => {
1018 let fixture : ComponentFixture < any > ;
@@ -101,6 +109,50 @@ describe('MdChipList', () => {
101109 } ) ;
102110
103111 describe ( 'keyboard behavior' , ( ) => {
112+ beforeEach ( ( ) => {
113+ manager = chipListInstance . _keyManager ;
114+ } ) ;
115+
116+ it ( 'left arrow focuses previous item' , ( ) => {
117+ let nativeChips = chipListNativeElement . querySelectorAll ( 'md-chip' ) ;
118+ let lastNativeChip = nativeChips [ nativeChips . length - 1 ] as HTMLElement ;
119+
120+ let LEFT_EVENT = new FakeKeyboardEvent ( LEFT_ARROW , lastNativeChip ) as any ;
121+ let array = chips . toArray ( ) ;
122+ let lastIndex = array . length - 1 ;
123+ let lastItem = array [ lastIndex ] ;
124+
125+ // Focus the last item in the array
126+ lastItem . focus ( ) ;
127+ expect ( manager . focusedItemIndex ) . toEqual ( lastIndex ) ;
128+
129+ // Press the LEFT arrow
130+ chipListInstance . _keydown ( LEFT_EVENT ) ;
131+ fixture . detectChanges ( ) ;
132+
133+ // It focuses the next-to-last item
134+ expect ( manager . focusedItemIndex ) . toEqual ( lastIndex - 1 ) ;
135+ } ) ;
136+
137+ it ( 'right arrow focuses next item' , ( ) => {
138+ let nativeChips = chipListNativeElement . querySelectorAll ( 'md-chip' ) ;
139+ let firstNativeChip = nativeChips [ 0 ] as HTMLElement ;
140+
141+ let RIGHT_EVENT : KeyboardEvent = new FakeKeyboardEvent ( RIGHT_ARROW , firstNativeChip ) as any ;
142+ let array = chips . toArray ( ) ;
143+ let firstItem = array [ 0 ] ;
144+
145+ // Focus the last item in the array
146+ firstItem . focus ( ) ;
147+ expect ( manager . focusedItemIndex ) . toEqual ( 0 ) ;
148+
149+ // Press the RIGHT arrow
150+ chipListInstance . _keydown ( RIGHT_EVENT ) ;
151+ fixture . detectChanges ( ) ;
152+
153+ // It focuses the next-to-last item
154+ expect ( manager . focusedItemIndex ) . toEqual ( 1 ) ;
155+ } ) ;
104156
105157 describe ( 'when selectable is true' , ( ) => {
106158 beforeEach ( ( ) => {
@@ -109,7 +161,10 @@ describe('MdChipList', () => {
109161 } ) ;
110162
111163 it ( 'SPACE selects/deselects the currently focused chip' , ( ) => {
112- let SPACE_EVENT : KeyboardEvent = new FakeEvent ( SPACE ) as KeyboardEvent ;
164+ let nativeChips = chipListNativeElement . querySelectorAll ( 'md-chip' ) ;
165+ let firstNativeChip = nativeChips [ 0 ] as HTMLElement ;
166+
167+ let SPACE_EVENT : KeyboardEvent = new FakeKeyboardEvent ( SPACE , firstNativeChip ) as any ;
113168 let firstChip : MdChip = chips . toArray ( ) [ 0 ] ;
114169
115170 spyOn ( testComponent , 'chipSelect' ) ;
@@ -181,6 +236,9 @@ class StaticChipList {
181236 selectable : boolean = true ;
182237 remove : Number ;
183238
184- chipSelect ( index : Number ) { }
185- chipDeselect ( index : Number ) { }
239+ chipSelect ( index : Number ) {
240+ }
241+
242+ chipDeselect ( index : Number ) {
243+ }
186244}
0 commit comments