|
| 1 | +// Auto-generated test file from .dev.mdx documentation |
| 2 | +// DO NOT EDIT - This file is generated by validate-docs-tests.ts |
| 3 | + |
| 4 | +import { describe, it, expect, vi } from "vitest"; |
| 5 | +import type { ReactNode } from "react"; |
| 6 | +import userEvent from "@testing-library/user-event"; |
| 7 | +import { NimbusProvider } from "@commercetools/nimbus"; |
| 8 | +import { TextInput } from "@commercetools/nimbus"; |
| 9 | +import { render, screen } from "@testing-library/react"; |
| 10 | + |
| 11 | +// Wrapper utility to provide NimbusProvider context |
| 12 | +function renderWithProvider(ui: React.ReactElement) { |
| 13 | + const result = render(<NimbusProvider>{ui}</NimbusProvider>); |
| 14 | + |
| 15 | + // Wrap rerender to also use provider |
| 16 | + const originalRerender = result.rerender; |
| 17 | + result.rerender = (rerenderUi: ReactNode) => { |
| 18 | + return originalRerender(<NimbusProvider>{rerenderUi}</NimbusProvider>); |
| 19 | + }; |
| 20 | + |
| 21 | + return result; |
| 22 | +} |
| 23 | + |
| 24 | +// Source: packages/nimbus/src/components/text-input/text-input.dev.mdx (Basic rendering tests) |
| 25 | +describe("TextInput - Basic rendering", () => { |
| 26 | + it("renders input element", () => { |
| 27 | + renderWithProvider(<TextInput placeholder="Enter text" />); |
| 28 | + |
| 29 | + // Verify input is present |
| 30 | + expect(screen.getByRole("textbox")).toBeInTheDocument(); |
| 31 | + }); |
| 32 | + |
| 33 | + it("renders with placeholder text", () => { |
| 34 | + renderWithProvider(<TextInput placeholder="Email address" />); |
| 35 | + |
| 36 | + expect(screen.getByPlaceholderText("Email address")).toBeInTheDocument(); |
| 37 | + }); |
| 38 | + |
| 39 | + it("renders with aria-label", () => { |
| 40 | + renderWithProvider(<TextInput aria-label="User email" />); |
| 41 | + |
| 42 | + expect( |
| 43 | + screen.getByRole("textbox", { name: /user email/i }) |
| 44 | + ).toBeInTheDocument(); |
| 45 | + }); |
| 46 | +}); |
| 47 | + |
| 48 | +// Source: packages/nimbus/src/components/text-input/text-input.dev.mdx (Interaction tests) |
| 49 | +describe("TextInput - Interactions", () => { |
| 50 | + it("updates value when user types", async () => { |
| 51 | + const user = userEvent.setup(); |
| 52 | + renderWithProvider(<TextInput placeholder="Type here" />); |
| 53 | + |
| 54 | + const input = screen.getByRole("textbox"); |
| 55 | + await user.type(input, "Hello World"); |
| 56 | + |
| 57 | + expect(input).toHaveValue("Hello World"); |
| 58 | + }); |
| 59 | + |
| 60 | + it("calls onChange callback with string value", async () => { |
| 61 | + const user = userEvent.setup(); |
| 62 | + const handleChange = vi.fn(); |
| 63 | + renderWithProvider(<TextInput onChange={handleChange} />); |
| 64 | + |
| 65 | + const input = screen.getByRole("textbox"); |
| 66 | + await user.type(input, "test"); |
| 67 | + |
| 68 | + expect(handleChange).toHaveBeenCalled(); |
| 69 | + expect(typeof handleChange.mock.calls[0][0]).toBe("string"); |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +// Source: packages/nimbus/src/components/text-input/text-input.dev.mdx (Testing controlled mode) |
| 74 | +describe("TextInput - Controlled mode", () => { |
| 75 | + it("displays controlled value", () => { |
| 76 | + renderWithProvider( |
| 77 | + <TextInput value="controlled value" onChange={() => {}} /> |
| 78 | + ); |
| 79 | + |
| 80 | + const input = screen.getByRole("textbox"); |
| 81 | + expect(input).toHaveValue("controlled value"); |
| 82 | + }); |
| 83 | + |
| 84 | + it("updates when controlled value changes", () => { |
| 85 | + const { rerender } = renderWithProvider( |
| 86 | + <TextInput value="first value" onChange={() => {}} /> |
| 87 | + ); |
| 88 | + |
| 89 | + expect(screen.getByRole("textbox")).toHaveValue("first value"); |
| 90 | + |
| 91 | + rerender(<TextInput value="second value" onChange={() => {}} />); |
| 92 | + |
| 93 | + expect(screen.getByRole("textbox")).toHaveValue("second value"); |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +// Source: packages/nimbus/src/components/text-input/text-input.dev.mdx (Testing leading and trailing elements) |
| 98 | +describe("TextInput - Elements", () => { |
| 99 | + it("renders leading element", () => { |
| 100 | + renderWithProvider( |
| 101 | + <TextInput |
| 102 | + leadingElement={<span data-testid="icon">🔍</span>} |
| 103 | + placeholder="Search" |
| 104 | + /> |
| 105 | + ); |
| 106 | + |
| 107 | + expect(screen.getByTestId("icon")).toBeInTheDocument(); |
| 108 | + }); |
| 109 | + |
| 110 | + it("renders trailing element", () => { |
| 111 | + renderWithProvider( |
| 112 | + <TextInput |
| 113 | + trailingElement={<button data-testid="clear">Clear</button>} |
| 114 | + placeholder="Input" |
| 115 | + /> |
| 116 | + ); |
| 117 | + |
| 118 | + expect(screen.getByTestId("clear")).toBeInTheDocument(); |
| 119 | + }); |
| 120 | + |
| 121 | + it("trailing button is interactive", async () => { |
| 122 | + const user = userEvent.setup(); |
| 123 | + const handleClick = vi.fn(); |
| 124 | + |
| 125 | + renderWithProvider( |
| 126 | + <TextInput |
| 127 | + trailingElement={<button onClick={handleClick}>Action</button>} |
| 128 | + /> |
| 129 | + ); |
| 130 | + |
| 131 | + await user.click(screen.getByText("Action")); |
| 132 | + |
| 133 | + expect(handleClick).toHaveBeenCalledTimes(1); |
| 134 | + }); |
| 135 | +}); |
| 136 | + |
| 137 | +// Source: packages/nimbus/src/components/text-input/text-input.dev.mdx (Testing validation states) |
| 138 | +describe("TextInput - Validation states", () => { |
| 139 | + it("renders disabled state", () => { |
| 140 | + renderWithProvider(<TextInput isDisabled placeholder="Disabled" />); |
| 141 | + |
| 142 | + const input = screen.getByRole("textbox"); |
| 143 | + expect(input).toBeDisabled(); |
| 144 | + }); |
| 145 | + |
| 146 | + it("renders invalid state", () => { |
| 147 | + renderWithProvider(<TextInput isInvalid placeholder="Invalid" />); |
| 148 | + |
| 149 | + const input = screen.getByRole("textbox"); |
| 150 | + expect(input).toHaveAttribute("aria-invalid", "true"); |
| 151 | + }); |
| 152 | + |
| 153 | + it("renders read-only state", () => { |
| 154 | + renderWithProvider( |
| 155 | + <TextInput isReadOnly value="Read-only" onChange={() => {}} /> |
| 156 | + ); |
| 157 | + |
| 158 | + const input = screen.getByRole("textbox"); |
| 159 | + expect(input).toHaveAttribute("readonly"); |
| 160 | + }); |
| 161 | + |
| 162 | + it("renders required state", () => { |
| 163 | + renderWithProvider(<TextInput isRequired placeholder="Required" />); |
| 164 | + |
| 165 | + const input = screen.getByRole("textbox"); |
| 166 | + expect(input).toHaveAttribute("aria-required", "true"); |
| 167 | + }); |
| 168 | +}); |
0 commit comments