@@ -19,6 +19,7 @@ describe('MatButtonToggle with forms', () => {
19
19
ButtonToggleGroupWithNgModel ,
20
20
ButtonToggleGroupWithFormControl ,
21
21
ButtonToggleGroupWithIndirectDescendantToggles ,
22
+ ButtonToggleGroupWithFormControlAndDynamicButtons ,
22
23
] ,
23
24
} ) ;
24
25
@@ -265,6 +266,45 @@ describe('MatButtonToggle with forms', () => {
265
266
expect ( fixture . componentInstance . control . value ) . toBe ( 'red' ) ;
266
267
expect ( groupInstance . _buttonToggles . length ) . toBe ( 3 ) ;
267
268
} ) ) ;
269
+
270
+ it ( 'should preserve the selection if the pre-selected option is removed and re-added' , ( ) => {
271
+ const fixture = TestBed . createComponent ( ButtonToggleGroupWithFormControlAndDynamicButtons ) ;
272
+ const instance = fixture . componentInstance ;
273
+ instance . control . setValue ( 'a' ) ;
274
+ fixture . detectChanges ( ) ;
275
+ const buttons = fixture . nativeElement . querySelectorAll ( '.mat-button-toggle-button' ) ;
276
+
277
+ expect ( instance . toggles . map ( t => t . checked ) ) . toEqual ( [ true , false , false ] ) ;
278
+
279
+ buttons [ 2 ] . click ( ) ;
280
+ fixture . detectChanges ( ) ;
281
+
282
+ instance . values . shift ( ) ;
283
+ fixture . detectChanges ( ) ;
284
+ expect ( instance . toggles . map ( t => t . checked ) ) . toEqual ( [ false , true ] ) ;
285
+
286
+ instance . values . unshift ( 'a' ) ;
287
+ fixture . detectChanges ( ) ;
288
+
289
+ expect ( instance . toggles . map ( t => t . checked ) ) . toEqual ( [ false , false , true ] ) ;
290
+ } ) ;
291
+
292
+ it ( 'should preserve the pre-selected option if it removed and re-added' , ( ) => {
293
+ const fixture = TestBed . createComponent ( ButtonToggleGroupWithFormControlAndDynamicButtons ) ;
294
+ const instance = fixture . componentInstance ;
295
+ instance . control . setValue ( 'a' ) ;
296
+ fixture . detectChanges ( ) ;
297
+ expect ( instance . toggles . map ( t => t . checked ) ) . toEqual ( [ true , false , false ] ) ;
298
+
299
+ instance . values . shift ( ) ;
300
+ fixture . detectChanges ( ) ;
301
+ expect ( instance . toggles . map ( t => t . checked ) ) . toEqual ( [ false , false ] ) ;
302
+
303
+ instance . values . unshift ( 'a' ) ;
304
+ fixture . detectChanges ( ) ;
305
+
306
+ expect ( instance . toggles . map ( t => t . checked ) ) . toEqual ( [ true , false , false ] ) ;
307
+ } ) ;
268
308
} ) ;
269
309
270
310
describe ( 'MatButtonToggle without forms' , ( ) => {
@@ -1097,3 +1137,16 @@ class ButtonToggleWithStaticChecked {
1097
1137
` ,
1098
1138
} )
1099
1139
class ButtonToggleWithStaticAriaAttributes { }
1140
+
1141
+ @Component ( {
1142
+ template : `
1143
+ <mat-button-toggle-group [formControl]="control">
1144
+ <mat-button-toggle *ngFor="let value of values" [value]="value">{{value}}</mat-button-toggle>
1145
+ </mat-button-toggle-group>
1146
+ ` ,
1147
+ } )
1148
+ class ButtonToggleGroupWithFormControlAndDynamicButtons {
1149
+ @ViewChildren ( MatButtonToggle ) toggles : QueryList < MatButtonToggle > ;
1150
+ control = new FormControl ( '' ) ;
1151
+ values = [ 'a' , 'b' , 'c' ] ;
1152
+ }
0 commit comments