@@ -193,19 +193,19 @@ describe('MdSelect', () => {
193193 } ) ;
194194
195195 it ( 'should focus the selected option if an option is selected' , async ( ( ) => {
196- trigger . click ( ) ;
197- fixture . detectChanges ( ) ;
198-
199- const options =
200- overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
201- options [ 1 ] . click ( ) ;
202- fixture . detectChanges ( ) ;
196+ // must wait for initial writeValue promise to finish
197+ fixture . whenStable ( ) . then ( ( ) => {
198+ fixture . componentInstance . control . setValue ( 'pizza-1' ) ;
199+ fixture . detectChanges ( ) ;
203200
204- trigger . click ( ) ;
205- fixture . detectChanges ( ) ;
201+ trigger . click ( ) ;
202+ fixture . detectChanges ( ) ;
206203
207- fixture . whenStable ( ) . then ( ( ) => {
208- expect ( fixture . componentInstance . select . _keyManager . focusedItemIndex ) . toEqual ( 1 ) ;
204+ // must wait for animation to finish
205+ fixture . whenStable ( ) . then ( ( ) => {
206+ fixture . detectChanges ( ) ;
207+ expect ( fixture . componentInstance . select . _keyManager . focusedItemIndex ) . toEqual ( 1 ) ;
208+ } ) ;
209209 } ) ;
210210 } ) ) ;
211211
@@ -307,6 +307,49 @@ describe('MdSelect', () => {
307307 . toEqual ( 'steak-0' , `Expected control's value to be set to the new option.` ) ;
308308 } ) ;
309309
310+ it ( 'should clear the selection when a nonexistent option value is selected' , ( ) => {
311+ fixture . componentInstance . control . setValue ( 'pizza-1' ) ;
312+ fixture . detectChanges ( ) ;
313+
314+ fixture . componentInstance . control . setValue ( 'gibberish' ) ;
315+ fixture . detectChanges ( ) ;
316+
317+ const value = fixture . debugElement . query ( By . css ( '.md-select-value' ) ) ;
318+ expect ( value ) . toBe ( null , `Expected trigger to be cleared when option value is not found.` ) ;
319+ expect ( trigger . textContent )
320+ . not . toContain ( 'Pizza' , `Expected trigger to be cleared when option value is not found.` ) ;
321+
322+ trigger . click ( ) ;
323+ fixture . detectChanges ( ) ;
324+
325+ const options =
326+ overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
327+ expect ( options [ 1 ] . classList )
328+ . not . toContain ( 'md-selected' , `Expected option with the old value not to be selected.` ) ;
329+ } ) ;
330+
331+
332+ it ( 'should clear the selection when the control is reset' , ( ) => {
333+ fixture . componentInstance . control . setValue ( 'pizza-1' ) ;
334+ fixture . detectChanges ( ) ;
335+
336+ fixture . componentInstance . control . reset ( ) ;
337+ fixture . detectChanges ( ) ;
338+
339+ const value = fixture . debugElement . query ( By . css ( '.md-select-value' ) ) ;
340+ expect ( value ) . toBe ( null , `Expected trigger to be cleared when option value is not found.` ) ;
341+ expect ( trigger . textContent )
342+ . not . toContain ( 'Pizza' , `Expected trigger to be cleared when option value is not found.` ) ;
343+
344+ trigger . click ( ) ;
345+ fixture . detectChanges ( ) ;
346+
347+ const options =
348+ overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
349+ expect ( options [ 1 ] . classList )
350+ . not . toContain ( 'md-selected' , `Expected option with the old value not to be selected.` ) ;
351+ } ) ;
352+
310353 it ( 'should set the control to touched when the select is touched' , ( ) => {
311354 expect ( fixture . componentInstance . control . touched )
312355 . toEqual ( false , `Expected the control to start off as untouched.` ) ;
0 commit comments