test: update tests

This commit is contained in:
2026-02-12 03:44:31 +05:00
parent 9e1eeb57bc
commit 95d7c81cdf
19 changed files with 413 additions and 484 deletions
+40 -9
View File
@@ -1,13 +1,44 @@
import Alignment from "$components/ui/Alignment.svelte";
import { render } from "@testing-library/svelte";
import { describe, it } from "vitest";
import { TextAlign } from "$lib/constants";
import { render, screen } from "@testing-library/svelte";
import userEvent from "@testing-library/user-event";
import { describe, expect, it } from "vitest";
import AlignmentTest from "./AlignmentTest.svelte";
describe("Alignment.svelte", () => {
it("should render without crashing", () => {
render(Alignment, {
props: {
align: "left",
},
});
it("should update state for every button by clicking in sequence", async () => {
const user = userEvent.setup();
render(AlignmentTest);
const buttons = screen.getAllByRole("button");
const stateDisplay = screen.getByTestId("align-value");
const allButtonsToClick = [...buttons, buttons[0]];
for (const button of allButtonsToClick) {
const label = button.getAttribute("aria-label")?.toLowerCase() || "";
await user.click(button);
const finalValue = stateDisplay.textContent?.toLowerCase() || "";
expect(label).toContain(finalValue);
}
});
it("should have initial state from props", () => {
render(AlignmentTest, { props: { align: TextAlign.RIGHT } });
expect(screen.getByTestId("align-value").textContent).toBe(TextAlign.RIGHT);
});
it("should sync with initial state and change state", async () => {
const user = userEvent.setup();
const { rerender } = render(AlignmentTest, { props: { align: TextAlign.RIGHT } });
const stateDisplay = screen.getByTestId("align-value");
expect(stateDisplay.textContent).toBe(TextAlign.RIGHT);
await rerender({ align: TextAlign.CENTER });
const centerBtn = screen.getByRole("button", { name: new RegExp(TextAlign.CENTER, "i") });
expect(centerBtn).toHaveClass("active");
});
});
@@ -0,0 +1,11 @@
<script lang="ts">
import Alignment from "$components/ui/Alignment.svelte";
import { TextAlign } from "$lib/constants";
let { align = TextAlign.LEFT } = $props();
</script>
<Alignment bind:align />
<div data-testid="align-value">{align}</div>
+24 -3
View File
@@ -1,5 +1,6 @@
import Button from "$components/ui/Button.svelte";
import { fireEvent, render, screen } from "@testing-library/svelte";
import { render, screen } from "@testing-library/svelte";
import userEvent from "@testing-library/user-event";
import { describe, expect, it, vi } from "vitest";
import MockIcon from "./MockIcon.svelte";
@@ -25,11 +26,15 @@ describe("Button.svelte", () => {
},
});
expect(screen.getByRole("button", { name: /test button/i })).toBeInTheDocument();
const button = screen.getByRole("button", { name: /test button/i });
expect(button).toBeInTheDocument();
expect(button.textContent.trim()).toBe("");
});
it("should call onclick handler", async () => {
const onclick = vi.fn();
const user = userEvent.setup();
render(Button, {
props: {
icon: MockIcon,
@@ -40,11 +45,27 @@ describe("Button.svelte", () => {
});
const button = screen.getByRole("button", { name: /test button/i });
await fireEvent.click(button);
await user.click(button);
expect(onclick).toHaveBeenCalledTimes(1);
});
it("should not throw error when clicked without onclick prop", async () => {
const user = userEvent.setup();
render(Button, {
props: {
icon: MockIcon,
label: "Click me",
ariaLabel: "Test button",
},
});
const button = screen.getByRole("button", { name: /test button/i });
expect(() => user.click(button)).not.toThrow();
});
it("should be disabled when disabled prop is true", () => {
render(Button, {
props: {
@@ -5,6 +5,7 @@ import { describe, expect, it, vi } from "vitest";
describe("RangeSlider.svelte", () => {
it("should call onchange handler", async () => {
const onchange = vi.fn();
render(RangeSlider, {
props: {
value: 50,