Added tests for buttons

This commit is contained in:
2025-11-17 23:08:37 -05:00
parent 7d9d0bdf73
commit 91c8169e97
22 changed files with 3342 additions and 1 deletions

View File

@@ -0,0 +1,311 @@
import DarkButton from "$/component/button/DarkButton";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, expect, it, vi } from "vitest";
const buttonText = "Dark Button";
describe("DarkButton Component", () => {
it("Renders with defaults", () => {
render(<DarkButton>{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
//Check button requirements
expect(button).toBeInTheDocument();
expect(button).toHaveTextContent(buttonText);
//Classes
expect(button).toHaveClass("rounded-lg");
expect(button).toHaveClass("px-4", "py-2");
expect(button).toHaveClass("border");
expect(button).toHaveClass("cursor-pointer");
//Colors
expect(button).toHaveClass("bg-black", "hover:bg-neutral-700", "active:bg-neutral-600");
expect(button).toHaveClass("border-black", "hover:border-neutral-700", "active:border-neutral-600");
expect(button).toHaveClass("text-white");
});
it("Renders with custom className", () => {
const customClass = "custom-dark-button-class";
render(<DarkButton className={customClass}>{buttonText}</DarkButton>);
//Check button requirements
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass(customClass);
});
it("Renders with aria-label", () => {
const ariaLabel = "Dark Button Aria Label";
render(<DarkButton aria-label={ariaLabel}>{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveAttribute("aria-label", ariaLabel);
});
it("Renders with onClick handler", async () => {
const handleClick = vi.fn();
render(<DarkButton onClick={handleClick}>{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
await userEvent.click(button);
expect(handleClick).toHaveBeenCalledTimes(1);
});
describe("DarkButton Component Rounding", () => {
it("Renders with small rounding", () => {
render(<DarkButton rounding="sm">{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("rounded-sm");
});
it("Renders with medium rounding", () => {
render(<DarkButton rounding="md">{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("rounded-md");
});
it("Renders with large rounding", () => {
render(<DarkButton rounding="lg">{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("rounded-lg");
});
it("Renders with full rounding", () => {
render(<DarkButton rounding="full">{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("rounded-full");
});
});
describe("DarkButton Component Size and Shape", () => {
it("Renders with xs square", () => {
render(<DarkButton size="xs" shape="square">{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("p-0");
});
it("Renders with sm square", () => {
render(<DarkButton size="sm" shape="square">{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("p-1");
});
it("Renders with md square", () => {
render(<DarkButton size="md" shape="square">{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("p-2");
});
it("Renders with lg square", () => {
render(<DarkButton size="lg" shape="square">{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("p-3");
});
it("Renders with xl square", () => {
render(<DarkButton size="xl" shape="square">{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("p-4");
});
it("Renders with xs rectangle", () => {
render(<DarkButton size="xs" shape="rectangle">{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("px-1", "py-0");
});
it("Renders with sm rectangle", () => {
render(<DarkButton size="sm" shape="rectangle">{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("px-2", "py-1");
});
it("Renders with md rectangle", () => {
render(<DarkButton size="md" shape="rectangle">{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("px-4", "py-2");
});
it("Renders with lg rectangle", () => {
render(<DarkButton size="lg" shape="rectangle">{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("px-6", "py-3");
});
it("Renders with xl rectangle", () => {
render(<DarkButton size="xl" shape="rectangle">{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("px-8", "py-4");
});
});
});
describe("DarkButton Component Disabled", () => {
it("Render with defaults when disabled", () => {
render(<DarkButton disabled>{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
//Check button requirements
expect(button).toBeInTheDocument();
expect(button).toHaveTextContent(buttonText);
//Classes
expect(button).toHaveClass("rounded-lg");
expect(button).toHaveClass("px-4", "py-2");
expect(button).toHaveClass("border");
//Disabled state
expect(button).not.toHaveClass("cursor-pointer");
expect(button).toHaveClass("bg-neutral-700/80");
expect(button).toHaveClass("border-neutral-700/80");
expect(button).toHaveClass("text-white");
});
it("Renders with custom className when disabled", () => {
const customClass = "custom-dark-button-class";
render(<DarkButton className={customClass} disabled>{buttonText}</DarkButton>);
//Check button requirements
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass(customClass);
});
it("Renders with aria-label when disabled", () => {
const ariaLabel = "Dark Button Aria Label";
render(<DarkButton aria-label={ariaLabel} disabled>{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveAttribute("aria-label", ariaLabel);
});
it("Does not trigger onClick handler when disabled", async () => {
const handleClick = vi.fn();
render(<DarkButton onClick={handleClick} disabled>{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
await userEvent.click(button);
expect(handleClick).toHaveBeenCalledTimes(0);
});
});
describe("DarkButton Component Variants", () => {
it("Renders with standard variant", () => {
render(<DarkButton variant="standard">{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("bg-black", "hover:bg-neutral-700", "active:bg-neutral-600");
expect(button).toHaveClass("border", "border-black", "hover:border-neutral-700", "active:border-neutral-600");
expect(button).toHaveClass("text-white");
});
it("Renders with outline variant", () => {
render(<DarkButton variant="outline">{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("bg-transparent");
expect(button).toHaveClass("border", "border-black", "hover:border-neutral-700", "active:border-neutral-600");
expect(button).toHaveClass("text-black", "hover:text-neutral-700", "active:text-neutral-600");
});
it("Renders with outline-ghost variant", () => {
render(<DarkButton variant="outline-ghost">{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("bg-transparent", "hover:bg-black", "active:bg-neutral-700");
expect(button).toHaveClass("border", "border-black", "hover:border-black", "active:border-neutral-700");
expect(button).toHaveClass("text-black", "hover:text-white", "active:text-white");
});
it("Renders with ghost variant", () => {
render(<DarkButton variant="ghost">{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("bg-transparent", "hover:bg-black", "active:bg-neutral-700");
expect(button).toHaveClass("border-none");
expect(button).toHaveClass("text-black", "hover:text-white", "active:text-white");
});
it("Renders with icon variant", () => {
render(<DarkButton variant="icon">{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("bg-transparent");
expect(button).toHaveClass("border-none");
expect(button).toHaveClass("text-black", "hover:text-neutral-700", "active:text-neutral-600");
});
});
describe("DarkButton Component Variants Disabled", () => {
it("Renders with standard variant when disabled", () => {
render(<DarkButton variant="standard" disabled>{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("bg-neutral-700/80");
expect(button).toHaveClass("border", "border-neutral-700/80");
expect(button).toHaveClass("text-white");
});
it("Renders with outline variant when disabled", () => {
render(<DarkButton variant="outline" disabled>{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("bg-transparent");
expect(button).toHaveClass("border", "border-neutral-700/80");
expect(button).toHaveClass("text-neutral-700/80");
});
it("Renders with outline-ghost variant when disabled", () => {
render(<DarkButton variant="outline-ghost" disabled>{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("bg-transparent");
expect(button).toHaveClass("border", "border-neutral-700/80");
expect(button).toHaveClass("text-neutral-700/80");
});
it("Renders with ghost variant when disabled", () => {
render(<DarkButton variant="ghost" disabled>{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("bg-transparent");
expect(button).toHaveClass("border-none");
expect(button).toHaveClass("text-neutral-700/80");
});
it("Renders with icon variant when disabled", () => {
render(<DarkButton variant="icon" disabled>{buttonText}</DarkButton>);
const button = screen.getByTestId("mattrixwv-dark-button");
expect(button).toBeInTheDocument();
expect(button).toHaveClass("bg-transparent");
expect(button).toHaveClass("border-none");
expect(button).toHaveClass("text-neutral-700/80");
});
});