77 lines
2.2 KiB
TypeScript
77 lines
2.2 KiB
TypeScript
import { render, screen } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import { FilePreviewPanel } from "@/components/FilePreviewPanel";
|
|
import { fetchFilePreview } from "@/lib/api";
|
|
|
|
vi.mock("@/components/CodeBlock", () => ({
|
|
CodeBlock: ({
|
|
code,
|
|
language,
|
|
highlight,
|
|
}: {
|
|
code: string;
|
|
language?: string;
|
|
highlight?: boolean;
|
|
}) => (
|
|
<pre
|
|
data-testid="mock-code-block"
|
|
data-language={language}
|
|
data-highlight={String(highlight)}
|
|
>
|
|
{code}
|
|
</pre>
|
|
),
|
|
}));
|
|
|
|
vi.mock("@/lib/api", async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import("@/lib/api")>();
|
|
return {
|
|
...actual,
|
|
fetchFilePreview: vi.fn(),
|
|
};
|
|
});
|
|
|
|
describe("FilePreviewPanel", () => {
|
|
beforeEach(() => {
|
|
vi.mocked(fetchFilePreview).mockReset();
|
|
});
|
|
|
|
it("shows a compact breadcrumb with one file name and a visible close action", async () => {
|
|
const user = userEvent.setup();
|
|
const onClose = vi.fn();
|
|
vi.mocked(fetchFilePreview).mockResolvedValue({
|
|
path: "/Users/hr/workspace/quicksort.py",
|
|
display_path: "quicksort.py",
|
|
language: "python",
|
|
content: "print('ok')",
|
|
truncated: false,
|
|
});
|
|
|
|
render(
|
|
<FilePreviewPanel
|
|
sessionKey="websocket:chat-1"
|
|
path="quicksort.py"
|
|
token="tok"
|
|
onClose={onClose}
|
|
/>,
|
|
);
|
|
|
|
const codeBlock = await screen.findByTestId("mock-code-block");
|
|
expect(codeBlock).toHaveTextContent("print('ok')");
|
|
expect(codeBlock).toHaveAttribute("data-language", "python");
|
|
expect(codeBlock).toHaveAttribute("data-highlight", "true");
|
|
expect(screen.getByTestId("file-preview-breadcrumb")).toHaveTextContent("...");
|
|
expect(screen.getByTestId("file-preview-breadcrumb")).toHaveTextContent("workspace");
|
|
expect(screen.getByTestId("file-preview-title")).toHaveTextContent("quicksort.py");
|
|
expect(screen.getAllByText("quicksort.py")).toHaveLength(1);
|
|
|
|
const closeButton = screen.getByRole("button", { name: "Close file preview" });
|
|
expect(closeButton).toBeVisible();
|
|
|
|
await user.click(closeButton);
|
|
expect(onClose).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|