Please help out with testing custom validator. Two weeks trying to resolve this issue nothing happens.
in html
<div>
<input type="text" class="value-input" [(ngModel)]="element.value" name = "RegisterValue" #ValueReg = "ngModel" [viewRegister] = 'viewChoiceDispleyReg' [viewType] = 'displeyView' valueName>
</div>
directive
import { AbstractControl, ValidatorFn, FormControl } from '@angular/forms';
import { Directive, ElementRef, Input } from '@angular/core';
import { NG_VALIDATORS, Validator } from '@angular/forms';
@Directive({
selector: '[valueName]',
providers: [
{ provide: NG_VALIDATORS, useExisting: valueValidator, multi: true }
]
})
export class valueValidator implements Validator{
validator: ValidatorFn;
@Input('viewType') viewType;
@Input('viewRegister') viewRegister;
constructor(private elementRef: ElementRef) {
this.validator = this.validateValueFactory();
}
validate(c: FormControl) {
return this.validator(c);
}
// validation function
validateValueFactory() : ValidatorFn {
return (c: AbstractControl) => {
console.log("validate: " + c.value + ", view type: " + this.viewType);
let isValid = c.value === '0';
if(isValid) {
return null;
} else {
return {
valueName: {
valid: false
}
};
}
}
}
}
I've studied a lot of material on stackoverflow on other sites. The main problem is that I don't know how to create a component for testing the validator. In this test the component is to add the required validator for the properties(@viewType, @viewRegister). How write unit tests for this validator?