Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 50 additions & 11 deletions apps/mobile/src/features/threads/git/GitBranchesSheet.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@ const state = vi.hoisted(() => ({
inputs: new Map<string, InputProps>(),
actions: new Map<string, ActionProps>(),
worktrees: [] as Array<{ baseBranch: string; newBranch: string }>,
focused: true,
goBackCalls: 0,
gitResult: Promise.resolve<object | null>(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,
Expand All @@ -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 },
Expand All @@ -49,17 +64,19 @@ 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,
}),
}));
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;
},
}),
}));
Expand All @@ -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(() => {
Expand All @@ -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());
},
Expand All @@ -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" },
]);
Expand All @@ -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<object>();
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);
},
);
6 changes: 3 additions & 3 deletions apps/mobile/src/features/threads/git/GitBranchesSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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();
});
}}
Expand Down
Loading