@@ -191,19 +191,19 @@ describe('MdSelect', () => {
191
191
} ) ;
192
192
193
193
it ( 'should focus the selected option if an option is selected' , async ( ( ) => {
194
- trigger . click ( ) ;
195
- fixture . detectChanges ( ) ;
196
-
197
- const options =
198
- overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
199
- options [ 1 ] . click ( ) ;
200
- fixture . detectChanges ( ) ;
194
+ // must wait for initial writeValue promise to finish
195
+ fixture . whenStable ( ) . then ( ( ) => {
196
+ fixture . componentInstance . control . setValue ( 'pizza-1' ) ;
197
+ fixture . detectChanges ( ) ;
201
198
202
- trigger . click ( ) ;
203
- fixture . detectChanges ( ) ;
199
+ trigger . click ( ) ;
200
+ fixture . detectChanges ( ) ;
204
201
205
- fixture . whenStable ( ) . then ( ( ) => {
206
- expect ( fixture . componentInstance . select . _keyManager . focusedItemIndex ) . toEqual ( 1 ) ;
202
+ // must wait for animation to finish
203
+ fixture . whenStable ( ) . then ( ( ) => {
204
+ fixture . detectChanges ( ) ;
205
+ expect ( fixture . componentInstance . select . _keyManager . focusedItemIndex ) . toEqual ( 1 ) ;
206
+ } ) ;
207
207
} ) ;
208
208
} ) ) ;
209
209
@@ -305,6 +305,27 @@ describe('MdSelect', () => {
305
305
. toEqual ( 'steak-0' , `Expected control's value to be set to the new option.` ) ;
306
306
} ) ;
307
307
308
+ it ( 'should clear the selection when a nonexistent option value is selected' , ( ) => {
309
+ fixture . componentInstance . control . setValue ( 'pizza-1' ) ;
310
+ fixture . detectChanges ( ) ;
311
+
312
+ fixture . componentInstance . control . setValue ( 'gibberish' ) ;
313
+ fixture . detectChanges ( ) ;
314
+
315
+ const value = fixture . debugElement . query ( By . css ( '.md-select-value' ) ) ;
316
+ expect ( value ) . toBe ( null , `Expected trigger to be cleared when option value is not found.` ) ;
317
+ expect ( trigger . textContent )
318
+ . not . toContain ( 'Pizza' , `Expected trigger to be cleared when option value is not found.` ) ;
319
+
320
+ trigger . click ( ) ;
321
+ fixture . detectChanges ( ) ;
322
+
323
+ const options =
324
+ overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
325
+ expect ( options [ 1 ] . classList )
326
+ . not . toContain ( 'md-selected' , `Expected option with the old value not to be selected.` ) ;
327
+ } ) ;
328
+
308
329
it ( 'should set the control to touched when the select is touched' , ( ) => {
309
330
expect ( fixture . componentInstance . control . touched )
310
331
. toEqual ( false , `Expected the control to start off as untouched.` ) ;
0 commit comments