Route render, unrecovered chunk and query failure screens now show the id a user quotes to support, and the render diagnostic records the same id as correlation_id. The route failure page also offers a reload. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
206 lines
6.7 KiB
TypeScript
206 lines
6.7 KiB
TypeScript
// @vitest-environment jsdom
|
|
|
|
import { render, screen } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { lazy, Suspense } from "react";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import {
|
|
ChunkRecoveryBoundary,
|
|
isChunkLoadFailure,
|
|
} from "../../src/presentation/boundaries/chunk-recovery-boundary.tsx";
|
|
import { FeatureBoundary } from "../../src/presentation/boundaries/render-error-boundary.tsx";
|
|
import { ApplicationProvider } from "../../src/presentation/providers/application-provider.tsx";
|
|
import { PreferenceProviders } from "../../src/presentation/providers/preference-providers.tsx";
|
|
import { AppRouter } from "../../src/presentation/routes/app-router.tsx";
|
|
import { createTestApplication } from "../helpers/create-test-application.ts";
|
|
|
|
// The states page stands in for a route chunk the network could not deliver:
|
|
// a rejected lazy module throws its rejection on every render, as this does.
|
|
vi.mock("../../src/presentation/examples/state-gallery-page.tsx", () => ({
|
|
default: function UnreachableChunk(): never {
|
|
throw new TypeError(
|
|
"Failed to fetch dynamically imported module: /assets/states.js",
|
|
);
|
|
},
|
|
}));
|
|
|
|
function ChunkDefect(): never {
|
|
throw new TypeError("Failed to fetch dynamically imported module");
|
|
}
|
|
|
|
function RenderDefect(): never {
|
|
throw new Error("ordinary render defect");
|
|
}
|
|
|
|
describe("chunk recovery boundary classification", () => {
|
|
it("recognizes lazy module failures without classifying ordinary render errors", () => {
|
|
expect(
|
|
isChunkLoadFailure(
|
|
new TypeError("Failed to fetch dynamically imported module"),
|
|
),
|
|
).toBe(true);
|
|
expect(isChunkLoadFailure(new Error("ordinary render defect"))).toBe(false);
|
|
});
|
|
|
|
it("recognizes the WebKit module failure and the Vite CSS preload failure", () => {
|
|
expect(
|
|
isChunkLoadFailure(new TypeError("Importing a module script failed.")),
|
|
).toBe(true);
|
|
expect(
|
|
isChunkLoadFailure(
|
|
new Error("Unable to preload CSS for /assets/states-3f2a.css"),
|
|
),
|
|
).toBe(true);
|
|
});
|
|
|
|
it("recovers when Vite rethrows a vite:preloadError into the lazy import", async () => {
|
|
// Vite dispatches `vite:preloadError` and, unless a listener prevents it,
|
|
// rejects the dynamic import with the same error. That rejection is what
|
|
// reaches this boundary.
|
|
const Page = lazy(() =>
|
|
Promise.reject(new Error("Unable to preload CSS for /assets/states.css")),
|
|
);
|
|
const recover = vi.fn(async () => ({
|
|
action: "support" as const,
|
|
reason: "reload-already-attempted",
|
|
}));
|
|
render(
|
|
<Suspense fallback={null}>
|
|
<ChunkRecoveryBoundary chunkId="route-states" recover={recover}>
|
|
<Page />
|
|
</ChunkRecoveryBoundary>
|
|
</Suspense>,
|
|
);
|
|
|
|
expect(
|
|
await screen.findByRole("heading", {
|
|
name: "화면 자산을 복구하지 못했습니다.",
|
|
}),
|
|
).toBeVisible();
|
|
expect(recover).toHaveBeenCalledWith({
|
|
chunkId: "route-states",
|
|
failureKind: "CHUNK_LOAD_FAILURE",
|
|
});
|
|
});
|
|
|
|
it("runs the recovery input only for a lazy chunk rejection", async () => {
|
|
const recover = vi.fn(async () => ({
|
|
action: "support" as const,
|
|
reason: "reload-already-attempted",
|
|
}));
|
|
render(
|
|
<ChunkRecoveryBoundary chunkId="route-home" recover={recover}>
|
|
<ChunkDefect />
|
|
</ChunkRecoveryBoundary>,
|
|
);
|
|
|
|
expect(
|
|
await screen.findByRole("heading", {
|
|
name: "화면 자산을 복구하지 못했습니다.",
|
|
}),
|
|
).toBeVisible();
|
|
expect(recover).toHaveBeenCalledOnce();
|
|
expect(recover).toHaveBeenCalledWith({
|
|
chunkId: "route-home",
|
|
failureKind: "CHUNK_LOAD_FAILURE",
|
|
});
|
|
});
|
|
|
|
it("rethrows an ordinary component defect to the local render boundary", () => {
|
|
const recover = vi.fn();
|
|
render(
|
|
<FeatureBoundary routeId="APP_HOME" buildId="build-a">
|
|
<ChunkRecoveryBoundary chunkId="route-home" recover={recover}>
|
|
<RenderDefect />
|
|
</ChunkRecoveryBoundary>
|
|
</FeatureBoundary>,
|
|
);
|
|
|
|
expect(screen.getByRole("alert")).toHaveTextContent(
|
|
"화면을 표시하지 못했습니다.",
|
|
);
|
|
expect(recover).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("offers a user-started reload on the failure surface", async () => {
|
|
render(
|
|
<ChunkRecoveryBoundary
|
|
chunkId="route-home"
|
|
recover={async () => ({
|
|
action: "support" as const,
|
|
reason: "reload-already-attempted",
|
|
})}
|
|
>
|
|
<ChunkDefect />
|
|
</ChunkRecoveryBoundary>,
|
|
);
|
|
|
|
expect(
|
|
await screen.findByRole("button", { name: "한 번 새로고침" }),
|
|
).toBeVisible();
|
|
});
|
|
|
|
// 08-07. An unrecovered chunk failure shows a reference the render
|
|
// diagnostic also records.
|
|
it("shows an unrecovered chunk failure's support reference and records it", async () => {
|
|
const record = vi.fn();
|
|
window.history.pushState({}, "", "/examples/states");
|
|
render(
|
|
<ApplicationProvider
|
|
application={createTestApplication({ diagnostics: { record } })}
|
|
>
|
|
<PreferenceProviders>
|
|
<AppRouter buildId="build-a" />
|
|
</PreferenceProviders>
|
|
</ApplicationProvider>,
|
|
);
|
|
|
|
const reference = await screen.findByText(/^지원 참조: build-a:[0-9a-f]{8}$/);
|
|
const incidentId = reference.textContent?.split(":").at(-1);
|
|
expect(record).toHaveBeenCalledWith({
|
|
level: "error",
|
|
eventId: "ui.render.failed",
|
|
context: expect.objectContaining({
|
|
route_id: "EXAMPLES_STATES",
|
|
build_id: "build-a",
|
|
correlation_id: incidentId,
|
|
}),
|
|
});
|
|
});
|
|
|
|
it("clears a failed route chunk when the user moves to a sibling route", async () => {
|
|
// React Router reuses the rendered-route instance between sibling routes,
|
|
// so a failure latched in one route's boundary would otherwise follow the
|
|
// user to every other page until a full reload.
|
|
const user = userEvent.setup();
|
|
window.history.pushState({}, "", "/examples/states");
|
|
render(
|
|
<ApplicationProvider application={createTestApplication()}>
|
|
<PreferenceProviders>
|
|
<AppRouter />
|
|
</PreferenceProviders>
|
|
</ApplicationProvider>,
|
|
);
|
|
|
|
expect(
|
|
await screen.findByRole("heading", {
|
|
name: "화면 자산을 복구하지 못했습니다.",
|
|
}),
|
|
).toBeVisible();
|
|
|
|
await user.click(screen.getAllByRole("link", { name: "UI 구성요소" })[0]!);
|
|
|
|
expect(
|
|
await screen.findByRole(
|
|
"heading",
|
|
{ name: "UI 구성요소", level: 1 },
|
|
{ timeout: 5000 },
|
|
),
|
|
).toBeVisible();
|
|
expect(
|
|
screen.queryByRole("heading", { name: "화면 자산을 복구하지 못했습니다." }),
|
|
).toBeNull();
|
|
});
|
|
});
|