-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
46 additions
and
63 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,80 +1,63 @@ | ||
import { cleanup, fireEvent, render } from "@testing-library/react" | ||
import { afterEach, describe, expect, it, vi } from "vitest" | ||
|
||
import { Textarea } from "@/components/textarea" // Adjust the import path as necessary | ||
|
||
afterEach(() => { | ||
cleanup() | ||
}) | ||
import { Textarea } from "@/components/textarea" | ||
|
||
describe("Textarea component", () => { | ||
describe("Controlled behavior", () => { | ||
it("updates value when provided as a prop", () => { | ||
const { getByRole, rerender } = render(<Textarea value="" />) | ||
const textareaElement = getByRole("textbox") as HTMLTextAreaElement | ||
|
||
expect(textareaElement.value).toBe("") | ||
|
||
rerender(<Textarea value="updated value" />) | ||
|
||
expect(textareaElement.value).toBe("updated value") | ||
}) | ||
|
||
it("calls onChange handler when typing", () => { | ||
const handleChange = vi.fn() | ||
const { getByRole } = render(<Textarea value="" onChange={handleChange} />) | ||
const textareaElement = getByRole("textbox") as HTMLTextAreaElement | ||
|
||
fireEvent.change(textareaElement, { target: { value: "test input" } }) | ||
afterEach(() => { | ||
cleanup() | ||
}) | ||
|
||
expect(handleChange).toHaveBeenCalledTimes(1) | ||
expect(handleChange).toHaveBeenCalledWith("test input") | ||
}) | ||
it("renders with provided props", () => { | ||
const handleChange = vi.fn() | ||
const { getByRole, getByText } = render( | ||
<Textarea | ||
label="Test Label" | ||
value="Test Value" | ||
onChange={handleChange} | ||
placeholder="Test Placeholder" | ||
errorMessage="Test Error" | ||
rows={3} | ||
/> | ||
) | ||
const textarea = getByRole("textbox") as HTMLTextAreaElement | ||
expect(textarea.value).toBe("Test Value") | ||
expect(textarea.getAttribute("placeholder")).toBe("Test Placeholder") | ||
expect(textarea.getAttribute("rows")).toBe("3") | ||
expect(getByText("Test Label")).toBeDefined() | ||
expect(getByText("Test Error")).toBeDefined() | ||
}) | ||
|
||
describe("Uncontrolled behavior", () => { | ||
it("updates its value when typing without onChange handler", () => { | ||
const { getByRole } = render(<Textarea />) | ||
const textareaElement = getByRole("textbox") as HTMLTextAreaElement | ||
it("calls onChange when value changes", () => { | ||
const handleChange = vi.fn() | ||
const { getByRole } = render(<Textarea onChange={handleChange} />) | ||
const textarea = getByRole("textbox") | ||
|
||
fireEvent.change(textareaElement, { target: { value: "test input" } }) | ||
fireEvent.change(textarea, { target: { value: "New Value" } }) | ||
|
||
expect(textareaElement.value).toBe("test input") | ||
}) | ||
expect(handleChange).toHaveBeenCalledTimes(1) | ||
expect(handleChange).toHaveBeenCalledWith("New Value") | ||
}) | ||
|
||
describe("Props and rendering", () => { | ||
it("renders with a label when provided", () => { | ||
const { getByText } = render(<Textarea label="Test Label" />) | ||
expect(getByText("Test Label")).toBeDefined() | ||
}) | ||
|
||
it("displays an error message when provided", () => { | ||
const { getByText } = render(<Textarea errorMessage="This is an error" />) | ||
expect(getByText("This is an error")).toBeDefined() | ||
}) | ||
|
||
it("applies custom className", () => { | ||
const { container } = render(<Textarea className="custom-class" />) | ||
expect(container.querySelector(".custom-class")).toBeDefined() | ||
}) | ||
it("applies custom className", () => { | ||
const { container } = render(<Textarea className="custom-class" />) | ||
const textareaElement = container.querySelector("textarea") | ||
expect(textareaElement?.classList.contains("custom-class")).toBe(true) | ||
}) | ||
|
||
it("sets the correct number of rows", () => { | ||
const { getByRole } = render(<Textarea rows={10} />) | ||
const textareaElement = getByRole("textbox") as HTMLTextAreaElement | ||
expect(textareaElement.rows).toBe(10) | ||
}) | ||
it("uses default rows when not specified", () => { | ||
const { getByRole } = render(<Textarea />) | ||
const textarea = getByRole("textbox") | ||
expect(textarea.getAttribute("rows")).toBe("5") | ||
}) | ||
|
||
it("uses default 5 rows when not specified", () => { | ||
const { getByRole } = render(<Textarea />) | ||
const textareaElement = getByRole("textbox") as HTMLTextAreaElement | ||
expect(textareaElement.rows).toBe(5) | ||
}) | ||
it("does not render label when not provided", () => { | ||
const { queryByText } = render(<Textarea />) | ||
expect(queryByText(/label/i)).toBeNull() | ||
}) | ||
|
||
it("sets the placeholder correctly", () => { | ||
const placeholder = "Enter your message here" | ||
const { getByPlaceholderText } = render(<Textarea placeholder={placeholder} />) | ||
expect(getByPlaceholderText(placeholder)).toBeDefined() | ||
}) | ||
it("does not render error message when not provided", () => { | ||
const { queryByText } = render(<Textarea />) | ||
expect(queryByText(/error/i)).toBeNull() | ||
}) | ||
}) |