@@ -2897,6 +2897,44 @@ describe('MatSelect', () => {
2897
2897
} ) ) ;
2898
2898
} ) ;
2899
2899
2900
+ describe ( 'with reset option and a form control' , ( ) => {
2901
+ let fixture : ComponentFixture < SelectWithResetOptionAndFormControl > ;
2902
+ let options : HTMLElement [ ] ;
2903
+
2904
+ beforeEach ( fakeAsync ( ( ) => {
2905
+ configureMatSelectTestingModule ( [ SelectWithResetOptionAndFormControl ] ) ;
2906
+ fixture = TestBed . createComponent ( SelectWithResetOptionAndFormControl ) ;
2907
+ fixture . detectChanges ( ) ;
2908
+ fixture . debugElement . query ( By . css ( '.mat-select-trigger' ) ) ! . nativeElement . click ( ) ;
2909
+ fixture . detectChanges ( ) ;
2910
+ options = Array . from ( overlayContainerElement . querySelectorAll ( 'mat-option' ) ) ;
2911
+ } ) ) ;
2912
+
2913
+ it ( 'should set the select value' , fakeAsync ( ( ) => {
2914
+ fixture . componentInstance . control . setValue ( 'a' ) ;
2915
+ fixture . detectChanges ( ) ;
2916
+ expect ( fixture . componentInstance . select . value ) . toBe ( 'a' ) ;
2917
+ } ) ) ;
2918
+
2919
+ it ( 'should reset the control value' , fakeAsync ( ( ) => {
2920
+ fixture . componentInstance . control . setValue ( 'a' ) ;
2921
+ fixture . detectChanges ( ) ;
2922
+
2923
+ options [ 0 ] . click ( ) ;
2924
+ fixture . detectChanges ( ) ;
2925
+ flush ( ) ;
2926
+ expect ( fixture . componentInstance . control . value ) . toBe ( undefined ) ;
2927
+ } ) ) ;
2928
+
2929
+ it ( 'should reflect the value in the form control' , fakeAsync ( ( ) => {
2930
+ options [ 1 ] . click ( ) ;
2931
+ fixture . detectChanges ( ) ;
2932
+ flush ( ) ;
2933
+ expect ( fixture . componentInstance . select . value ) . toBe ( 'a' ) ;
2934
+ expect ( fixture . componentInstance . control . value ) . toBe ( 'a' ) ;
2935
+ } ) ) ;
2936
+ } ) ;
2937
+
2900
2938
describe ( 'without Angular forms' , ( ) => {
2901
2939
beforeEach ( async ( ( ) => configureMatSelectTestingModule ( [
2902
2940
BasicSelectWithoutForms ,
@@ -3174,6 +3212,63 @@ describe('MatSelect', () => {
3174
3212
. toBeFalsy ( 'Expected no value after tabbing away.' ) ;
3175
3213
} ) ) ;
3176
3214
3215
+ it ( 'should emit once when a reset value is selected' , fakeAsync ( ( ) => {
3216
+ const fixture = TestBed . createComponent ( BasicSelectWithoutForms ) ;
3217
+ const instance = fixture . componentInstance ;
3218
+ const spy = jasmine . createSpy ( 'change spy' ) ;
3219
+
3220
+ instance . selectedFood = 'sandwich-2' ;
3221
+ instance . foods [ 0 ] . value = null ;
3222
+ fixture . detectChanges ( ) ;
3223
+
3224
+ const subscription = instance . select . selectionChange . subscribe ( spy ) ;
3225
+
3226
+ fixture . debugElement . query ( By . css ( '.mat-select-trigger' ) ) . nativeElement . click ( ) ;
3227
+ fixture . detectChanges ( ) ;
3228
+ flush ( ) ;
3229
+
3230
+ ( overlayContainerElement . querySelector ( 'mat-option' ) as HTMLElement ) . click ( ) ;
3231
+ fixture . detectChanges ( ) ;
3232
+ flush ( ) ;
3233
+
3234
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
3235
+
3236
+ subscription . unsubscribe ( ) ;
3237
+ } ) ) ;
3238
+
3239
+ it ( 'should not emit the change event multiple times when a reset option is ' +
3240
+ 'selected twice in a row' , fakeAsync ( ( ) => {
3241
+ const fixture = TestBed . createComponent ( BasicSelectWithoutForms ) ;
3242
+ const instance = fixture . componentInstance ;
3243
+ const spy = jasmine . createSpy ( 'change spy' ) ;
3244
+
3245
+ instance . foods [ 0 ] . value = null ;
3246
+ fixture . detectChanges ( ) ;
3247
+
3248
+ const subscription = instance . select . selectionChange . subscribe ( spy ) ;
3249
+
3250
+ fixture . debugElement . query ( By . css ( '.mat-select-trigger' ) ) . nativeElement . click ( ) ;
3251
+ fixture . detectChanges ( ) ;
3252
+ flush ( ) ;
3253
+
3254
+ ( overlayContainerElement . querySelector ( 'mat-option' ) as HTMLElement ) . click ( ) ;
3255
+ fixture . detectChanges ( ) ;
3256
+ flush ( ) ;
3257
+
3258
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
3259
+
3260
+ fixture . debugElement . query ( By . css ( '.mat-select-trigger' ) ) . nativeElement . click ( ) ;
3261
+ fixture . detectChanges ( ) ;
3262
+ flush ( ) ;
3263
+
3264
+ ( overlayContainerElement . querySelector ( 'mat-option' ) as HTMLElement ) . click ( ) ;
3265
+ fixture . detectChanges ( ) ;
3266
+ flush ( ) ;
3267
+
3268
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
3269
+
3270
+ subscription . unsubscribe ( ) ;
3271
+ } ) ) ;
3177
3272
3178
3273
} ) ;
3179
3274
@@ -5302,3 +5397,23 @@ class MultiSelectWithLotsOfOptions {
5302
5397
this . value = [ ] ;
5303
5398
}
5304
5399
}
5400
+
5401
+
5402
+ @Component ( {
5403
+ selector : 'basic-select-with-reset' ,
5404
+ template : `
5405
+ <mat-form-field>
5406
+ <mat-select [formControl]="control">
5407
+ <mat-option>Reset</mat-option>
5408
+ <mat-option value="a">A</mat-option>
5409
+ <mat-option value="b">B</mat-option>
5410
+ <mat-option value="c">C</mat-option>
5411
+ </mat-select>
5412
+ </mat-form-field>
5413
+ `
5414
+ } )
5415
+ class SelectWithResetOptionAndFormControl {
5416
+ @ViewChild ( MatSelect ) select : MatSelect ;
5417
+ @ViewChildren ( MatOption ) options : QueryList < MatOption > ;
5418
+ control = new FormControl ( ) ;
5419
+ }
0 commit comments