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