toast.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import {
  5. resolveToastStackClassName,
  6. upsertToastItem,
  7. resolveToastAnchorLeft,
  8. ToastMessageView,
  9. } from "@/components/ui/toast.js";
  10. describe("toast", () => {
  11. it("同一目标与动作替换旧消息,不同消息保持最新在底部", () => {
  12. const first = {
  13. id: 1,
  14. message: "Provider A 保存成功",
  15. durationMs: 3000,
  16. position: "bottom-left" as const,
  17. dedupeKey: "provider-save:provider-a",
  18. };
  19. const second = {
  20. id: 2,
  21. message: "model-a 连接成功",
  22. durationMs: 3000,
  23. position: "bottom-left" as const,
  24. dedupeKey: "model-test:provider-a:model-a",
  25. };
  26. const replacement = { ...first, id: 3, message: "Provider A 再次保存成功" };
  27. expect(upsertToastItem(upsertToastItem([], first), second)).toEqual([first, second]);
  28. expect(upsertToastItem([first, second], replacement)).toEqual([second, replacement]);
  29. });
  30. it("centers an anchored toast within its region", () => {
  31. expect(resolveToastAnchorLeft({ left: 280, width: 960 })).toBe(760);
  32. });
  33. it("uses real viewport insets for every stack and never translates a bottom stack outside", () => {
  34. expect(resolveToastStackClassName("top-center")).toContain("top-16");
  35. expect(resolveToastStackClassName("top-right")).toContain("top-16");
  36. expect(resolveToastStackClassName("bottom-left")).toContain("bottom-");
  37. expect(resolveToastStackClassName("bottom-center")).toContain("bottom-");
  38. expect(resolveToastStackClassName("bottom-left")).not.toContain("translate-y");
  39. expect(resolveToastStackClassName("bottom-center")).not.toContain("translate-y");
  40. expect(resolveToastStackClassName("bottom-center")).toContain("left-1/2");
  41. expect(resolveToastStackClassName("bottom-center")).toContain("-translate-x-1/2");
  42. });
  43. it("renders update toasts as a softer actionable notice", () => {
  44. const html = renderToStaticMarkup(
  45. createElement(ToastMessageView, {
  46. item: {
  47. id: 1,
  48. message: "反馈有新回复\n模型切换报错有新的处理回复,可查看详情。",
  49. durationMs: 7000,
  50. position: "bottom-left",
  51. variant: "update",
  52. actionLabel: "查看",
  53. onAction: vi.fn(),
  54. },
  55. visible: true,
  56. isBottom: true,
  57. }),
  58. );
  59. expect(html).toContain("反馈有新回复");
  60. expect(html).toContain("查看");
  61. expect(html).toContain("border-popover-border");
  62. expect(html).toContain("bg-toast/60");
  63. expect(html).toContain("backdrop-blur-xl");
  64. expect(html).toContain("rounded-2xl");
  65. expect(html).not.toContain("rounded-full border");
  66. expect(html).not.toContain("bg-popover");
  67. expect(html).toContain("origin-bottom-left");
  68. });
  69. it("keeps update toasts compact when title and action are present", () => {
  70. const html = renderToStaticMarkup(
  71. createElement(ToastMessageView, {
  72. item: {
  73. id: 2,
  74. message: "Update ready\nRestart the app to use the latest version.",
  75. durationMs: 7000,
  76. position: "bottom-left",
  77. variant: "update",
  78. actionLabel: "Restart",
  79. onAction: vi.fn(),
  80. },
  81. visible: true,
  82. isBottom: true,
  83. }),
  84. );
  85. expect(html).toContain("max-w-[min(300px,calc(100vw-1rem))]");
  86. expect(html).toContain("w-[min(300px,calc(100vw-1rem))]");
  87. expect(html).toContain("grid-cols-[auto_minmax(0,1fr)_auto]");
  88. expect(html).toContain("max-w-14");
  89. });
  90. it("renders a dismissible actionable info toast for subscription guidance", () => {
  91. const html = renderToStaticMarkup(
  92. createElement(ToastMessageView, {
  93. item: {
  94. id: 3,
  95. message: "Idle-time tasks are available for Coding Plan subscribers only.",
  96. durationMs: 8000,
  97. position: "top-right",
  98. variant: "info",
  99. actionLabel: "Upgrade",
  100. onAction: vi.fn(),
  101. dismissible: true,
  102. },
  103. visible: true,
  104. onDismiss: vi.fn(),
  105. }),
  106. );
  107. expect(html).toContain("Coding Plan subscribers only");
  108. expect(html).toContain("Upgrade");
  109. expect(html).toContain('aria-label="Close"');
  110. expect(html).toContain("w-[min(536px,calc(100vw-2rem))]");
  111. expect(html).toContain("gap-4");
  112. expect(html).toContain("flex-[1_0_0]");
  113. expect(html).toContain("py-3");
  114. expect(html).not.toContain("min-h-15");
  115. expect(html).toContain("whitespace-normal");
  116. expect(html).toContain("max-w-1/2");
  117. expect(html).toContain("underline");
  118. expect(html).toContain("bg-toast/60");
  119. expect(html).toContain("backdrop-blur-xl");
  120. expect(html).toContain("rounded-2xl");
  121. expect(html).not.toContain("rounded-full border");
  122. });
  123. it("centers a notice action vertically beside multiline content", () => {
  124. const html = renderToStaticMarkup(
  125. createElement(ToastMessageView, {
  126. item: {
  127. id: 4,
  128. message: "文档技能\n插件未安装",
  129. durationMs: 5000,
  130. position: "top-right",
  131. variant: "info",
  132. actionLabel: "一键安装",
  133. onAction: vi.fn(),
  134. },
  135. visible: true,
  136. }),
  137. );
  138. expect(html).toContain("文档技能");
  139. expect(html).toContain("插件未安装");
  140. expect(html).toContain("一键安装");
  141. expect(html).toContain("self-center");
  142. });
  143. it("moves top-right toasts from right to left with reduced-motion fallback", () => {
  144. const item = {
  145. id: 5,
  146. message: "Idle-time tasks are available for Coding Plan subscribers only.",
  147. durationMs: 8000,
  148. position: "top-right" as const,
  149. variant: "info" as const,
  150. };
  151. const hiddenHtml = renderToStaticMarkup(
  152. createElement(ToastMessageView, {
  153. item,
  154. visible: false,
  155. }),
  156. );
  157. const visibleHtml = renderToStaticMarkup(
  158. createElement(ToastMessageView, {
  159. item,
  160. visible: true,
  161. }),
  162. );
  163. expect(hiddenHtml).toContain("transition-[transform,opacity]");
  164. expect(hiddenHtml).toContain("duration-200");
  165. expect(hiddenHtml).toContain("ease-[cubic-bezier(0.77,0,0.175,1)]");
  166. expect(hiddenHtml).toContain("translate-x-[calc(100%+1rem)]");
  167. expect(hiddenHtml).toContain("opacity-0");
  168. expect(hiddenHtml).toContain("motion-reduce:transform-none");
  169. expect(visibleHtml).toContain("translate-x-0");
  170. expect(visibleHtml).toContain("opacity-100");
  171. expect(visibleHtml).not.toContain("-translate-y-1");
  172. });
  173. });