1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.