Simple way to mock FormControlName

Viewed 18

I have te follow directive:

@Directive({
  selector: '[ifxTrimFormFields]',
})
export class TrimFormFieldsDirective {
  @Input() type: string;

  constructor(@Optional() private formControlDir: FormControlDirective,
              @Optional() private formControlName: FormControlName) {}

  @HostListener('blur')
  @HostListener('keydown.enter')
  trimValue() {
    const control = this.formControlDir?.control || this.formControlName?.control;
    if (typeof control.value === 'string' && this.type !== 'password') {
      control.setValue(
        (control.value as string)
          .replaceAll(/\s{2,}/g, ' ')
          .trim(),
      );
    }
  }
}

Simple and works good. Now I will test this directive. But I cannot mock FormControlDirective oder FormControlName. FormControlDirective must have a ControlValueAccessor, but google dont help me.

Can you help me?

0 Answers
Related