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}