|
| 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 Enums Tests", () => { |
| 8 | + const arraySchema = z.object({ |
| 9 | + gender: z.enum(["male", "female"]), |
| 10 | + }); |
| 11 | + const schemaProvider = new ZodProvider(arraySchema); |
| 12 | + |
| 13 | + const arraySchemaWithDefault = z.object({ |
| 14 | + gender: z.enum(["male", "female"]).default("male"), |
| 15 | + }); |
| 16 | + const schemaProviderWithDefault = new ZodProvider(arraySchemaWithDefault); |
| 17 | + |
| 18 | + it("renders enums fields correctly", () => { |
| 19 | + cy.mount( |
| 20 | + <TestWrapper> |
| 21 | + <AutoForm |
| 22 | + schema={schemaProvider} |
| 23 | + onSubmit={cy.stub().as("onSubmit")} |
| 24 | + withSubmit |
| 25 | + /> |
| 26 | + </TestWrapper>, |
| 27 | + ); |
| 28 | + |
| 29 | + // Check if select trigger exists |
| 30 | + cy.get('[role="combobox"]').should("exist"); |
| 31 | + |
| 32 | + // Open the select dropdown |
| 33 | + cy.get('[role="combobox"]').click(); |
| 34 | + |
| 35 | + // Verify options are rendered correctly |
| 36 | + cy.get('[role="option"]').should("have.length", 2); |
| 37 | + cy.get('[role="option"]').eq(0).should("have.text", "male"); |
| 38 | + cy.get('[role="option"]').eq(1).should("have.text", "female"); |
| 39 | + |
| 40 | + // Test form submission with selected value |
| 41 | + cy.get('[role="option"]').contains("female").click(); |
| 42 | + cy.get("form").submit(); |
| 43 | + cy.get("@onSubmit").should("have.been.calledWith", { |
| 44 | + gender: "female", |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it("handles default values for enums correctly", () => { |
| 49 | + cy.mount( |
| 50 | + <TestWrapper> |
| 51 | + <AutoForm |
| 52 | + schema={schemaProviderWithDefault} |
| 53 | + onSubmit={cy.stub().as("onSubmit")} |
| 54 | + withSubmit |
| 55 | + /> |
| 56 | + </TestWrapper>, |
| 57 | + ); |
| 58 | + |
| 59 | + // Verify default value is selected |
| 60 | + cy.get('[role="combobox"]').should("contain.text", "male"); |
| 61 | + |
| 62 | + // Submit form without changes to verify default value |
| 63 | + cy.get("form").submit(); |
| 64 | + cy.get("@onSubmit").should("have.been.calledWith", { |
| 65 | + gender: "male", |
| 66 | + }); |
| 67 | + }); |
| 68 | +}); |
0 commit comments