controlHintTooltip.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { createElement } from "react";
  2. import { readFileSync } from "node:fs";
  3. import { resolve } from "node:path";
  4. import { renderToStaticMarkup } from "react-dom/server";
  5. import { describe, expect, it } from "vitest";
  6. import { ControlHintTooltip } from "@/ControlHintTooltip.js";
  7. import { Button } from "@/components/ui/button.js";
  8. import { TooltipProvider } from "@/components/ui/tooltip.js";
  9. describe("ControlHintTooltip", () => {
  10. it("uses sm typography for tooltip copy and xs typography for shortcuts", () => {
  11. const tooltipSource = readFileSync(
  12. resolve(process.cwd(), "packages/ui/src/components/ui/tooltip.tsx"),
  13. "utf8",
  14. );
  15. const controlHintSource = readFileSync(
  16. resolve(process.cwd(), "packages/ui/src/ControlHintTooltip.tsx"),
  17. "utf8",
  18. );
  19. expect(tooltipSource).toContain("text-ui-sm text-tooltip-foreground");
  20. expect(tooltipSource).not.toContain("text-ui-base text-tooltip-foreground");
  21. expect(controlHintSource).not.toContain("text-ui-base");
  22. expect(controlHintSource).toContain("text-ui-xs font-medium text-tooltip-tag-foreground");
  23. });
  24. it("only creates a Radix tooltip provider for standalone tooltip instances", () => {
  25. const source = readFileSync(
  26. resolve(process.cwd(), "packages/ui/src/ControlHintTooltip.tsx"),
  27. "utf8",
  28. );
  29. // 共享 Provider 统一放在 Root(见 Root.tsx)。这里只允许 `standalone` 逃生舱各自建一个,
  30. // 因此断言的是"Provider 必须被 standalone 门控",而不是"源码里不许出现 Provider"——
  31. // 后者会把 standalone 这个有意的能力一并禁掉。
  32. expect(source).toContain("standalone ? <TooltipProvider>{tooltip}</TooltipProvider> : tooltip");
  33. expect(source.match(/<TooltipProvider>/g)).toHaveLength(1);
  34. expect(source.match(/<\/TooltipProvider>/g)).toHaveLength(1);
  35. // 承重项:默认必须是 false。一旦默认值翻转,大会话里每条消息动作都会各建一个
  36. // Provider,Radix 上下文树被放大到消息数量级——这正是本用例要挡住的退化。
  37. expect(source).toContain("standalone = false");
  38. });
  39. it("keeps the composed trigger ref stable across Radix asChild renders", () => {
  40. const source = readFileSync(
  41. resolve(process.cwd(), "packages/ui/src/ControlHintTooltip.tsx"),
  42. "utf8",
  43. );
  44. expect(source).not.toContain("ref: composeRefs(");
  45. expect(source).toContain("const composedTriggerRef = useCallback(");
  46. expect(source).toContain("[childRef, triggerRef]");
  47. });
  48. it("keeps long localized titles visible within the viewport", () => {
  49. const source = readFileSync(
  50. resolve(process.cwd(), "packages/ui/src/ControlHintTooltip.tsx"),
  51. "utf8",
  52. );
  53. expect(source).toContain("max-w-[min(28rem,calc(100vw-1rem))]");
  54. expect(source).toContain("whitespace-pre-line break-words");
  55. expect(source).not.toContain("font-medium whitespace-nowrap text-tooltip-foreground");
  56. });
  57. it("renders an icon button trigger without falling back to a nested Radix button", () => {
  58. const html = renderToStaticMarkup(
  59. createElement(
  60. TooltipProvider,
  61. null,
  62. createElement(
  63. ControlHintTooltip,
  64. { title: "Send", shortcut: "Enter" },
  65. createElement(
  66. Button,
  67. { type: "submit", size: "icon-lg", "aria-label": "Send" },
  68. createElement("span", { "aria-hidden": true }, "↑"),
  69. createElement("span", { className: "sr-only" }, "Send"),
  70. ),
  71. ),
  72. ),
  73. );
  74. expect(html).toMatch(/^<button[^>]*data-slot="tooltip-trigger"/);
  75. expect(html).not.toContain('<span data-slot="tooltip-trigger"');
  76. expect(html).toContain('aria-label="Send"');
  77. expect(html).toContain('type="submit"');
  78. });
  79. });