I am testing this html form:
<input #nhcInput type="text" class="form-control" name="nhc" id="field_nhc"
[(ngModel)]="paciente.nhc" maxlength="38" pattern="[0-9]+"/>
<div [hidden]="!(editForm.controls.nhc?.dirty && editForm.controls.nhc?.invalid)">
<small class="form-text text-danger"
[hidden]="!editForm.controls.nhc?.errors?.maxlength" jhiTranslate="entity.validation.maxlength" translateValues="{ max: 38 }">
This field cannot be longer than 38 characters.
</small>
TEH RESULT {{nhcInput.className}} //This line prints ng-valid/ dirty, touched correctly
I have this in my component:
paciente: Paciente = {nhc: '23423' } as Paciente;
it ('NHC cannot have more than 38 characters', async(() => {
comp.paciente.nhc = 'rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr' ;
console.log(fixture.nativeElement.querySelector('input[name="nhc"]').className);
fixture.detectChanges();
expect(fixture.nativeElement.querySelector('input[name="nhc"]').className.includes('ng-invalid')).toEqual(true);
}));
Now I want to test the validity by checking the vaidator. The console.log prints out only form-control without the type of validator, since it is not finding it.
I put a validator for this fild like this in my component:
@ViewChild('editForm') editForm: any;
editform.controls["nhc"].setValidators([ Validators.maxLength(38)]);
But this doesnt work. Am I doing soemthing wrong here?
Thanks!