diff --git a/apps/mobile/src/features/threads/git/GitBranchesSheet.test.tsx b/apps/mobile/src/features/threads/git/GitBranchesSheet.test.tsx index fef45b5c88003..084dd7880fba1 100644 --- a/apps/mobile/src/features/threads/git/GitBranchesSheet.test.tsx +++ b/apps/mobile/src/features/threads/git/GitBranchesSheet.test.tsx @@ -14,9 +14,17 @@ const state = vi.hoisted(() => ({ inputs: new Map(), actions: new Map(), worktrees: [] as Array<{ baseBranch: string; newBranch: string }>, + focused: true, + goBackCalls: 0, + gitResult: Promise.resolve(null), })); -vi.mock("@react-navigation/native", () => ({ useNavigation: () => ({ goBack() {} }) })); +vi.mock("@react-navigation/native", () => ({ + useNavigation: () => ({ + goBack: () => state.goBackCalls++, + isFocused: () => state.focused, + }), +})); vi.mock("../../../components/ui/SheetHeader", () => ({ SheetHeader: () => null })); vi.mock("../../../components/ui/FitSheet", () => ({ FitSheet: ({ children }: { children: ReactNode }) => children, @@ -29,8 +37,15 @@ vi.mock("../../../components/ui/FormSection", () => ({ return null; }, })); -vi.mock("../../../components/ui/ListSection", () => ({ ListSection: () => null })); -vi.mock("../../../components/ui/ListChoiceRow", () => ({ ListChoiceRow: () => null })); +vi.mock("../../../components/ui/ListSection", () => ({ + ListSection: ({ children }: { children: ReactNode }) => children, +})); +vi.mock("../../../components/ui/ListChoiceRow", () => ({ + ListChoiceRow: (props: ActionProps) => { + state.actions.set(props.label, props); + return null; + }, +})); vi.mock("../../../state/query", () => ({ useEnvironmentQuery: () => ({ data: state.refName === null ? undefined : { refName: state.refName }, @@ -49,7 +64,7 @@ vi.mock("../../../state/use-selected-thread-worktree", () => ({ })); vi.mock("../../../state/use-selected-thread-git-state", () => ({ useSelectedThreadGitState: () => ({ - selectedThreadBranches: [], + selectedThreadBranches: [{ name: "release/stable", worktreePath: null }], selectedThreadBranchesLoading: false, gitOperationLabel: null, }), @@ -57,9 +72,11 @@ vi.mock("../../../state/use-selected-thread-git-state", () => ({ vi.mock("../../../state/use-selected-thread-git-actions", () => ({ useSelectedThreadGitActions: () => ({ canChangeThreadBranch: true, - onCreateSelectedThreadWorktree: async (input: { baseBranch: string; newBranch: string }) => { + onCreateSelectedThreadBranch: () => state.gitResult, + onCheckoutSelectedThreadBranch: () => state.gitResult, + onCreateSelectedThreadWorktree: (input: { baseBranch: string; newBranch: string }) => { state.worktrees.push(input); - return null; + return state.gitResult; }, }), })); @@ -80,6 +97,9 @@ function setup(branch: string | null = null) { state.inputs.clear(); state.actions.clear(); state.worktrees = []; + state.gitResult = Promise.resolve(null); + state.focused = true; + state.goBackCalls = 0; const root = createRoot(document.createElement("div")); const render = () => act(() => { @@ -92,12 +112,12 @@ function setup(branch: string | null = null) { render(); return { render, + press(label: string) { + act(() => state.actions.get(label)!.onPress()); + }, edit(label: string, value: string) { act(() => state.inputs.get(label)!.onChangeText(value)); }, - create() { - act(() => state.actions.get("Create worktree")!.onPress()); - }, [Symbol.dispose]() { act(() => root.unmount()); }, @@ -111,7 +131,7 @@ it.each([null, "stale-branch"])( screen.edit("Worktree branch name", "feature/new-worktree"); state.refName = "release/stable"; screen.render(); - screen.create(); + screen.press("Create worktree"); expect(state.worktrees).toEqual([ { baseBranch: "release/stable", newBranch: "feature/new-worktree" }, ]); @@ -126,8 +146,27 @@ it.each(["custom-base", ""])("preserves an edited base %j when status arrives", screen.render(); expect(state.inputs.get("Worktree base branch")!.value).toBe(base); expect(state.actions.get("Create worktree")!.disabled).toBe(base === ""); - screen.create(); + screen.press("Create worktree"); expect(state.worktrees).toEqual( base === "" ? [] : [{ baseBranch: base, newBranch: "feature/new-worktree" }], ); }); + +it.each(["Create worktree", "Create & checkout", "release/stable"])( + "does not navigate back when %s finishes after the sheet lost focus", + async (action) => { + using screen = setup(); + const gitResult = Promise.withResolvers(); + state.gitResult = gitResult.promise; + screen.edit("New branch name", "feature/new-branch"); + screen.edit("Worktree branch name", "feature/new-worktree"); + screen.press(action); + state.focused = false; + + await act(async () => { + gitResult.resolve({ refName: "feature/ready" }); + await gitResult.promise; + }); + expect(state.goBackCalls).toBe(0); + }, +); diff --git a/apps/mobile/src/features/threads/git/GitBranchesSheet.tsx b/apps/mobile/src/features/threads/git/GitBranchesSheet.tsx index 7c610a4456675..df1ad6649c8f7 100644 --- a/apps/mobile/src/features/threads/git/GitBranchesSheet.tsx +++ b/apps/mobile/src/features/threads/git/GitBranchesSheet.tsx @@ -83,7 +83,7 @@ export function GitBranchesSheet(_props: GitBranchesSheetProps) { const branch = sanitizeFeatureBranchName(newBranchName.trim()); if (branch.length === 0) return; void gitActions.onCreateSelectedThreadBranch(branch).then((result) => { - if (result === null) return; + if (result === null || !navigation.isFocused()) return; setNewBranchName(""); navigation.goBack(); }); @@ -123,7 +123,7 @@ export function GitBranchesSheet(_props: GitBranchesSheetProps) { void gitActions .onCreateSelectedThreadWorktree({ baseBranch, newBranch }) .then((result) => { - if (result === null) return; + if (result === null || !navigation.isFocused()) return; setWorktreeBranchName(""); navigation.goBack(); }); @@ -161,7 +161,7 @@ export function GitBranchesSheet(_props: GitBranchesSheetProps) { disabled={!canChangeThreadBranch || busy || disabled} onPress={() => { void gitActions.onCheckoutSelectedThreadBranch(branch.name).then((result) => { - if (result === null) return; + if (result === null || !navigation.isFocused()) return; navigation.goBack(); }); }}