Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

GitConfirmSheet.test.tsx

124 lines4.2 KB

1import { act, type ReactNode } from "react";
2import { createRoot } from "react-dom/client";
3import { expect, it, vi } from "vite-plus/test";
4​
5type ActionProps = { label: string; disabled?: boolean; onPress: () => void };
6type IntroProps = { title: string; description: string };
7const state = vi.hoisted(() => ({
8 refName: null as string | null,
9 intro: null as IntroProps | null,
10 actions: [] as ActionProps[],
11 ran: [] as object[],
12}));
13​
14vi.mock("react-native", () => ({
15 View: ({ children }: { children: ReactNode }) => children,
16}));
17vi.mock("@react-navigation/native", () => ({
18 useNavigation: () => ({ goBack: () => {} }),
19}));
20vi.mock("../../../components/ui/SheetHeader", () => ({ SheetHeader: () => null }));
21vi.mock("../../../components/ui/FitSheet", () => ({
22 FitSheet: ({ children }: { children: ReactNode }) => children,
23}));
24vi.mock("../../../components/ui/SheetIntro", () => ({
25 SheetIntro: (props: IntroProps) => {
26 state.intro = props;
27 return null;
28 },
29}));
30vi.mock("../../../state/query", () => ({
31 useEnvironmentQuery: () => ({
32 data: state.refName === null ? undefined : { refName: state.refName },
33 }),
34}));
35vi.mock("../../../state/use-thread-selection", () => ({
36 useThreadSelection: () => ({
37 selectedThread: { environmentId: "environment" },
38 selectedThreadProject: null,
39 }),
40}));
41vi.mock("../../../state/use-selected-thread-worktree", () => ({
42 useSelectedThreadWorktree: () => ({ selectedThreadCwd: "/repo" }),
43}));
44vi.mock("../../../state/use-selected-thread-git-state", () => ({
45 useSelectedThreadGitState: () => ({ selectedThreadBranches: [] }),
46}));
47vi.mock("../../../state/use-selected-thread-git-actions", () => ({
48 useSelectedThreadGitActions: () => ({
49 canWriteSourceControl: true,
50 canCreateFeatureBranch: true,
51 onRunSelectedThreadGitAction: (input: object) => {
52 state.ran.push(input);
53 return Promise.resolve(null);
54 },
55 }),
56}));
57vi.mock("../../../state/vcs", () => ({ vcsEnvironment: { status: () => null } }));
58vi.mock("../thread-inspector-navigation", () => ({ useReturnToThread: () => () => {} }));
59vi.mock("./gitSheetComponents", () => ({
60 SheetActionButton: (props: ActionProps) => {
61 state.actions.push(props);
62 return null;
63 },
64}));
65​
66import { GitConfirmSheet } from "./GitConfirmSheet";
67​
68function setup(refName: string | null, params: Record<string, string>) {
69 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
70 state.refName = refName;
71 state.intro = null;
72 state.actions = [];
73 state.ran = [];
74 const root = createRoot(document.createElement("div"));
75 act(() => {
76 root.render(
77 <GitConfirmSheet
78 route={{ params: { environmentId: "environment", threadId: "thread", ...params } }}
79 />,
80 );
81 });
82 // Each render registers Continue first, then the feature branch button.
83 const [continueButton, featureBranchButton] = state.actions.slice(-2);
84 return {
85 continueButton: continueButton!,
86 featureBranchButton: featureBranchButton!,
87 [Symbol.dispose]() {
88 act(() => root.unmount());
89 },
90 };
92​
93it("describes the action it runs on the branch that is checked out", async () => {
94 using screen = setup("main", {
95 confirmAction: "commit_push",
96 branchName: "docs",
97 includesCommit: "false",
98 commitMessage: "from the route",
99 filePaths: "a.ts,b.ts",
100 });
101 expect(state.intro).toMatchObject({
102 title: "Commit & push to default branch?",
103 description: expect.stringContaining('commit and push changes on "main"'),
104 });
105 expect(screen.continueButton.label).toBe("Commit & push to main");
106 await act(async () => screen.continueButton.onPress());
107 await act(async () => screen.featureBranchButton.onPress());
108 expect(state.ran).toEqual([
109 { action: "commit_push" },
110 { action: "commit_push", featureBranch: true },
111 ]);
112});
113​
114it.each([
115 ["an action it does not confirm", "main", "commit"],
116 ["no checked out branch", null, "push"],
117])("runs nothing for %s", async (_case, refName, confirmAction) => {
118 using screen = setup(refName, { confirmAction });
119 expect(screen.continueButton.disabled).toBe(true);
120 expect(screen.featureBranchButton.disabled).toBe(true);
121 await act(async () => screen.continueButton.onPress());
122 await act(async () => screen.featureBranchButton.onPress());
123 expect(state.ran).toEqual([]);
124});