Jest unit test for angular custom validation to select atleast one control in formgroup

676 Views Asked by At

I'm writing a unit test with jest cases for angular custom validator which is user must select atleast one filter in formgroup. I'm new to angular unit test. Please let me know what I have missed?

Error:

Matcher error: received value must be a mock or spy function
Received has type:  function
Received has value: [Function anonymous]

custom.validator.ts

export const atLeastOne =
  (validator: ValidatorFn) =>
  (group: FormGroup): ValidationErrors | null => {

    const hasAtLeastOne =
      group &&
      group.controls &&
      Object.keys(group.controls).some((k) => !validator(group.controls[k]));

    return hasAtLeastOne ? null : { atLeastOne: true };
  };

custom.validator.spec.ts

describe("atLeastOne()", () => {
  it("should return true if no filter is selected", () => {
    const mockValidator = jest.fn();
    const mockForm = {
      controls: {
        assetsClassesControl: "Object",
        riskControl: "Object",
      },
      errors: {
        atLeastOne: true,
      },
      status: "INVALID",
      pristine: true,
    };
    expect(atLeastOne(mockValidator)).toHaveBeenCalled();
  });
});

app.ts

ngOnInit(): void {
  this.filterForm.setValidators(atLeastOne(Validators.required));
}
0

There are 0 best solutions below