|
| 1 | +import { render, screen, fireEvent, act } from "@testing-library/react"; |
| 2 | +import "@testing-library/jest-dom"; |
| 3 | +import { describe, it, beforeEach, jest } from "@jest/globals"; |
| 4 | +import ListPane from "../ListPane"; |
| 5 | + |
| 6 | +describe("ListPane", () => { |
| 7 | + const mockItems = [ |
| 8 | + { id: 1, name: "Tool 1", description: "First tool" }, |
| 9 | + { id: 2, name: "Tool 2", description: "Second tool" }, |
| 10 | + { id: 3, name: "Another Tool", description: "Third tool" }, |
| 11 | + ]; |
| 12 | + |
| 13 | + const defaultProps = { |
| 14 | + items: mockItems, |
| 15 | + listItems: jest.fn(), |
| 16 | + clearItems: jest.fn(), |
| 17 | + setSelectedItem: jest.fn(), |
| 18 | + renderItem: (item: (typeof mockItems)[0]) => <div>{item.name}</div>, |
| 19 | + title: "List tools", |
| 20 | + buttonText: "Load Tools", |
| 21 | + }; |
| 22 | + |
| 23 | + const renderListPane = (props = {}) => { |
| 24 | + return render(<ListPane {...defaultProps} {...props} />); |
| 25 | + }; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + jest.clearAllMocks(); |
| 29 | + }); |
| 30 | + |
| 31 | + describe("Rendering", () => { |
| 32 | + it("should render with title and button", () => { |
| 33 | + renderListPane(); |
| 34 | + |
| 35 | + expect(screen.getByText("List tools")).toBeInTheDocument(); |
| 36 | + expect( |
| 37 | + screen.getByRole("button", { name: "Load Tools" }), |
| 38 | + ).toBeInTheDocument(); |
| 39 | + expect(screen.getByRole("button", { name: "Clear" })).toBeInTheDocument(); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should render items when provided", () => { |
| 43 | + renderListPane(); |
| 44 | + |
| 45 | + expect(screen.getByText("Tool 1")).toBeInTheDocument(); |
| 46 | + expect(screen.getByText("Tool 2")).toBeInTheDocument(); |
| 47 | + expect(screen.getByText("Another Tool")).toBeInTheDocument(); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should render empty state when no items", () => { |
| 51 | + renderListPane({ items: [] }); |
| 52 | + |
| 53 | + expect(screen.queryByText("Tool 1")).not.toBeInTheDocument(); |
| 54 | + expect(screen.queryByText("Tool 2")).not.toBeInTheDocument(); |
| 55 | + }); |
| 56 | + |
| 57 | + it("should render custom item content", () => { |
| 58 | + const customRenderItem = (item: (typeof mockItems)[0]) => ( |
| 59 | + <div> |
| 60 | + <span>{item.name}</span> |
| 61 | + <small>{item.description}</small> |
| 62 | + </div> |
| 63 | + ); |
| 64 | + |
| 65 | + renderListPane({ renderItem: customRenderItem }); |
| 66 | + |
| 67 | + expect(screen.getByText("Tool 1")).toBeInTheDocument(); |
| 68 | + expect(screen.getByText("First tool")).toBeInTheDocument(); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe("Search Functionality", () => { |
| 73 | + it("should show search icon initially", () => { |
| 74 | + renderListPane(); |
| 75 | + |
| 76 | + const searchButton = screen.getByRole("button", { name: "Search" }); |
| 77 | + expect(searchButton).toBeInTheDocument(); |
| 78 | + expect(searchButton.querySelector("svg")).toBeInTheDocument(); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should expand search input when search icon is clicked", async () => { |
| 82 | + renderListPane(); |
| 83 | + |
| 84 | + const searchButton = screen.getByRole("button", { name: "Search" }); |
| 85 | + await act(async () => { |
| 86 | + fireEvent.click(searchButton); |
| 87 | + }); |
| 88 | + |
| 89 | + const searchInput = screen.getByPlaceholderText("Search..."); |
| 90 | + expect(searchInput).toBeInTheDocument(); |
| 91 | + |
| 92 | + // Wait for the setTimeout to complete and focus to be set |
| 93 | + await act(async () => { |
| 94 | + await new Promise((resolve) => setTimeout(resolve, 150)); |
| 95 | + }); |
| 96 | + |
| 97 | + expect(searchInput).toHaveFocus(); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should filter items based on search query", async () => { |
| 101 | + renderListPane(); |
| 102 | + |
| 103 | + const searchButton = screen.getByRole("button", { name: "Search" }); |
| 104 | + await act(async () => { |
| 105 | + fireEvent.click(searchButton); |
| 106 | + }); |
| 107 | + |
| 108 | + const searchInput = screen.getByPlaceholderText("Search..."); |
| 109 | + await act(async () => { |
| 110 | + fireEvent.change(searchInput, { target: { value: "Tool" } }); |
| 111 | + }); |
| 112 | + |
| 113 | + expect(screen.getByText("Tool 1")).toBeInTheDocument(); |
| 114 | + expect(screen.getByText("Tool 2")).toBeInTheDocument(); |
| 115 | + expect(screen.getByText("Another Tool")).toBeInTheDocument(); |
| 116 | + |
| 117 | + await act(async () => { |
| 118 | + fireEvent.change(searchInput, { target: { value: "Another" } }); |
| 119 | + }); |
| 120 | + |
| 121 | + expect(screen.queryByText("Tool 1")).not.toBeInTheDocument(); |
| 122 | + expect(screen.queryByText("Tool 2")).not.toBeInTheDocument(); |
| 123 | + expect(screen.getByText("Another Tool")).toBeInTheDocument(); |
| 124 | + }); |
| 125 | + |
| 126 | + it("should show 'No items found of matching \"NonExistent\"' when search has no results", async () => { |
| 127 | + renderListPane(); |
| 128 | + |
| 129 | + const searchButton = screen.getByRole("button", { name: "Search" }); |
| 130 | + await act(async () => { |
| 131 | + fireEvent.click(searchButton); |
| 132 | + }); |
| 133 | + |
| 134 | + const searchInput = screen.getByPlaceholderText("Search..."); |
| 135 | + |
| 136 | + await act(async () => { |
| 137 | + fireEvent.change(searchInput, { target: { value: "NonExistent" } }); |
| 138 | + }); |
| 139 | + |
| 140 | + expect( |
| 141 | + screen.getByText('No items found matching "NonExistent"'), |
| 142 | + ).toBeInTheDocument(); |
| 143 | + expect(screen.queryByText("Tool 1")).not.toBeInTheDocument(); |
| 144 | + }); |
| 145 | + |
| 146 | + it("should collapse search when input is empty and loses focus", async () => { |
| 147 | + renderListPane(); |
| 148 | + |
| 149 | + const searchButton = screen.getByRole("button", { name: "Search" }); |
| 150 | + await act(async () => { |
| 151 | + fireEvent.click(searchButton); |
| 152 | + }); |
| 153 | + |
| 154 | + const searchInput = screen.getByPlaceholderText("Search..."); |
| 155 | + |
| 156 | + await act(async () => { |
| 157 | + fireEvent.change(searchInput, { target: { value: "test" } }); |
| 158 | + fireEvent.change(searchInput, { target: { value: "" } }); |
| 159 | + fireEvent.blur(searchInput); |
| 160 | + }); |
| 161 | + |
| 162 | + const searchButtonAfterCollapse = screen.getByRole("button", { |
| 163 | + name: "Search", |
| 164 | + }); |
| 165 | + expect(searchButtonAfterCollapse).toBeInTheDocument(); |
| 166 | + expect(searchButtonAfterCollapse).not.toHaveClass("opacity-0"); |
| 167 | + }); |
| 168 | + |
| 169 | + it("should keep search expanded when input has content and loses focus", async () => { |
| 170 | + renderListPane(); |
| 171 | + |
| 172 | + const searchButton = screen.getByRole("button", { name: "Search" }); |
| 173 | + await act(async () => { |
| 174 | + fireEvent.click(searchButton); |
| 175 | + }); |
| 176 | + |
| 177 | + const searchInput = screen.getByPlaceholderText("Search..."); |
| 178 | + await act(async () => { |
| 179 | + fireEvent.change(searchInput, { target: { value: "test" } }); |
| 180 | + fireEvent.blur(searchInput); |
| 181 | + }); |
| 182 | + |
| 183 | + expect(screen.getByPlaceholderText("Search...")).toBeInTheDocument(); |
| 184 | + }); |
| 185 | + |
| 186 | + it("should search through all item properties (description)", async () => { |
| 187 | + renderListPane(); |
| 188 | + |
| 189 | + const searchButton = screen.getByRole("button", { name: "Search" }); |
| 190 | + await act(async () => { |
| 191 | + fireEvent.click(searchButton); |
| 192 | + }); |
| 193 | + |
| 194 | + const searchInput = screen.getByPlaceholderText("Search..."); |
| 195 | + await act(async () => { |
| 196 | + fireEvent.change(searchInput, { target: { value: "First tool" } }); |
| 197 | + }); |
| 198 | + |
| 199 | + expect(screen.getByText("Tool 1")).toBeInTheDocument(); |
| 200 | + expect(screen.queryByText("Tool 2")).not.toBeInTheDocument(); |
| 201 | + }); |
| 202 | + }); |
| 203 | +}); |
0 commit comments