дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-11-09 03:05:40 +02:00
43 рядки
1.9 KiB
TypeScript
43 рядки
1.9 KiB
TypeScript
import { fireEvent, render, screen } from "@testing-library/react";
|
|
import { beforeEach, expect, it, vi } from "vitest";
|
|
import { Markdown } from "./Markdown";
|
|
|
|
const { openFile, openExternalUrl } = vi.hoisted(() => ({
|
|
openFile: vi.fn().mockResolvedValue(undefined),
|
|
openExternalUrl: vi.fn().mockResolvedValue(undefined),
|
|
}));
|
|
vi.mock("@/workbench/contributions", () => ({ editorService: () => ({ openFile }) }));
|
|
vi.mock("@/platform/openUrl", () => ({ openExternalUrl }));
|
|
|
|
beforeEach(() => vi.clearAllMocks());
|
|
|
|
it.each([
|
|
["/Users/thomas/vmos-shots/tiktok-home.jpg", "/Users/thomas/vmos-shots/tiktok-home.jpg", undefined],
|
|
["images/home.jpg", "/repo/images/home.jpg", undefined],
|
|
["/tmp/截图%20one.jpg", "/tmp/截图 one.jpg", undefined],
|
|
["/repo/main.rs:12:5", "/repo/main.rs", { line: 11, col: 4 }],
|
|
["/repo/main.rs#L7", "/repo/main.rs", { line: 6, col: 0 }],
|
|
])("opens a Markdown file link: %s", (href, path, selection) => {
|
|
render(<Markdown text={`[查看文件](${href})`} cwd="/repo" />);
|
|
fireEvent.click(screen.getByRole("link", { name: "查看文件" }));
|
|
expect(openFile).toHaveBeenCalledWith(path, { focus: true, selection });
|
|
expect(openExternalUrl).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("keeps web links external", () => {
|
|
render(<Markdown text="[Website](https://example.com/image.jpg)" cwd="/repo" />);
|
|
fireEvent.click(screen.getByRole("link"));
|
|
expect(openExternalUrl).toHaveBeenCalledWith("https://example.com/image.jpg");
|
|
expect(openFile).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it.each(["#section", "//example.com/image.jpg", "javascript:alert(1)", "/tmp/%ZZ.jpg"])(
|
|
"does not open unsupported links as files: %s",
|
|
(href) => {
|
|
const { container } = render(<Markdown text={`[Link](${href})`} cwd="/repo" />);
|
|
fireEvent.click(container.querySelector("a")!);
|
|
expect(openFile).not.toHaveBeenCalled();
|
|
expect(openExternalUrl).not.toHaveBeenCalled();
|
|
},
|
|
);
|