contextAttachmentPill.test.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // @vitest-environment jsdom
  2. import { fireEvent, render, screen } from "@testing-library/react";
  3. import { FileIcon } from "lucide-react";
  4. import { createElement } from "react";
  5. import { afterEach, describe, expect, it, vi } from "vitest";
  6. import { ContextAttachmentPill } from "@/v4/composer/ContextAttachmentPill.js";
  7. afterEach(() => document.body.replaceChildren());
  8. describe("ContextAttachmentPill", () => {
  9. it("uses symmetric horizontal padding when the conversation pill has no remove action", () => {
  10. render(
  11. createElement(
  12. ContextAttachmentPill,
  13. {
  14. icon: createElement(FileIcon),
  15. label: "Conversation context",
  16. removeLabel: "Remove context",
  17. },
  18. createElement("div", null, "Context details"),
  19. ),
  20. );
  21. const trigger = screen.getByRole("button", { name: "Conversation context" });
  22. expect(trigger.className).toContain("px-3");
  23. expect(trigger.className).not.toContain("pr-1.5");
  24. });
  25. it("keeps one pill trigger while rendering details in the hover-card portal", async () => {
  26. const onRemoveAll = vi.fn();
  27. const { container } = render(
  28. createElement(
  29. "div",
  30. { "data-testid": "attachment-area" },
  31. createElement(
  32. ContextAttachmentPill,
  33. {
  34. icon: createElement(FileIcon),
  35. label: "1 context",
  36. removeLabel: "Remove context",
  37. onRemoveAll,
  38. },
  39. createElement("div", null, "Context details"),
  40. ),
  41. ),
  42. );
  43. const area = screen.getByTestId("attachment-area");
  44. const trigger = screen.getByRole("button", { name: "1 context" });
  45. expect(trigger.parentElement).toBe(area);
  46. expect(trigger.className).toContain("rounded-full");
  47. expect(trigger.className).toContain("bg-surface");
  48. expect(trigger.className).toContain("pl-3");
  49. expect(trigger.className).toContain("pr-1.5");
  50. const content = trigger.querySelector(":scope > span");
  51. expect(content?.className).toBe("flex min-w-0 items-center gap-1.5");
  52. fireEvent.pointerEnter(trigger);
  53. const details = await screen.findByText("Context details");
  54. const hoverContent = details.closest('[data-slot="hover-card-content"]');
  55. expect(hoverContent?.className).toContain("bg-tooltip");
  56. expect(hoverContent?.className).toContain("border-border");
  57. expect(hoverContent?.className).not.toContain("bg-menu");
  58. const removeButton = screen.getByRole("button", { name: "Remove context" });
  59. expect(removeButton.className).toContain("size-5");
  60. expect(removeButton.className).toContain("rounded-full");
  61. expect(removeButton.className).toContain("text-foreground-subtle");
  62. expect(removeButton.className).toContain("opacity-0");
  63. expect(removeButton.className).toContain("group-hover:opacity-100");
  64. expect(removeButton.querySelector("svg")?.getAttribute("class")).toContain("size-3.5");
  65. fireEvent.click(removeButton);
  66. expect(onRemoveAll).toHaveBeenCalledTimes(1);
  67. expect(container.querySelectorAll('[aria-label="1 context"]')).toHaveLength(1);
  68. });
  69. });