Skip to content

Commit

Permalink
Validator test
Browse files Browse the repository at this point in the history
  • Loading branch information
thwalker6 committed Jan 22, 2025
1 parent e4de82e commit e6bda20
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions react-app/src/components/RHF/tests/validator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { describe, expect, it } from "vitest";
import { validateInput, validateOption } from "../utils";

describe("Test for RHF validator", () => {
const rules = { required: true, min: "10", max: "test" };
it("checks to see if a field is missing while required", () => {
let missingRequired = validateInput(undefined, rules);
expect(missingRequired).toBe("*Required");
missingRequired = validateInput("", rules);
expect(missingRequired).toBe("*Required");
missingRequired = validateInput([], rules);
expect(missingRequired).toBe("*Required");
});
it("checks to see if a number is within the limits", () => {
const rules = {
required: true,
min: { value: "5", message: "too short" },
max: { value: "10", message: "too long" },
};
const valid = validateInput("6", rules);
expect(valid).toBe("");
const tooShort = validateInput("4", rules);
expect(tooShort).toBe("too short");
const tooLong = validateInput("20", rules);
expect(tooLong).toBe("too long");
});
it("checks to see if a date is within the limits", () => {
const past = new Date(2023, 11, 25).toISOString();
const future = new Date(2025, 11, 25).toISOString();
const rules = {
required: true,
min: { value: past, message: "too far back" },
max: { value: future, message: "too far in the future" },
};
const valid = validateInput("12/25/2024", rules);
expect(valid).toBe("");
const tooShort = validateInput("12/25/2020", rules);
expect(tooShort).toBe("too far back");
const tooLong = validateInput("12/25,2027", rules);
expect(tooLong).toBe("too far in the future");
});
it("checks to see if a string is within the limits", () => {
const rules = {
maxLength: { value: 10, message: "too long" },
minLength: { value: 5, message: "too short" },
};
const valid = validateInput("ABCDEF", rules);
expect(valid).toBe("");
const tooShort = validateInput("ABC", rules);
expect(tooShort).toBe("too short");
const tooLong = validateInput("ABCDEFGHIJK", rules);
expect(tooLong).toBe("too long");
});
it("checks the regex pattern", () => {
const patternValue = /^t.st value$/;
const rules = {
required: true,
pattern: {
value: patternValue,
message: "Does not match",
},
};
const valid = validateInput("test value", rules);
expect(valid).toBe("");
const notMatch = validateInput("no matching", rules);
expect(notMatch).toBe("Does not match");
});
it("checks validate options", () => {
const options = [{ value: "1" }, { value: "2" }];

const match = validateOption("1", options);
expect(match).toStrictEqual({ value: "1" });
const misMatch = validateOption("3", options);
expect(misMatch).toBeUndefined();
});
});

0 comments on commit e6bda20

Please sign in to comment.