gitActionMenuDisplay.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. canRenderGitActionMenu,
  4. canPushGitBranch,
  5. canUseGitActionMenu,
  6. resolveGitActionMenuPrimaryAction,
  7. } from "../src/git-action-menu/display.js";
  8. describe("canUseGitActionMenu", () => {
  9. it("keeps the entry rendered but disables actions when git is unavailable", () => {
  10. expect(
  11. canUseGitActionMenu({
  12. isGitAvailable: false,
  13. isRepository: false,
  14. }),
  15. ).toBe(false);
  16. expect(
  17. canUseGitActionMenu({
  18. isGitAvailable: true,
  19. isRepository: false,
  20. }),
  21. ).toBe(false);
  22. expect(
  23. canUseGitActionMenu({
  24. isGitAvailable: true,
  25. isRepository: true,
  26. }),
  27. ).toBe(true);
  28. });
  29. });
  30. describe("canRenderGitActionMenu", () => {
  31. it("renders only when commit or push is available", () => {
  32. expect(
  33. canRenderGitActionMenu({
  34. actionAvailable: true,
  35. commitEnabled: true,
  36. pushEnabled: false,
  37. }),
  38. ).toBe(true);
  39. expect(
  40. canRenderGitActionMenu({
  41. actionAvailable: true,
  42. commitEnabled: false,
  43. pushEnabled: true,
  44. }),
  45. ).toBe(true);
  46. expect(
  47. canRenderGitActionMenu({
  48. actionAvailable: true,
  49. commitEnabled: false,
  50. pushEnabled: false,
  51. }),
  52. ).toBe(false);
  53. });
  54. it("hides when git actions are unavailable", () => {
  55. expect(
  56. canRenderGitActionMenu({
  57. actionAvailable: false,
  58. commitEnabled: true,
  59. pushEnabled: true,
  60. }),
  61. ).toBe(false);
  62. });
  63. });
  64. describe("canPushGitBranch", () => {
  65. it("requires a branch checkout before pushing", () => {
  66. expect(
  67. canPushGitBranch({
  68. headRefType: "detached",
  69. branchName: "main",
  70. trackingBranchName: "origin/main",
  71. ahead: 2,
  72. }),
  73. ).toBe(false);
  74. });
  75. it("allows first push without an upstream and hides redundant tracked no-op pushes", () => {
  76. expect(
  77. canPushGitBranch({
  78. headRefType: "branch",
  79. branchName: "feature/publish",
  80. trackingBranchName: null,
  81. ahead: 0,
  82. }),
  83. ).toBe(true);
  84. expect(
  85. canPushGitBranch({
  86. headRefType: "branch",
  87. branchName: "main",
  88. trackingBranchName: "origin/main",
  89. ahead: 0,
  90. }),
  91. ).toBe(false);
  92. });
  93. });
  94. describe("resolveGitActionMenuPrimaryAction", () => {
  95. it("prefers commit, then push", () => {
  96. expect(
  97. resolveGitActionMenuPrimaryAction({
  98. actionAvailable: true,
  99. commitEnabled: true,
  100. pushEnabled: true,
  101. }),
  102. ).toBe("commit");
  103. expect(
  104. resolveGitActionMenuPrimaryAction({
  105. actionAvailable: true,
  106. commitEnabled: false,
  107. pushEnabled: true,
  108. }),
  109. ).toBe("push");
  110. expect(
  111. resolveGitActionMenuPrimaryAction({
  112. actionAvailable: true,
  113. commitEnabled: false,
  114. pushEnabled: false,
  115. }),
  116. ).toBeNull();
  117. });
  118. it("has no primary action when git actions are unavailable", () => {
  119. expect(
  120. resolveGitActionMenuPrimaryAction({
  121. actionAvailable: false,
  122. commitEnabled: false,
  123. pushEnabled: false,
  124. }),
  125. ).toBeNull();
  126. });
  127. });