gitBranchSwitcher.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { createElement, type ReactNode } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. vi.mock("lucide-react", () => {
  5. const createIcon = (name: string) => (props: Record<string, unknown>) =>
  6. createElement("svg", { "data-icon": name, ...props });
  7. return {
  8. ChevronDownIcon: createIcon("chevron-down"),
  9. GitBranchIcon: createIcon("git-branch"),
  10. GitGraph: createIcon("git-graph"),
  11. LoaderIcon: createIcon("loader"),
  12. PlusIcon: createIcon("plus"),
  13. };
  14. });
  15. vi.mock("@/components/ui/button.js", () => ({
  16. Button: ({ children, ...props }: { children: ReactNode }) =>
  17. createElement("button", props, children),
  18. }));
  19. vi.mock("@/components/ui/command.js", () => ({
  20. Command: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  21. CommandEmpty: ({ children }: { children: ReactNode }) =>
  22. createElement("div", null, children),
  23. CommandGroup: ({ children }: { children: ReactNode }) =>
  24. createElement("div", null, children),
  25. CommandInput: (props: Record<string, unknown>) => createElement("input", props),
  26. CommandItem: ({ children, ...props }: { children: ReactNode }) =>
  27. createElement("div", props, children),
  28. CommandList: ({ children }: { children: ReactNode }) =>
  29. createElement("div", null, children),
  30. }));
  31. vi.mock("@/components/ui/popover.js", () => ({
  32. Popover: ({ children }: { children: ReactNode }) =>
  33. createElement("div", { "data-testid": "popover" }, children),
  34. PopoverContent: ({ children }: { children: ReactNode }) =>
  35. createElement("div", { "data-testid": "popover-content" }, children),
  36. PopoverTrigger: ({ children }: { children: ReactNode }) => children,
  37. }));
  38. vi.mock("@/git-branch-switcher/GitBranchDialogs.js", () => ({
  39. GitBranchCreateDialog: () => null,
  40. GitBranchSwitchAssistDialog: () => null,
  41. }));
  42. vi.mock("@/git-graph/GitGraphDialog.js", () => ({
  43. GitGraphDialog: () => null,
  44. }));
  45. vi.mock("@/hooks/useGitBranchSwitcher.js", () => ({
  46. useGitBranchSwitcher: () => ({
  47. branchesResult: null,
  48. closeSwitchAssistDialog: vi.fn(),
  49. commitAndSwitchBranch: vi.fn(),
  50. commitError: null,
  51. commitMessage: "",
  52. createBranchAndSwitch: vi.fn(),
  53. createBranchName: "",
  54. createDialogOpen: false,
  55. gitGraphDialogOpen: false,
  56. loadingBranches: false,
  57. mutationPending: false,
  58. open: false,
  59. openSwitchCommitDialog: vi.fn(),
  60. setCommitMessage: vi.fn(),
  61. setCreateBranchName: vi.fn(),
  62. setCreateDialogOpen: vi.fn(),
  63. setGitGraphDialogOpen: vi.fn(),
  64. setOpen: vi.fn(),
  65. switchAssistState: null,
  66. switchAssistStep: "commit",
  67. switchBranch: vi.fn(),
  68. }),
  69. }));
  70. vi.mock("@/i18n/IntlProvider.js", () => ({
  71. useZCodeIntl: () => ({
  72. intl: {
  73. formatMessage: ({ id }: { id: string }) => id,
  74. },
  75. locale: "zh-CN",
  76. }),
  77. }));
  78. describe("GitBranchSwitcher", () => {
  79. it("marks the primary branch icon separately from trailing status icons", async () => {
  80. const { GitBranchSwitcher } = await import("@/GitBranchSwitcher.js");
  81. const html = renderToStaticMarkup(
  82. createElement(GitBranchSwitcher, {
  83. dirtyFileCount: 0,
  84. gitSummary: {
  85. branchName: "main",
  86. headRefType: "branch",
  87. isGitAvailable: true,
  88. isRepository: true,
  89. },
  90. onRefreshGit: vi.fn(),
  91. workspacePath: "/workspace/zcode-cli",
  92. }),
  93. );
  94. expect(html).toContain('data-branch-switcher-primary-icon="true"');
  95. expect(html).toContain('data-branch-switcher-trailing-icon="true"');
  96. expect(html).toContain("main");
  97. });
  98. it("keeps the primary branch icon in compact remote mode", async () => {
  99. const { GitBranchSwitcher } = await import("@/GitBranchSwitcher.js");
  100. const html = renderToStaticMarkup(
  101. createElement(GitBranchSwitcher, {
  102. compactForRemoteControl: true,
  103. dirtyFileCount: 0,
  104. gitSummary: {
  105. branchName: "main",
  106. headRefType: "branch",
  107. isGitAvailable: true,
  108. isRepository: true,
  109. },
  110. onRefreshGit: vi.fn(),
  111. workspacePath: "/workspace/zcode-cli",
  112. }),
  113. );
  114. expect(html).toContain('data-branch-switcher-primary-icon="true"');
  115. expect(html).not.toContain('data-branch-switcher-trailing-icon="true"');
  116. expect(html).not.toContain("main");
  117. });
  118. it("can hide branch creation and graph footer actions", async () => {
  119. const { GitBranchSwitcher } = await import("@/GitBranchSwitcher.js");
  120. const html = renderToStaticMarkup(
  121. createElement(GitBranchSwitcher, {
  122. dirtyFileCount: 0,
  123. gitSummary: {
  124. branchName: "main",
  125. headRefType: "branch",
  126. isGitAvailable: true,
  127. isRepository: true,
  128. },
  129. onRefreshGit: vi.fn(),
  130. showFooterActions: false,
  131. workspacePath: "/workspace/zcode-cli",
  132. }),
  133. );
  134. expect(html).not.toContain("git.branchSwitcher.createAction");
  135. expect(html).not.toContain("gitGraph.menuAction");
  136. });
  137. });