/** * @file MarkdownPreview.test.tsx * @description The markdown preview: GFM renders (headings, tables, task * lists), raw HTML stays text, links open in a new tab, frontmatter is a * code block; and the config FileViewer offers Raw / Preview only for a * markdown file. * @author Nguyễn Ngọc Trí Vĩ */ import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { describe, expect, it, vi } from "vitest"; import { MarkdownPreview, isMarkdown } from "../MarkdownPreview"; import { FileViewer } from "../config/FileViewer"; describe("MarkdownPreview", () => { it("renders GFM: headings, tables and task lists", () => { render( ); expect(screen.getByRole("heading", { level: 1, name: "Title" })).toBeInTheDocument(); expect(screen.getByRole("table")).toBeInTheDocument(); expect(screen.getAllByRole("checkbox")).toHaveLength(2); }); it("never runs raw HTML and opens links in a new tab", () => { const { container } = render( alert(1)\n\n[x](https://a.example)'} /> ); expect(container.querySelector("script")).toBeNull(); expect(container.querySelector("img")).toBeNull(); const link = screen.getByRole("link", { name: "x" }); expect(link).toHaveAttribute("target", "_blank"); expect(link).toHaveAttribute("rel", "noreferrer noopener"); }); it("shows frontmatter as a block, not as a heading", () => { const { container } = render( ); expect(container.querySelector(".md-frontmatter")).toHaveTextContent("name: demo"); expect(screen.queryByRole("heading")).toBeNull(); expect(screen.getByText("Body text")).toBeInTheDocument(); }); it("knows a markdown path", () => { expect(isMarkdown("/a/SKILL.md")).toBe(true); expect(isMarkdown("README.markdown")).toBe(true); expect(isMarkdown("config.toml")).toBe(false); expect(isMarkdown(null)).toBe(false); }); }); describe("FileViewer — markdown preview", () => { const data = { text: "# Hello\n", truncated: false } as never; it("switches a markdown file between raw and preview", async () => { render(); expect(screen.queryByTestId("markdown-preview")).toBeNull(); expect(screen.getByText("# Hello")).toBeInTheDocument(); await userEvent.click(screen.getByRole("button", { name: "Preview" })); expect(screen.getByRole("heading", { name: "Hello" })).toBeInTheDocument(); await userEvent.click(screen.getByRole("button", { name: "Raw" })); expect(screen.queryByTestId("markdown-preview")).toBeNull(); }); it("offers no preview for another file type", () => { render(); expect(screen.queryByRole("button", { name: "Preview" })).toBeNull(); }); });