test: make tests less fragile
This commit is contained in:
@@ -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" });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user