From e6bda20344479626096b2f839c7de6ecc71e3cfe Mon Sep 17 00:00:00 2001 From: Thomas Date: Wed, 22 Jan 2025 13:45:50 -0500 Subject: [PATCH] Validator test --- .../components/RHF/tests/validator.test.ts | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 react-app/src/components/RHF/tests/validator.test.ts diff --git a/react-app/src/components/RHF/tests/validator.test.ts b/react-app/src/components/RHF/tests/validator.test.ts new file mode 100644 index 000000000..4f09141b2 --- /dev/null +++ b/react-app/src/components/RHF/tests/validator.test.ts @@ -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(); + }); +});