Hi i made a input component, but i keep getting errors when i run my test coverage:
Chrome Headless 109.0.5414.75 (Windows 10) InputFieldComponent should create FAILED
TypeError: Cannot read properties of undefined (reading 'setValidators')
I'm having more test errors, everything works just want the tests to succeed
component.html
<div class="form-row">
<div class="form-label">
<label for="{{id}}">{{label}}</label>
</div>
<div [class.field-error]="control.touched && control.invalid" class="form-field">
<input [formControl]="control" id="{{id}}" maxlength="{{maxLength}}" name="{{id}}" type="text" >
<div *ngIf="control.touched && control.errors?.['required']" class="inline-notification error">
<span class="action-warning">{{label}} is verplicht.</span>
</div>
<div *ngIf="control.touched && control.errors?.['pattern']" class="inline-notification error">
<span class="action-warning">{{label}} moet uit minstens 1 karakter bestaan.</span>
</div>
</div>
</div>
component.ts
import {Component, Input, OnInit} from '@angular/core';
import {FormControl, Validators} from '@angular/forms';
@Component({
selector: 'lib-input-field',
templateUrl: './input-field.component.html',
})
export class InputFieldComponent implements OnInit{
@Input() control: FormControl;
@Input() label: string;
@Input() id: string;
@Input() maxLength: number;
@Input() required: boolean;
ngOnInit() {
if(this.required) this.control.setValidators(Validators.required)
this.control.setValidators([Validators.maxLength(this.maxLength), Validators.minLength(1)])
}
}