85 lines
2.9 KiB
TypeScript
85 lines
2.9 KiB
TypeScript
// @vitest-environment jsdom
|
|
|
|
import { render, screen } from "@testing-library/react";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { ContextErrorBoundary } from "../../../src/platform/errors/context-error-boundary.tsx";
|
|
import { FeatureErrorBoundary } from "../../../src/platform/errors/feature-error-boundary.tsx";
|
|
|
|
function ThrowsOnRender(): never {
|
|
throw new Error("feature render failed");
|
|
}
|
|
|
|
describe("ownership-aligned failure boundaries", () => {
|
|
it("keeps sibling feature UI mounted when one feature throws during render", () => {
|
|
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
render(
|
|
<ContextErrorBoundary>
|
|
<FeatureErrorBoundary featureId="broken">
|
|
<ThrowsOnRender />
|
|
</FeatureErrorBoundary>
|
|
<div>healthy-feature</div>
|
|
</ContextErrorBoundary>,
|
|
);
|
|
|
|
expect(screen.getByText("healthy-feature")).toBeInTheDocument();
|
|
expect(screen.getByTestId("feature-fallback-broken")).toBeInTheDocument();
|
|
});
|
|
|
|
it("keeps sibling contexts mounted when one context fails outside any feature boundary", () => {
|
|
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
render(
|
|
<main>
|
|
<ContextErrorBoundary contextId="commerce">
|
|
<ThrowsOnRender />
|
|
</ContextErrorBoundary>
|
|
<ContextErrorBoundary contextId="account">
|
|
<p>account-context</p>
|
|
</ContextErrorBoundary>
|
|
<nav>app-shell</nav>
|
|
</main>,
|
|
);
|
|
|
|
expect(screen.getByTestId("context-fallback-commerce")).toBeInTheDocument();
|
|
expect(screen.getByText("account-context")).toBeInTheDocument();
|
|
expect(screen.getByText("app-shell")).toBeInTheDocument();
|
|
});
|
|
|
|
it("reports once per contained failure and survives a throwing reporter", () => {
|
|
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
const onError = vi.fn(() => {
|
|
throw new Error("diagnostics down");
|
|
});
|
|
render(
|
|
<FeatureErrorBoundary featureId="broken" onError={onError} fallback={<p>fallback</p>}>
|
|
<ThrowsOnRender />
|
|
</FeatureErrorBoundary>,
|
|
);
|
|
expect(onError).toHaveBeenCalledWith({ scope: "feature", ownerId: "broken" });
|
|
expect(screen.getByText("fallback")).toBeInTheDocument();
|
|
});
|
|
|
|
it("clears a contained failure when its reset key changes", () => {
|
|
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
let shouldThrow = true;
|
|
function Flaky() {
|
|
if (shouldThrow) throw new Error("flaky");
|
|
return <p>recovered</p>;
|
|
}
|
|
const view = render(
|
|
<FeatureErrorBoundary featureId="flaky" resetKey="/a">
|
|
<Flaky />
|
|
</FeatureErrorBoundary>,
|
|
);
|
|
expect(screen.getByTestId("feature-fallback-flaky")).toBeInTheDocument();
|
|
|
|
shouldThrow = false;
|
|
view.rerender(
|
|
<FeatureErrorBoundary featureId="flaky" resetKey="/b">
|
|
<Flaky />
|
|
</FeatureErrorBoundary>,
|
|
);
|
|
expect(screen.getByText("recovered")).toBeInTheDocument();
|
|
});
|
|
});
|