File size: 4,958 Bytes
b59aa07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, fireEvent } from "@testing-library/react";
import { RepositorySelectionForm } from "../src/components/features/home/repo-selection-form";
import { useUserRepositories } from "../src/hooks/query/use-user-repositories";
import { useRepositoryBranches } from "../src/hooks/query/use-repository-branches";
import { useCreateConversation } from "../src/hooks/mutation/use-create-conversation";
import { useIsCreatingConversation } from "../src/hooks/use-is-creating-conversation";

// Mock the hooks
vi.mock("../src/hooks/query/use-user-repositories");
vi.mock("../src/hooks/query/use-repository-branches");
vi.mock("../src/hooks/mutation/use-create-conversation");
vi.mock("../src/hooks/use-is-creating-conversation");
vi.mock("react-i18next", () => ({
  useTranslation: () => ({
    t: (key: string) => key,
  }),
}));

describe("RepositorySelectionForm", () => {
  const mockOnRepoSelection = vi.fn();
  
  beforeEach(() => {
    vi.resetAllMocks();
    
    // Mock the hooks with default values
    (useUserRepositories as any).mockReturnValue({
      data: [
        { id: "1", full_name: "test/repo1" },
        { id: "2", full_name: "test/repo2" }
      ],
      isLoading: false,
      isError: false,
    });
    
    (useRepositoryBranches as any).mockReturnValue({
      data: [
        { name: "main" },
        { name: "develop" }
      ],
      isLoading: false,
      isError: false,
    });
    
    (useCreateConversation as any).mockReturnValue({
      mutate: vi.fn(),
      isPending: false,
      isSuccess: false,
    });
    
    (useIsCreatingConversation as any).mockReturnValue(false);
  });
  
  it("should clear selected branch when input is empty", async () => {
    render(<RepositorySelectionForm onRepoSelection={mockOnRepoSelection} />);
    
    // First select a repository to enable the branch dropdown
    const repoDropdown = screen.getByTestId("repository-dropdown");
    fireEvent.change(repoDropdown, { target: { value: "test/repo1" } });
    
    // Get the branch dropdown and verify it's enabled
    const branchDropdown = screen.getByTestId("branch-dropdown");
    expect(branchDropdown).not.toBeDisabled();
    
    // Simulate deleting all text in the branch input
    fireEvent.change(branchDropdown, { target: { value: "" } });
    
    // Verify the branch input is cleared (no selected branch)
    expect(branchDropdown).toHaveValue("");
  });
  
  it("should clear selected branch when input contains only whitespace", async () => {
    render(<RepositorySelectionForm onRepoSelection={mockOnRepoSelection} />);
    
    // First select a repository to enable the branch dropdown
    const repoDropdown = screen.getByTestId("repository-dropdown");
    fireEvent.change(repoDropdown, { target: { value: "test/repo1" } });
    
    // Get the branch dropdown and verify it's enabled
    const branchDropdown = screen.getByTestId("branch-dropdown");
    expect(branchDropdown).not.toBeDisabled();
    
    // Simulate entering only whitespace in the branch input
    fireEvent.change(branchDropdown, { target: { value: "   " } });
    
    // Verify the branch input is cleared (no selected branch)
    expect(branchDropdown).toHaveValue("");
  });

  it("should keep branch empty after being cleared even with auto-selection", async () => {
    render(<RepositorySelectionForm onRepoSelection={mockOnRepoSelection} />);
    
    // First select a repository to enable the branch dropdown
    const repoDropdown = screen.getByTestId("repository-dropdown");
    fireEvent.change(repoDropdown, { target: { value: "test/repo1" } });
    
    // Get the branch dropdown and verify it's enabled
    const branchDropdown = screen.getByTestId("branch-dropdown");
    expect(branchDropdown).not.toBeDisabled();
    
    // The branch should be auto-selected to "main" initially
    expect(branchDropdown).toHaveValue("main");
    
    // Simulate deleting all text in the branch input
    fireEvent.change(branchDropdown, { target: { value: "" } });
    
    // Verify the branch input is cleared (no selected branch)
    expect(branchDropdown).toHaveValue("");
    
    // Trigger a re-render by changing something else
    fireEvent.change(repoDropdown, { target: { value: "test/repo2" } });
    fireEvent.change(repoDropdown, { target: { value: "test/repo1" } });
    
    // The branch should be auto-selected to "main" again after repo change
    expect(branchDropdown).toHaveValue("main");
    
    // Clear it again
    fireEvent.change(branchDropdown, { target: { value: "" } });
    
    // Verify it stays empty
    expect(branchDropdown).toHaveValue("");
    
    // Simulate a component update without changing repos
    // This would normally trigger the useEffect if our fix wasn't working
    fireEvent.blur(branchDropdown);
    
    // Verify it still stays empty
    expect(branchDropdown).toHaveValue("");
  });
});