@@ -56,7 +56,8 @@ describe('MdSelect', () => {
56
56
SelectEarlyAccessSibling ,
57
57
BasicSelectInitiallyHidden ,
58
58
BasicSelectNoPlaceholder ,
59
- BasicSelectWithTheming
59
+ BasicSelectWithTheming ,
60
+ ResetValuesSelect
60
61
] ,
61
62
providers : [
62
63
{ provide : OverlayContainer , useFactory : ( ) => {
@@ -1995,6 +1996,72 @@ describe('MdSelect', () => {
1995
1996
1996
1997
} ) ;
1997
1998
1999
+
2000
+ describe ( 'reset values' , ( ) => {
2001
+ let fixture : ComponentFixture < ResetValuesSelect > ;
2002
+ let trigger : HTMLElement ;
2003
+ let placeholder : HTMLElement ;
2004
+ let options : NodeListOf < HTMLElement > ;
2005
+
2006
+ beforeEach ( ( ) => {
2007
+ fixture = TestBed . createComponent ( ResetValuesSelect ) ;
2008
+ fixture . detectChanges ( ) ;
2009
+ trigger = fixture . debugElement . query ( By . css ( '.mat-select-trigger' ) ) . nativeElement ;
2010
+ placeholder = fixture . debugElement . query ( By . css ( '.mat-select-placeholder' ) ) . nativeElement ;
2011
+
2012
+ trigger . click ( ) ;
2013
+ fixture . detectChanges ( ) ;
2014
+ options = overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
2015
+
2016
+ options [ 0 ] . click ( ) ;
2017
+ fixture . detectChanges ( ) ;
2018
+ } ) ;
2019
+
2020
+ it ( 'should reset when an option with an undefined value is selected' , ( ) => {
2021
+ options [ 4 ] . click ( ) ;
2022
+ fixture . detectChanges ( ) ;
2023
+
2024
+ expect ( fixture . componentInstance . control . value ) . toBeUndefined ( ) ;
2025
+ expect ( fixture . componentInstance . select . selected ) . toBeFalsy ( ) ;
2026
+ expect ( placeholder . classList ) . not . toContain ( 'mat-floating-placeholder' ) ;
2027
+ expect ( trigger . textContent ) . not . toContain ( 'Undefined' ) ;
2028
+ } ) ;
2029
+
2030
+ it ( 'should reset when an option with a null value is selected' , ( ) => {
2031
+ options [ 5 ] . click ( ) ;
2032
+ fixture . detectChanges ( ) ;
2033
+
2034
+ expect ( fixture . componentInstance . control . value ) . toBeNull ( ) ;
2035
+ expect ( fixture . componentInstance . select . selected ) . toBeFalsy ( ) ;
2036
+ expect ( placeholder . classList ) . not . toContain ( 'mat-floating-placeholder' ) ;
2037
+ expect ( trigger . textContent ) . not . toContain ( 'Null' ) ;
2038
+ } ) ;
2039
+
2040
+ it ( 'should not reset when any other falsy option is selected' , ( ) => {
2041
+ options [ 3 ] . click ( ) ;
2042
+ fixture . detectChanges ( ) ;
2043
+
2044
+ expect ( fixture . componentInstance . control . value ) . toBe ( false ) ;
2045
+ expect ( fixture . componentInstance . select . selected ) . toBeTruthy ( ) ;
2046
+ expect ( placeholder . classList ) . toContain ( 'mat-floating-placeholder' ) ;
2047
+ expect ( trigger . textContent ) . toContain ( 'Falsy' ) ;
2048
+ } ) ;
2049
+
2050
+ it ( 'should not consider the reset values as selected when resetting the form control' , ( ) => {
2051
+ expect ( placeholder . classList ) . toContain ( 'mat-floating-placeholder' ) ;
2052
+
2053
+ fixture . componentInstance . control . reset ( ) ;
2054
+ fixture . detectChanges ( ) ;
2055
+
2056
+ expect ( fixture . componentInstance . control . value ) . toBeNull ( ) ;
2057
+ expect ( fixture . componentInstance . select . selected ) . toBeFalsy ( ) ;
2058
+ expect ( placeholder . classList ) . not . toContain ( 'mat-floating-placeholder' ) ;
2059
+ expect ( trigger . textContent ) . not . toContain ( 'Null' ) ;
2060
+ expect ( trigger . textContent ) . not . toContain ( 'Undefined' ) ;
2061
+ } ) ;
2062
+
2063
+ } ) ;
2064
+
1998
2065
} ) ;
1999
2066
2000
2067
@@ -2339,3 +2406,29 @@ class BasicSelectWithTheming {
2339
2406
@ViewChild ( MdSelect ) select : MdSelect ;
2340
2407
theme : string ;
2341
2408
}
2409
+
2410
+ @Component ( {
2411
+ selector : 'reset-values-select' ,
2412
+ template : `
2413
+ <md-select placeholder="Food" [formControl]="control" [required]="isRequired">
2414
+ <md-option *ngFor="let food of foods" [value]="food.value">
2415
+ {{ food.viewValue }}
2416
+ </md-option>
2417
+ </md-select>
2418
+ `
2419
+ } )
2420
+ class ResetValuesSelect {
2421
+ foods : any [ ] = [
2422
+ { value : 'steak-0' , viewValue : 'Steak' } ,
2423
+ { value : 'pizza-1' , viewValue : 'Pizza' } ,
2424
+ { value : 'tacos-2' , viewValue : 'Tacos' } ,
2425
+ { value : false , viewValue : 'Falsy' } ,
2426
+ { viewValue : 'Undefined' } ,
2427
+ { value : null , viewValue : 'Null' } ,
2428
+ ] ;
2429
+ control = new FormControl ( ) ;
2430
+ isRequired : boolean ;
2431
+
2432
+ @ViewChild ( MdSelect ) select : MdSelect ;
2433
+ @ViewChildren ( MdOption ) options : QueryList < MdOption > ;
2434
+ }
0 commit comments