attachmentsPreview.test.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it } from "vitest";
  4. import { Attachment, AttachmentInfo, Attachments } from "@/components/ai-elements/attachments.js";
  5. function renderAttachmentWithOpen(openable: boolean) {
  6. return renderToStaticMarkup(
  7. createElement(
  8. Attachments,
  9. { variant: "inline" },
  10. createElement(
  11. Attachment,
  12. {
  13. data: {
  14. id: "screenshot",
  15. type: "file",
  16. filename: "screenshot.png",
  17. mediaType: "image/png",
  18. url: "blob:zcode-preview",
  19. },
  20. onOpen: openable ? () => undefined : undefined,
  21. openLabel: "Open image preview",
  22. },
  23. "screenshot.png",
  24. ),
  25. ),
  26. );
  27. }
  28. describe("Attachment preview affordance", () => {
  29. it("renders uploaded image attachments as keyboard-openable preview triggers", () => {
  30. const html = renderAttachmentWithOpen(true);
  31. expect(html).toContain('role="button"');
  32. expect(html).toContain('tabindex="0"');
  33. expect(html).toContain('aria-label="Open image preview"');
  34. });
  35. it("does not add button semantics when no preview action is provided", () => {
  36. const html = renderAttachmentWithOpen(false);
  37. expect(html).not.toContain('role="button"');
  38. expect(html).not.toContain('tabindex="0"');
  39. });
  40. it("renders inline clipboard text attachments as a single line with description", () => {
  41. const html = renderToStaticMarkup(
  42. createElement(
  43. Attachments,
  44. { variant: "inline" },
  45. createElement(
  46. Attachment,
  47. {
  48. data: {
  49. description: "128 行",
  50. displayName: "粘贴文本",
  51. id: "pasted-text",
  52. filename: "pasted-text-20260627-153012.txt",
  53. mediaType: "text/plain",
  54. sourceKind: "clipboard-text",
  55. type: "file",
  56. url: "",
  57. },
  58. },
  59. createElement(AttachmentInfo),
  60. ),
  61. ),
  62. );
  63. expect(html).toContain("粘贴文本");
  64. expect(html).toContain("128 行");
  65. expect(html).not.toContain("18 KB");
  66. expect(html).not.toContain("pasted-text-20260627-153012.txt");
  67. });
  68. });