-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
180d309
commit 34a25a6
Showing
1 changed file
with
108 additions
and
63 deletions.
There are no files selected for viewing
171 changes: 108 additions & 63 deletions
171
react-app/src/components/RHF/tests/additionalRules.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,116 @@ | ||
import { RegisterOptions } from "react-hook-form"; | ||
import { RuleGenerator, SortFuncs, AdditionalRule } from "shared-types"; | ||
import { describe, test, expect } from "vitest"; | ||
import { sortFunctions, ruleGenerator } from "../utils/additionalRules"; | ||
|
||
export const sortFunctions: { | ||
[x in SortFuncs]: (a: string, b: string) => number; | ||
} = { | ||
noSort: () => 0, | ||
reverseSort: (a, b) => b.localeCompare(a), | ||
}; | ||
type VO = Record<string, any>; | ||
|
||
export const ruleGenerator: RuleGenerator = (rules, addtnlRules) => { | ||
if (!rules && !addtnlRules) return undefined; | ||
const simpleRules = rules ?? {}; | ||
const customRules = addtnlRules | ||
? { validate: addtnlRules.reduce(valReducer, {}) } | ||
: {}; | ||
describe("Additional Rules Tests", () => { | ||
describe("Sort Function Tests", () => { | ||
const dataArray = ["Florida, Ohio, Washington, Maine"]; | ||
test("no sort", () => { | ||
const testArr = [...dataArray].sort(sortFunctions.noSort); | ||
expect(testArr.toString()).toBe(dataArray.toString()); | ||
}); | ||
test("reverse sort", () => { | ||
const testArr = [...dataArray].sort(sortFunctions.reverseSort); | ||
const compareArr = [...dataArray].sort().reverse(); | ||
expect(compareArr.toString()).toBe(testArr.toString()); | ||
}); | ||
}); | ||
|
||
return { ...simpleRules, ...customRules }; | ||
}; | ||
describe("Custom Validation Tests", () => { | ||
const testData = { | ||
testCompField1: "0", | ||
testCompField2: "10", | ||
testCompField3: undefined, | ||
}; | ||
|
||
export const valReducer = ( | ||
valSet: RegisterOptions["validate"], | ||
rule: AdditionalRule, | ||
index: number, | ||
): RegisterOptions["validate"] => { | ||
const valName = `${rule.type}_${index}`; | ||
test("Less than field", () => { | ||
const rules = ruleGenerator(undefined, [ | ||
{ | ||
fieldName: "testCompField1", | ||
type: "lessThanField", | ||
message: "Validation Failed 1", | ||
}, | ||
{ | ||
fieldName: "testCompField1", | ||
type: "lessThanField", | ||
message: "Validation Failed 2", | ||
strictGreater: true, | ||
}, | ||
]); | ||
|
||
if (!rules) throw new Error("Failed to create rule set."); | ||
|
||
const valFunc1 = (rules.validate as VO)["lessThanField_0"]; | ||
expect(valFunc1).toBeTruthy(); | ||
expect(valFunc1(0, testData)).toBeTruthy(); | ||
expect(valFunc1(10, testData)).toBeTruthy(); | ||
expect(valFunc1(100, testData)).toBe("Validation Failed 1"); | ||
|
||
switch (rule.type) { | ||
case "lessThanField": | ||
return { | ||
...valSet, | ||
[valName]: (value, fields) => { | ||
if ( | ||
!rule.strictGreater && | ||
parseFloat(value) <= parseFloat(fields[rule.fieldName]) | ||
) | ||
return true; | ||
else if (parseFloat(value) < parseFloat(fields[rule.fieldName])) | ||
return true; | ||
return rule.message; | ||
const valFunc2 = (rules.validate as VO)["lessThanField_1"]; | ||
expect(valFunc2).toBeTruthy(); | ||
expect(valFunc2(0, testData)).toBeTruthy(); | ||
expect(valFunc2(10, testData)).toBe("Validation Failed 2"); | ||
expect(valFunc2(100, testData)).toBe("Validation Failed 2"); | ||
}); | ||
|
||
test("Greater than field", () => { | ||
const rules = ruleGenerator(undefined, [ | ||
{ | ||
fieldName: "testCompField2", | ||
type: "greaterThanField", | ||
message: "Validation Failed 1", | ||
}, | ||
}; | ||
case "greaterThanField": | ||
return { | ||
...valSet, | ||
[valName]: (value, fields) => { | ||
if ( | ||
!rule.strictGreater && | ||
parseFloat(value) >= parseFloat(fields[rule.fieldName]) | ||
) | ||
return true; | ||
else if (parseFloat(value) > parseFloat(fields[rule.fieldName])) | ||
return true; | ||
return rule.message; | ||
{ | ||
fieldName: "testCompField2", | ||
type: "greaterThanField", | ||
message: "Validation Failed 2", | ||
strictGreater: true, | ||
}, | ||
}; | ||
case "cannotCoexist": | ||
return { | ||
...valSet, | ||
[valName]: (value, fields) => { | ||
if (value !== undefined && fields[rule.fieldName] === undefined) | ||
return true; | ||
if (value === undefined && fields[rule.fieldName] !== undefined) | ||
return true; | ||
return rule.message; | ||
]); | ||
|
||
if (!rules) throw new Error("Failed to create rule set."); | ||
|
||
const valFunc1 = (rules.validate as VO)["greaterThanField_0"]; | ||
expect(valFunc1).toBeTruthy(); | ||
expect(valFunc1(0, testData)).toBeTruthy(); | ||
expect(valFunc1(10, testData)).toBeTruthy(); | ||
expect(valFunc1(100, testData)).toBeTruthy(); | ||
expect(valFunc1(-1, testData)).toBe("Validation Failed 1"); | ||
|
||
const valFunc2 = (rules.validate as VO)["greaterThanField_1"]; | ||
expect(valFunc2).toBeTruthy(); | ||
expect(valFunc2(10, testData)).toBeTruthy(); | ||
expect(valFunc2(100, testData)).toBeTruthy(); | ||
expect(valFunc2(0, testData)).toBe("Validation Failed 2"); | ||
expect(valFunc2(-1, testData)).toBe("Validation Failed 2"); | ||
}); | ||
|
||
test("Cannot coexist", () => { | ||
const rules = ruleGenerator(undefined, [ | ||
{ | ||
fieldName: "testCompField1", | ||
type: "cannotCoexist", | ||
message: "Validation Failed 1", | ||
}, | ||
{ | ||
fieldName: "testCompField3", | ||
type: "cannotCoexist", | ||
message: "Validation Failed 2", | ||
}, | ||
}; | ||
default: | ||
return { ...valSet }; | ||
} | ||
}; | ||
]); | ||
|
||
if (!rules) throw new Error("Failed to create rule set."); | ||
|
||
const valFunc1 = (rules.validate as VO)["cannotCoexist_0"]; | ||
expect(valFunc1).toBeTruthy(); | ||
expect(valFunc1(undefined, testData)).toBeTruthy(); | ||
expect(valFunc1("test", testData)).toBe("Validation Failed 1"); | ||
|
||
const valFunc2 = (rules.validate as VO)["cannotCoexist_1"]; | ||
expect(valFunc2).toBeTruthy(); | ||
expect(valFunc2(undefined, testData)).toBeTruthy(); | ||
expect(valFunc2("test", testData)).toBeTruthy(); | ||
}); | ||
}); | ||
}); |