-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 🐛 fix SelectField of Shadcn UI (#154)
Select field give validation error even if value is selected Default value for select from zod schema not pre selected Add Test case for Shadcn zod enum type
- Loading branch information
1 parent
37a3ada
commit c688ee8
Showing
3 changed files
with
86 additions
and
6 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
apps/web/cypress/component/autoform/shadcn-zod/enums.cy.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import React from "react"; | ||
import { AutoForm } from "@autoform/shadcn/components/ui/autoform/AutoForm"; | ||
import { ZodProvider } from "@autoform/zod"; | ||
import { z } from "zod"; | ||
import { TestWrapper } from "./utils"; | ||
|
||
describe("AutoForm Enums Tests", () => { | ||
const arraySchema = z.object({ | ||
gender: z.enum(["male", "female"]), | ||
}); | ||
const schemaProvider = new ZodProvider(arraySchema); | ||
|
||
const arraySchemaWithDefault = z.object({ | ||
gender: z.enum(["male", "female"]).default("male"), | ||
}); | ||
const schemaProviderWithDefault = new ZodProvider(arraySchemaWithDefault); | ||
|
||
it("renders enums fields correctly", () => { | ||
cy.mount( | ||
<TestWrapper> | ||
<AutoForm | ||
schema={schemaProvider} | ||
onSubmit={cy.stub().as("onSubmit")} | ||
withSubmit | ||
/> | ||
</TestWrapper>, | ||
); | ||
|
||
// Check if select trigger exists | ||
cy.get('[role="combobox"]').should("exist"); | ||
|
||
// Open the select dropdown | ||
cy.get('[role="combobox"]').click(); | ||
|
||
// Verify options are rendered correctly | ||
cy.get('[role="option"]').should("have.length", 2); | ||
cy.get('[role="option"]').eq(0).should("have.text", "male"); | ||
cy.get('[role="option"]').eq(1).should("have.text", "female"); | ||
|
||
// Test form submission with selected value | ||
cy.get('[role="option"]').contains("female").click(); | ||
cy.get("form").submit(); | ||
cy.get("@onSubmit").should("have.been.calledWith", { | ||
gender: "female", | ||
}); | ||
}); | ||
|
||
it("handles default values for enums correctly", () => { | ||
cy.mount( | ||
<TestWrapper> | ||
<AutoForm | ||
schema={schemaProviderWithDefault} | ||
onSubmit={cy.stub().as("onSubmit")} | ||
withSubmit | ||
/> | ||
</TestWrapper>, | ||
); | ||
|
||
// Verify default value is selected | ||
cy.get('[role="combobox"]').should("contain.text", "male"); | ||
|
||
// Submit form without changes to verify default value | ||
cy.get("form").submit(); | ||
cy.get("@onSubmit").should("have.been.calledWith", { | ||
gender: "male", | ||
}); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters