From fedbb037b7b3a62f50a38c8c8046af8fa1f6333b Mon Sep 17 00:00:00 2001 From: quality Date: Wed, 30 Sep 2026 23:32:23 -0400 Subject: [PATCH] test: add behavior tests for DocsSidebar collapse state and navigation Covers src/components/docs/DocsSidebar.tsx: mount-time findActivePath/ collapseAll state (nested active page, theme.collapsed=false, general sections, docs guide), getFirstChildRoute recursion and the no-route toggle button, folder and project link-vs-chevron toggle semantics, hidden index/_meta/#/Separator items, the requestAnimationFrame scroll throttle, and mobile close / footer collapse wiring. DocsSidebar.tsx statements 76.97% -> 91.16%; the only remaining uncovered region is renderLegacyGroup, which is never called. Closes #147 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: quality --- changelog.d/added-docs-sidebar-tests.md | 1 + src/__tests__/DocsSidebar.behavior.test.tsx | 474 ++++++++++++++++++++ 2 files changed, 475 insertions(+) create mode 100644 changelog.d/added-docs-sidebar-tests.md create mode 100644 src/__tests__/DocsSidebar.behavior.test.tsx diff --git a/changelog.d/added-docs-sidebar-tests.md b/changelog.d/added-docs-sidebar-tests.md new file mode 100644 index 0000000..dd28564 --- /dev/null +++ b/changelog.d/added-docs-sidebar-tests.md @@ -0,0 +1 @@ +- Add behavior tests for the DocsSidebar component (mount-time collapse state, folder and project row toggles, scroll offset throttling). diff --git a/src/__tests__/DocsSidebar.behavior.test.tsx b/src/__tests__/DocsSidebar.behavior.test.tsx new file mode 100644 index 0000000..bfb2e57 --- /dev/null +++ b/src/__tests__/DocsSidebar.behavior.test.tsx @@ -0,0 +1,474 @@ +// @vitest-environment jsdom +// +// Covers the branches of src/components/docs/DocsSidebar.tsx that +// DocsChrome.behavior.test.tsx leaves untouched: +// - mount-time collapse state: the folder chain leading to the active page +// stays open (findActivePath), siblings collapse (collapseAll), and +// `theme.collapsed === false` pins a folder open +// - general sections (Community/Contributing/News): expanded only when the +// current path is inside them, with their own nested active-path handling +// - getFirstChildRoute(): recursion through nested folders and the +// no-navigable-route fallback that renders a plain toggle button +// - folder click expands a collapsed folder without re-collapsing an +// open one; the chevron button toggles either way +// - hidden items (index, _meta, '#', Separator, Meta, blank title) are not +// rendered +// - active-project header button collapses/expands the whole tree; the tree +// is omitted when the pageMap has no project items +// - non-active project rows: label link expands a collapsed row, chevron +// toggles, and the docs guide path renders every project as a link +// - scroll handling: requestAnimationFrame-throttled offset recalculation +// - mobile close button and SidebarFooter collapse wiring through DocsProvider +import React, { type ReactNode } from "react"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { + act, + cleanup, + fireEvent, + render, + screen, +} from "@testing-library/react"; + +vi.mock("next/link", () => ({ + default: ({ + href, + children, + ...rest + }: { + href?: string; + children?: ReactNode; + }) => ( + key !== "prefetch") + )} + > + {children} + + ), +})); + +let pathname = "/docs/hive/overview/introduction"; +vi.mock("next/navigation", () => ({ + usePathname: () => pathname, +})); + +import { DocsProvider } from "../components/docs/DocsProvider"; +import { DocsSidebar } from "../components/docs/DocsSidebar"; + +type MenuItem = React.ComponentProps["pageMap"][number]; + +function renderSidebar(pageMap: MenuItem[], projectId = "hive") { + return render( + + + + ); +} + +/** The chevron