|
| 1 | +// @vitest-environment happy-dom |
| 2 | +import { render, screen, waitFor } from "@testing-library/react"; |
| 3 | +import userEvent from "@testing-library/user-event"; |
| 4 | + |
| 5 | +import { IconPicker } from "./IconPicker"; |
| 6 | + |
| 7 | +describe("IconPicker", () => { |
| 8 | + it("should render Browse button", () => { |
| 9 | + render(<IconPicker onSelect={vi.fn()} />); |
| 10 | + expect(screen.getByRole("button", { name: /browse/i })).toBeInTheDocument(); |
| 11 | + }); |
| 12 | + |
| 13 | + it("should open popover with search input on click", async () => { |
| 14 | + const user = userEvent.setup(); |
| 15 | + render(<IconPicker onSelect={vi.fn()} />); |
| 16 | + |
| 17 | + await user.click(screen.getByRole("button", { name: /browse/i })); |
| 18 | + |
| 19 | + expect(screen.getByPlaceholderText("Search icons...")).toBeInTheDocument(); |
| 20 | + }); |
| 21 | + |
| 22 | + it("should show icons in the grid", async () => { |
| 23 | + const user = userEvent.setup(); |
| 24 | + render(<IconPicker onSelect={vi.fn()} />); |
| 25 | + |
| 26 | + await user.click(screen.getByRole("button", { name: /browse/i })); |
| 27 | + |
| 28 | + // Wait for at least some icon buttons to appear in the grid |
| 29 | + await waitFor(() => { |
| 30 | + const iconButtons = screen |
| 31 | + .getAllByRole("button") |
| 32 | + .filter(btn => btn.title && btn.title !== ""); |
| 33 | + expect(iconButtons.length).toBeGreaterThan(0); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should filter icons when searching", async () => { |
| 38 | + const user = userEvent.setup(); |
| 39 | + render(<IconPicker onSelect={vi.fn()} />); |
| 40 | + |
| 41 | + await user.click(screen.getByRole("button", { name: /browse/i })); |
| 42 | + const searchInput = screen.getByPlaceholderText("Search icons..."); |
| 43 | + |
| 44 | + await user.type(searchInput, "user"); |
| 45 | + |
| 46 | + await waitFor(() => { |
| 47 | + const iconButtons = screen |
| 48 | + .getAllByRole("button") |
| 49 | + .filter(btn => btn.title && btn.title.includes("user")); |
| 50 | + expect(iconButtons.length).toBeGreaterThan(0); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should show truncation hint when results are capped", async () => { |
| 55 | + const user = userEvent.setup(); |
| 56 | + render(<IconPicker onSelect={vi.fn()} />); |
| 57 | + |
| 58 | + await user.click(screen.getByRole("button", { name: /browse/i })); |
| 59 | + |
| 60 | + expect(screen.getByText(/Showing 64 of/)).toBeInTheDocument(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should hide truncation hint when fewer results than cap", async () => { |
| 64 | + const user = userEvent.setup(); |
| 65 | + render(<IconPicker onSelect={vi.fn()} />); |
| 66 | + |
| 67 | + await user.click(screen.getByRole("button", { name: /browse/i })); |
| 68 | + const searchInput = screen.getByPlaceholderText("Search icons..."); |
| 69 | + |
| 70 | + await user.type(searchInput, "airplay"); |
| 71 | + |
| 72 | + expect(screen.queryByText(/Showing 64 of/)).not.toBeInTheDocument(); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should show no results message for invalid search", async () => { |
| 76 | + const user = userEvent.setup(); |
| 77 | + render(<IconPicker onSelect={vi.fn()} />); |
| 78 | + |
| 79 | + await user.click(screen.getByRole("button", { name: /browse/i })); |
| 80 | + const searchInput = screen.getByPlaceholderText("Search icons..."); |
| 81 | + |
| 82 | + await user.type(searchInput, "zzzznotanicon"); |
| 83 | + |
| 84 | + expect(screen.getByText("No icons found")).toBeInTheDocument(); |
| 85 | + }); |
| 86 | + |
| 87 | + it("should call onSelect with lucide:<name> reference when icon is clicked", async () => { |
| 88 | + const user = userEvent.setup(); |
| 89 | + const onSelect = vi.fn(); |
| 90 | + render(<IconPicker onSelect={onSelect} />); |
| 91 | + |
| 92 | + await user.click(screen.getByRole("button", { name: /browse/i })); |
| 93 | + |
| 94 | + await waitFor(() => { |
| 95 | + const iconButtons = screen |
| 96 | + .getAllByRole("button") |
| 97 | + .filter(btn => btn.title && btn.title !== ""); |
| 98 | + expect(iconButtons.length).toBeGreaterThan(0); |
| 99 | + }); |
| 100 | + |
| 101 | + const firstIcon = screen |
| 102 | + .getAllByRole("button") |
| 103 | + .filter(btn => btn.title && btn.title !== "")[0]; |
| 104 | + const iconName = firstIcon.getAttribute("title"); |
| 105 | + await user.click(firstIcon); |
| 106 | + |
| 107 | + expect(onSelect).toHaveBeenCalledWith( |
| 108 | + `lucide:${iconName}`, |
| 109 | + "image/svg+xml", |
| 110 | + ); |
| 111 | + }); |
| 112 | + |
| 113 | + it("should highlight the icon matching currentIconUrl", async () => { |
| 114 | + const user = userEvent.setup(); |
| 115 | + render(<IconPicker currentIconUrl="lucide:airplay" onSelect={vi.fn()} />); |
| 116 | + |
| 117 | + await user.click(screen.getByRole("button", { name: /browse/i })); |
| 118 | + |
| 119 | + await waitFor(() => { |
| 120 | + const airplayBtn = screen |
| 121 | + .getAllByRole("button") |
| 122 | + .find(btn => btn.title === "airplay"); |
| 123 | + expect(airplayBtn).toBeDefined(); |
| 124 | + expect(airplayBtn).toHaveAttribute("aria-pressed", "true"); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + it("should not highlight any icon when currentIconUrl is not a lucide ref", async () => { |
| 129 | + const user = userEvent.setup(); |
| 130 | + render( |
| 131 | + <IconPicker |
| 132 | + currentIconUrl="data:image/svg+xml;base64,XXXX" |
| 133 | + onSelect={vi.fn()} |
| 134 | + />, |
| 135 | + ); |
| 136 | + |
| 137 | + await user.click(screen.getByRole("button", { name: /browse/i })); |
| 138 | + |
| 139 | + await waitFor(() => { |
| 140 | + const iconButtons = screen |
| 141 | + .getAllByRole("button") |
| 142 | + .filter(btn => btn.title && btn.title !== ""); |
| 143 | + expect(iconButtons.length).toBeGreaterThan(0); |
| 144 | + for (const btn of iconButtons) { |
| 145 | + expect(btn).toHaveAttribute("aria-pressed", "false"); |
| 146 | + } |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + it("should close popover after selecting an icon", async () => { |
| 151 | + const user = userEvent.setup(); |
| 152 | + render(<IconPicker onSelect={vi.fn()} />); |
| 153 | + |
| 154 | + await user.click(screen.getByRole("button", { name: /browse/i })); |
| 155 | + |
| 156 | + await waitFor(() => { |
| 157 | + const iconButtons = screen |
| 158 | + .getAllByRole("button") |
| 159 | + .filter(btn => btn.title && btn.title !== ""); |
| 160 | + expect(iconButtons.length).toBeGreaterThan(0); |
| 161 | + }); |
| 162 | + |
| 163 | + const firstIcon = screen |
| 164 | + .getAllByRole("button") |
| 165 | + .filter(btn => btn.title && btn.title !== "")[0]; |
| 166 | + await user.click(firstIcon); |
| 167 | + |
| 168 | + await waitFor(() => { |
| 169 | + expect( |
| 170 | + screen.queryByPlaceholderText("Search icons..."), |
| 171 | + ).not.toBeInTheDocument(); |
| 172 | + }); |
| 173 | + }); |
| 174 | +}); |
0 commit comments