chat-message.test.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { render, screen } from "@testing-library/react";
  2. import userEvent from "@testing-library/user-event";
  3. import { describe, it, expect, test } from "vitest";
  4. import { ChatMessage } from "#/components/chat-message";
  5. describe("ChatMessage", () => {
  6. it("should render a user message", () => {
  7. render(<ChatMessage type="user" message="Hello, World!" />);
  8. expect(screen.getByTestId("user-message")).toBeInTheDocument();
  9. expect(screen.getByText("Hello, World!")).toBeInTheDocument();
  10. });
  11. it("should render an assistant message", () => {
  12. render(<ChatMessage type="assistant" message="Hello, World!" />);
  13. expect(screen.getByTestId("assistant-message")).toBeInTheDocument();
  14. expect(screen.getByText("Hello, World!")).toBeInTheDocument();
  15. });
  16. it.skip("should support code syntax highlighting", () => {
  17. const code = "```js\nconsole.log('Hello, World!')\n```";
  18. render(<ChatMessage type="user" message={code} />);
  19. // SyntaxHighlighter breaks the code blocks into "tokens"
  20. expect(screen.getByText("console")).toBeInTheDocument();
  21. expect(screen.getByText("log")).toBeInTheDocument();
  22. expect(screen.getByText("'Hello, World!'")).toBeInTheDocument();
  23. });
  24. it.todo("should support markdown content");
  25. it("should render the copy to clipboard button when the user hovers over the message", async () => {
  26. const user = userEvent.setup();
  27. render(<ChatMessage type="user" message="Hello, World!" />);
  28. const message = screen.getByText("Hello, World!");
  29. expect(screen.getByTestId("copy-to-clipboard")).not.toBeVisible();
  30. await user.hover(message);
  31. expect(screen.getByTestId("copy-to-clipboard")).toBeVisible();
  32. });
  33. it("should copy content to clipboard", async () => {
  34. const user = userEvent.setup();
  35. render(<ChatMessage type="user" message="Hello, World!" />);
  36. const copyToClipboardButton = screen.getByTestId("copy-to-clipboard");
  37. await user.click(copyToClipboardButton);
  38. expect(navigator.clipboard.readText()).resolves.toBe("Hello, World!");
  39. });
  40. // BUG: vi.useFakeTimers() seems to break the tests
  41. it.todo(
  42. "should display a checkmark for 200ms and disable the button after copying content to clipboard",
  43. );
  44. it("should display an error toast if copying content to clipboard fails", async () => {});
  45. test.todo("push a toast after successfully copying content to clipboard");
  46. it("should render a component passed as a prop", () => {
  47. function Component() {
  48. return <div data-testid="custom-component">Custom Component</div>;
  49. }
  50. render(
  51. <ChatMessage type="user" message="Hello, World">
  52. <Component />
  53. </ChatMessage>,
  54. );
  55. expect(screen.getByTestId("custom-component")).toBeInTheDocument();
  56. });
  57. });