test: make tests less fragile

This commit is contained in:
2026-02-09 09:57:39 +05:00
parent 2bbe59107c
commit c4103e937d
12 changed files with 49 additions and 55 deletions
+9 -12
View File
@@ -1,10 +1,10 @@
import TextInput from "$components/text/TextInput.svelte";
import { fireEvent, render } from "@testing-library/svelte";
import { fireEvent, render, screen } from "@testing-library/svelte";
import { describe, expect, it, vi } from "vitest";
describe("TextInput.svelte", () => {
it("should render with initial value", () => {
const { container } = render(TextInput, {
render(TextInput, {
props: {
text: "Test text",
onenter: vi.fn(),
@@ -12,13 +12,13 @@ describe("TextInput.svelte", () => {
},
});
const input = container.querySelector("input");
const input = screen.getByRole("textbox", { name: /test input/i });
expect(input).toBeInTheDocument();
expect(input).toHaveValue("Test text");
});
it("should update value on input", async () => {
const { container } = render(TextInput, {
render(TextInput, {
props: {
text: "Initial",
onenter: vi.fn(),
@@ -26,8 +26,7 @@ describe("TextInput.svelte", () => {
},
});
const input = container.querySelector("input");
if (!input) throw new Error("Input element not found");
const input = screen.getByRole("textbox", { name: /test input/i });
await fireEvent.input(input, { target: { value: "Updated text" } });
expect(input).toHaveValue("Updated text");
@@ -35,7 +34,7 @@ describe("TextInput.svelte", () => {
it("should call onenter when Enter key is pressed", async () => {
const onenter = vi.fn();
const { container } = render(TextInput, {
render(TextInput, {
props: {
text: "Test",
onenter,
@@ -43,8 +42,7 @@ describe("TextInput.svelte", () => {
},
});
const input = container.querySelector("input");
if (!input) throw new Error("Input element not found");
const input = screen.getByRole("textbox", { name: /test input/i });
await fireEvent.keyDown(input, { key: "Enter" });
expect(onenter).toHaveBeenCalledTimes(1);
@@ -52,7 +50,7 @@ describe("TextInput.svelte", () => {
it("should not call onenter when other keys are pressed", async () => {
const onenter = vi.fn();
const { container } = render(TextInput, {
render(TextInput, {
props: {
text: "Test",
onenter,
@@ -60,8 +58,7 @@ describe("TextInput.svelte", () => {
},
});
const input = container.querySelector("input");
if (!input) throw new Error("Input element not found");
const input = screen.getByRole("textbox", { name: /test input/i });
await fireEvent.keyDown(input, { key: "Escape" });
await fireEvent.keyDown(input, { key: "Tab" });