commandCenterTypography.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // @vitest-environment jsdom
  2. import { cleanup, fireEvent, render, screen } from "@testing-library/react";
  3. import { createElement } from "react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import type { CommandCenterSearchHistoryEntry } from "@/command-center/commandCenterSearchHistory.js";
  6. import {
  7. CommandCenterConversationTimestamp,
  8. CommandCenterScopeButton,
  9. CommandCenterSearchHistory,
  10. } from "@/command-center/CommandCenterDialog.js";
  11. // Cmd+K 命令中心内字号语义(DESIGN.md)的固化为单测目标:
  12. // - scope tabs / 历史 chip 搜索词:可交互/可读内容 → text-ui-base
  13. // - 历史分区标题 / 时间戳:次要文案、helper text → text-ui-sm
  14. // - chip scope 前缀:badge 标记 → text-ui-xs
  15. afterEach(() => {
  16. cleanup();
  17. });
  18. describe("CommandCenterScopeButton", () => {
  19. it("使用 text-ui-base 而非过小的 text-ui-xs", () => {
  20. render(
  21. createElement(
  22. CommandCenterScopeButton,
  23. { active: false, label: "全部", onClick: vi.fn() },
  24. createElement("span", { "data-testid": "icon" }),
  25. ),
  26. );
  27. const tab = screen.getByRole("tab", { name: "全部" });
  28. expect(tab.className).toContain("text-ui-base");
  29. expect(tab.className).not.toContain("text-ui-xs");
  30. });
  31. it("点击时触发 onClick", () => {
  32. const onClick = vi.fn();
  33. render(
  34. createElement(
  35. CommandCenterScopeButton,
  36. { active: false, label: "命令", onClick },
  37. null,
  38. ),
  39. );
  40. fireEvent.click(screen.getByRole("tab", { name: "命令" }));
  41. expect(onClick).toHaveBeenCalledOnce();
  42. });
  43. it("active 状态反映在 aria-selected", () => {
  44. const { rerender } = render(
  45. createElement(
  46. CommandCenterScopeButton,
  47. { active: true, label: "对话", onClick: vi.fn() },
  48. null,
  49. ),
  50. );
  51. const tab = screen.getByRole("tab", { name: "对话" });
  52. expect(tab.getAttribute("aria-selected")).toBe("true");
  53. rerender(
  54. createElement(
  55. CommandCenterScopeButton,
  56. { active: false, label: "对话", onClick: vi.fn() },
  57. null,
  58. ),
  59. );
  60. expect(tab.getAttribute("aria-selected")).toBe("false");
  61. });
  62. });
  63. describe("CommandCenterConversationTimestamp", () => {
  64. it("使用 text-ui-sm 而非 text-ui-xs", () => {
  65. render(
  66. createElement(
  67. CommandCenterConversationTimestamp,
  68. null,
  69. "2小时前",
  70. ),
  71. );
  72. const span = screen.getByText("2小时前");
  73. expect(span.className).toContain("text-ui-sm");
  74. expect(span.className).not.toContain("text-ui-xs");
  75. });
  76. });
  77. function makeEntry(overrides: Partial<CommandCenterSearchHistoryEntry> = {}): CommandCenterSearchHistoryEntry {
  78. return {
  79. query: "登录",
  80. scope: "all",
  81. updatedAt: 1700000000000,
  82. ...overrides,
  83. };
  84. }
  85. describe("CommandCenterSearchHistory", () => {
  86. function renderHistory(
  87. overrides: Record<string, unknown> = {},
  88. ) {
  89. const props = {
  90. entries: [makeEntry()],
  91. expanded: false,
  92. onToggleExpanded: vi.fn(),
  93. onPickEntry: vi.fn(),
  94. onClear: vi.fn(),
  95. historyLabel: "历史搜索",
  96. clearLabel: "清除历史",
  97. expandLabel: "展开",
  98. collapseLabel: "收起",
  99. ...overrides,
  100. };
  101. render(createElement(CommandCenterSearchHistory, props));
  102. return props;
  103. }
  104. it("分区标题使用 text-ui-sm", () => {
  105. renderHistory();
  106. const heading = screen.getByText("历史搜索");
  107. expect(heading.className).not.toContain("text-ui");
  108. // 标题在 div 容器上,向上取父元素校验
  109. const container = heading.parentElement;
  110. expect(container?.className ?? "").toContain("text-ui-sm");
  111. });
  112. it("历史 chip 搜索词使用 text-ui-base,scope 前缀保持 text-ui-xs", () => {
  113. renderHistory({
  114. entries: [makeEntry({ query: "登录", scope: "commands" })],
  115. });
  116. // chip 是 button;点击它再检查 className
  117. const chip = screen.getByText("登录").closest("button");
  118. expect(chip).not.toBeNull();
  119. expect(chip!.className).toContain("text-ui-base");
  120. expect(chip!.className).not.toContain("text-ui-xs");
  121. // commands scope 的前缀是 ">",作为 badge 保持 text-ui-xs
  122. const prefix = screen.getByText(">");
  123. expect(prefix.className).toContain("text-ui-xs");
  124. });
  125. it("点击历史 chip 触发 onPickEntry", () => {
  126. const onPickEntry = vi.fn();
  127. renderHistory({ onPickEntry });
  128. fireEvent.click(screen.getByText("登录"));
  129. expect(onPickEntry).toHaveBeenCalledOnce();
  130. const entry = onPickEntry.mock.calls[0]![0] as CommandCenterSearchHistoryEntry;
  131. expect(entry.query).toBe("登录");
  132. });
  133. it("清除按钮触发 onClear", () => {
  134. const onClear = vi.fn();
  135. renderHistory({ onClear });
  136. fireEvent.click(screen.getByLabelText("清除历史"));
  137. expect(onClear).toHaveBeenCalledOnce();
  138. });
  139. it("条目超过 6 条时显示展开按钮,并触发 onToggleExpanded", () => {
  140. const onToggleExpanded = vi.fn();
  141. const entries = Array.from({ length: 7 }, (_, i) =>
  142. makeEntry({ query: `查询${i}`, updatedAt: 1700000000000 + i }),
  143. );
  144. renderHistory({ entries, onToggleExpanded });
  145. const expandBtn = screen.getByLabelText("展开");
  146. fireEvent.click(expandBtn);
  147. expect(onToggleExpanded).toHaveBeenCalledOnce();
  148. });
  149. it("条目不超过 6 条时不显示展开按钮", () => {
  150. renderHistory();
  151. expect(screen.queryByLabelText("展开")).toBeNull();
  152. expect(screen.queryByLabelText("收起")).toBeNull();
  153. });
  154. // CR-01: text-ui-base 会随 --ui-font-size(12–20px)缩放,但固定高度 h-5(20px
  155. // border-box) 不会变化。字号调到 20px 时,扣除 2px 边框后内容区仅剩 18px,
  156. // 无法容纳 20px 字体的行盒,配合外层 overflow-hidden 会导致文字上下裁切。
  157. // 可缩放 UI 文本禁止绑定固定高度,与 uiFontTokenMigration 的约束同源。
  158. it("历史 chip 不用固定高度 h-5 限制可缩放的 text-ui-base", () => {
  159. renderHistory({
  160. entries: [makeEntry({ query: "登录" })],
  161. });
  162. const chip = screen.getByText("登录").closest("button");
  163. expect(chip).not.toBeNull();
  164. // 可缩放文本应使用 min-h + 行高,而非固定高度 h-5(min-h-5 是允许的)
  165. expect(chip!.className).not.toMatch(/(^| )h-5( |$)/);
  166. // 必须用 min-h-5 让高度随字号增长
  167. expect(chip!.className).toContain("min-h-5");
  168. expect(chip!.className).toContain("leading-none");
  169. });
  170. });