Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple form tests #26

Merged
merged 11 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading