|
| 1 | +import React from "react"; |
| 2 | +import { AutoForm } from "@autoform/shadcn/components/ui/autoform/AutoForm"; |
| 3 | +import { ZodProvider } from "@autoform/zod"; |
| 4 | +import { z } from "zod"; |
| 5 | +import { TestWrapper } from "./utils"; |
| 6 | + |
| 7 | +describe("AutoForm Form Props Tests (shadcn)", () => { |
| 8 | + const schema = z.object({ |
| 9 | + name: z.string(), |
| 10 | + }); |
| 11 | + |
| 12 | + const schemaProvider = new ZodProvider(schema); |
| 13 | + |
| 14 | + it("applies custom form props", () => { |
| 15 | + cy.mount( |
| 16 | + <TestWrapper> |
| 17 | + <AutoForm |
| 18 | + schema={schemaProvider} |
| 19 | + onSubmit={cy.stub().as("onSubmit")} |
| 20 | + withSubmit |
| 21 | + formProps={{ |
| 22 | + className: "custom-form-class", |
| 23 | + "data-testid": "custom-form", |
| 24 | + onKeyDown: cy.stub().as("onKeyDown"), |
| 25 | + }} |
| 26 | + /> |
| 27 | + </TestWrapper> |
| 28 | + ); |
| 29 | + |
| 30 | + cy.get("form") |
| 31 | + .should("have.class", "custom-form-class") |
| 32 | + .and("have.attr", "data-testid", "custom-form"); |
| 33 | + |
| 34 | + cy.get('input[name="name"]').type("{enter}"); |
| 35 | + cy.get("@onKeyDown").should("have.been.called"); |
| 36 | + }); |
| 37 | + |
| 38 | + it("prevents form submission on enter key", () => { |
| 39 | + cy.mount( |
| 40 | + <TestWrapper> |
| 41 | + <AutoForm |
| 42 | + schema={schemaProvider} |
| 43 | + onSubmit={cy.stub().as("onSubmit")} |
| 44 | + withSubmit |
| 45 | + formProps={{ |
| 46 | + onKeyDown: (e) => { |
| 47 | + if (e.key === "Enter") e.preventDefault(); |
| 48 | + }, |
| 49 | + }} |
| 50 | + /> |
| 51 | + </TestWrapper> |
| 52 | + ); |
| 53 | + |
| 54 | + cy.get('input[name="name"]').type("John Doe{enter}"); |
| 55 | + cy.get("@onSubmit").should("not.have.been.called"); |
| 56 | + }); |
| 57 | +}); |
0 commit comments