Skip to content

The platform for continuously improving AI agents.

1.1 KB
Raw Blame
// @vitest-environment jsdom
// covers: console-logic
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, expect, it, vi } from "vitest";

import MarkdownContent from "./markdown-content";

afterEach(() => {
  cleanup();
  vi.unstubAllGlobals();
});

const reply = "Run this:\n\n```python\nprint('hi')\n```\n";

it("lets a chat reply's code block be copied without its trailing newline", () => {
  const writeText = vi.fn(() => Promise.resolve());
  vi.stubGlobal("navigator", { clipboard: { writeText } });
  const { container } = render(<MarkdownContent chat>{reply}</MarkdownContent>);
  expect(container.firstElementChild?.classList.contains("chat-prose")).toBe(true);
  fireEvent.click(screen.getByRole("button", { name: "Copy code" }));
  expect(writeText).toHaveBeenCalledWith("print('hi')");
  expect(screen.getByRole("button", { name: "Copied" })).toBeTruthy();
});

it("keeps other Markdown without a copy button", () => {
  render(<MarkdownContent>{reply}</MarkdownContent>);
  expect(screen.queryByRole("button", { name: "Copy code" })).toBeNull();
});