|
| 1 | +import { render, screen, waitFor } from "@testing-library/react"; |
| 2 | +import userEvent from "@testing-library/user-event"; |
| 3 | + |
| 4 | +import { IconPicker } from "./IconPicker"; |
| 5 | + |
| 6 | +describe("IconPicker", () => { |
| 7 | + it("should render Browse button", () => { |
| 8 | + render(<IconPicker onSelect={vi.fn()} />); |
| 9 | + expect(screen.getByRole("button", { name: /browse/i })).toBeInTheDocument(); |
| 10 | + }); |
| 11 | + |
| 12 | + it("should open popover with search input on click", async () => { |
| 13 | + const user = userEvent.setup(); |
| 14 | + render(<IconPicker onSelect={vi.fn()} />); |
| 15 | + |
| 16 | + await user.click(screen.getByRole("button", { name: /browse/i })); |
| 17 | + |
| 18 | + expect(screen.getByPlaceholderText("Search icons...")).toBeInTheDocument(); |
| 19 | + }); |
| 20 | + |
| 21 | + it("should show icons in the grid", async () => { |
| 22 | + const user = userEvent.setup(); |
| 23 | + render(<IconPicker onSelect={vi.fn()} />); |
| 24 | + |
| 25 | + await user.click(screen.getByRole("button", { name: /browse/i })); |
| 26 | + |
| 27 | + // Wait for at least some icon buttons to appear in the grid |
| 28 | + await waitFor(() => { |
| 29 | + const iconButtons = screen |
| 30 | + .getAllByRole("button") |
| 31 | + .filter(btn => btn.title && btn.title !== ""); |
| 32 | + expect(iconButtons.length).toBeGreaterThan(0); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should filter icons when searching", async () => { |
| 37 | + const user = userEvent.setup(); |
| 38 | + render(<IconPicker onSelect={vi.fn()} />); |
| 39 | + |
| 40 | + await user.click(screen.getByRole("button", { name: /browse/i })); |
| 41 | + const searchInput = screen.getByPlaceholderText("Search icons..."); |
| 42 | + |
| 43 | + await user.type(searchInput, "user"); |
| 44 | + |
| 45 | + await waitFor(() => { |
| 46 | + const iconButtons = screen |
| 47 | + .getAllByRole("button") |
| 48 | + .filter(btn => btn.title && btn.title.includes("user")); |
| 49 | + expect(iconButtons.length).toBeGreaterThan(0); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should show no results message for invalid search", async () => { |
| 54 | + const user = userEvent.setup(); |
| 55 | + render(<IconPicker onSelect={vi.fn()} />); |
| 56 | + |
| 57 | + await user.click(screen.getByRole("button", { name: /browse/i })); |
| 58 | + const searchInput = screen.getByPlaceholderText("Search icons..."); |
| 59 | + |
| 60 | + await user.type(searchInput, "zzzznotanicon"); |
| 61 | + |
| 62 | + expect(screen.getByText("No icons found")).toBeInTheDocument(); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should call onSelect with data URI when icon is clicked", async () => { |
| 66 | + const user = userEvent.setup(); |
| 67 | + const onSelect = vi.fn(); |
| 68 | + render(<IconPicker onSelect={onSelect} />); |
| 69 | + |
| 70 | + await user.click(screen.getByRole("button", { name: /browse/i })); |
| 71 | + |
| 72 | + // Wait for icons to load then click the first one |
| 73 | + await waitFor(() => { |
| 74 | + const iconButtons = screen |
| 75 | + .getAllByRole("button") |
| 76 | + .filter(btn => btn.title && btn.title !== ""); |
| 77 | + expect(iconButtons.length).toBeGreaterThan(0); |
| 78 | + }); |
| 79 | + |
| 80 | + const firstIcon = screen |
| 81 | + .getAllByRole("button") |
| 82 | + .filter(btn => btn.title && btn.title !== "")[0]; |
| 83 | + await user.click(firstIcon); |
| 84 | + |
| 85 | + await waitFor(() => { |
| 86 | + expect(onSelect).toHaveBeenCalledWith( |
| 87 | + expect.stringMatching(/^data:image\/svg\+xml;base64,/), |
| 88 | + "image/svg+xml", |
| 89 | + ); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it("should close popover after selecting an icon", async () => { |
| 94 | + const user = userEvent.setup(); |
| 95 | + render(<IconPicker onSelect={vi.fn()} />); |
| 96 | + |
| 97 | + await user.click(screen.getByRole("button", { name: /browse/i })); |
| 98 | + |
| 99 | + await waitFor(() => { |
| 100 | + const iconButtons = screen |
| 101 | + .getAllByRole("button") |
| 102 | + .filter(btn => btn.title && btn.title !== ""); |
| 103 | + expect(iconButtons.length).toBeGreaterThan(0); |
| 104 | + }); |
| 105 | + |
| 106 | + const firstIcon = screen |
| 107 | + .getAllByRole("button") |
| 108 | + .filter(btn => btn.title && btn.title !== "")[0]; |
| 109 | + await user.click(firstIcon); |
| 110 | + |
| 111 | + await waitFor(() => { |
| 112 | + expect( |
| 113 | + screen.queryByPlaceholderText("Search icons..."), |
| 114 | + ).not.toBeInTheDocument(); |
| 115 | + }); |
| 116 | + }); |
| 117 | +}); |
0 commit comments