// This file is part of Shadcn Admin Kit (https://github.com/marmelab/shadcn-admin-kit) import { RecordContextProvider, ResourceContextProvider, ArrayInputContext, } from "ra-core"; import { describe, expect, it, vi } from "vitest"; import { render } from "vitest-browser-react"; import { FormProvider, useForm } from "react-hook-form"; import { ArrayInput } from "./array-input"; import { useContext, useEffect } from "react"; function TestChild({ source }: { source: string }) { const arrayContext = useContext(ArrayInputContext); return (
{source} {arrayContext ? "true" : "false"}
{arrayContext?.fields.length || 0}
); } function TestWrapper({ children, defaultValues = {}, resource = "test", }: { children: React.ReactNode; defaultValues?: Record; resource?: string; }) { const methods = useForm({ defaultValues }); return ( {children} ); } describe("ArrayInput", () => { it("should render with basic props", async () => { const screen = render( , ); await expect.element(screen.getByText("Items")).toBeInTheDocument(); await expect.element(screen.getByTestId("test-child")).toBeInTheDocument(); }); it("should provide ArrayInputContext to children", async () => { const screen = render( , ); await expect .element(screen.getByTestId("has-context")) .toHaveTextContent("true"); }); it("should handle default values", async () => { const defaultValues = { items: [{ name: "item1" }, { name: "item2" }], }; const screen = render( , ); await expect .element(screen.getByTestId("field-count")) .toHaveTextContent("2"); }); it("should display loading skeleton when isPending is true", async () => { const screen = render( , ); await expect .element(screen.getByTestId("test-child")) .not.toBeInTheDocument(); }); it("should apply custom className", async () => { const screen = render( , ); const arrayInput = screen.container.querySelector(".ra-input"); await expect.element(arrayInput).toHaveClass("custom-class"); }); it("should show helper text when provided", async () => { const screen = render( , ); await expect .element(screen.getByText("This is helper text")) .toBeInTheDocument(); }); it("should support array of validators", async () => { const validator1 = vi.fn().mockReturnValue(undefined); const validator2 = vi.fn().mockReturnValue(undefined); const screen = render( , ); await expect.element(screen.getByTestId("test-child")).toBeInTheDocument(); }); it("should handle empty defaultValue", async () => { const screen = render( , ); await expect .element(screen.getByTestId("field-count")) .toHaveTextContent("0"); }); it("should allow adding items through context", async () => { const screen = render( , ); await expect .element(screen.getByTestId("field-count")) .toHaveTextContent("0"); const addButton = screen.getByTestId("add-button"); await addButton.click(); await expect .element(screen.getByTestId("field-count")) .toHaveTextContent("1"); }); it("should not show helper text when helperText is false", async () => { const screen = render( , ); // Vérifier qu'aucun texte d'aide n'est affiché const helperTextElement = screen.container.querySelector(".text-destructive"); await expect.element(helperTextElement).not.toBeInTheDocument(); }); it("should show validation error when present", async () => { const record = {}; const TestWrapperWithError = ({ children, }: { children: React.ReactNode; }) => { const methods = useForm({ defaultValues: { items: [] }, mode: "onChange", }); useEffect(() => { const timer = setTimeout(() => { methods.setError("items", { message: "This field is required" }); }, 100); return () => clearTimeout(timer); }, [methods]); return ( {children} ); }; const screen = render( , { wrapper: TestWrapperWithError }, ); await expect .element(screen.getByText("This field is required")) .toBeInTheDocument(); }); it("should register and unregister field with form groups", async () => { const screen = render( , ); await expect.element(screen.getByTestId("test-child")).toBeInTheDocument(); screen.unmount(); }); it("should handle required validation", async () => { const requiredValidator = (value: unknown[]) => { if (!value || value.length === 0) { return "This field is required"; } return undefined; }; const screen = render( , ); await expect.element(screen.getByTestId("test-child")).toBeInTheDocument(); }); it("should handle async validation", async () => { const asyncValidator = async (value: unknown[]) => { await new Promise((resolve) => setTimeout(resolve, 10)); if (!value || value.length === 0) { return "Async validation error"; } return undefined; }; const screen = render( , ); await expect.element(screen.getByTestId("test-child")).toBeInTheDocument(); }); it("should provide correct source context to children", async () => { const screen = render( , ); await expect .element(screen.getByTestId("source")) .toHaveTextContent("date"); }); it("should handle nested sources correctly", async () => { const screen = render( , ); await expect.element(screen.getByTestId("test-child")).toBeInTheDocument(); }); it("should work with resource context", async () => { const screen = render( , ); await expect.element(screen.getByTestId("test-child")).toBeInTheDocument(); }); });