@@ -79,7 +79,7 @@ describe('MatButtonToggle with forms', () => {
7979 let buttonToggleInstances : MatButtonToggle [ ] ;
8080 let testComponent : ButtonToggleGroupWithNgModel ;
8181 let groupNgModel : NgModel ;
82- let buttonToggleLabels : HTMLElement [ ] ;
82+ let innerButtons : HTMLElement [ ] ;
8383
8484 beforeEach ( fakeAsync ( ( ) => {
8585 fixture = TestBed . createComponent ( ButtonToggleGroupWithNgModel ) ;
@@ -92,7 +92,7 @@ describe('MatButtonToggle with forms', () => {
9292
9393 buttonToggleDebugElements = fixture . debugElement . queryAll ( By . directive ( MatButtonToggle ) ) ;
9494 buttonToggleInstances = buttonToggleDebugElements . map ( debugEl => debugEl . componentInstance ) ;
95- buttonToggleLabels = buttonToggleDebugElements . map (
95+ innerButtons = buttonToggleDebugElements . map (
9696 debugEl => debugEl . query ( By . css ( 'button' ) ) . nativeElement ) ;
9797
9898 fixture . detectChanges ( ) ;
@@ -102,7 +102,7 @@ describe('MatButtonToggle with forms', () => {
102102 expect ( testComponent . modelValue ) . toBeUndefined ( ) ;
103103 expect ( testComponent . lastEvent ) . toBeUndefined ( ) ;
104104
105- buttonToggleLabels [ 0 ] . click ( ) ;
105+ innerButtons [ 0 ] . click ( ) ;
106106 fixture . detectChanges ( ) ;
107107
108108 tick ( ) ;
@@ -122,6 +122,18 @@ describe('MatButtonToggle with forms', () => {
122122 }
123123 } ) ;
124124
125+ it ( 'should update the name of radio DOM elements if the name of the group changes' , ( ) => {
126+ expect ( innerButtons . every ( button => button . getAttribute ( 'name' ) === groupInstance . name ) )
127+ . toBe ( true , 'Expected all buttons to have the initial name.' ) ;
128+
129+ fixture . componentInstance . groupName = 'changed-name' ;
130+ fixture . detectChanges ( ) ;
131+
132+ expect ( groupInstance . name ) . toBe ( 'changed-name' ) ;
133+ expect ( innerButtons . every ( button => button . getAttribute ( 'name' ) === groupInstance . name ) )
134+ . toBe ( true , 'Expected all buttons to have the new name.' ) ;
135+ } ) ;
136+
125137 it ( 'should check the corresponding button toggle on a group value change' , ( ) => {
126138 expect ( groupInstance . value ) . toBeFalsy ( ) ;
127139 for ( let buttonToggle of buttonToggleInstances ) {
@@ -152,7 +164,7 @@ describe('MatButtonToggle with forms', () => {
152164 expect ( groupNgModel . pristine ) . toBe ( true ) ;
153165 expect ( groupNgModel . touched ) . toBe ( false ) ;
154166
155- buttonToggleLabels [ 2 ] . click ( ) ;
167+ innerButtons [ 2 ] . click ( ) ;
156168 fixture . detectChanges ( ) ;
157169 tick ( ) ;
158170
@@ -162,7 +174,7 @@ describe('MatButtonToggle with forms', () => {
162174 } ) ) ;
163175
164176 it ( 'should update the ngModel value when selecting a button toggle' , fakeAsync ( ( ) => {
165- buttonToggleLabels [ 1 ] . click ( ) ;
177+ innerButtons [ 1 ] . click ( ) ;
166178 fixture . detectChanges ( ) ;
167179
168180 tick ( ) ;
@@ -175,8 +187,8 @@ describe('MatButtonToggle with forms', () => {
175187
176188 expect ( groupElement . querySelectorAll ( '.mat-ripple-element' ) . length ) . toBe ( 0 ) ;
177189
178- dispatchMouseEvent ( buttonToggleLabels [ 0 ] , 'mousedown' ) ;
179- dispatchMouseEvent ( buttonToggleLabels [ 0 ] , 'mouseup' ) ;
190+ dispatchMouseEvent ( innerButtons [ 0 ] , 'mousedown' ) ;
191+ dispatchMouseEvent ( innerButtons [ 0 ] , 'mouseup' ) ;
180192
181193 expect ( groupElement . querySelectorAll ( '.mat-ripple-element' ) . length ) . toBe ( 1 ) ;
182194 } ) ;
@@ -189,8 +201,8 @@ describe('MatButtonToggle with forms', () => {
189201
190202 expect ( groupElement . querySelectorAll ( '.mat-ripple-element' ) . length ) . toBe ( 0 ) ;
191203
192- dispatchMouseEvent ( buttonToggleLabels [ 0 ] , 'mousedown' ) ;
193- dispatchMouseEvent ( buttonToggleLabels [ 0 ] , 'mouseup' ) ;
204+ dispatchMouseEvent ( innerButtons [ 0 ] , 'mousedown' ) ;
205+ dispatchMouseEvent ( innerButtons [ 0 ] , 'mouseup' ) ;
194206
195207 expect ( groupElement . querySelectorAll ( '.mat-ripple-element' ) . length ) . toBe ( 0 ) ;
196208 } ) ;
@@ -819,7 +831,10 @@ class ButtonTogglesInsideButtonToggleGroup {
819831
820832@Component ( {
821833 template : `
822- <mat-button-toggle-group [(ngModel)]="modelValue" (change)="lastEvent = $event">
834+ <mat-button-toggle-group
835+ [name]="groupName"
836+ [(ngModel)]="modelValue"
837+ (change)="lastEvent = $event">
823838 <mat-button-toggle *ngFor="let option of options" [value]="option.value"
824839 [disableRipple]="disableRipple">
825840 {{option.label}}
@@ -828,6 +843,7 @@ class ButtonTogglesInsideButtonToggleGroup {
828843 `
829844} )
830845class ButtonToggleGroupWithNgModel {
846+ groupName = 'group-name' ;
831847 modelValue : string ;
832848 options = [
833849 { label : 'Red' , value : 'red' } ,
0 commit comments