@@ -348,6 +348,48 @@ describe('MatMdcInput without forms', () => {
348
348
expect ( label . nativeElement . querySelector ( '.mat-mdc-form-field-required-marker' ) ) . toBeTruthy ( ) ;
349
349
} ) ) ;
350
350
351
+ it ( 'should show the required star when FormControl is reassigned' , fakeAsync ( ( ) => {
352
+ const fixture = createComponent ( MatInputWithRequiredAssignableFormControl ) ;
353
+ fixture . detectChanges ( ) ;
354
+
355
+ // should have star by default
356
+ let label = fixture . debugElement . query ( By . css ( 'label' ) ) ! ;
357
+ expect ( label . nativeElement . querySelector ( '.mat-mdc-form-field-required-marker' ) ) . toBeTruthy ( ) ;
358
+
359
+ fixture . componentInstance . reassignFormControl ( ) ;
360
+ fixture . changeDetectorRef . markForCheck ( ) ;
361
+ fixture . detectChanges ( ) ;
362
+
363
+ // should be removed as form was reassigned with no required validators
364
+ label = fixture . debugElement . query ( By . css ( 'label' ) ) ! ;
365
+ expect ( label . nativeElement . querySelector ( '.mat-mdc-form-field-required-marker' ) ) . toBeFalsy ( ) ;
366
+ } ) ) ;
367
+
368
+ it ( 'should show the required star when required validator is toggled' , fakeAsync ( ( ) => {
369
+ const fixture = createComponent ( MatInputWithRequiredAssignableFormControl ) ;
370
+ fixture . detectChanges ( ) ;
371
+
372
+ // should have star by default
373
+ let label = fixture . debugElement . query ( By . css ( 'label' ) ) ! ;
374
+ expect ( label . nativeElement . querySelector ( '.mat-mdc-form-field-required-marker' ) ) . toBeTruthy ( ) ;
375
+
376
+ fixture . componentInstance . removeRequiredValidtor ( ) ;
377
+ fixture . changeDetectorRef . markForCheck ( ) ;
378
+ fixture . detectChanges ( ) ;
379
+
380
+ // should be removed as control validator was removed
381
+ label = fixture . debugElement . query ( By . css ( 'label' ) ) ! ;
382
+ expect ( label . nativeElement . querySelector ( '.mat-mdc-form-field-required-marker' ) ) . toBeFalsy ( ) ;
383
+
384
+ fixture . componentInstance . addRequiredValidator ( ) ;
385
+ fixture . changeDetectorRef . markForCheck ( ) ;
386
+ fixture . detectChanges ( ) ;
387
+
388
+ // should contain star as control validator was readded
389
+ label = fixture . debugElement . query ( By . css ( 'label' ) ) ! ;
390
+ expect ( label . nativeElement . querySelector ( '.mat-mdc-form-field-required-marker' ) ) . toBeTruthy ( ) ;
391
+ } ) ) ;
392
+
351
393
it ( 'should not hide the required star if input is disabled' , ( ) => {
352
394
const fixture = createComponent ( MatInputLabelRequiredTestComponent ) ;
353
395
@@ -2321,3 +2363,29 @@ class MatInputSimple {}
2321
2363
standalone : false ,
2322
2364
} )
2323
2365
class InputWithNgContainerPrefixAndSuffix { }
2366
+
2367
+ @Component ( {
2368
+ template : `
2369
+ <mat-form-field>
2370
+ <mat-label>Hello</mat-label>
2371
+ <input matInput [formControl]="formControl">
2372
+ </mat-form-field>` ,
2373
+ standalone : false ,
2374
+ } )
2375
+ class MatInputWithRequiredAssignableFormControl {
2376
+ formControl = new FormControl ( '' , [ Validators . required ] ) ;
2377
+
2378
+ reassignFormControl ( ) {
2379
+ this . formControl = new FormControl ( ) ;
2380
+ }
2381
+
2382
+ addRequiredValidator ( ) {
2383
+ this . formControl . setValidators ( [ Validators . required ] ) ;
2384
+ this . formControl . updateValueAndValidity ( ) ;
2385
+ }
2386
+
2387
+ removeRequiredValidtor ( ) {
2388
+ this . formControl . setValidators ( [ ] ) ;
2389
+ this . formControl . updateValueAndValidity ( ) ;
2390
+ }
2391
+ }
0 commit comments