@@ -14,11 +14,12 @@ import {OverlayContainer} from '../core/overlay/overlay-container';
14
14
import { MdSelect , MdSelectFloatPlaceholderType } from './select' ;
15
15
import { MdOption } from '../core/option/option' ;
16
16
import { Dir } from '../core/rtl/dir' ;
17
+ import { DOWN_ARROW , UP_ARROW } from '../core/keyboard/keycodes' ;
17
18
import {
18
19
ControlValueAccessor , FormControl , FormsModule , NG_VALUE_ACCESSOR , ReactiveFormsModule
19
20
} from '@angular/forms' ;
20
21
import { ViewportRuler } from '../core/overlay/position/viewport-ruler' ;
21
- import { dispatchFakeEvent } from '../core/testing/dispatch-events' ;
22
+ import { dispatchFakeEvent , dispatchKeyboardEvent } from '../core/testing/dispatch-events' ;
22
23
23
24
describe ( 'MdSelect' , ( ) => {
24
25
let overlayContainerElement : HTMLElement ;
@@ -1079,6 +1080,81 @@ describe('MdSelect', () => {
1079
1080
expect ( select . getAttribute ( 'tabindex' ) ) . toEqual ( '0' ) ;
1080
1081
} ) ;
1081
1082
1083
+ it ( 'should be able to select options via the arrow keys on a closed select' , ( ) => {
1084
+ const formControl = fixture . componentInstance . control ;
1085
+ const options = fixture . componentInstance . options . toArray ( ) ;
1086
+
1087
+ expect ( formControl . value ) . toBeFalsy ( 'Expected no initial value.' ) ;
1088
+
1089
+ dispatchKeyboardEvent ( select , 'keydown' , DOWN_ARROW ) ;
1090
+
1091
+ expect ( options [ 0 ] . selected ) . toBe ( true , 'Expected first option to be selected.' ) ;
1092
+ expect ( formControl . value ) . toBe ( options [ 0 ] . value ,
1093
+ 'Expected value from first option to have been set on the model.' ) ;
1094
+
1095
+ dispatchKeyboardEvent ( select , 'keydown' , DOWN_ARROW ) ;
1096
+ dispatchKeyboardEvent ( select , 'keydown' , DOWN_ARROW ) ;
1097
+
1098
+ // Note that the third option is skipped, because it is disabled.
1099
+ expect ( options [ 3 ] . selected ) . toBe ( true , 'Expected fourth option to be selected.' ) ;
1100
+ expect ( formControl . value ) . toBe ( options [ 3 ] . value ,
1101
+ 'Expected value from fourth option to have been set on the model.' ) ;
1102
+
1103
+ dispatchKeyboardEvent ( select , 'keydown' , UP_ARROW ) ;
1104
+
1105
+ expect ( options [ 1 ] . selected ) . toBe ( true , 'Expected second option to be selected.' ) ;
1106
+ expect ( formControl . value ) . toBe ( options [ 1 ] . value ,
1107
+ 'Expected value from second option to have been set on the model.' ) ;
1108
+ } ) ;
1109
+
1110
+ it ( 'should do nothing if the key manager did not change the active item' , ( ) => {
1111
+ const formControl = fixture . componentInstance . control ;
1112
+
1113
+ expect ( formControl . value ) . toBeNull ( 'Expected form control value to be empty.' ) ;
1114
+ expect ( formControl . pristine ) . toBe ( true , 'Expected form control to be clean.' ) ;
1115
+
1116
+ dispatchKeyboardEvent ( select , 'keydown' , 16 ) ; // Press a random key.
1117
+
1118
+ expect ( formControl . value ) . toBeNull ( 'Expected form control value to stay empty.' ) ;
1119
+ expect ( formControl . pristine ) . toBe ( true , 'Expected form control to stay clean.' ) ;
1120
+ } ) ;
1121
+
1122
+ it ( 'should continue from the selected option when the value is set programmatically' , ( ) => {
1123
+ const formControl = fixture . componentInstance . control ;
1124
+
1125
+ formControl . setValue ( 'eggs-5' ) ;
1126
+ fixture . detectChanges ( ) ;
1127
+
1128
+ dispatchKeyboardEvent ( select , 'keydown' , DOWN_ARROW ) ;
1129
+
1130
+ expect ( formControl . value ) . toBe ( 'pasta-6' ) ;
1131
+ expect ( fixture . componentInstance . options . toArray ( ) [ 6 ] . selected ) . toBe ( true ) ;
1132
+ } ) ;
1133
+
1134
+ it ( 'should not cycle through the options if the control is disabled' , ( ) => {
1135
+ const formControl = fixture . componentInstance . control ;
1136
+
1137
+ formControl . setValue ( 'eggs-5' ) ;
1138
+ formControl . disable ( ) ;
1139
+ dispatchKeyboardEvent ( select , 'keydown' , DOWN_ARROW ) ;
1140
+
1141
+ expect ( formControl . value ) . toBe ( 'eggs-5' , 'Expected value to remain unchaged.' ) ;
1142
+ } ) ;
1143
+
1144
+ it ( 'should not wrap selection around after reaching the end of the options' , ( ) => {
1145
+ const lastOption = fixture . componentInstance . options . last ;
1146
+
1147
+ fixture . componentInstance . options . forEach ( ( ) => {
1148
+ dispatchKeyboardEvent ( select , 'keydown' , DOWN_ARROW ) ;
1149
+ } ) ;
1150
+
1151
+ expect ( lastOption . selected ) . toBe ( true , 'Expected last option to be selected.' ) ;
1152
+
1153
+ dispatchKeyboardEvent ( select , 'keydown' , DOWN_ARROW ) ;
1154
+
1155
+ expect ( lastOption . selected ) . toBe ( true , 'Expected last option to stay selected.' ) ;
1156
+ } ) ;
1157
+
1082
1158
} ) ;
1083
1159
1084
1160
describe ( 'for options' , ( ) => {
0 commit comments