feat(repositories): guard delete behind active-session check + UI fixes (#968)
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import { describe, it, expect, vi, afterEach, beforeEach } from "vitest";
|
||||
import { render, screen, cleanup } from "@testing-library/react";
|
||||
|
||||
import { DiffErrorBoundary } from "./diff-error-boundary";
|
||||
|
||||
function Boom({ message = "boom" }: { message?: string }): never {
|
||||
throw new Error(message);
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
// React logs caught errors to console.error — silence so test output stays clean.
|
||||
vi.spyOn(console, "error").mockImplementation(() => undefined);
|
||||
});
|
||||
|
||||
describe("DiffErrorBoundary", () => {
|
||||
it("renders children when no error is thrown", () => {
|
||||
render(
|
||||
<DiffErrorBoundary filePath="src/foo.ts">
|
||||
<div data-testid="child">child content</div>
|
||||
</DiffErrorBoundary>,
|
||||
);
|
||||
expect(screen.getByTestId("child")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("renders fallback when a child throws during render", () => {
|
||||
render(
|
||||
<DiffErrorBoundary filePath="src/foo.ts">
|
||||
<Boom />
|
||||
</DiffErrorBoundary>,
|
||||
);
|
||||
expect(screen.getByText(/Unable to render diff/i)).toBeTruthy();
|
||||
});
|
||||
|
||||
it("resets fallback when filePath changes", () => {
|
||||
const { rerender } = render(
|
||||
<DiffErrorBoundary filePath="src/foo.ts">
|
||||
<Boom />
|
||||
</DiffErrorBoundary>,
|
||||
);
|
||||
expect(screen.getByText(/Unable to render diff/i)).toBeTruthy();
|
||||
|
||||
rerender(
|
||||
<DiffErrorBoundary filePath="src/bar.ts">
|
||||
<div data-testid="child">recovered</div>
|
||||
</DiffErrorBoundary>,
|
||||
);
|
||||
expect(screen.getByTestId("child")).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,52 @@
|
||||
"use client";
|
||||
|
||||
import { Component, type ErrorInfo, type ReactNode } from "react";
|
||||
import { IconAlertTriangle } from "@tabler/icons-react";
|
||||
|
||||
type DiffErrorBoundaryProps = {
|
||||
filePath: string;
|
||||
children: ReactNode;
|
||||
};
|
||||
|
||||
type DiffErrorBoundaryState = {
|
||||
error: Error | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Catches throws from @pierre/diffs renderers so a single malformed diff
|
||||
* (e.g. stale cached hunk header vs live working-tree content for an
|
||||
* untracked file that was edited after the snapshot) cannot tear down
|
||||
* the whole review list. Renders a small per-file fallback instead.
|
||||
*/
|
||||
export class DiffErrorBoundary extends Component<DiffErrorBoundaryProps, DiffErrorBoundaryState> {
|
||||
state: DiffErrorBoundaryState = { error: null };
|
||||
|
||||
static getDerivedStateFromError(error: Error): DiffErrorBoundaryState {
|
||||
return { error };
|
||||
}
|
||||
|
||||
componentDidCatch(error: Error, info: ErrorInfo): void {
|
||||
console.error("[DiffErrorBoundary]", this.props.filePath, error, info.componentStack);
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps: DiffErrorBoundaryProps): void {
|
||||
if (prevProps.filePath !== this.props.filePath && this.state.error) {
|
||||
this.setState({ error: null });
|
||||
}
|
||||
}
|
||||
|
||||
render(): ReactNode {
|
||||
if (this.state.error) {
|
||||
return (
|
||||
<div className="flex items-center gap-2 py-4 px-3 text-xs text-muted-foreground border-t">
|
||||
<IconAlertTriangle className="h-4 w-4 text-amber-500 shrink-0" />
|
||||
<span>
|
||||
Unable to render diff for this file. The cached diff may be out of sync with the working
|
||||
tree — refresh to retry.
|
||||
</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return this.props.children;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
export { DiffViewerResolved as DiffViewer } from "./diff-viewer-resolver";
|
||||
export { DiffViewInlineResolved as DiffViewInline } from "./diff-viewer-resolver";
|
||||
export { FileDiffViewer } from "./file-diff-viewer";
|
||||
export { DiffErrorBoundary } from "./diff-error-boundary";
|
||||
export type { RevertBlockInfo } from "./diff-viewer-resolver";
|
||||
export { CommentForm } from "./comment-form";
|
||||
export { CommentDisplay } from "./comment-display";
|
||||
|
||||
Reference in New Issue
Block a user