menuRadius.test.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { readFile } from "node:fs/promises";
  2. import { describe, expect, it } from "vitest";
  3. const cases = [
  4. [
  5. "dropdown-menu",
  6. ["DropdownMenuContent", "DropdownMenuSubContent"],
  7. [
  8. "DropdownMenuItem",
  9. "DropdownMenuCheckboxItem",
  10. "DropdownMenuRadioItem",
  11. "DropdownMenuSubTrigger",
  12. ],
  13. ],
  14. [
  15. "context-menu",
  16. ["ContextMenuContent", "ContextMenuSubContent"],
  17. ["ContextMenuItem", "ContextMenuSubTrigger"],
  18. ],
  19. ["select", ["SelectContent"], ["SelectItem", "SelectRichItem"]],
  20. ] as const;
  21. describe("menu radius hierarchy", () => {
  22. for (const [file, shells, items] of cases) {
  23. it(`${file}: independent shells use lg and options use md`, async () => {
  24. const source = await readFile(
  25. new URL(`../src/components/ui/${file}.tsx`, import.meta.url),
  26. "utf8",
  27. );
  28. for (const [names, radius] of [
  29. [shells, "lg"],
  30. [items, "md"],
  31. ] as const) {
  32. for (const name of names) {
  33. const start = source.search(new RegExp(`(?:function ${name}\\(|const ${name} =)`));
  34. expect(start).toBeGreaterThanOrEqual(0);
  35. // 组件体截到下一个顶层声明:forwardRef 组件是 `const X =`,只认 function 会把相邻组件并进来。
  36. const boundaries = ["\nfunction ", "\nconst "]
  37. .map((marker) => source.indexOf(marker, start + 1))
  38. .filter((index) => index >= 0);
  39. const next = boundaries.length > 0 ? Math.min(...boundaries) : -1;
  40. const body = source.slice(start, next < 0 ? undefined : next);
  41. expect(body.match(/\brounded-(?:sm|md|lg|xl|2xl|3xl|full)\b/g), name).toEqual([
  42. `rounded-${radius}`,
  43. ]);
  44. }
  45. }
  46. });
  47. }
  48. });
  49. describe("business menus inherit shared radius", () => {
  50. for (const file of [
  51. "WindowsCaptionMenuButton",
  52. "settings/OffPeakEditActionsMenu",
  53. "settings/AutomationEditView",
  54. "settings/OffPeakHistoryTab",
  55. ]) {
  56. it(file, async () => {
  57. const source = await readFile(new URL(`../src/${file}.tsx`, import.meta.url), "utf8");
  58. const tags = source.match(/<DropdownMenu(?:Content|Item)\b[\s\S]*?(?=\n\s*>|>\n)/g) ?? [];
  59. expect(tags.length).toBeGreaterThan(0);
  60. for (const tag of tags) {
  61. expect(tag).not.toMatch(/rounded-(?:lg|xl|2xl|3xl|\[)/);
  62. }
  63. });
  64. }
  65. });