@@ -55,7 +55,8 @@ describe('MdSelect', () => {
55
55
SelectEarlyAccessSibling ,
56
56
BasicSelectInitiallyHidden ,
57
57
BasicSelectNoPlaceholder ,
58
- BasicSelectWithTheming
58
+ BasicSelectWithTheming ,
59
+ ResetValuesSelect
59
60
] ,
60
61
providers : [
61
62
{ provide : OverlayContainer , useFactory : ( ) => {
@@ -2022,6 +2023,82 @@ describe('MdSelect', () => {
2022
2023
2023
2024
} ) ;
2024
2025
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
+
2025
2102
} ) ;
2026
2103
2027
2104
@@ -2366,3 +2443,29 @@ class BasicSelectWithTheming {
2366
2443
@ViewChild ( MdSelect ) select : MdSelect ;
2367
2444
theme : string ;
2368
2445
}
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