BranchIndicator.test.tsx (2198B)
1 import { describe, it, expect, vi } from "vitest"; 2 import { render, screen, fireEvent } from "@testing-library/react"; 3 import { BranchIndicator } from "./BranchIndicator"; 4 5 const singleBranch = [ 6 { session_id: "fork-1", parent_session_id: "s1", fork_message_index: 3 }, 7 ]; 8 9 const multipleBranches = [ 10 { session_id: "fork-1", parent_session_id: "s1", fork_message_index: 3 }, 11 { session_id: "fork-2", parent_session_id: "s1", fork_message_index: 5 }, 12 ]; 13 14 describe("BranchIndicator", () => { 15 it("renders nothing when no branches", () => { 16 const { container } = render( 17 <BranchIndicator branches={[]} currentSessionId="s1" />, 18 ); 19 expect(container.firstChild).toBeNull(); 20 }); 21 22 it("shows branch count when branches exist", () => { 23 render( 24 <BranchIndicator branches={singleBranch} currentSessionId="s1" />, 25 ); 26 expect(screen.getByText(/1 branch/)).toBeInTheDocument(); 27 }); 28 29 it("shows plural form for multiple branches", () => { 30 render( 31 <BranchIndicator branches={multipleBranches} currentSessionId="s1" />, 32 ); 33 expect(screen.getByText(/2 branches/)).toBeInTheDocument(); 34 }); 35 36 it("opens dropdown on click", () => { 37 render( 38 <BranchIndicator branches={multipleBranches} currentSessionId="s1" />, 39 ); 40 fireEvent.click(screen.getByText(/2 branches/)); 41 expect(screen.getByText("fork-1")).toBeInTheDocument(); 42 expect(screen.getByText("fork-2")).toBeInTheDocument(); 43 }); 44 45 it("calls onSwitchBranch when a branch is selected", () => { 46 const onSwitch = vi.fn(); 47 render( 48 <BranchIndicator 49 branches={multipleBranches} 50 currentSessionId="s1" 51 onSwitchBranch={onSwitch} 52 />, 53 ); 54 fireEvent.click(screen.getByText(/2 branches/)); 55 fireEvent.click(screen.getByText("fork-2")); 56 expect(onSwitch).toHaveBeenCalledWith("fork-2"); 57 }); 58 59 it("highlights the current session in the dropdown", () => { 60 render( 61 <BranchIndicator 62 branches={singleBranch} 63 currentSessionId="fork-1" 64 />, 65 ); 66 fireEvent.click(screen.getByText(/1 branch/)); 67 const item = screen.getByText("fork-1"); 68 expect(item.className).toContain("bg-blue"); 69 }); 70 });