|
| 1 | +import { render, screen } from "@testing-library/react" |
| 2 | +import userEvent from "@testing-library/user-event" |
| 3 | +import { ChatInput } from "./ChatInput" |
| 4 | + |
| 5 | +describe("ChatInput", () => { |
| 6 | + it("renders a textarea and submit button", () => { |
| 7 | + render(<ChatInput onSubmit={vi.fn()} />) |
| 8 | + expect(screen.getByRole("textbox")).toBeInTheDocument() |
| 9 | + expect(screen.getByRole("button")).toBeInTheDocument() |
| 10 | + }) |
| 11 | + |
| 12 | + it("calls onSubmit with trimmed value on button click", async () => { |
| 13 | + const user = userEvent.setup() |
| 14 | + const onSubmit = vi.fn() |
| 15 | + render(<ChatInput onSubmit={onSubmit} />) |
| 16 | + await user.type(screen.getByRole("textbox"), " hello ") |
| 17 | + await user.click(screen.getByRole("button")) |
| 18 | + expect(onSubmit).toHaveBeenCalledWith("hello") |
| 19 | + }) |
| 20 | + |
| 21 | + it("calls onSubmit on Enter key", async () => { |
| 22 | + const user = userEvent.setup() |
| 23 | + const onSubmit = vi.fn() |
| 24 | + render(<ChatInput onSubmit={onSubmit} />) |
| 25 | + await user.type(screen.getByRole("textbox"), "spell{Enter}") |
| 26 | + expect(onSubmit).toHaveBeenCalledWith("spell") |
| 27 | + }) |
| 28 | + |
| 29 | + it("does not call onSubmit on Shift+Enter", async () => { |
| 30 | + const user = userEvent.setup() |
| 31 | + const onSubmit = vi.fn() |
| 32 | + render(<ChatInput onSubmit={onSubmit} />) |
| 33 | + await user.type(screen.getByRole("textbox"), "spell") |
| 34 | + await user.keyboard("{Shift>}{Enter}{/Shift}") |
| 35 | + expect(onSubmit).not.toHaveBeenCalled() |
| 36 | + }) |
| 37 | + |
| 38 | + it("clears the textarea after submit", async () => { |
| 39 | + const user = userEvent.setup() |
| 40 | + render(<ChatInput onSubmit={vi.fn()} />) |
| 41 | + const textarea = screen.getByRole("textbox") |
| 42 | + await user.type(textarea, "hello") |
| 43 | + await user.click(screen.getByRole("button")) |
| 44 | + expect(textarea).toHaveValue("") |
| 45 | + }) |
| 46 | + |
| 47 | + it("does not call onSubmit when textarea is empty", async () => { |
| 48 | + const user = userEvent.setup() |
| 49 | + const onSubmit = vi.fn() |
| 50 | + render(<ChatInput onSubmit={onSubmit} />) |
| 51 | + await user.click(screen.getByRole("button")) |
| 52 | + expect(onSubmit).not.toHaveBeenCalled() |
| 53 | + }) |
| 54 | + |
| 55 | + it("button is disabled when textarea is empty", () => { |
| 56 | + render(<ChatInput onSubmit={vi.fn()} />) |
| 57 | + expect(screen.getByRole("button")).toBeDisabled() |
| 58 | + }) |
| 59 | + |
| 60 | + it("textarea and button are disabled when isDisabled is true", async () => { |
| 61 | + const user = userEvent.setup() |
| 62 | + const onSubmit = vi.fn() |
| 63 | + render(<ChatInput onSubmit={onSubmit} isDisabled />) |
| 64 | + expect(screen.getByRole("textbox")).toBeDisabled() |
| 65 | + expect(screen.getByRole("button")).toBeDisabled() |
| 66 | + await user.type(screen.getByRole("textbox"), "test{Enter}") |
| 67 | + expect(onSubmit).not.toHaveBeenCalled() |
| 68 | + }) |
| 69 | +}) |
0 commit comments