@@ -55,7 +55,8 @@ describe('MdSelect', () => {
5555 SelectEarlyAccessSibling ,
5656 BasicSelectInitiallyHidden ,
5757 BasicSelectNoPlaceholder ,
58- BasicSelectWithTheming
58+ BasicSelectWithTheming ,
59+ ResetValuesSelect
5960 ] ,
6061 providers : [
6162 { provide : OverlayContainer , useFactory : ( ) => {
@@ -2022,6 +2023,82 @@ describe('MdSelect', () => {
20222023
20232024 } ) ;
20242025
2026+
2027+ describe ( 'reset values' , ( ) => {
2028+ let fixture : ComponentFixture < ResetValuesSelect > ;
2029+ let trigger : HTMLElement ;
2030+ let placeholder : HTMLElement ;
2031+ let options : NodeListOf < HTMLElement > ;
2032+
2033+ beforeEach ( ( ) => {
2034+ fixture = TestBed . createComponent ( ResetValuesSelect ) ;
2035+ fixture . detectChanges ( ) ;
2036+ trigger = fixture . debugElement . query ( By . css ( '.mat-select-trigger' ) ) . nativeElement ;
2037+ placeholder = fixture . debugElement . query ( By . css ( '.mat-select-placeholder' ) ) . nativeElement ;
2038+
2039+ trigger . click ( ) ;
2040+ fixture . detectChanges ( ) ;
2041+ options = overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
2042+
2043+ options [ 0 ] . click ( ) ;
2044+ fixture . detectChanges ( ) ;
2045+ } ) ;
2046+
2047+ it ( 'should reset when an option with an undefined value is selected' , ( ) => {
2048+ options [ 4 ] . click ( ) ;
2049+ fixture . detectChanges ( ) ;
2050+
2051+ expect ( fixture . componentInstance . control . value ) . toBeUndefined ( ) ;
2052+ expect ( fixture . componentInstance . select . selected ) . toBeFalsy ( ) ;
2053+ expect ( placeholder . classList ) . not . toContain ( 'mat-floating-placeholder' ) ;
2054+ expect ( trigger . textContent ) . not . toContain ( 'Undefined' ) ;
2055+ } ) ;
2056+
2057+ it ( 'should reset when an option with a null value is selected' , ( ) => {
2058+ options [ 5 ] . click ( ) ;
2059+ fixture . detectChanges ( ) ;
2060+
2061+ expect ( fixture . componentInstance . control . value ) . toBeNull ( ) ;
2062+ expect ( fixture . componentInstance . select . selected ) . toBeFalsy ( ) ;
2063+ expect ( placeholder . classList ) . not . toContain ( 'mat-floating-placeholder' ) ;
2064+ expect ( trigger . textContent ) . not . toContain ( 'Null' ) ;
2065+ } ) ;
2066+
2067+ it ( 'should reset when a blank option is selected' , ( ) => {
2068+ options [ 6 ] . click ( ) ;
2069+ fixture . detectChanges ( ) ;
2070+
2071+ expect ( fixture . componentInstance . control . value ) . toBeUndefined ( ) ;
2072+ expect ( fixture . componentInstance . select . selected ) . toBeFalsy ( ) ;
2073+ expect ( placeholder . classList ) . not . toContain ( 'mat-floating-placeholder' ) ;
2074+ expect ( trigger . textContent ) . not . toContain ( 'None' ) ;
2075+ } ) ;
2076+
2077+ it ( 'should not reset when any other falsy option is selected' , ( ) => {
2078+ options [ 3 ] . click ( ) ;
2079+ fixture . detectChanges ( ) ;
2080+
2081+ expect ( fixture . componentInstance . control . value ) . toBe ( false ) ;
2082+ expect ( fixture . componentInstance . select . selected ) . toBeTruthy ( ) ;
2083+ expect ( placeholder . classList ) . toContain ( 'mat-floating-placeholder' ) ;
2084+ expect ( trigger . textContent ) . toContain ( 'Falsy' ) ;
2085+ } ) ;
2086+
2087+ it ( 'should not consider the reset values as selected when resetting the form control' , ( ) => {
2088+ expect ( placeholder . classList ) . toContain ( 'mat-floating-placeholder' ) ;
2089+
2090+ fixture . componentInstance . control . reset ( ) ;
2091+ fixture . detectChanges ( ) ;
2092+
2093+ expect ( fixture . componentInstance . control . value ) . toBeNull ( ) ;
2094+ expect ( fixture . componentInstance . select . selected ) . toBeFalsy ( ) ;
2095+ expect ( placeholder . classList ) . not . toContain ( 'mat-floating-placeholder' ) ;
2096+ expect ( trigger . textContent ) . not . toContain ( 'Null' ) ;
2097+ expect ( trigger . textContent ) . not . toContain ( 'Undefined' ) ;
2098+ } ) ;
2099+
2100+ } ) ;
2101+
20252102} ) ;
20262103
20272104
@@ -2366,3 +2443,29 @@ class BasicSelectWithTheming {
23662443 @ViewChild ( MdSelect ) select : MdSelect ;
23672444 theme : string ;
23682445}
2446+
2447+ @Component ( {
2448+ selector : 'reset-values-select' ,
2449+ template : `
2450+ <md-select placeholder="Food" [formControl]="control">
2451+ <md-option *ngFor="let food of foods" [value]="food.value">
2452+ {{ food.viewValue }}
2453+ </md-option>
2454+
2455+ <md-option>None</md-option>
2456+ </md-select>
2457+ `
2458+ } )
2459+ class ResetValuesSelect {
2460+ foods : any [ ] = [
2461+ { value : 'steak-0' , viewValue : 'Steak' } ,
2462+ { value : 'pizza-1' , viewValue : 'Pizza' } ,
2463+ { value : 'tacos-2' , viewValue : 'Tacos' } ,
2464+ { value : false , viewValue : 'Falsy' } ,
2465+ { viewValue : 'Undefined' } ,
2466+ { value : null , viewValue : 'Null' } ,
2467+ ] ;
2468+ control = new FormControl ( ) ;
2469+
2470+ @ViewChild ( MdSelect ) select : MdSelect ;
2471+ }
0 commit comments