Search code examples
angularunit-testingjestjsform-control

Testing FormControl under Angular invalid and valid


ts:
budget = new FormControl(null, [Validators.required]);

html:

<input
    id="budget"
    currencyMask
    placeholder="Dollar"
    [ngModel]="modelService.budget"
    [formControl]="budget"
    value="{{ modelService.budget}}"
    [min]="1.0"
    [options]="{decimal: ',', thousands: '.', allowNegative: false }"
    type="text"
/>

spec:

it('should not valid', fakeAsync(() => {
    component.ngOnInit();
    component.budget.setValue(0);
    tick();
    fixture.detectChanges();
    expect(component.budget.valid).toBe(false);
}));

my test ist always true. How can i patch my budget Value and control to my valid. 0 -> should invalid 10 -> should valid


Solution

  • The min attribute on the input element doesn't make the FormControl invalid. Please add the min validator.

    budget = new FormControl(null, [Validators.required, Validators.min(1.0)]);