From a9e3fbb5bb5b2cc69c4e9c8696894caaae497fed Mon Sep 17 00:00:00 2001 From: margrop <1731827+margrop@users.noreply.github.com> Date: Fri, 4 Dec 2026 04:17:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=AF=95=E5=8D=B7=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E9=A1=B5=E5=8A=A0=E8=BD=BD=E5=A4=B1=E8=B4=A5=E5=B1=95=E7=A4=BA?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E6=80=81=E4=B8=8E=E8=BF=94=E5=9B=9E=E8=AF=95?= =?UTF-8?q?=E5=8D=B7=E5=85=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 区分加载中与加载失败,失败时显示明确提示并返回试卷详情 - 留白下拉补齐无障碍标签 - 补充错误态单元测试 Refs: https://github.com/margrop/wrong-notebook/issues/141 --- src/__tests__/unit/exam-print-page.test.tsx | 57 +++++++++++++++++++++ src/app/exams/[id]/print/page.tsx | 20 ++++++-- src/lib/i18n/en.ts | 2 + src/lib/i18n/zh.ts | 2 + 4 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 src/__tests__/unit/exam-print-page.test.tsx diff --git a/src/__tests__/unit/exam-print-page.test.tsx b/src/__tests__/unit/exam-print-page.test.tsx new file mode 100644 index 00000000..9c51f88a --- /dev/null +++ b/src/__tests__/unit/exam-print-page.test.tsx @@ -0,0 +1,57 @@ +/** + * 试卷打印页加载失败状态:不再永久停在「加载中」,展示错误提示与返回试卷入口 + */ +import { act } from "react"; +import { createRoot } from "react-dom/client"; +import type { Root } from "react-dom/client"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import ExamPrintPage from "@/app/exams/[id]/print/page"; +import { LanguageProvider } from "@/contexts/LanguageContext"; +import { translations } from "@/lib/translations"; + +const mocks = vi.hoisted(() => ({ get: vi.fn() })); + +vi.mock("@/lib/api-client", () => ({ apiClient: { get: mocks.get, patch: vi.fn() } })); +vi.mock("next/navigation", () => ({ useParams: () => ({ id: "paper-1" }) })); +vi.mock("@/components/ui/back-button", () => ({ + BackButton: ({ fallbackUrl }: { fallbackUrl: string }) => 返回试卷, +})); + +let container: HTMLDivElement; +let root: Root; + +async function flush() { + await act(async () => { + const { promise, resolve } = Promise.withResolvers(); + setTimeout(resolve, 0); + await promise; + }); +} + +beforeEach(() => { + vi.clearAllMocks(); + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); + localStorage.clear(); + container = document.createElement("div"); + document.body.appendChild(container); + root = createRoot(container); +}); + +afterEach(async () => { + await act(async () => root.unmount()); + container.remove(); + vi.unstubAllGlobals(); +}); + +describe("试卷打印页加载失败", () => { + it("展示错误提示与返回试卷入口,不再显示加载中", async () => { + mocks.get.mockRejectedValue(new Error("404")); + await act(async () => root.render()); + await flush(); + + expect(container.textContent).toContain(translations.zh.exam.printLoadFailed); + expect(container.textContent).toContain(translations.zh.exam.printLoadFailedHint); + expect(container.textContent).not.toContain(translations.zh.common.loading); + expect(container.querySelector('a[href="/exams/paper-1"]')).toBeTruthy(); + }); +}); diff --git a/src/app/exams/[id]/print/page.tsx b/src/app/exams/[id]/print/page.tsx index 4ac7e3e1..0403fae3 100644 --- a/src/app/exams/[id]/print/page.tsx +++ b/src/app/exams/[id]/print/page.tsx @@ -34,6 +34,7 @@ export default function ExamPrintPage() { const params = useParams(); const { t, language } = useLanguage(); const [paper, setPaper] = useState(null); + const [loadError, setLoadError] = useState(false); usePageTitle(t.exam.title, paper?.title, t.pageTitle.examPrint); // 「包含答案页」默认不勾选,选择结果记在本机浏览器,下次打印沿用 const [includeAnswers, setIncludeAnswers] = useStoredChoice(EXAM_PRINT_INCLUDE_ANSWERS_STORAGE_KEY, ["1", "0"] as const, "0"); @@ -130,10 +131,23 @@ export default function ExamPrintPage() { useEffect(() => { apiClient.get(`/api/exams/${params.id}`) .then(setPaper) - .catch(() => setPaper(null)); + .catch(() => setLoadError(true)); }, [params.id]); - if (!paper) return
{t.common.loading}
; + if (!paper) { + if (loadError) { + return ( +
+
+

{t.exam.printLoadFailed}

+

{t.exam.printLoadFailedHint}

+ +
+
+ ); + } + return
{t.common.loading}
; + } const figureQuestions = paper.questions.filter((question) => question.status === "ready" && question.questionText && Boolean(question.figureSvg || question.figureImage || question.figureUploadImage)); const readyCount = paper.questions.filter((question) => question.status === "ready" && question.questionText).length; @@ -193,7 +207,7 @@ export default function ExamPrintPage() {
{t.exam.answerSpace}