);
const detailsElement = screen.getByText("Test").closest("details");
expect(detailsElement).not.toHaveAttribute("open");
});
it("should be open when open prop is true", () => {
render(
Content
);
const detailsElement = screen.getByText("Test").closest("details");
expect(detailsElement).toHaveAttribute("open");
});
it("should toggle open state on summary click", async () => {
const user = userEvent.setup();
render(
Content
);
const summaryElement = screen.getByText("Test");
const detailsElement = summaryElement.closest("details")!;
// Initially closed
expect(detailsElement).not.toHaveAttribute("open");
// Click to open
await user.click(summaryElement);
expect(detailsElement).toHaveAttribute("open");
// Click to close
await user.click(summaryElement);
expect(detailsElement).not.toHaveAttribute("open");
});
});
describe("Keyboard Interaction", () => {
it("should be keyboard accessible with focusable summary", () => {
render(
Content
);
const summaryElement = screen.getByText("Test");
summaryElement.focus();
// Summary element should be focusable for keyboard navigation
expect(summaryElement).toHaveFocus();
expect(summaryElement.tagName).toBe("SUMMARY");
});
it("should support keyboard interaction through native browser behavior", () => {
// Note: Keyboard interaction (Space, Enter) on is handled
// natively by the browser and is part of the HTML5 spec.
// These interactions are tested in Storybook interaction tests
// where the browser's native behavior is fully available.
render(
Content
);
const summaryElement = screen.getByText("Test");
const detailsElement = summaryElement.closest("details");
// Verify semantic HTML structure that enables keyboard support
expect(summaryElement.tagName).toBe("SUMMARY");
expect(detailsElement?.tagName).toBe("DETAILS");
});
it("should call onToggle callback when interaction occurs", async () => {
const user = userEvent.setup();
const handleToggle = vi.fn();
render(
);
const summaryElement = screen.getByText("Test");
await user.click(summaryElement);
// Should only be called once per toggle
expect(handleToggle).toHaveBeenCalledTimes(1);
});
});
describe("Accordion Behavior", () => {
it("should support name attribute for accordion groups", () => {
render(
<>
Content 1
Content 2
>
);
const firstDetails = screen.getByText("First").closest("details");
const secondDetails = screen.getByText("Second").closest("details");
expect(firstDetails).toHaveAttribute("name", "accordion-group");
expect(secondDetails).toHaveAttribute("name", "accordion-group");
});
it("should configure accordion group with name attribute", () => {
// Note: The exclusive open/close behavior of accordion groups
// is handled natively by browsers and tested in Storybook.
// Here we verify the name attribute is correctly applied.
render(
<>
Content 1
Content 2
>
);
const firstDetails = screen.getByText("First").closest("details");
const secondDetails = screen.getByText("Second").closest("details");
// Both should have the same name for accordion grouping
expect(firstDetails).toHaveAttribute("name", "test-accordion");
expect(secondDetails).toHaveAttribute("name", "test-accordion");
});
});
describe("Accessibility", () => {
it("should use semantic details element", () => {
render(