Skip to content

Commit

Permalink
Simple form tests (#26)
Browse files Browse the repository at this point in the history
* Setup hooks test + test array validation hook

* Tests for array and common validation rules

* Tests for date-time validation rules

* Tests for email validation rule

* Tests for number validation rules

* Tests for text validation rules

* Tests for uri validation rules

* Tests for utils

* Tests for utils

* Fix some spelling errors

* Rush change
  • Loading branch information
ilbrando authored Dec 11, 2024
1 parent 1b5bff8 commit 40efbe9
Show file tree
Hide file tree
Showing 19 changed files with 643 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@ilbrando/simple-form",
"comment": "Implement tests",
"type": "patch"
}
],
"packageName": "@ilbrando/simple-form"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@ilbrando/utils",
"comment": "Fixed the type for getFieldValues",
"type": "major"
}
],
"packageName": "@ilbrando/utils"
}
92 changes: 79 additions & 13 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type FormFields = {
jobTitle: string;
};

const FormStory = ({ onSubmit }: { onSubmit?: (values: MakeNullable<FormFields>) => void }) => {
const FormStory = ({ onSubmit }: { onSubmit?: (values: Partial<MakeNullable<FormFields>>) => void }) => {
const { required, maxLength } = useValidationRules();

const [isSubmitting, setIsSubmitting] = useState(false); // story book simulate submitting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type FormFields = {
age: number;
};

const FormStory = ({ onSubmit }: { onSubmit?: (values: MakeNullable<FormFields>) => void }) => {
const FormStory = ({ onSubmit }: { onSubmit?: (values: Partial<MakeNullable<FormFields>>) => void }) => {
const { required, maxLength, min } = useValidationRules();

const [isSubmitting, setIsSubmitting] = useState(false); // story book simulate submitting
Expand Down
8 changes: 7 additions & 1 deletion packages/simple-form/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,22 @@
"vite": "vite",
"type-check": "tsc --noEmit --watch",
"build": "vite build && tsc -p tsconfig-declarations.json",
"test": "vitest run",
"eslint": "eslint --max-warnings=0"
},
"devDependencies": {
"@types/react": "~18",
"@ilbrando/eslint-plugin": "workspace:*",
"@vitejs/plugin-react": "~4.3.4",
"@testing-library/react": "~16.1.0",
"eslint": "~9.16.0",
"happy-dom": "~15.11.7",
"npm-run-all": "~4.1.5",
"react": "^18",
"react-dom": "^18",
"typescript": "~5.7.2",
"vite": "~6.0.3"
"vite": "~6.0.3",
"vitest": "~2.1.4"
},
"dependencies": {
"@ilbrando/utils": "workspace:*"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { describe, test } from "vitest";
import { renderHook } from "@testing-library/react";

import { useArrayValidationRules } from "./array";
import { assertValidationResult, genericErrorMessage } from "./validation-test-utils";

describe("array", () => {
test.each`
value | parameter | expected
${[1, 2]} | ${1} | ${genericErrorMessage}
${[1, 2]} | ${3} | ${genericErrorMessage}
${[1, 2]} | ${2} | ${undefined}
${[]} | ${0} | ${undefined}
`("count($parameter)($value) => $expected", ({ value, parameter, expected }) => {
// Arrange
const { result } = renderHook(() => useArrayValidationRules());

// Act
const actual = result.current.count(parameter)(value);

// Assert
assertValidationResult(expected, actual);
});

test.each`
value | parameter | expected
${[1, 2]} | ${1} | ${genericErrorMessage}
${[1, 2]} | ${3} | ${undefined}
${[1, 2]} | ${2} | ${undefined}
${[]} | ${0} | ${undefined}
`("maxCount($parameter)($value) => $expected", ({ value, parameter, expected }) => {
// Arrange
const { result } = renderHook(() => useArrayValidationRules());

// Act
const actual = result.current.maxCount(parameter)(value);

// Assert
assertValidationResult(expected, actual);
});

test.each`
value | parameter | expected
${[1, 2]} | ${1} | ${undefined}
${[1, 2]} | ${3} | ${genericErrorMessage}
${[1, 2]} | ${2} | ${undefined}
${[]} | ${0} | ${undefined}
`("minCount($parameter)($value) => $expected", ({ value, parameter, expected }) => {
// Arrange
const { result } = renderHook(() => useArrayValidationRules());

// Act
const actual = result.current.minCount(parameter)(value);

// Assert
assertValidationResult(expected, actual);
});
});
Loading

0 comments on commit 40efbe9

Please sign in to comment.