@@ -349,6 +349,48 @@ describe('MatMdcInput without forms', () => {
349349 expect ( label . nativeElement . querySelector ( '.mat-mdc-form-field-required-marker' ) ) . toBeTruthy ( ) ;
350350 } ) ) ;
351351
352+ it ( 'should show the required star when FormControl is reassigned' , fakeAsync ( ( ) => {
353+ const fixture = createComponent ( MatInputWithRequiredAssignableFormControl ) ;
354+ fixture . detectChanges ( ) ;
355+
356+ // should have star by default
357+ let label = fixture . debugElement . query ( By . css ( 'label' ) ) ! ;
358+ expect ( label . nativeElement . querySelector ( '.mat-mdc-form-field-required-marker' ) ) . toBeTruthy ( ) ;
359+
360+ fixture . componentInstance . reassignFormControl ( ) ;
361+ fixture . changeDetectorRef . markForCheck ( ) ;
362+ fixture . detectChanges ( ) ;
363+
364+ // should be removed as form was reassigned with no required validators
365+ label = fixture . debugElement . query ( By . css ( 'label' ) ) ! ;
366+ expect ( label . nativeElement . querySelector ( '.mat-mdc-form-field-required-marker' ) ) . toBeFalsy ( ) ;
367+ } ) ) ;
368+
369+ it ( 'should show the required star when required validator is toggled' , fakeAsync ( ( ) => {
370+ const fixture = createComponent ( MatInputWithRequiredAssignableFormControl ) ;
371+ fixture . detectChanges ( ) ;
372+
373+ // should have star by default
374+ let label = fixture . debugElement . query ( By . css ( 'label' ) ) ! ;
375+ expect ( label . nativeElement . querySelector ( '.mat-mdc-form-field-required-marker' ) ) . toBeTruthy ( ) ;
376+
377+ fixture . componentInstance . removeRequiredValidtor ( ) ;
378+ fixture . changeDetectorRef . markForCheck ( ) ;
379+ fixture . detectChanges ( ) ;
380+
381+ // should be removed as control validator was removed
382+ label = fixture . debugElement . query ( By . css ( 'label' ) ) ! ;
383+ expect ( label . nativeElement . querySelector ( '.mat-mdc-form-field-required-marker' ) ) . toBeFalsy ( ) ;
384+
385+ fixture . componentInstance . addRequiredValidator ( ) ;
386+ fixture . changeDetectorRef . markForCheck ( ) ;
387+ fixture . detectChanges ( ) ;
388+
389+ // should contain star as control validator was readded
390+ label = fixture . debugElement . query ( By . css ( 'label' ) ) ! ;
391+ expect ( label . nativeElement . querySelector ( '.mat-mdc-form-field-required-marker' ) ) . toBeTruthy ( ) ;
392+ } ) ) ;
393+
352394 it ( 'should not hide the required star if input is disabled' , ( ) => {
353395 const fixture = createComponent ( MatInputLabelRequiredTestComponent ) ;
354396
@@ -2333,3 +2375,29 @@ class MatInputSimple {}
23332375 standalone : false ,
23342376} )
23352377class InputWithNgContainerPrefixAndSuffix { }
2378+
2379+ @Component ( {
2380+ template : `
2381+ <mat-form-field>
2382+ <mat-label>Hello</mat-label>
2383+ <input matInput [formControl]="formControl">
2384+ </mat-form-field>` ,
2385+ standalone : false ,
2386+ } )
2387+ class MatInputWithRequiredAssignableFormControl {
2388+ formControl = new FormControl ( '' , [ Validators . required ] ) ;
2389+
2390+ reassignFormControl ( ) {
2391+ this . formControl = new FormControl ( ) ;
2392+ }
2393+
2394+ addRequiredValidator ( ) {
2395+ this . formControl . setValidators ( [ Validators . required ] ) ;
2396+ this . formControl . updateValueAndValidity ( ) ;
2397+ }
2398+
2399+ removeRequiredValidtor ( ) {
2400+ this . formControl . setValidators ( [ ] ) ;
2401+ this . formControl . updateValueAndValidity ( ) ;
2402+ }
2403+ }
0 commit comments