@@ -349,6 +349,48 @@ describe('MatMdcInput without forms', () => {
349
349
expect ( label . nativeElement . querySelector ( '.mat-mdc-form-field-required-marker' ) ) . toBeTruthy ( ) ;
350
350
} ) ) ;
351
351
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
+
352
394
it ( 'should not hide the required star if input is disabled' , ( ) => {
353
395
const fixture = createComponent ( MatInputLabelRequiredTestComponent ) ;
354
396
@@ -2324,3 +2366,29 @@ class MatInputSimple {}
2324
2366
standalone : false ,
2325
2367
} )
2326
2368
class InputWithNgContainerPrefixAndSuffix { }
2369
+
2370
+ @Component ( {
2371
+ template : `
2372
+ <mat-form-field>
2373
+ <mat-label>Hello</mat-label>
2374
+ <input matInput [formControl]="formControl">
2375
+ </mat-form-field>` ,
2376
+ standalone : false ,
2377
+ } )
2378
+ class MatInputWithRequiredAssignableFormControl {
2379
+ formControl = new FormControl ( '' , [ Validators . required ] ) ;
2380
+
2381
+ reassignFormControl ( ) {
2382
+ this . formControl = new FormControl ( ) ;
2383
+ }
2384
+
2385
+ addRequiredValidator ( ) {
2386
+ this . formControl . setValidators ( [ Validators . required ] ) ;
2387
+ this . formControl . updateValueAndValidity ( ) ;
2388
+ }
2389
+
2390
+ removeRequiredValidtor ( ) {
2391
+ this . formControl . setValidators ( [ ] ) ;
2392
+ this . formControl . updateValueAndValidity ( ) ;
2393
+ }
2394
+ }
0 commit comments