|
| 1 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 2 | +import { render, screen, waitFor, within } from "@testing-library/react"; |
| 3 | +import userEvent from "@testing-library/user-event"; |
| 4 | +import { HttpResponse, http } from "msw"; |
| 5 | +import { setupServer } from "msw/node"; |
| 6 | +import { |
| 7 | + afterAll, |
| 8 | + afterEach, |
| 9 | + beforeAll, |
| 10 | + describe, |
| 11 | + expect, |
| 12 | + it, |
| 13 | + vi, |
| 14 | +} from "vitest"; |
| 15 | +import { FlowRunTagsSelect } from "./flow-run-tags-select"; |
| 16 | + |
| 17 | +// MSW server to mock API endpoints |
| 18 | +const server = setupServer( |
| 19 | + http.post("/api/flow_runs/paginate", () => { |
| 20 | + return HttpResponse.json({ |
| 21 | + results: [ |
| 22 | + { id: "1", tags: ["alpha", "prod"] }, |
| 23 | + { id: "2", tags: ["beta", "prod"] }, |
| 24 | + { id: "3", tags: [] }, |
| 25 | + ], |
| 26 | + pages: 1, |
| 27 | + next: null, |
| 28 | + }); |
| 29 | + }), |
| 30 | +); |
| 31 | + |
| 32 | +beforeAll(() => { |
| 33 | + // Ensure the client builds requests against the MSW base URL |
| 34 | + vi.stubEnv("VITE_API_URL", "/api"); |
| 35 | + // Polyfill scrollIntoView used by cmdk |
| 36 | + Object.defineProperty(HTMLElement.prototype, "scrollIntoView", { |
| 37 | + value: vi.fn(), |
| 38 | + configurable: true, |
| 39 | + writable: true, |
| 40 | + }); |
| 41 | + server.listen(); |
| 42 | +}); |
| 43 | +afterEach(() => server.resetHandlers()); |
| 44 | +afterAll(() => server.close()); |
| 45 | + |
| 46 | +function renderWithQueryClient(ui: React.ReactElement) { |
| 47 | + const queryClient = new QueryClient({ |
| 48 | + defaultOptions: { queries: { retry: false }, mutations: { retry: false } }, |
| 49 | + }); |
| 50 | + return render( |
| 51 | + <QueryClientProvider client={queryClient}>{ui}</QueryClientProvider>, |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +describe("FlowRunTagsSelect", () => { |
| 56 | + it("shows placeholder when no tags selected", () => { |
| 57 | + renderWithQueryClient( |
| 58 | + <FlowRunTagsSelect |
| 59 | + value={[]} |
| 60 | + onChange={vi.fn()} |
| 61 | + placeholder="All tags" |
| 62 | + />, |
| 63 | + ); |
| 64 | + // The trigger shows the placeholder text |
| 65 | + expect(screen.getByText("All tags")).toBeInTheDocument(); |
| 66 | + }); |
| 67 | + |
| 68 | + it("shows selected tags in the trigger", () => { |
| 69 | + renderWithQueryClient( |
| 70 | + <FlowRunTagsSelect value={["alpha", "beta"]} onChange={vi.fn()} />, |
| 71 | + ); |
| 72 | + // Without opening dropdown, tags are visible in trigger |
| 73 | + expect(screen.getByText("alpha")).toBeInTheDocument(); |
| 74 | + expect(screen.getByText("beta")).toBeInTheDocument(); |
| 75 | + }); |
| 76 | + |
| 77 | + // Suggestion rendering is covered implicitly by other integration tests; core interactions below |
| 78 | + |
| 79 | + it("adds a freeform tag on Enter", async () => { |
| 80 | + const onChange = vi.fn(); |
| 81 | + const user = userEvent.setup(); |
| 82 | + renderWithQueryClient(<FlowRunTagsSelect value={[]} onChange={onChange} />); |
| 83 | + |
| 84 | + const trigger = screen.getByRole("button", { name: /flow run tags/i }); |
| 85 | + await user.click(trigger); |
| 86 | + |
| 87 | + const combo = screen.getByRole("combobox"); |
| 88 | + await user.type(combo, "newtag"); |
| 89 | + await user.keyboard("{Enter}"); |
| 90 | + |
| 91 | + expect(onChange).toHaveBeenCalledWith(["newtag"]); |
| 92 | + }); |
| 93 | + |
| 94 | + it("adds a tag when typing a trailing comma", async () => { |
| 95 | + const onChange = vi.fn(); |
| 96 | + const user = userEvent.setup(); |
| 97 | + renderWithQueryClient(<FlowRunTagsSelect value={[]} onChange={onChange} />); |
| 98 | + |
| 99 | + const trigger = screen.getByRole("button", { name: /flow run tags/i }); |
| 100 | + await user.click(trigger); |
| 101 | + |
| 102 | + const input2 = screen.getByRole("combobox"); |
| 103 | + await user.type(input2, "temp,"); |
| 104 | + |
| 105 | + // Comma commits the tag and clears input; we expect onChange invoked |
| 106 | + await waitFor(() => { |
| 107 | + expect(onChange).toHaveBeenCalledWith(["temp"]); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + it("removes last tag on Backspace when input empty", async () => { |
| 112 | + const onChange = vi.fn(); |
| 113 | + const user = userEvent.setup(); |
| 114 | + // Start with one selected tag |
| 115 | + renderWithQueryClient( |
| 116 | + <FlowRunTagsSelect value={["alpha"]} onChange={onChange} />, |
| 117 | + ); |
| 118 | + |
| 119 | + const trigger = screen.getByRole("button", { name: /flow run tags/i }); |
| 120 | + await user.click(trigger); |
| 121 | + |
| 122 | + await user.keyboard("{Backspace}"); |
| 123 | + |
| 124 | + expect(onChange).toHaveBeenCalledWith([]); |
| 125 | + }); |
| 126 | + |
| 127 | + it("removes a tag via dropdown remove button", async () => { |
| 128 | + const onChange = vi.fn(); |
| 129 | + const user = userEvent.setup(); |
| 130 | + renderWithQueryClient( |
| 131 | + <FlowRunTagsSelect value={["alpha", "beta"]} onChange={onChange} />, |
| 132 | + ); |
| 133 | + |
| 134 | + // Open and remove 'alpha' via dropdown |
| 135 | + const trigger = screen.getByRole("button", { name: /flow run tags/i }); |
| 136 | + await user.click(trigger); |
| 137 | + const removeAlpha = await screen.findByRole("button", { |
| 138 | + name: /remove alpha tag/i, |
| 139 | + }); |
| 140 | + await user.click(removeAlpha); |
| 141 | + |
| 142 | + expect(onChange).toHaveBeenCalledWith(["beta"]); |
| 143 | + }); |
| 144 | + |
| 145 | + it("clears all tags via clear action", async () => { |
| 146 | + const onChange = vi.fn(); |
| 147 | + const user = userEvent.setup(); |
| 148 | + renderWithQueryClient( |
| 149 | + <FlowRunTagsSelect value={["alpha", "beta"]} onChange={onChange} />, |
| 150 | + ); |
| 151 | + |
| 152 | + const trigger = screen.getByRole("button", { name: /flow run tags/i }); |
| 153 | + await user.click(trigger); |
| 154 | + |
| 155 | + const listbox = await screen.findByRole("listbox"); |
| 156 | + // Clear all item should be visible regardless of suggestions |
| 157 | + const clearItem = await within(listbox).findByText(/clear all tags/i); |
| 158 | + await user.click(clearItem); |
| 159 | + |
| 160 | + expect(onChange).toHaveBeenCalledWith([]); |
| 161 | + }); |
| 162 | + |
| 163 | + it("does not show 'No tags found' when API returns no tags", async () => { |
| 164 | + // Return empty results |
| 165 | + server.use( |
| 166 | + http.post("/api/flow_runs/paginate", () => |
| 167 | + HttpResponse.json({ results: [], pages: 0, next: null }), |
| 168 | + ), |
| 169 | + ); |
| 170 | + |
| 171 | + const user = userEvent.setup(); |
| 172 | + renderWithQueryClient(<FlowRunTagsSelect value={[]} onChange={vi.fn()} />); |
| 173 | + |
| 174 | + const trigger = screen.getByRole("button", { name: /flow run tags/i }); |
| 175 | + await user.click(trigger); |
| 176 | + |
| 177 | + // There is a listbox but no empty message |
| 178 | + const listbox = await screen.findByRole("listbox"); |
| 179 | + expect( |
| 180 | + within(listbox).queryByText(/no tags found/i), |
| 181 | + ).not.toBeInTheDocument(); |
| 182 | + }); |
| 183 | +}); |
0 commit comments