diff --git a/src/features/vcs/__tests__/logUserFilter.test.tsx b/src/features/vcs/__tests__/logUserFilter.test.tsx new file mode 100644 index 0000000000..c8d617f00b --- /dev/null +++ b/src/features/vcs/__tests__/logUserFilter.test.tsx @@ -0,0 +1,76 @@ +import { fireEvent, render, screen, within } from "@testing-library/react"; +import { beforeEach, describe, expect, it, vi } from "vitest"; + +const authors = Array.from({ length: 40 }, (_, i) => `Author ${String(i).padStart(2, "0")} `); + +vi.mock("@/platform/ipc", async (importOriginal) => { + const real = await importOriginal(); + const ok = (data: T) => Promise.resolve({ status: "ok" as const, data }); + return { + ...real, + commands: { + ...real.commands, + vcsBranches: () => ok([]), + vcsTags: () => ok([]), + vcsLog: () => ok({ commits: [], hasMore: false }), + vcsConfigGet: () => ok(null), + vcsLogUsers: () => ok(authors), + }, + }; +}); + +import { ContextMenuHost } from "@/ui"; +import { FilterBar } from "../log/FilterBar"; +import { fetchLogUsers } from "../log/logUsers"; +import { emptyFilters, useGitLog } from "../logStore"; +import { useVcs } from "../store"; + +describe("Git Log User filter menu (#7254)", () => { + beforeEach(async () => { + localStorage.clear(); + useVcs.setState({ root: "/p", repo: "/p", roots: ["/p"] }); + useGitLog.setState({ filters: emptyFilters() }); + await fetchLogUsers("/p"); + }); + + const openUserMenu = () => { + render( + <> + + + , + ); + fireEvent.click(screen.getByRole("button", { name: /^User/ })); + const menu = screen.getByRole("menu"); + return within(menu) + .getAllByRole("menuitem") + .map((item) => item.textContent?.trim()); + }; + + it("puts Select… and me first, then the recent filters, then the authors", () => { + localStorage.setItem( + "openidea.vcs.logRecent.v1:/p:authors", + JSON.stringify([["Author 03 ", "Author 07 "]]), + ); + const items = openUserMenu(); + expect(items.slice(0, 4)).toEqual([ + "Select…", + "me", + "Author 03 , Author 07 ", + "Author 00 ", + ]); + expect(screen.getByText("Recent")).toBeInTheDocument(); + }); + + it("has no Recent header without recent filters", () => { + const items = openUserMenu(); + expect(items.slice(0, 3)).toEqual(["Select…", "me", "Author 00 "]); + expect(screen.queryByText("Recent")).toBeNull(); + }); + + it("sets the filter to a single author on click", () => { + openUserMenu(); + fireEvent.click(screen.getByRole("menuitem", { name: "Author 05 " })); + expect(useGitLog.getState().filters.authors).toEqual(["Author 05 "]); + }); +}); diff --git a/src/features/vcs/log/FilterBar.tsx b/src/features/vcs/log/FilterBar.tsx index a17b369d09..3e89af6f66 100644 --- a/src/features/vcs/log/FilterBar.tsx +++ b/src/features/vcs/log/FilterBar.tsx @@ -214,22 +214,14 @@ export function FilterBar({ fileHistory = false }: { fileHistory?: boolean }) { a.localeCompare(b), ); const authors = authorsOf(roots.map((repo) => knownLogUsers(repo))); + const recent: MenuItem[] = recentFilterValues("authors").map((authors) => ({ + verbatim: true, + label: authors.join(", "), + run: () => setLogFilters({ authors }), + })); + // IDEA's order: Select… and me first, so both stay in view however many authors follow, + // then the recent filters (several users apply in one click). menuBelow(anchor, [ - ...recentFilterValues("authors").map((authors) => ({ - verbatim: true, - label: authors.join(", "), - run: () => setLogFilters({ authors }), - })), - // A click sets the filter to that user alone, as IDEA does (several users go through Select…), so the - // rows carry no check marks that would read as a multi-select. - { label: ME, run: () => setLogFilters({ authors: [ME] }) }, - ...(authors.length ? [{ kind: "separator" as const }] : []), - ...authors.slice(0, 30).map((a) => ({ - verbatim: true, - label: a, - run: () => setLogFilters({ authors: [a] }), - })), - { kind: "separator" }, { label: "Select…", run: async () => { @@ -242,6 +234,16 @@ export function FilterBar({ fileHistory = false }: { fileHistory?: boolean }) { if (r) setLogFilters({ authors: r }); }, }, + // A click sets the filter to that user alone, as IDEA does (several users go through Select…), so the + // rows carry no check marks that would read as a multi-select. + { label: ME, run: () => setLogFilters({ authors: [ME] }) }, + ...(recent.length ? [{ kind: "header" as const, label: "Recent" }, ...recent] : []), + ...(authors.length ? [{ kind: "separator" as const }] : []), + ...authors.slice(0, 30).map((a) => ({ + verbatim: true, + label: a, + run: () => setLogFilters({ authors: [a] }), + })), ]); };