diff --git a/src/components/FormBuilder/fields/FieldArray.tsx b/src/components/FormBuilder/fields/FieldArray.tsx index 3818d44..433a9ca 100644 --- a/src/components/FormBuilder/fields/FieldArray.tsx +++ b/src/components/FormBuilder/fields/FieldArray.tsx @@ -167,6 +167,7 @@ export const FieldArray = withConditional( // eslint-disable-next-line jsx-a11y/control-has-associated-label + + ); +}; + +describe("Form Components", () => { + beforeEach(() => { + vi.clearAllMocks(); + }); + + it("renders the form correctly", () => { + render(); + expect(screen.getByText("Test Field")).toBeInTheDocument(); + expect(screen.getByText("This is a test field")).toBeInTheDocument(); + expect(screen.getByText("Submit")).toBeInTheDocument(); + }); + + // it("displays error message when field is invalid", async () => { + // render(); + // fireEvent.submit(screen.getByText("Submit")); + // expect(await screen.findByRole("alert")).toHaveTextContent( + // "Field is required" + // ); + // }); + + it("passes validation when field is valid", async () => { + render(); + fireEvent.change(screen.getByRole("textbox"), { + target: { value: "Valid input" }, + }); + fireEvent.submit(screen.getByText("Submit")); + expect(screen.queryByText("Field is required")).not.toBeInTheDocument(); + }); + + it("renders the CSRF token input", () => { + render(); + expect(screen.getByTestId("csrf-token")).toHaveValue("mocked-csrf-token"); + }); +}); diff --git a/tests/setup.js b/tests/setup.ts similarity index 60% rename from tests/setup.js rename to tests/setup.ts index 971941d..d875b58 100644 --- a/tests/setup.js +++ b/tests/setup.ts @@ -2,6 +2,12 @@ import { expect, afterEach } from "vitest"; import { cleanup } from "@testing-library/react"; import * as matchers from "@testing-library/jest-dom/matchers"; +declare module "vitest" { + interface Assertion + extends jest.Matchers, + matchers.TestingLibraryMatchers {} +} + expect.extend(matchers); afterEach(() => { diff --git a/tsconfig.json b/tsconfig.json index b7204cc..2bb2744 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,6 +15,8 @@ "skipLibCheck": true, "strict": true, "lib": ["dom", "dom.iterable", "esnext"], + "types": ["vitest/globals"], + "plugins": [ { "transform": "typescript-transform-paths" }, { "transform": "typescript-transform-paths", "afterDeclarations": true } diff --git a/vitest.config.mts b/vitest.config.mts index 270ff1f..3c251c9 100644 --- a/vitest.config.mts +++ b/vitest.config.mts @@ -2,14 +2,21 @@ import { defineConfig as viteDefineConfig } from "vite"; import react from "@vitejs/plugin-react"; import { defineConfig, mergeConfig } from "vitest/config"; +import path from "path"; export default mergeConfig( viteDefineConfig({ plugins: [react()] }), defineConfig({ test: { + globals: true, environment: "jsdom", - setupFiles: "./tests/setup.js", + setupFiles: "./tests/setup.ts", passWithNoTests: true, }, + resolve: { + alias: { + "#": path.resolve(__dirname, "./src"), + }, + }, }) );