toast.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import {
  5. upsertToastItem,
  6. resolveToastAnchorLeft,
  7. ToastMessageView,
  8. } from "@/components/ui/toast.js";
  9. describe("toast", () => {
  10. it("同一目标与动作替换旧消息,不同消息保持最新在底部", () => {
  11. const first = {
  12. id: 1,
  13. message: "Provider A 保存成功",
  14. durationMs: 3000,
  15. position: "bottom-left" as const,
  16. dedupeKey: "provider-save:provider-a",
  17. };
  18. const second = {
  19. id: 2,
  20. message: "model-a 连接成功",
  21. durationMs: 3000,
  22. position: "bottom-left" as const,
  23. dedupeKey: "model-test:provider-a:model-a",
  24. };
  25. const replacement = { ...first, id: 3, message: "Provider A 再次保存成功" };
  26. expect(upsertToastItem(upsertToastItem([], first), second)).toEqual([first, second]);
  27. expect(upsertToastItem([first, second], replacement)).toEqual([second, replacement]);
  28. });
  29. it("centers an anchored toast within its region", () => {
  30. expect(resolveToastAnchorLeft({ left: 280, width: 960 })).toBe(760);
  31. });
  32. it("renders update toast content and an action", () => {
  33. const html = renderToStaticMarkup(
  34. createElement(ToastMessageView, {
  35. item: {
  36. id: 1,
  37. message: "反馈有新回复\n模型切换报错有新的处理回复,可查看详情。",
  38. durationMs: 7000,
  39. position: "bottom-left",
  40. variant: "update",
  41. actionLabel: "查看",
  42. onAction: vi.fn(),
  43. },
  44. visible: true,
  45. isBottom: true,
  46. }),
  47. );
  48. expect(html).toContain("反馈有新回复");
  49. expect(html).toContain("查看");
  50. });
  51. it("renders Source Server update copy and an accessible dismiss action", () => {
  52. const html = renderToStaticMarkup(
  53. createElement(ToastMessageView, {
  54. item: {
  55. id: 6,
  56. message: "linux-amd64-dev 必须更新后才能连接\n当前 3.8.1,需要 3.9.0。",
  57. durationMs: 8000,
  58. position: "top-center",
  59. variant: "source-server-update",
  60. actionLabel: "立即更新",
  61. onAction: vi.fn(),
  62. dismissible: true,
  63. dismissLabel: "关闭",
  64. },
  65. visible: true,
  66. onDismiss: vi.fn(),
  67. }),
  68. );
  69. expect(html).toContain("linux-amd64-dev 必须更新后才能连接");
  70. expect(html).toContain("当前 3.8.1,需要 3.9.0。");
  71. expect(html).toContain("立即更新");
  72. expect(html).toContain('aria-label="关闭"');
  73. });
  74. it("renders a dismissible actionable info toast for subscription guidance", () => {
  75. const html = renderToStaticMarkup(
  76. createElement(ToastMessageView, {
  77. item: {
  78. id: 3,
  79. message: "Idle-time tasks are available for Coding Plan subscribers only.",
  80. durationMs: 8000,
  81. position: "top-right",
  82. variant: "info",
  83. actionLabel: "Upgrade",
  84. onAction: vi.fn(),
  85. dismissible: true,
  86. },
  87. visible: true,
  88. onDismiss: vi.fn(),
  89. }),
  90. );
  91. expect(html).toContain("Coding Plan subscribers only");
  92. expect(html).toContain("Upgrade");
  93. expect(html).toContain('aria-label="Close"');
  94. });
  95. it("renders multiline notice copy with its action", () => {
  96. const html = renderToStaticMarkup(
  97. createElement(ToastMessageView, {
  98. item: {
  99. id: 4,
  100. message: "文档技能\n插件未安装",
  101. durationMs: 5000,
  102. position: "top-right",
  103. variant: "info",
  104. actionLabel: "一键安装",
  105. onAction: vi.fn(),
  106. },
  107. visible: true,
  108. }),
  109. );
  110. expect(html).toContain("文档技能");
  111. expect(html).toContain("插件未安装");
  112. expect(html).toContain("一键安装");
  113. });
  114. it("moves top-right toasts from right to left with reduced-motion fallback", () => {
  115. const item = {
  116. id: 5,
  117. message: "Idle-time tasks are available for Coding Plan subscribers only.",
  118. durationMs: 8000,
  119. position: "top-right" as const,
  120. variant: "info" as const,
  121. };
  122. const hiddenHtml = renderToStaticMarkup(
  123. createElement(ToastMessageView, {
  124. item,
  125. visible: false,
  126. }),
  127. );
  128. const visibleHtml = renderToStaticMarkup(
  129. createElement(ToastMessageView, {
  130. item,
  131. visible: true,
  132. }),
  133. );
  134. expect(hiddenHtml).toContain("transition-[transform,opacity]");
  135. expect(hiddenHtml).toContain("duration-200");
  136. expect(hiddenHtml).toContain("ease-[cubic-bezier(0.77,0,0.175,1)]");
  137. expect(hiddenHtml).toContain("translate-x-[calc(100%+1rem)]");
  138. expect(hiddenHtml).toContain("opacity-0");
  139. expect(hiddenHtml).toContain("motion-reduce:transform-none");
  140. expect(visibleHtml).toContain("translate-x-0");
  141. expect(visibleHtml).toContain("opacity-100");
  142. expect(visibleHtml).not.toContain("-translate-y-1");
  143. });
  144. });