@@ -79,7 +79,7 @@ describe('MatButtonToggle with forms', () => {
79
79
let buttonToggleInstances : MatButtonToggle [ ] ;
80
80
let testComponent : ButtonToggleGroupWithNgModel ;
81
81
let groupNgModel : NgModel ;
82
- let buttonToggleLabels : HTMLElement [ ] ;
82
+ let innerButtons : HTMLElement [ ] ;
83
83
84
84
beforeEach ( fakeAsync ( ( ) => {
85
85
fixture = TestBed . createComponent ( ButtonToggleGroupWithNgModel ) ;
@@ -92,7 +92,7 @@ describe('MatButtonToggle with forms', () => {
92
92
93
93
buttonToggleDebugElements = fixture . debugElement . queryAll ( By . directive ( MatButtonToggle ) ) ;
94
94
buttonToggleInstances = buttonToggleDebugElements . map ( debugEl => debugEl . componentInstance ) ;
95
- buttonToggleLabels = buttonToggleDebugElements . map (
95
+ innerButtons = buttonToggleDebugElements . map (
96
96
debugEl => debugEl . query ( By . css ( 'button' ) ) . nativeElement ) ;
97
97
98
98
fixture . detectChanges ( ) ;
@@ -102,7 +102,7 @@ describe('MatButtonToggle with forms', () => {
102
102
expect ( testComponent . modelValue ) . toBeUndefined ( ) ;
103
103
expect ( testComponent . lastEvent ) . toBeUndefined ( ) ;
104
104
105
- buttonToggleLabels [ 0 ] . click ( ) ;
105
+ innerButtons [ 0 ] . click ( ) ;
106
106
fixture . detectChanges ( ) ;
107
107
108
108
tick ( ) ;
@@ -122,6 +122,18 @@ describe('MatButtonToggle with forms', () => {
122
122
}
123
123
} ) ;
124
124
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
+
125
137
it ( 'should check the corresponding button toggle on a group value change' , ( ) => {
126
138
expect ( groupInstance . value ) . toBeFalsy ( ) ;
127
139
for ( let buttonToggle of buttonToggleInstances ) {
@@ -152,7 +164,7 @@ describe('MatButtonToggle with forms', () => {
152
164
expect ( groupNgModel . pristine ) . toBe ( true ) ;
153
165
expect ( groupNgModel . touched ) . toBe ( false ) ;
154
166
155
- buttonToggleLabels [ 2 ] . click ( ) ;
167
+ innerButtons [ 2 ] . click ( ) ;
156
168
fixture . detectChanges ( ) ;
157
169
tick ( ) ;
158
170
@@ -162,7 +174,7 @@ describe('MatButtonToggle with forms', () => {
162
174
} ) ) ;
163
175
164
176
it ( 'should update the ngModel value when selecting a button toggle' , fakeAsync ( ( ) => {
165
- buttonToggleLabels [ 1 ] . click ( ) ;
177
+ innerButtons [ 1 ] . click ( ) ;
166
178
fixture . detectChanges ( ) ;
167
179
168
180
tick ( ) ;
@@ -175,8 +187,8 @@ describe('MatButtonToggle with forms', () => {
175
187
176
188
expect ( groupElement . querySelectorAll ( '.mat-ripple-element' ) . length ) . toBe ( 0 ) ;
177
189
178
- dispatchMouseEvent ( buttonToggleLabels [ 0 ] , 'mousedown' ) ;
179
- dispatchMouseEvent ( buttonToggleLabels [ 0 ] , 'mouseup' ) ;
190
+ dispatchMouseEvent ( innerButtons [ 0 ] , 'mousedown' ) ;
191
+ dispatchMouseEvent ( innerButtons [ 0 ] , 'mouseup' ) ;
180
192
181
193
expect ( groupElement . querySelectorAll ( '.mat-ripple-element' ) . length ) . toBe ( 1 ) ;
182
194
} ) ;
@@ -189,8 +201,8 @@ describe('MatButtonToggle with forms', () => {
189
201
190
202
expect ( groupElement . querySelectorAll ( '.mat-ripple-element' ) . length ) . toBe ( 0 ) ;
191
203
192
- dispatchMouseEvent ( buttonToggleLabels [ 0 ] , 'mousedown' ) ;
193
- dispatchMouseEvent ( buttonToggleLabels [ 0 ] , 'mouseup' ) ;
204
+ dispatchMouseEvent ( innerButtons [ 0 ] , 'mousedown' ) ;
205
+ dispatchMouseEvent ( innerButtons [ 0 ] , 'mouseup' ) ;
194
206
195
207
expect ( groupElement . querySelectorAll ( '.mat-ripple-element' ) . length ) . toBe ( 0 ) ;
196
208
} ) ;
@@ -819,7 +831,10 @@ class ButtonTogglesInsideButtonToggleGroup {
819
831
820
832
@Component ( {
821
833
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">
823
838
<mat-button-toggle *ngFor="let option of options" [value]="option.value"
824
839
[disableRipple]="disableRipple">
825
840
{{option.label}}
@@ -828,6 +843,7 @@ class ButtonTogglesInsideButtonToggleGroup {
828
843
`
829
844
} )
830
845
class ButtonToggleGroupWithNgModel {
846
+ groupName = 'group-name' ;
831
847
modelValue : string ;
832
848
options = [
833
849
{ label : 'Red' , value : 'red' } ,
0 commit comments