fix(web): use consistent PR section toggles (#11763)
This commit is contained in:
1 parent
494cfac249
commit
47ace94962
2 files changed
+193
-72
No files matched your search
@@ -0,0 +1,141 @@
|
||||
import { EnvironmentId, ProjectId, type PullRequestDetailView } from "@t3tools/contracts";
|
||||
import { act } from "react";
|
||||
import { create, type ReactTestRenderer } from "react-test-renderer";
|
||||
import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
|
||||
|
||||
vi.mock("~/state/use-atom-command", () => ({ useAtomCommand: () => vi.fn() }));
|
||||
vi.mock("~/state/pullRequests", () => ({ pullRequestEnvironment: {} }));
|
||||
vi.mock("~/browser/useOpenLink", () => ({ useOpenLink: () => vi.fn() }));
|
||||
vi.mock("./PullRequestMarkdown", () => ({
|
||||
PullRequestMarkdown: ({ text }: { text: string }) => <p>{text}</p>,
|
||||
}));
|
||||
|
||||
import { PullRequestSummaryTab } from "./PullRequestSummaryTab";
|
||||
|
||||
const detail: PullRequestDetailView = {
|
||||
provider: "github",
|
||||
projectId: ProjectId.make("project"),
|
||||
projectTitle: "Project",
|
||||
workspaceRoot: "/workspace",
|
||||
repository: "owner/repo",
|
||||
number: 1,
|
||||
title: "Test pull request",
|
||||
body: "Original description",
|
||||
url: "https://github.com/owner/repo/pull/1",
|
||||
author: { login: "author", name: null, avatarUrl: null },
|
||||
viewer: "author",
|
||||
state: "open",
|
||||
isDraft: false,
|
||||
mergeability: "mergeable",
|
||||
additions: 1,
|
||||
deletions: 0,
|
||||
changedFiles: 1,
|
||||
headBranch: "feature",
|
||||
baseBranch: "main",
|
||||
createdAt: "2026-09-01T00:00:00Z",
|
||||
updatedAt: "2026-09-01T00:00:00Z",
|
||||
mergedAt: null,
|
||||
closedAt: null,
|
||||
reviewers: [],
|
||||
labels: [],
|
||||
checks: [{ name: "Unit tests", status: "success", description: null, url: null }],
|
||||
comments: [],
|
||||
commentCount: 0,
|
||||
commentsTruncated: false,
|
||||
reviewThreads: [],
|
||||
commits: [],
|
||||
mergeCapabilities: { merge: false, squash: false, rebase: false },
|
||||
capabilities: {
|
||||
diff: true,
|
||||
comment: true,
|
||||
search: true,
|
||||
actions: [],
|
||||
mergeMethods: [],
|
||||
review: { inlineComment: false, reply: false, resolve: false, verdicts: [] },
|
||||
reviewers: { request: false, listCandidates: false },
|
||||
edit: { changeRequest: true, comment: true },
|
||||
},
|
||||
viewerPermissions: {
|
||||
actions: [],
|
||||
comment: true,
|
||||
resolve: false,
|
||||
verdicts: [],
|
||||
requestReviewers: false,
|
||||
},
|
||||
};
|
||||
|
||||
let renderer: ReactTestRenderer;
|
||||
beforeEach(() => vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true));
|
||||
afterEach(() => {
|
||||
act(() => renderer?.unmount());
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
function render(value = detail) {
|
||||
return (
|
||||
<PullRequestSummaryTab
|
||||
environmentId={EnvironmentId.make("environment")}
|
||||
threadRef={null}
|
||||
reference={value}
|
||||
detail={value}
|
||||
activityPending={false}
|
||||
activityError={null}
|
||||
onRefresh={() => {}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function heading(title: string) {
|
||||
const section = renderer.root
|
||||
.findAllByType("section")
|
||||
.find((section) => section.props["aria-label"] === title)!;
|
||||
return section
|
||||
.findAllByType("button")
|
||||
.find((button) => button.findAllByType("span").some((span) => span.children.includes(title)))!;
|
||||
}
|
||||
|
||||
function click(title: string) {
|
||||
act(() =>
|
||||
heading(title).props.onClick({ nativeEvent: {}, preventDefault() {}, stopPropagation() {} }),
|
||||
);
|
||||
}
|
||||
|
||||
it("toggles checks from their heading and resets sections for another pull request", () => {
|
||||
act(() => {
|
||||
renderer = create(render());
|
||||
});
|
||||
expect(
|
||||
renderer.root.findAllByType("span").some((span) => span.children.includes("Unit tests")),
|
||||
).toBe(false);
|
||||
click("Checks");
|
||||
expect(
|
||||
renderer.root.findAllByType("span").some((span) => span.children.includes("Unit tests")),
|
||||
).toBe(true);
|
||||
click("Checks");
|
||||
expect(heading("Checks").props["aria-expanded"]).toBe(false);
|
||||
click("Checks");
|
||||
click("Description");
|
||||
act(() =>
|
||||
renderer.update(render({ ...detail, url: "https://github.com/owner/repo/pull/2", number: 2 })),
|
||||
);
|
||||
expect(heading("Checks").props["aria-expanded"]).toBe(false);
|
||||
expect(heading("Description").props["aria-expanded"]).toBe(true);
|
||||
});
|
||||
|
||||
it("keeps an unsaved description when collapsed and reopened", () => {
|
||||
act(() => {
|
||||
renderer = create(render());
|
||||
});
|
||||
act(() => renderer.root.findByProps({ "aria-label": "Edit description" }).props.onClick());
|
||||
act(() =>
|
||||
renderer.root.findByType("textarea").props.onChange({
|
||||
target: { value: "Unsaved description" },
|
||||
currentTarget: { value: "Unsaved description" },
|
||||
nativeEvent: {},
|
||||
}),
|
||||
);
|
||||
click("Description");
|
||||
expect(heading("Description").props["aria-expanded"]).toBe(false);
|
||||
click("Description");
|
||||
expect(renderer.root.findByType("textarea").props.value).toBe("Unsaved description");
|
||||
});
|
||||
@@ -15,7 +15,7 @@ import {
|
||||
TagIcon,
|
||||
UsersIcon,
|
||||
} from "lucide-react";
|
||||
import { useId, useRef, useState, type ReactNode } from "react";
|
||||
import { useRef, useState, type ReactNode } from "react";
|
||||
|
||||
import { useAtomCommand } from "~/state/use-atom-command";
|
||||
import { pullRequestEnvironment } from "~/state/pullRequests";
|
||||
@@ -237,11 +237,13 @@ function MetaRow({
|
||||
function Section({
|
||||
title,
|
||||
defaultOpen = true,
|
||||
keepMounted = false,
|
||||
actions,
|
||||
children,
|
||||
}: {
|
||||
title: string;
|
||||
defaultOpen?: boolean;
|
||||
keepMounted?: boolean;
|
||||
/** Heading controls stay separate from the collapse trigger so they remain independently usable. */
|
||||
actions?: ReactNode;
|
||||
children: ReactNode;
|
||||
@@ -271,6 +273,7 @@ function Section({
|
||||
<Collapsible
|
||||
open={open}
|
||||
onOpenChange={setOpenWithScrollAnchor}
|
||||
render={<section aria-label={title} />}
|
||||
data-pull-request-summary-section
|
||||
>
|
||||
{/* The heading rides the top of the scroll box the way a diff's file header does, so a
|
||||
@@ -292,7 +295,7 @@ function Section({
|
||||
</CollapsibleTrigger>
|
||||
{actions}
|
||||
</div>
|
||||
<CollapsiblePanel>
|
||||
<CollapsiblePanel keepMounted={keepMounted}>
|
||||
<div className="px-4 pb-4">{children}</div>
|
||||
</CollapsiblePanel>
|
||||
</Collapsible>
|
||||
@@ -334,9 +337,6 @@ export function PullRequestSummaryTab({
|
||||
// Keyed by the pull request, so opening another one starts at the end of its conversation
|
||||
// rather than wherever the last one had been read back to.
|
||||
const [shown, setShown] = useState({ url: detail.url, count: COMMENT_PAGE });
|
||||
const checksId = useId();
|
||||
const [expandedChecksUrl, setExpandedChecksUrl] = useState<string | null>(null);
|
||||
const showChecks = expandedChecksUrl === detail.url;
|
||||
const shownComments = shown.url === detail.url ? shown.count : COMMENT_PAGE;
|
||||
// Windowed by recency regardless of display order: expanding always reaches further back in
|
||||
// time, whether the newest comment currently reads first or last.
|
||||
@@ -472,7 +472,7 @@ export function PullRequestSummaryTab({
|
||||
|
||||
return (
|
||||
<div className="h-full overflow-y-auto" data-pull-request-summary-scroll>
|
||||
<section className="px-4 py-2.5">
|
||||
<section className="px-4 pt-2.5 pb-1">
|
||||
<div className="space-y-2">
|
||||
<MetaRow icon={<UsersIcon className="size-3.5" />} label="Reviewers">
|
||||
<span className="flex min-w-0 flex-wrap items-center gap-1.5">
|
||||
@@ -601,7 +601,7 @@ export function PullRequestSummaryTab({
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section aria-label="Description" className="px-4 pt-2 pb-1">
|
||||
<Section key={`description:${detail.url}`} title="Description" keepMounted>
|
||||
<div className="group">
|
||||
{bodyScope === detail.url ? (
|
||||
<PullRequestMarkdownEditor
|
||||
@@ -640,78 +640,58 @@ export function PullRequestSummaryTab({
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</section>
|
||||
</Section>
|
||||
|
||||
<section aria-label="Checks" className="px-4 py-3">
|
||||
<Section key={`checks:${detail.url}`} title="Checks" defaultOpen={false}>
|
||||
{detail.checks.length === 0 ? (
|
||||
<p className="text-xs text-muted-foreground">No checks reported.</p>
|
||||
) : (
|
||||
<div>
|
||||
<div className="flex items-center gap-1 text-xs">
|
||||
<span className="font-medium text-muted-foreground">Checks</span>
|
||||
<Button
|
||||
size="icon-xs"
|
||||
variant="ghost-muted"
|
||||
aria-label={showChecks ? "Hide checks" : "Show checks"}
|
||||
aria-expanded={showChecks}
|
||||
aria-controls={checksId}
|
||||
onClick={() => setExpandedChecksUrl(showChecks ? null : detail.url)}
|
||||
detail.checks.map((check, index) => {
|
||||
const finding = { kind: "check", check } as const;
|
||||
const failing = check.status === "failure" || check.status === "cancelled";
|
||||
return (
|
||||
<div
|
||||
// Position too: the host decides how many runs share a name, and a repeated
|
||||
// key would be a rendering fault on top of whatever the list already says.
|
||||
key={`${index}:${check.name}:${check.url ?? ""}`}
|
||||
className="group flex items-center gap-2 rounded-md pr-1 hover:bg-accent/60"
|
||||
>
|
||||
<ChevronRightIcon
|
||||
aria-hidden
|
||||
className={cn("size-3.5 text-muted-foreground/60", showChecks && "rotate-90")}
|
||||
/>
|
||||
</Button>
|
||||
</div>
|
||||
<div id={checksId} className={showChecks ? "mt-2" : "hidden"}>
|
||||
{(showChecks ? detail.checks : []).map((check, index) => {
|
||||
const finding = { kind: "check", check } as const;
|
||||
const failing = check.status === "failure" || check.status === "cancelled";
|
||||
return (
|
||||
<div
|
||||
// Position too: the host decides how many runs share a name, and a repeated
|
||||
// key would be a rendering fault on top of whatever the list already says.
|
||||
key={`${index}:${check.name}:${check.url ?? ""}`}
|
||||
className="group flex items-center gap-2 rounded-md pr-1 hover:bg-accent/60"
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
disabled={!check.url}
|
||||
onClick={() => check.url && openCheck(check.url)}
|
||||
className={cn(
|
||||
"flex min-w-0 flex-1 items-start gap-2 rounded-md px-2 py-2 text-left text-xs leading-5 [&>svg]:mt-0.5",
|
||||
check.url ? "cursor-pointer" : "cursor-default",
|
||||
)}
|
||||
>
|
||||
<PullRequestCheckStatusIcon status={check.status} />
|
||||
<span className="min-w-0 flex-1 wrap-anywhere">{check.name}</span>
|
||||
<span className="shrink-0 text-muted-foreground">
|
||||
{pullRequestCheckStatusLabel(check)}
|
||||
</span>
|
||||
</button>
|
||||
{/* Only where there is something to fix. A passing check has no failure to
|
||||
<button
|
||||
type="button"
|
||||
disabled={!check.url}
|
||||
onClick={() => check.url && openCheck(check.url)}
|
||||
className={cn(
|
||||
"flex min-w-0 flex-1 items-start gap-2 rounded-md px-2 py-2 text-left text-xs leading-5 [&>svg]:mt-0.5",
|
||||
check.url ? "cursor-pointer" : "cursor-default",
|
||||
)}
|
||||
>
|
||||
<PullRequestCheckStatusIcon status={check.status} />
|
||||
<span className="min-w-0 flex-1 wrap-anywhere">{check.name}</span>
|
||||
<span className="shrink-0 text-muted-foreground">
|
||||
{pullRequestCheckStatusLabel(check)}
|
||||
</span>
|
||||
</button>
|
||||
{/* Only where there is something to fix. A passing check has no failure to
|
||||
reproduce, and the button would be an invitation to waste a thread. */}
|
||||
{onFixFinding && failing ? (
|
||||
<Button
|
||||
size="xs"
|
||||
variant="ghost"
|
||||
className="shrink-0"
|
||||
disabled={pendingFinding !== null && pendingFinding !== undefined}
|
||||
onClick={() => onFixFinding(finding)}
|
||||
>
|
||||
<HammerIcon className="size-3" />
|
||||
{pendingFinding === pullRequestFindingKey(finding)
|
||||
? "Preparing..."
|
||||
: fixCheckLabel}
|
||||
</Button>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
{onFixFinding && failing ? (
|
||||
<Button
|
||||
size="xs"
|
||||
variant="ghost"
|
||||
className="shrink-0"
|
||||
disabled={pendingFinding !== null && pendingFinding !== undefined}
|
||||
onClick={() => onFixFinding(finding)}
|
||||
>
|
||||
<HammerIcon className="size-3" />
|
||||
{pendingFinding === pullRequestFindingKey(finding)
|
||||
? "Preparing..."
|
||||
: fixCheckLabel}
|
||||
</Button>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
})
|
||||
)}
|
||||
</section>
|
||||
</Section>
|
||||
|
||||
<Section
|
||||
title="Comments"
|
||||
|
||||
Reference in new issue
Block a user