// @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(
healthy-feature
,
);
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(
account-context
,
);
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(
fallback
}>
,
);
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 recovered
;
}
const view = render(
,
);
expect(screen.getByTestId("feature-fallback-flaky")).toBeInTheDocument();
shouldThrow = false;
view.rerender(
,
);
expect(screen.getByText("recovered")).toBeInTheDocument();
});
});