2026-09-02 09:08:50 -07:00
|
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
|
import { render, screen, fireEvent } from "@testing-library/react";
|
|
|
|
|
import Button from "./Button";
|
|
|
|
|
|
|
|
|
|
const onClick = vi.fn();
|
|
|
|
|
const onKeyDown = vi.fn();
|
|
|
|
|
|
|
|
|
|
describe("Button", () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.clearAllMocks();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("still supports the native disabled attribute", () => {
|
|
|
|
|
render(
|
|
|
|
|
<Button disabled onClick={onClick}>
|
|
|
|
|
Save
|
|
|
|
|
</Button>,
|
|
|
|
|
);
|
|
|
|
|
expect(screen.getByRole("button", { name: "Save" })).toBeDisabled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("stays in the accessibility tree when unavailable, and says why", () => {
|
|
|
|
|
render(
|
|
|
|
|
<Button unavailable unavailableReason="Stop the container first.">
|
|
|
|
|
Save
|
|
|
|
|
</Button>,
|
|
|
|
|
);
|
|
|
|
|
const button = screen.getByRole("button", { name: "Save" });
|
|
|
|
|
expect(button).not.toBeDisabled();
|
|
|
|
|
expect(button).toHaveAttribute("aria-disabled", "true");
|
|
|
|
|
expect(button).toHaveAccessibleDescription("Stop the container first.");
|
|
|
|
|
// The reason is a description, not part of the name.
|
|
|
|
|
expect(button).toHaveAccessibleName("Save");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("guards clicks and Enter/Space while unavailable", () => {
|
|
|
|
|
render(
|
|
|
|
|
<Button unavailable unavailableReason="Stop the container first." onClick={onClick}>
|
|
|
|
|
Save
|
|
|
|
|
</Button>,
|
|
|
|
|
);
|
|
|
|
|
const button = screen.getByRole("button", { name: "Save" });
|
|
|
|
|
fireEvent.click(button);
|
|
|
|
|
fireEvent.keyDown(button, { key: "Enter" });
|
|
|
|
|
fireEvent.keyDown(button, { key: " " });
|
|
|
|
|
expect(onClick).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("still forwards keys that are not activation keys", () => {
|
|
|
|
|
render(
|
|
|
|
|
<Button
|
|
|
|
|
unavailable
|
|
|
|
|
unavailableReason="Stop the container first."
|
|
|
|
|
onKeyDown={onKeyDown}
|
|
|
|
|
>
|
|
|
|
|
Save
|
|
|
|
|
</Button>,
|
|
|
|
|
);
|
|
|
|
|
fireEvent.keyDown(screen.getByRole("button", { name: "Save" }), {
|
|
|
|
|
key: "Escape",
|
|
|
|
|
});
|
|
|
|
|
expect(onKeyDown).toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("behaves like an ordinary button when available", () => {
|
|
|
|
|
render(
|
|
|
|
|
<Button unavailable={false} unavailableReason="Stop the container first." onClick={onClick}>
|
|
|
|
|
Save
|
|
|
|
|
</Button>,
|
|
|
|
|
);
|
|
|
|
|
const button = screen.getByRole("button", { name: "Save" });
|
|
|
|
|
expect(button).not.toHaveAttribute("aria-disabled");
|
|
|
|
|
expect(button).toHaveAccessibleDescription("");
|
|
|
|
|
fireEvent.click(button);
|
|
|
|
|
expect(onClick).toHaveBeenCalledTimes(1);
|
|
|
|
|
});
|
2026-11-17 12:16:55 -08:00
|
|
|
|
|
|
|
|
it("wraps a long label only when asked", () => {
|
|
|
|
|
render(
|
|
|
|
|
<>
|
|
|
|
|
<Button>Short</Button>
|
|
|
|
|
<Button wrap size="md">
|
|
|
|
|
Request an environment from an admin…
|
|
|
|
|
</Button>
|
|
|
|
|
</>,
|
|
|
|
|
);
|
|
|
|
|
const plain = screen.getByRole("button", { name: "Short" });
|
|
|
|
|
expect(plain.className).toContain("whitespace-nowrap");
|
|
|
|
|
expect(plain.className).toContain("h-6");
|
|
|
|
|
const long = screen.getByRole("button", { name: "Request an environment from an admin…" });
|
|
|
|
|
expect(long.className).toContain("whitespace-normal");
|
|
|
|
|
expect(long.className).toContain("min-h-8");
|
|
|
|
|
expect(long.className).toContain("max-w-full");
|
|
|
|
|
expect(long.className).not.toMatch(/(^|\s)h-8(\s|$)/);
|
|
|
|
|
});
|
2026-09-02 09:08:50 -07:00
|
|
|
});
|