All files / app/identity/validators/confirm-match match-error.signal.ts

100% Statements 8/8
100% Branches 9/9
100% Functions 2/2
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31                          1x 66x   66x       51x 51x 8x   43x       66x    
import type { Signal } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import type { FormControl, FormControlStatus, FormGroup } from '@angular/forms';
import { debounceTime, map } from 'rxjs';
import type { Observable } from 'rxjs';
 
import { FORMS } from '@app/shared/constants';
 
import { ERROR_NAME } from './confirm-match.validator';
 
/**
 * Only display the confirmmatch error if the two controls are valid, and the form has the error.
 */
export const confirmMatchFormErrors = (form: FormGroup, control1: FormControl, control2: FormControl): Signal<boolean> => {
  const initialValue: boolean = form.invalid && control1.valid && control2.valid && form.errors?.[ERROR_NAME] != undefined;
 
  const formStatus$: Observable<boolean> = form.statusChanges.pipe(
    // Wait for input to stop before displaying error messages
    debounceTime(FORMS.inputDebounce),
    map((status: FormControlStatus): boolean => {
      const { errors } = form;
      if (status === 'INVALID' && control1.valid && control2.valid && errors) {
        return errors[ERROR_NAME] != undefined;
      }
      return false;
    }),
  );
 
  return toSignal(formStatus$, { initialValue });
};