@@ -16,6 +16,7 @@ import {MdSelect, MdSelectFloatPlaceholderType} from './select';
16
16
import { MdSelectDynamicMultipleError , MdSelectNonArrayValueError } from './select-errors' ;
17
17
import { MdOption } from '../core/option/option' ;
18
18
import { Dir } from '../core/rtl/dir' ;
19
+ import { DOWN_ARROW , UP_ARROW } from '../core/keyboard/keycodes' ;
19
20
import {
20
21
ControlValueAccessor , FormControl , FormsModule , NG_VALUE_ACCESSOR , ReactiveFormsModule
21
22
} from '@angular/forms' ;
@@ -1342,6 +1343,81 @@ describe('MdSelect', () => {
1342
1343
expect ( select . getAttribute ( 'tabindex' ) ) . toEqual ( '0' ) ;
1343
1344
} ) ;
1344
1345
1346
+ it ( 'should be able to select options via the arrow keys on a closed select' , ( ) => {
1347
+ const formControl = fixture . componentInstance . control ;
1348
+ const options = fixture . componentInstance . options . toArray ( ) ;
1349
+
1350
+ expect ( formControl . value ) . toBeFalsy ( 'Expected no initial value.' ) ;
1351
+
1352
+ dispatchKeyboardEvent ( select , 'keydown' , DOWN_ARROW ) ;
1353
+
1354
+ expect ( options [ 0 ] . selected ) . toBe ( true , 'Expected first option to be selected.' ) ;
1355
+ expect ( formControl . value ) . toBe ( options [ 0 ] . value ,
1356
+ 'Expected value from first option to have been set on the model.' ) ;
1357
+
1358
+ dispatchKeyboardEvent ( select , 'keydown' , DOWN_ARROW ) ;
1359
+ dispatchKeyboardEvent ( select , 'keydown' , DOWN_ARROW ) ;
1360
+
1361
+ // Note that the third option is skipped, because it is disabled.
1362
+ expect ( options [ 3 ] . selected ) . toBe ( true , 'Expected fourth option to be selected.' ) ;
1363
+ expect ( formControl . value ) . toBe ( options [ 3 ] . value ,
1364
+ 'Expected value from fourth option to have been set on the model.' ) ;
1365
+
1366
+ dispatchKeyboardEvent ( select , 'keydown' , UP_ARROW ) ;
1367
+
1368
+ expect ( options [ 1 ] . selected ) . toBe ( true , 'Expected second option to be selected.' ) ;
1369
+ expect ( formControl . value ) . toBe ( options [ 1 ] . value ,
1370
+ 'Expected value from second option to have been set on the model.' ) ;
1371
+ } ) ;
1372
+
1373
+ it ( 'should do nothing if the key manager did not change the active item' , ( ) => {
1374
+ const formControl = fixture . componentInstance . control ;
1375
+
1376
+ expect ( formControl . value ) . toBeNull ( 'Expected form control value to be empty.' ) ;
1377
+ expect ( formControl . pristine ) . toBe ( true , 'Expected form control to be clean.' ) ;
1378
+
1379
+ dispatchKeyboardEvent ( select , 'keydown' , 16 ) ; // Press a random key.
1380
+
1381
+ expect ( formControl . value ) . toBeNull ( 'Expected form control value to stay empty.' ) ;
1382
+ expect ( formControl . pristine ) . toBe ( true , 'Expected form control to stay clean.' ) ;
1383
+ } ) ;
1384
+
1385
+ it ( 'should continue from the selected option when the value is set programmatically' , ( ) => {
1386
+ const formControl = fixture . componentInstance . control ;
1387
+
1388
+ formControl . setValue ( 'eggs-5' ) ;
1389
+ fixture . detectChanges ( ) ;
1390
+
1391
+ dispatchKeyboardEvent ( select , 'keydown' , DOWN_ARROW ) ;
1392
+
1393
+ expect ( formControl . value ) . toBe ( 'pasta-6' ) ;
1394
+ expect ( fixture . componentInstance . options . toArray ( ) [ 6 ] . selected ) . toBe ( true ) ;
1395
+ } ) ;
1396
+
1397
+ it ( 'should not cycle through the options if the control is disabled' , ( ) => {
1398
+ const formControl = fixture . componentInstance . control ;
1399
+
1400
+ formControl . setValue ( 'eggs-5' ) ;
1401
+ formControl . disable ( ) ;
1402
+ dispatchKeyboardEvent ( select , 'keydown' , DOWN_ARROW ) ;
1403
+
1404
+ expect ( formControl . value ) . toBe ( 'eggs-5' , 'Expected value to remain unchaged.' ) ;
1405
+ } ) ;
1406
+
1407
+ it ( 'should not wrap selection around after reaching the end of the options' , ( ) => {
1408
+ const lastOption = fixture . componentInstance . options . last ;
1409
+
1410
+ fixture . componentInstance . options . forEach ( ( ) => {
1411
+ dispatchKeyboardEvent ( select , 'keydown' , DOWN_ARROW ) ;
1412
+ } ) ;
1413
+
1414
+ expect ( lastOption . selected ) . toBe ( true , 'Expected last option to be selected.' ) ;
1415
+
1416
+ dispatchKeyboardEvent ( select , 'keydown' , DOWN_ARROW ) ;
1417
+
1418
+ expect ( lastOption . selected ) . toBe ( true , 'Expected last option to stay selected.' ) ;
1419
+ } ) ;
1420
+
1345
1421
} ) ;
1346
1422
1347
1423
describe ( 'for options' , ( ) => {
0 commit comments