|
| 1 | +import { render, screen, fireEvent } from "@/utils/test-utils" |
| 2 | + |
| 3 | +import type { Command } from "@roo/ExtensionMessage" |
| 4 | + |
| 5 | +import { SlashCommandItemSimple } from "../SlashCommandItemSimple" |
| 6 | + |
| 7 | +describe("SlashCommandItemSimple", () => { |
| 8 | + const mockCommand: Command = { |
| 9 | + name: "test-command", |
| 10 | + description: "Test command description", |
| 11 | + source: "global", |
| 12 | + filePath: "/path/to/command.md", |
| 13 | + } |
| 14 | + |
| 15 | + const mockOnClick = vi.fn() |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + vi.clearAllMocks() |
| 19 | + }) |
| 20 | + |
| 21 | + it("renders command name with slash prefix", () => { |
| 22 | + render(<SlashCommandItemSimple command={mockCommand} onClick={mockOnClick} />) |
| 23 | + |
| 24 | + expect(screen.getByText("/test-command")).toBeInTheDocument() |
| 25 | + }) |
| 26 | + |
| 27 | + it("renders command description when provided", () => { |
| 28 | + render(<SlashCommandItemSimple command={mockCommand} onClick={mockOnClick} />) |
| 29 | + |
| 30 | + expect(screen.getByText("Test command description")).toBeInTheDocument() |
| 31 | + }) |
| 32 | + |
| 33 | + it("does not render description when not provided", () => { |
| 34 | + const commandWithoutDescription: Command = { |
| 35 | + ...mockCommand, |
| 36 | + description: undefined, |
| 37 | + } |
| 38 | + |
| 39 | + render(<SlashCommandItemSimple command={commandWithoutDescription} onClick={mockOnClick} />) |
| 40 | + |
| 41 | + expect(screen.queryByText("Test command description")).not.toBeInTheDocument() |
| 42 | + }) |
| 43 | + |
| 44 | + it("calls onClick handler when clicked", () => { |
| 45 | + render(<SlashCommandItemSimple command={mockCommand} onClick={mockOnClick} />) |
| 46 | + |
| 47 | + // The outer div is the clickable element |
| 48 | + const commandElement = screen.getByText("/test-command").closest("div.px-4") |
| 49 | + expect(commandElement).toBeInTheDocument() |
| 50 | + |
| 51 | + fireEvent.click(commandElement!) |
| 52 | + |
| 53 | + expect(mockOnClick).toHaveBeenCalledTimes(1) |
| 54 | + expect(mockOnClick).toHaveBeenCalledWith(mockCommand) |
| 55 | + }) |
| 56 | + |
| 57 | + it("does not throw error when onClick is not provided", () => { |
| 58 | + render(<SlashCommandItemSimple command={mockCommand} />) |
| 59 | + |
| 60 | + const commandElement = screen.getByText("/test-command").closest("div.px-4") |
| 61 | + expect(commandElement).toBeInTheDocument() |
| 62 | + |
| 63 | + // Should not throw error |
| 64 | + expect(() => fireEvent.click(commandElement!)).not.toThrow() |
| 65 | + }) |
| 66 | + |
| 67 | + it("applies hover styles", () => { |
| 68 | + render(<SlashCommandItemSimple command={mockCommand} onClick={mockOnClick} />) |
| 69 | + |
| 70 | + // The outer div has the hover styles |
| 71 | + const commandElement = screen.getByText("/test-command").closest("div.px-4") |
| 72 | + expect(commandElement).toHaveClass("hover:bg-vscode-list-hoverBackground") |
| 73 | + }) |
| 74 | + |
| 75 | + it("applies cursor pointer style", () => { |
| 76 | + render(<SlashCommandItemSimple command={mockCommand} onClick={mockOnClick} />) |
| 77 | + |
| 78 | + const commandElement = screen.getByText("/test-command").closest("div.px-4") |
| 79 | + expect(commandElement).toHaveClass("cursor-pointer") |
| 80 | + }) |
| 81 | + |
| 82 | + it("renders with correct layout classes", () => { |
| 83 | + render(<SlashCommandItemSimple command={mockCommand} onClick={mockOnClick} />) |
| 84 | + |
| 85 | + const commandElement = screen.getByText("/test-command").closest("div.px-4") |
| 86 | + expect(commandElement).toHaveClass("px-4", "py-2", "text-sm", "flex", "items-center") |
| 87 | + }) |
| 88 | + |
| 89 | + it("renders command name with correct text color", () => { |
| 90 | + render(<SlashCommandItemSimple command={mockCommand} onClick={mockOnClick} />) |
| 91 | + |
| 92 | + const nameElement = screen.getByText("/test-command") |
| 93 | + expect(nameElement).toHaveClass("text-vscode-foreground") |
| 94 | + }) |
| 95 | + |
| 96 | + it("renders description with correct text styling", () => { |
| 97 | + render(<SlashCommandItemSimple command={mockCommand} onClick={mockOnClick} />) |
| 98 | + |
| 99 | + const descriptionElement = screen.getByText("Test command description") |
| 100 | + expect(descriptionElement).toHaveClass("text-xs", "text-vscode-descriptionForeground", "truncate", "mt-0.5") |
| 101 | + }) |
| 102 | + |
| 103 | + it("handles built-in commands correctly", () => { |
| 104 | + const builtInCommand: Command = { |
| 105 | + ...mockCommand, |
| 106 | + source: "built-in", |
| 107 | + } |
| 108 | + |
| 109 | + render(<SlashCommandItemSimple command={builtInCommand} onClick={mockOnClick} />) |
| 110 | + |
| 111 | + expect(screen.getByText("/test-command")).toBeInTheDocument() |
| 112 | + expect(screen.getByText("Test command description")).toBeInTheDocument() |
| 113 | + |
| 114 | + // Should still be clickable |
| 115 | + const commandElement = screen.getByText("/test-command").closest("div.px-4") |
| 116 | + fireEvent.click(commandElement!) |
| 117 | + expect(mockOnClick).toHaveBeenCalledWith(builtInCommand) |
| 118 | + }) |
| 119 | + |
| 120 | + it("handles project commands correctly", () => { |
| 121 | + const projectCommand: Command = { |
| 122 | + ...mockCommand, |
| 123 | + source: "project", |
| 124 | + } |
| 125 | + |
| 126 | + render(<SlashCommandItemSimple command={projectCommand} onClick={mockOnClick} />) |
| 127 | + |
| 128 | + expect(screen.getByText("/test-command")).toBeInTheDocument() |
| 129 | + expect(screen.getByText("Test command description")).toBeInTheDocument() |
| 130 | + |
| 131 | + // Should still be clickable |
| 132 | + const commandElement = screen.getByText("/test-command").closest("div.px-4") |
| 133 | + fireEvent.click(commandElement!) |
| 134 | + expect(mockOnClick).toHaveBeenCalledWith(projectCommand) |
| 135 | + }) |
| 136 | + |
| 137 | + it("truncates long command names", () => { |
| 138 | + const longNameCommand: Command = { |
| 139 | + ...mockCommand, |
| 140 | + name: "this-is-a-very-long-command-name-that-should-be-truncated-in-the-ui", |
| 141 | + } |
| 142 | + |
| 143 | + render(<SlashCommandItemSimple command={longNameCommand} onClick={mockOnClick} />) |
| 144 | + |
| 145 | + const nameElement = screen.getByText("/this-is-a-very-long-command-name-that-should-be-truncated-in-the-ui") |
| 146 | + expect(nameElement).toHaveClass("truncate") |
| 147 | + }) |
| 148 | + |
| 149 | + it("truncates long descriptions", () => { |
| 150 | + const longDescriptionCommand: Command = { |
| 151 | + ...mockCommand, |
| 152 | + description: |
| 153 | + "This is a very long description that should be truncated in the UI to prevent overflow and maintain a clean layout", |
| 154 | + } |
| 155 | + |
| 156 | + render(<SlashCommandItemSimple command={longDescriptionCommand} onClick={mockOnClick} />) |
| 157 | + |
| 158 | + const descriptionElement = screen.getByText( |
| 159 | + "This is a very long description that should be truncated in the UI to prevent overflow and maintain a clean layout", |
| 160 | + ) |
| 161 | + expect(descriptionElement).toHaveClass("truncate") |
| 162 | + }) |
| 163 | +}) |
0 commit comments