@@ -145,7 +145,7 @@ describe('radio harness', () => {
145145 describe ( 'MatRadioButtonHarness' , ( ) => {
146146 it ( 'should load all radio-button harnesses' , async ( ) => {
147147 const radios = await loader . getAllHarnesses ( MatRadioButtonHarness ) ;
148- expect ( radios . length ) . toBe ( 9 ) ;
148+ expect ( radios . length ) . toBe ( 10 ) ;
149149 } ) ;
150150
151151 it ( 'should load radio-button with exact label' , async ( ) => {
@@ -267,10 +267,25 @@ describe('radio harness', () => {
267267 expect ( await radioButton . isChecked ( ) ) . toBe ( true ) ;
268268 } ) ;
269269
270- it ( 'should get required state' , async ( ) => {
270+ // radios with group should not contain required attribute as group itself is marked if its
271+ // required or not, see #30399
272+ it ( 'should have falsy required state if used with MatRadioGroup' , async ( ) => {
273+ const radioButton = await loader . getHarness (
274+ MatRadioButtonHarness . with ( { selector : '#required-radio-inside-group' } ) ,
275+ ) ;
276+ expect ( await radioButton . isRequired ( ) ) . toBe ( false ) ;
277+ } ) ;
278+
279+ it ( 'should set required state of radio without group' , async ( ) => {
271280 const radioButton = await loader . getHarness (
272281 MatRadioButtonHarness . with ( { selector : '#required-radio' } ) ,
273282 ) ;
283+ expect ( await radioButton . isRequired ( ) ) . toBe ( false ) ;
284+
285+ fixture . componentInstance . standaloneRequiredRadio = true ;
286+ fixture . changeDetectorRef . markForCheck ( ) ;
287+ fixture . detectChanges ( ) ;
288+
274289 expect ( await radioButton . isRequired ( ) ) . toBe ( true ) ;
275290 } ) ;
276291 } ) ;
@@ -302,11 +317,15 @@ describe('radio harness', () => {
302317
303318
304319 <mat-radio-group [id]="secondGroupId" [name]="secondGroupId + '-name'">
305- <mat-radio-button id="required-radio" required [value]="true">
320+ <mat-radio-button id="required-radio-inside-group " required [value]="true">
306321 Accept terms of conditions
307322 </mat-radio-button>
308323 </mat-radio-group>
309324
325+ <mat-radio-button id="required-radio" [required]="standaloneRequiredRadio" [value]="true">
326+ Accept terms of conditions
327+ </mat-radio-button>
328+
310329 <mat-radio-group [name]="thirdGroupName">
311330 <mat-radio-button [value]="true">First</mat-radio-button>
312331 <mat-radio-button [value]="false" [name]="thirdGroupButtonName"></mat-radio-button>
@@ -321,4 +340,5 @@ class MultipleRadioButtonsHarnessTest {
321340 secondGroupId = 'my-group-2' ;
322341 thirdGroupName : string = 'third-group-name' ;
323342 thirdGroupButtonName : string | undefined = undefined ;
343+ standaloneRequiredRadio = false ;
324344}
0 commit comments