|
| 1 | +import React from "react"; |
| 2 | +import { AutoForm } from "@autoform/chakra"; |
| 3 | +import { ZodProvider, fieldConfig } from "@autoform/zod"; |
| 4 | +import { z } from "zod"; |
| 5 | + |
| 6 | +describe("AutoForm Advanced Features Tests", () => { |
| 7 | + const advancedSchema = z.object({ |
| 8 | + username: z |
| 9 | + .string() |
| 10 | + .min(3, "Username must be at least 3 characters") |
| 11 | + .superRefine( |
| 12 | + fieldConfig({ |
| 13 | + description: "Choose a unique username", |
| 14 | + order: 1, |
| 15 | + inputProps: { |
| 16 | + placeholder: "Enter username", |
| 17 | + }, |
| 18 | + }) |
| 19 | + ), |
| 20 | + password: z |
| 21 | + .string() |
| 22 | + .min(8, "Password must be at least 8 characters") |
| 23 | + .superRefine( |
| 24 | + fieldConfig({ |
| 25 | + description: "Use a strong password", |
| 26 | + order: 2, |
| 27 | + inputProps: { |
| 28 | + type: "password", |
| 29 | + }, |
| 30 | + }) |
| 31 | + ), |
| 32 | + favoriteColor: z.enum(["red", "green", "blue"]).superRefine( |
| 33 | + fieldConfig({ |
| 34 | + fieldType: "select", |
| 35 | + order: 3, |
| 36 | + }) |
| 37 | + ), |
| 38 | + bio: z |
| 39 | + .string() |
| 40 | + .optional() |
| 41 | + .superRefine( |
| 42 | + fieldConfig({ |
| 43 | + order: 4, |
| 44 | + }) |
| 45 | + ), |
| 46 | + }); |
| 47 | + |
| 48 | + const schemaProvider = new ZodProvider(advancedSchema); |
| 49 | + |
| 50 | + it("renders fields in the correct order", () => { |
| 51 | + cy.mount( |
| 52 | + <AutoForm |
| 53 | + schema={schemaProvider} |
| 54 | + onSubmit={cy.stub().as("onSubmit")} |
| 55 | + withSubmit |
| 56 | + /> |
| 57 | + ); |
| 58 | + |
| 59 | + cy.get(".chakra-field__root") |
| 60 | + .eq(0) |
| 61 | + .find("input") |
| 62 | + .should("have.attr", "name", "username"); |
| 63 | + cy.get(".chakra-field__root") |
| 64 | + .eq(1) |
| 65 | + .find("input") |
| 66 | + .should("have.attr", "name", "password"); |
| 67 | + cy.get(".chakra-field__root").eq(2).find("select"); |
| 68 | + cy.get(".chakra-field__root") |
| 69 | + .eq(3) |
| 70 | + .find("input") |
| 71 | + .should("have.attr", "name", "bio"); |
| 72 | + }); |
| 73 | + |
| 74 | + it("displays field descriptions", () => { |
| 75 | + cy.mount( |
| 76 | + <AutoForm |
| 77 | + schema={schemaProvider} |
| 78 | + onSubmit={cy.stub().as("onSubmit")} |
| 79 | + withSubmit |
| 80 | + /> |
| 81 | + ); |
| 82 | + |
| 83 | + cy.contains("Choose a unique username").should("be.visible"); |
| 84 | + cy.contains("Use a strong password").should("be.visible"); |
| 85 | + }); |
| 86 | + |
| 87 | + it("applies custom input props", () => { |
| 88 | + cy.mount( |
| 89 | + <AutoForm |
| 90 | + schema={schemaProvider} |
| 91 | + onSubmit={cy.stub().as("onSubmit")} |
| 92 | + withSubmit |
| 93 | + /> |
| 94 | + ); |
| 95 | + |
| 96 | + cy.get('input[name="username"]').should( |
| 97 | + "have.attr", |
| 98 | + "placeholder", |
| 99 | + "Enter username" |
| 100 | + ); |
| 101 | + cy.get('input[name="password"]').should("have.attr", "type", "password"); |
| 102 | + }); |
| 103 | + |
| 104 | + it("renders select field correctly", () => { |
| 105 | + cy.mount( |
| 106 | + <AutoForm |
| 107 | + schema={schemaProvider} |
| 108 | + onSubmit={cy.stub().as("onSubmit")} |
| 109 | + withSubmit |
| 110 | + /> |
| 111 | + ); |
| 112 | + |
| 113 | + cy.get('.chakra-select__root').should("exist").within(() => { |
| 114 | + cy.get('select').should("have.attr", "name", "favoriteColor"); |
| 115 | + }).click(); |
| 116 | + |
| 117 | + cy.get('.chakra-select__content').should("exist").within(() => { |
| 118 | + cy.get('.chakra-select__item').should('exist').should('contain', 'red'); |
| 119 | + cy.get('.chakra-select__item').should('exist').should('contain', 'green'); |
| 120 | + cy.get('.chakra-select__item').should('exist').should('contain', 'blue'); |
| 121 | + }); |
| 122 | + |
| 123 | + }); |
| 124 | + |
| 125 | + it("renders textarea field correctly", () => { |
| 126 | + cy.mount( |
| 127 | + <AutoForm |
| 128 | + schema={schemaProvider} |
| 129 | + onSubmit={cy.stub().as("onSubmit")} |
| 130 | + withSubmit |
| 131 | + /> |
| 132 | + ); |
| 133 | + |
| 134 | + cy.get('input[name="bio"]').should("exist"); |
| 135 | + }); |
| 136 | +}); |
0 commit comments