import { render, screen } from "@testing-library/react";
import { userEvent } from "@testing-library/user-event";
import { DrawerCustomTrigger } from "./DrawerCustomTrigger";
import { DrawerRoot } from "./DrawerRoot";
describe("DrawerCustomTrigger", () => {
const customRender = (ui, contextProps) => {
return render({ui});
};
test("renders visibly into the document", () => {
render(Trigger Drawer);
expect(screen.getByText("Trigger Drawer")).toBeVisible();
});
test("sets aria-expanded based on context open", () => {
customRender(Trigger Drawer, {
open: true,
});
expect(screen.getByRole("button", { expanded: true })).toBeVisible();
});
test("sets aria-controls based on context contentId", () => {
customRender(Trigger Drawer, {
id: "test-id",
});
expect(screen.getByRole("button")).toHaveAttribute(
"aria-controls",
"test-id"
);
});
test("uses openChange handler from context", async () => {
const user = userEvent.setup();
const handleChange = jest.fn();
customRender(Trigger Drawer, {
onOpenChange: handleChange,
});
await user.click(screen.getByRole("button"));
expect(handleChange).toHaveBeenCalledWith(true);
});
test("has keyboard accessibility", async () => {
const user = userEvent.setup();
const handleChange = jest.fn();
customRender(
Trigger Drawer,
{
onOpenChange: handleChange,
}
);
await user.tab();
expect(screen.getByRole("button")).toHaveFocus();
await user.keyboard("a");
expect(handleChange).not.toHaveBeenCalled();
await user.keyboard("{enter}");
expect(handleChange).toHaveBeenCalledWith(true);
});
});