Files
twitch-panels/tests/unit/components/text/TextInput.test.ts
T

69 lines
1.9 KiB
TypeScript

import { render, screen, fireEvent } from "@testing-library/svelte";
import { describe, expect, it, vi } from "vitest";
import TextInput from "$components/text/TextInput.svelte";
describe("TextInput.svelte", () => {
it("should render with initial value", () => {
const { container } = render(TextInput, {
props: {
text: "Test text",
onenter: vi.fn(),
},
});
const input = container.querySelector("input");
expect(input).toBeInTheDocument();
expect(input).toHaveValue("Test text");
});
it("should update value on input", async () => {
const { container } = render(TextInput, {
props: {
text: "Initial",
onenter: vi.fn(),
},
});
const input = container.querySelector("input");
if (!input) throw new Error("Input element not found");
await fireEvent.input(input, { target: { value: "Updated text" } });
expect(input).toHaveValue("Updated text");
});
it("should call onenter when Enter key is pressed", async () => {
const onenter = vi.fn();
const { container } = render(TextInput, {
props: {
text: "Test",
onenter,
},
});
const input = container.querySelector("input");
if (!input) throw new Error("Input element not found");
await fireEvent.keyDown(input, { key: "Enter" });
expect(onenter).toHaveBeenCalledTimes(1);
});
it("should not call onenter when other keys are pressed", async () => {
const onenter = vi.fn();
const { container } = render(TextInput, {
props: {
text: "Test",
onenter,
},
});
const input = container.querySelector("input");
if (!input) throw new Error("Input element not found");
await fireEvent.keyDown(input, { key: "Escape" });
await fireEvent.keyDown(input, { key: "Tab" });
expect(onenter).not.toHaveBeenCalled();
});
});