|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +import { expect, test } from "vitest" |
| 6 | +import { fireEvent, render, screen } from "test/utils" |
| 7 | +import { Form, FormProps } from "src/core/components/fields/Form" |
| 8 | +import { z } from "zod" |
| 9 | +import LabeledTextField from "./LabeledTextField" |
| 10 | + |
| 11 | +interface MyFormProps<S extends z.ZodType<any, any>> extends FormProps<S> { |
| 12 | + name: string |
| 13 | + placeholder: string |
| 14 | + label: string |
| 15 | + inputType?: "text" | "password" | "email" | "number" | "textarea" |
| 16 | + onChangeCallback: (val) => void |
| 17 | +} |
| 18 | + |
| 19 | +const placeholder = "Container placeholder" |
| 20 | +const name = "input 1" |
| 21 | +const label = "input_label" |
| 22 | + |
| 23 | +function MyForm<S extends z.ZodType<any, any>>(props: MyFormProps<S>) { |
| 24 | + const { name, placeholder, inputType, onChangeCallback, label, ...formProps } = props |
| 25 | + |
| 26 | + return ( |
| 27 | + <div> |
| 28 | + <Form {...formProps}> |
| 29 | + <LabeledTextField |
| 30 | + name={name} |
| 31 | + label={label} |
| 32 | + placeholder={placeholder} |
| 33 | + type={inputType} |
| 34 | + className="input mb-4 w-1/2 text-primary input-primary input-bordered border-2 bg-base-300" |
| 35 | + onChange={onChangeCallback} |
| 36 | + /> |
| 37 | + </Form> |
| 38 | + </div> |
| 39 | + ) |
| 40 | +} |
| 41 | + |
| 42 | +test("renders labeled field for text type", async () => { |
| 43 | + const inputType = "text" |
| 44 | + let value = "" |
| 45 | + const onChangeHandle = (val) => { |
| 46 | + value = val.currentTarget.value |
| 47 | + } |
| 48 | + |
| 49 | + render( |
| 50 | + <MyForm |
| 51 | + name={name} |
| 52 | + placeholder={placeholder} |
| 53 | + onChangeCallback={onChangeHandle} |
| 54 | + onSubmit={(val) => {}} |
| 55 | + inputType={inputType} |
| 56 | + label={label} |
| 57 | + ></MyForm> |
| 58 | + ) |
| 59 | + |
| 60 | + const elView = screen.getByTestId("labeledinput-testid") |
| 61 | + const input = screen.getByPlaceholderText(placeholder) |
| 62 | + expect(elView).toBeInTheDocument() |
| 63 | + expect(input).toBeInTheDocument() |
| 64 | + expect(screen.getByText(/input_label/i)).toBeInTheDocument() |
| 65 | + expect(input).not.toHaveAttribute("value", "test value") |
| 66 | + fireEvent.change(input, { target: { value: "test value" } }) |
| 67 | + expect(value).equals("test value") |
| 68 | + expect(input).toHaveAttribute("type", inputType) |
| 69 | + expect(input).toHaveAttribute("name", name) |
| 70 | + expect(input).not.toHaveAttribute("type", "password") |
| 71 | +}) |
| 72 | + |
| 73 | +test("renders labeled field for numeric type", async () => { |
| 74 | + const inputType = "number" |
| 75 | + let value = "" |
| 76 | + const onChangeHandle = (val) => { |
| 77 | + value = val.currentTarget.value |
| 78 | + } |
| 79 | + |
| 80 | + render( |
| 81 | + <MyForm |
| 82 | + name={name} |
| 83 | + placeholder={placeholder} |
| 84 | + onChangeCallback={onChangeHandle} |
| 85 | + onSubmit={(val) => {}} |
| 86 | + label={label} |
| 87 | + inputType={inputType} |
| 88 | + ></MyForm> |
| 89 | + ) |
| 90 | + const elView = screen.getByTestId("labeledinput-testid") |
| 91 | + const input = screen.getByPlaceholderText(placeholder) |
| 92 | + expect(elView).toBeInTheDocument() |
| 93 | + expect(input).toBeInTheDocument() |
| 94 | + expect(input).not.toHaveAttribute("value", "1234567") |
| 95 | + fireEvent.change(input, { target: { value: "1234567a" } }) |
| 96 | + expect(value).not.equal("1234567") |
| 97 | + fireEvent.change(input, { target: { value: "1234567" } }) |
| 98 | + expect(value).equal("1234567") |
| 99 | + expect(input).toHaveAttribute("type", inputType) |
| 100 | + expect(input).not.toHaveAttribute("type", "text") |
| 101 | +}) |
| 102 | + |
| 103 | +test("renders labeled field for password type", async () => { |
| 104 | + const inputType = "password" |
| 105 | + const testPassword = "pass" |
| 106 | + let value = "" |
| 107 | + const onChangeHandle = (val) => { |
| 108 | + value = val.currentTarget.value |
| 109 | + } |
| 110 | + |
| 111 | + render( |
| 112 | + <MyForm |
| 113 | + name={name} |
| 114 | + placeholder={placeholder} |
| 115 | + onChangeCallback={onChangeHandle} |
| 116 | + onSubmit={(val) => {}} |
| 117 | + label={label} |
| 118 | + inputType={inputType} |
| 119 | + ></MyForm> |
| 120 | + ) |
| 121 | + |
| 122 | + const elView = screen.getByTestId("labeledinput-testid") |
| 123 | + const input = screen.getByPlaceholderText(placeholder) |
| 124 | + expect(elView).toBeInTheDocument() |
| 125 | + expect(input).toBeInTheDocument() |
| 126 | + expect(input).not.toHaveAttribute("value", testPassword) |
| 127 | + fireEvent.change(input, { target: { value: testPassword } }) |
| 128 | + expect(value).equal(testPassword) |
| 129 | + expect(input).toHaveAttribute("type", inputType) |
| 130 | + expect(input).not.toHaveAttribute("type", "text") |
| 131 | +}) |
0 commit comments