|
| 1 | +import { Fragment, type ComponentProps, type ReactNode } from "react"; |
| 2 | +import { act, create, type ReactTestRenderer } from "react-test-renderer"; |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 4 | + |
| 5 | +import { WorkingChangesTree } from "./WorkingChangesTree"; |
| 6 | + |
| 7 | +// Exercise selection and context actions without a virtual viewport or portal positioning. |
| 8 | +vi.mock("@legendapp/list/react", () => ({ |
| 9 | + LegendList: ({ |
| 10 | + data, |
| 11 | + renderItem, |
| 12 | + }: { |
| 13 | + data: ReadonlyArray<{ scope: string; kind: string; path: string }>; |
| 14 | + renderItem: (input: { item: (typeof data)[number] }) => ReactNode; |
| 15 | + }) => ( |
| 16 | + <> |
| 17 | + {data.map((item) => ( |
| 18 | + <Fragment key={`${item.scope}:${item.kind}:${item.path}`}>{renderItem({ item })}</Fragment> |
| 19 | + ))} |
| 20 | + </> |
| 21 | + ), |
| 22 | +})); |
| 23 | +vi.mock("../../hooks/useTheme", () => ({ useTheme: () => ({ resolvedTheme: "dark" }) })); |
| 24 | +vi.mock("../chat/PierreEntryIcon", () => ({ PierreEntryIcon: () => null })); |
| 25 | +vi.mock("../MorphIcon", () => ({ MorphIcon: () => null })); |
| 26 | +vi.mock("../ui/tooltip", () => ({ |
| 27 | + Tooltip: ({ children }: { children: ReactNode }) => children, |
| 28 | + TooltipTrigger: ({ render, children }: { render: ReactNode; children: ReactNode }) => ( |
| 29 | + <> |
| 30 | + {render} |
| 31 | + {children} |
| 32 | + </> |
| 33 | + ), |
| 34 | + TooltipPopup: () => null, |
| 35 | +})); |
| 36 | +vi.mock("../ui/menu", () => ({ |
| 37 | + Menu: ({ open, children }: { open: boolean; children: ReactNode }) => (open ? children : null), |
| 38 | + MenuPopup: ({ children }: { children: ReactNode }) => children, |
| 39 | + MenuItem: ({ |
| 40 | + children, |
| 41 | + disabled, |
| 42 | + onClick, |
| 43 | + }: { |
| 44 | + children: ReactNode; |
| 45 | + disabled: boolean; |
| 46 | + onClick: () => void; |
| 47 | + }) => ( |
| 48 | + <button role="menuitem" disabled={disabled} onClick={onClick}> |
| 49 | + {children} |
| 50 | + </button> |
| 51 | + ), |
| 52 | +})); |
| 53 | + |
| 54 | +type Props = ComponentProps<typeof WorkingChangesTree>; |
| 55 | +type Scope = "staged" | "unstaged"; |
| 56 | +const unstaged: Props["unstaged"] = [ |
| 57 | + { path: "src/lib/a.ts", status: "modified" }, |
| 58 | + { path: "src/lib/deep/b.ts", status: "modified" }, |
| 59 | + { path: "src/library/neighbor.ts", status: "modified" }, |
| 60 | + { path: "tests/a.test.ts", status: "modified" }, |
| 61 | +]; |
| 62 | +const staged: Props["staged"] = [ |
| 63 | + { path: "src/lib/a.ts", status: "modified" }, |
| 64 | + { path: "staged/old.ts", status: "modified" }, |
| 65 | +]; |
| 66 | + |
| 67 | +describe("working changes context actions", () => { |
| 68 | + let renderer: ReactTestRenderer | undefined; |
| 69 | + const onStage = vi.fn<Props["onStage"]>(); |
| 70 | + const onRevert = vi.fn<Props["onRevert"]>(); |
| 71 | + const onSelect = vi.fn<Props["onSelect"]>(); |
| 72 | + |
| 73 | + async function mount(overrides: Partial<Props> = {}) { |
| 74 | + await act(async () => { |
| 75 | + renderer = create( |
| 76 | + <WorkingChangesTree |
| 77 | + staged={staged} |
| 78 | + unstaged={unstaged} |
| 79 | + selected={null} |
| 80 | + disabled={false} |
| 81 | + revertDisabled={false} |
| 82 | + onSelect={onSelect} |
| 83 | + onStage={onStage} |
| 84 | + onRevert={onRevert} |
| 85 | + onViewAll={() => {}} |
| 86 | + {...overrides} |
| 87 | + />, |
| 88 | + ); |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + async function select(scope: Scope, path: string, toggle = false) { |
| 93 | + await act(async () => { |
| 94 | + renderer!.root.findByProps({ "aria-label": `${scope}: ${path}` }).props.onClick({ |
| 95 | + metaKey: toggle, |
| 96 | + ctrlKey: false, |
| 97 | + shiftKey: false, |
| 98 | + }); |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + async function rightClick(scope: Scope, path: string) { |
| 103 | + await act(async () => { |
| 104 | + renderer!.root |
| 105 | + .findByProps({ "data-git-scope": scope, "data-git-path": path }) |
| 106 | + .props.onContextMenu({ |
| 107 | + preventDefault: vi.fn(), |
| 108 | + stopPropagation: vi.fn(), |
| 109 | + clientX: 241, |
| 110 | + clientY: 173, |
| 111 | + }); |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + const menuItems = () => renderer!.root.findAllByProps({ role: "menuitem" }); |
| 116 | + const item = (label: string) => menuItems().find((node) => node.children.includes(label))!; |
| 117 | + async function choose(label: string) { |
| 118 | + await act(async () => item(label).props.onClick()); |
| 119 | + } |
| 120 | + |
| 121 | + beforeEach(() => { |
| 122 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 123 | + onStage.mockClear(); |
| 124 | + onRevert.mockClear(); |
| 125 | + onSelect.mockClear(); |
| 126 | + }); |
| 127 | + afterEach(async () => { |
| 128 | + await act(async () => renderer?.unmount()); |
| 129 | + renderer = undefined; |
| 130 | + vi.unstubAllGlobals(); |
| 131 | + }); |
| 132 | + |
| 133 | + it("stages or reverts all selected files and collapsed folders", async () => { |
| 134 | + await mount(); |
| 135 | + await select("unstaged", "src/lib"); |
| 136 | + await select("unstaged", "tests/a.test.ts", true); |
| 137 | + await rightClick("unstaged", "src/lib"); |
| 138 | + expect(menuItems()).toHaveLength(2); |
| 139 | + await choose("Stage changes"); |
| 140 | + const paths = ["src/lib/a.ts", "src/lib/deep/b.ts", "tests/a.test.ts"]; |
| 141 | + expect(onStage).toHaveBeenCalledWith("unstaged", paths); |
| 142 | + |
| 143 | + await rightClick("unstaged", "tests/a.test.ts"); |
| 144 | + await choose("Revert changes"); |
| 145 | + expect(onRevert).toHaveBeenCalledWith(paths); |
| 146 | + }); |
| 147 | + |
| 148 | + it("keeps the selection when right-clicking a descendant of a selected folder", async () => { |
| 149 | + await mount(); |
| 150 | + await select("unstaged", "src/lib", true); |
| 151 | + await select("unstaged", "tests/a.test.ts", true); |
| 152 | + await rightClick("unstaged", "src/lib/a.ts"); |
| 153 | + await choose("Stage changes"); |
| 154 | + expect(onStage).toHaveBeenCalledWith("unstaged", [ |
| 155 | + "src/lib/a.ts", |
| 156 | + "src/lib/deep/b.ts", |
| 157 | + "tests/a.test.ts", |
| 158 | + ]); |
| 159 | + expect(onSelect).not.toHaveBeenCalled(); |
| 160 | + }); |
| 161 | + |
| 162 | + it("targets an unselected row instead of applying its menu to an unrelated selection", async () => { |
| 163 | + await mount(); |
| 164 | + await select("unstaged", "src/lib"); |
| 165 | + await select("unstaged", "tests/a.test.ts", true); |
| 166 | + await rightClick("unstaged", "src/library/neighbor.ts"); |
| 167 | + await choose("Revert changes"); |
| 168 | + expect(onRevert).toHaveBeenCalledWith(["src/library/neighbor.ts"]); |
| 169 | + }); |
| 170 | + |
| 171 | + it("offers only Unstage for staged rows and excludes selected unstaged files", async () => { |
| 172 | + await mount(); |
| 173 | + await select("staged", "src/lib"); |
| 174 | + await select("staged", "staged/old.ts", true); |
| 175 | + await select("unstaged", "tests/a.test.ts", true); |
| 176 | + await rightClick("staged", "src/lib"); |
| 177 | + expect(menuItems()).toHaveLength(1); |
| 178 | + await choose("Unstage changes"); |
| 179 | + expect(onStage).toHaveBeenCalledWith("staged", ["src/lib/a.ts", "staged/old.ts"]); |
| 180 | + expect(onRevert).not.toHaveBeenCalled(); |
| 181 | + }); |
| 182 | + |
| 183 | + it("keeps staging and revert availability independent", async () => { |
| 184 | + await mount({ disabled: true }); |
| 185 | + await rightClick("unstaged", "tests/a.test.ts"); |
| 186 | + expect(item("Stage changes").props.disabled).toBe(true); |
| 187 | + expect(item("Revert changes").props.disabled).toBe(false); |
| 188 | + }); |
| 189 | +}); |
0 commit comments