feat: hydrate persisted PR details on task disclosure (#3055)

This commit is contained in:
Carlos Florêncio
2026-08-26 15:54:59 +00:00
committed by GitHub
parent 49dd944bd3
commit a6896bca9a
68 changed files with 1764 additions and 130 deletions
@@ -1,12 +1,21 @@
import { afterEach, describe, expect, it } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { ReactNode } from "react";
import { cleanup, render } from "@testing-library/react";
import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
import { TooltipProvider } from "@kandev/ui/tooltip";
import { StateProvider } from "@/components/state-provider";
import { TaskContributionIcons } from "@/components/task/task-contribution-icons";
import { PRTaskIcon } from "./pr-task-icon";
import type { AppState } from "@/lib/state/store";
import type { TaskPR } from "@/lib/types/github";
const listTaskPRsMock = vi.hoisted(() => vi.fn());
const TASK_ID = "task-1";
const WORKSPACE_ID = "workspace-1";
vi.mock("@/lib/api/domains/github-api", () => ({
listTaskPRs: listTaskPRsMock,
}));
function renderWithStore(initialState: Partial<AppState> | undefined, ui: ReactNode) {
return render(
<StateProvider initialState={initialState}>
@@ -18,7 +27,8 @@ function renderWithStore(initialState: Partial<AppState> | undefined, ui: ReactN
function makePR(overrides: Partial<TaskPR> = {}): TaskPR {
return {
id: "id",
task_id: "task-1",
workspace_id: WORKSPACE_ID,
task_id: TASK_ID,
owner: "o",
repo: "r",
pr_number: 1,
@@ -48,18 +58,22 @@ function makePR(overrides: Partial<TaskPR> = {}): TaskPR {
};
}
beforeEach(() => {
listTaskPRsMock.mockReset().mockReturnValue(new Promise(() => {}));
});
afterEach(() => cleanup());
describe("PRTaskIcon corrupted store entry", () => {
// Regression: an upstream payload (partial hydration, WS reorder, etc.) once
// landed in taskPRs.byTaskId["task-1"] as a non-array truthy value. The
// landed in taskPRs.byTaskId[TASK_ID] as a non-array truthy value. The
// length-based guards then fell through into MultiPRIcon, where for-of
// threw `prs is not iterable`. PRTaskIcon must bail rather than crash.
it("renders nothing when byTaskId[taskId] is a non-array object", () => {
const { container } = renderWithStore(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
{ taskPRs: { byTaskId: { "task-1": {} as any } } } as Partial<AppState>,
<PRTaskIcon taskId="task-1" />,
{ taskPRs: { byTaskId: { [TASK_ID]: {} as any } } } as Partial<AppState>,
<PRTaskIcon taskId={TASK_ID} />,
);
expect(container.firstChild).toBeNull();
});
@@ -71,10 +85,10 @@ describe("PRTaskIcon corrupted store entry", () => {
it("renders an icon when byTaskId[taskId] is a valid array of one PR", () => {
const { container } = renderWithStore(
{ taskPRs: { byTaskId: { "task-1": [makePR()] } } },
<PRTaskIcon taskId="task-1" />,
{ taskPRs: { byTaskId: { [TASK_ID]: [makePR()] } } },
<PRTaskIcon taskId={TASK_ID} />,
);
expect(container.querySelector('[data-testid="pr-task-icon-task-1"]')).not.toBeNull();
expect(container.querySelector(`[data-testid="pr-task-icon-${TASK_ID}"]`)).not.toBeNull();
});
it("renders the multi-PR icon when byTaskId[taskId] has multiple PRs", () => {
@@ -82,17 +96,84 @@ describe("PRTaskIcon corrupted store entry", () => {
{
taskPRs: {
byTaskId: {
"task-1": [
[TASK_ID]: [
makePR({ id: "a", repository_id: "repo-a", pr_number: 1 }),
makePR({ id: "b", repository_id: "repo-b", pr_number: 2 }),
],
},
},
},
<PRTaskIcon taskId="task-1" />,
<PRTaskIcon taskId={TASK_ID} />,
);
const icon = container.querySelector('[data-testid="pr-task-icon-task-1"]');
const icon = container.querySelector(`[data-testid="pr-task-icon-${TASK_ID}"]`);
expect(icon).not.toBeNull();
expect(icon?.getAttribute("data-pr-count")).toBe("2");
expect(icon?.getAttribute("data-pr-ready-to-merge")).toBe("false");
});
it("opens a loading disclosure for a compact PR projection", () => {
renderWithStore(
{ workspaces: { items: [], activeId: WORKSPACE_ID } },
<TaskContributionIcons
taskId={TASK_ID}
prInfo={{ number: 7, state: "open", aggregateState: "pending" }}
/>,
);
const icon = screen.getByTestId(`pr-task-icon-${TASK_ID}`);
expect(icon.getAttribute("role")).toBe("img");
fireEvent.pointerEnter(icon, { pointerType: "mouse" });
expect(screen.getAllByTestId("pr-task-tooltip-loading").length).toBeGreaterThan(0);
});
it("opens a loading disclosure when a compact PR projection receives keyboard focus", () => {
renderWithStore(
{ workspaces: { items: [], activeId: WORKSPACE_ID } },
<TaskContributionIcons
taskId={TASK_ID}
prInfo={{ number: 7, state: "open", aggregateState: "pending" }}
/>,
);
const icon = screen.getByTestId(`pr-task-icon-${TASK_ID}`);
const matches = vi.spyOn(icon, "matches").mockReturnValue(true);
fireEvent.focus(icon);
matches.mockRestore();
expect(screen.getAllByTestId("pr-task-tooltip-loading").length).toBeGreaterThan(0);
});
it("keeps keyboard focus and the open tooltip when hydration completes", async () => {
let resolveResponse!: (value: { task_prs: Record<string, TaskPR[]> }) => void;
const response = new Promise<{ task_prs: Record<string, TaskPR[]> }>((resolve) => {
resolveResponse = resolve;
});
listTaskPRsMock.mockReturnValue(response);
renderWithStore(
{ workspaces: { items: [], activeId: WORKSPACE_ID } },
<TaskContributionIcons
taskId={TASK_ID}
prInfo={{ number: 7, state: "open", aggregateState: "pending" }}
/>,
);
const icon = screen.getByTestId(`pr-task-icon-${TASK_ID}`);
const matches = vi.spyOn(icon, "matches").mockReturnValue(true);
icon.focus();
matches.mockRestore();
await waitFor(() =>
expect(screen.getAllByTestId("pr-task-tooltip-loading").length).toBeGreaterThan(0),
);
await act(async () => {
resolveResponse({ task_prs: { [TASK_ID]: [makePR()] } });
await response;
});
await waitFor(() =>
expect(screen.getAllByTestId("pr-task-status-summary").length).toBeGreaterThan(0),
);
expect(document.activeElement).toBe(screen.getByTestId(`pr-task-icon-${TASK_ID}`));
});
});