From 600af169ff3ac27c313e94307d90a44f36727785 Mon Sep 17 00:00:00 2001 From: Sterling Camden Date: Fri, 31 Mar 2023 16:25:15 -0700 Subject: [PATCH] test(recursion): ensure specifically mapped arrays can still render regardless of recursion --- src/__tests__/createSchemaForm.test.tsx | 86 +++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/src/__tests__/createSchemaForm.test.tsx b/src/__tests__/createSchemaForm.test.tsx index f4fbfc4..d2ae9db 100644 --- a/src/__tests__/createSchemaForm.test.tsx +++ b/src/__tests__/createSchemaForm.test.tsx @@ -1507,4 +1507,90 @@ describe("createSchemaForm", () => { await userEvent.click(button); expect(mockOnSubmit).toHaveBeenCalledWith(defaultValues); }); + + it("should render an array component despite recusions", async () => { + const mockOnSubmit = jest.fn(() => {}); + function DynamicArray() { + const { + field: { value, onChange }, + } = useTsController(); + + return ( +
+ + {value?.map((val, i) => { + return ( + + onChange(value?.map((v, j) => (i === j ? e.target.value : v))) + } + /> + ); + })} +
+ ); + } + + function NumberField() { + return
number
; + } + + const mapping = [ + [z.string().array(), DynamicArray], + [z.number(), NumberField], + ] as const; + + const Form = createTsForm(mapping); + + const schema = z.object({ + arrayField: z.string().array(), + numberArray: z.number().array(), + }); + const defaultValues = { + arrayField: ["name", "name2"], + numberArray: [1, 2, 3], + }; + render( +
{ + return ; + }} + >
+ ); + + const numberNodes = screen.queryAllByText("number"); + numberNodes.forEach((node) => expect(node).toBeInTheDocument()); + expect(numberNodes).toHaveLength(3); + + expect(screen.getByTestId("dynamic-array")).toBeInTheDocument(); + const addElementButton = screen.getByTestId("add-element"); + await userEvent.click(addElementButton); + + const inputs = screen.getAllByTestId(/dynamic-array-input/); + expect(inputs.length).toBe(3); + + const input3 = screen.getByTestId("dynamic-array-input2"); + await userEvent.type(input3, "name3"); + const button = screen.getByText("submit"); + await userEvent.click(button); + expect(mockOnSubmit).toHaveBeenCalledWith({ + arrayField: ["name", "name2", "name3"], + numberArray: [1, 2, 3], + }); + }); });