Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/components/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type ButtonHTMLAttributes } from "react";
import { Spinner } from "./Spinner";

type Variant = "primary" | "secondary" | "danger";

Expand All @@ -23,13 +24,21 @@ export function Button({
variant = "primary",
loading = false,
className = "",
disabled,
children,
...rest
}: ButtonProps) {
const isDisabled = disabled || loading;

return (
<button
{...rest}
disabled={isDisabled}
aria-busy={loading || undefined}
className={`rounded-full px-5 py-2 text-sm font-medium disabled:opacity-50 disabled:cursor-not-allowed ${variants[variant]} ${ring} ${className}`}
/>
className={`inline-flex items-center justify-center gap-2 rounded-full px-5 py-2 text-sm font-medium disabled:opacity-50 disabled:cursor-not-allowed ${variants[variant]} ${ring} ${className}`}
>
{loading && <Spinner label="Loading" />}
{children}
</button>
);
}
30 changes: 30 additions & 0 deletions src/components/__tests__/Button.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { render, screen } from "@testing-library/react";
import { Button } from "../Button";

describe("Button", () => {
it("renders children without busy state by default", () => {
render(<Button>Save</Button>);

const button = screen.getByRole("button", { name: "Save" });
expect(button).toBeEnabled();
expect(button).not.toHaveAttribute("aria-busy");
expect(screen.queryByRole("status")).not.toBeInTheDocument();
});

it("disables the button and exposes busy state while loading", () => {
render(<Button loading>Save</Button>);

const button = screen.getByRole("button", { name: /save/i });
expect(button).toBeDisabled();
expect(button).toHaveAttribute("aria-busy", "true");
expect(screen.getByRole("status")).toHaveTextContent("Loading");
});

it("preserves explicit disabled state when not loading", () => {
render(<Button disabled>Delete</Button>);

const button = screen.getByRole("button", { name: "Delete" });
expect(button).toBeDisabled();
expect(button).not.toHaveAttribute("aria-busy");
});
});