diff --git a/web/src/pages/EntityHistory.test.ts b/web/src/pages/EntityHistory.test.ts new file mode 100644 index 000000000..2857a7b61 --- /dev/null +++ b/web/src/pages/EntityHistory.test.ts @@ -0,0 +1,33 @@ +import { createElement, type ReactNode } from "react"; +import { renderToStaticMarkup } from "react-dom/server"; +import { expect, it, vi } from "vitest"; +import { S } from "../i18n"; +import { EntityHistory } from "./EntityHistory"; + +const history = vi.hoisted(() => ({ + error: new Error("History request failed"), + refetch: vi.fn(), + retry: undefined as (() => void) | undefined, +})); + +vi.mock("@tanstack/react-query", async (importOriginal) => ({ + ...await importOriginal(), + useQuery: () => ({ isError: true, error: history.error, refetch: history.refetch }), +})); +vi.mock("../ui", async (importOriginal) => ({ + ...await importOriginal(), + LinkButton: ({ onClick, children }: { onClick: () => void; children: ReactNode }) => { + history.retry = onClick; + return createElement("button", null, children); + }, +})); + +it("shows a history load failure and lets the reader retry", () => { + const markup = renderToStaticMarkup(createElement(EntityHistory, { kbId: "kb", entityId: "entity" })); + expect(markup).toContain(history.error.message); + expect(markup).not.toContain(S.graph.historyEmpty); + expect(markup).toContain(S.ask.retryHistory); + + history.retry?.(); + expect(history.refetch).toHaveBeenCalledOnce(); +}); diff --git a/web/src/pages/EntityHistory.tsx b/web/src/pages/EntityHistory.tsx index 8660f593a..b3cbb9385 100644 --- a/web/src/pages/EntityHistory.tsx +++ b/web/src/pages/EntityHistory.tsx @@ -10,7 +10,7 @@ import { api, type EntityHistoryEvent } from "../api"; import { S } from "../i18n"; import { predicateSentence } from "../predicateText"; import { useKbId } from "../kb"; -import { Pager } from "../ui"; +import { LinkButton, Pager } from "../ui"; const PER = 20; @@ -175,6 +175,14 @@ export function EntityHistory({ kbId, entityId }: { kbId: string; entityId: stri const total = q.data?.total ?? 0; if (q.isPending) return

{S.nav.loading}

; + if (q.isError && !q.data) { + return ( +
+

{q.error.message}

+ void q.refetch()}>{S.ask.retryHistory} +
+ ); + } // 只有"一条都没有"才是空。记录轴上首次断言本身就是一次事件—— // "我们何时、从哪份文档得知这件事"是这条轴要回答的问题的一半 if (total === 0) return

{S.graph.historyEmpty}

;