|
1 | 1 | // @vitest-environment jsdom |
2 | | -import { fireEvent, render, screen } from "@testing-library/react"; |
| 2 | +import { act, fireEvent, render, screen, waitFor } from "@testing-library/react"; |
3 | 3 | import { afterEach, beforeEach, describe, expect, it, type MockInstance, vi } from "vitest"; |
4 | 4 | import { ProposalCard } from "./ProposalCard"; |
5 | 5 | import type { ConnectionRequirement, PendingProposal } from "./types"; |
@@ -193,4 +193,140 @@ describe("ProposalCard", () => { |
193 | 193 | expect(screen.getByText("GitHub App")).toBeTruthy(); |
194 | 194 | expect(screen.getByText("installed")).toBeTruthy(); |
195 | 195 | }); |
| 196 | + |
| 197 | + it("calls the in-place connect handler instead of navigating when onConnect is set", () => { |
| 198 | + const navigate = vi.fn(); |
| 199 | + const onConnect = vi.fn(() => Promise.resolve()); |
| 200 | + const req: ConnectionRequirement = { provider: "slack", connected: false }; |
| 201 | + render( |
| 202 | + <ProposalCard |
| 203 | + proposal={withReq(req)} |
| 204 | + confirming={false} |
| 205 | + onConfirm={noop} |
| 206 | + onCancel={noop} |
| 207 | + navigate={navigate} |
| 208 | + onConnect={onConnect} |
| 209 | + />, |
| 210 | + ); |
| 211 | + fireEvent.click(screen.getByRole("button", { name: /Connect/ })); |
| 212 | + // In-place connect runs; the navigate/new-tab fallbacks stay untouched. |
| 213 | + expect(onConnect).toHaveBeenCalledWith(req); |
| 214 | + expect(navigate).not.toHaveBeenCalled(); |
| 215 | + expect(openSpy).not.toHaveBeenCalled(); |
| 216 | + }); |
| 217 | + |
| 218 | + it("shows a disabled busy state while the in-place connect is in flight", async () => { |
| 219 | + let resolveConnect!: () => void; |
| 220 | + const onConnect = vi.fn( |
| 221 | + () => |
| 222 | + new Promise<void>((resolve) => { |
| 223 | + resolveConnect = resolve; |
| 224 | + }), |
| 225 | + ); |
| 226 | + render( |
| 227 | + <ProposalCard |
| 228 | + proposal={withReq({ provider: "slack", connected: false })} |
| 229 | + confirming={false} |
| 230 | + onConfirm={noop} |
| 231 | + onCancel={noop} |
| 232 | + onConnect={onConnect} |
| 233 | + />, |
| 234 | + ); |
| 235 | + fireEvent.click(screen.getByRole("button", { name: /Connect/ })); |
| 236 | + const busy = screen.getByRole("button", { name: /Connecting/ }); |
| 237 | + expect((busy as HTMLButtonElement).disabled).toBe(true); |
| 238 | + // Once the host resolves, the row leaves the busy state (the parent flips the |
| 239 | + // requirement to connected in the real flow; here the isolated card just |
| 240 | + // restores the actionable button). |
| 241 | + await act(async () => { |
| 242 | + resolveConnect(); |
| 243 | + }); |
| 244 | + expect(screen.queryByRole("button", { name: /Connecting/ })).toBeNull(); |
| 245 | + expect( |
| 246 | + (screen.getByRole("button", { name: /Connect/ }) as HTMLButtonElement) |
| 247 | + .disabled, |
| 248 | + ).toBe(false); |
| 249 | + }); |
| 250 | + |
| 251 | + it("falls back to navigation when no onConnect handler is provided", () => { |
| 252 | + const navigate = vi.fn(); |
| 253 | + render( |
| 254 | + <ProposalCard |
| 255 | + proposal={withReq({ provider: "slack", connected: false })} |
| 256 | + confirming={false} |
| 257 | + onConfirm={noop} |
| 258 | + onCancel={noop} |
| 259 | + navigate={navigate} |
| 260 | + />, |
| 261 | + ); |
| 262 | + fireEvent.click(screen.getByRole("button", { name: /Connect/ })); |
| 263 | + expect(navigate).toHaveBeenCalledWith("/app/integrations"); |
| 264 | + }); |
| 265 | + |
| 266 | + it("offers in-place connect even when connectUrl is null (host owns the flow)", () => { |
| 267 | + const onConnect = vi.fn(() => Promise.resolve()); |
| 268 | + render( |
| 269 | + <ProposalCard |
| 270 | + proposal={withReq({ |
| 271 | + provider: "github", |
| 272 | + kind: "github_app", |
| 273 | + connected: false, |
| 274 | + connectUrl: null, |
| 275 | + })} |
| 276 | + confirming={false} |
| 277 | + onConfirm={noop} |
| 278 | + onCancel={noop} |
| 279 | + onConnect={onConnect} |
| 280 | + />, |
| 281 | + ); |
| 282 | + // With a host handler the row is actionable even with no connect URL. |
| 283 | + fireEvent.click(screen.getByRole("button", { name: /Install/ })); |
| 284 | + expect(onConnect).toHaveBeenCalled(); |
| 285 | + }); |
| 286 | + |
| 287 | + it("clears the busy state when the in-place handler rejects (no unhandled rejection)", async () => { |
| 288 | + const onConnect = vi.fn(() => Promise.reject(new Error("connect failed"))); |
| 289 | + render( |
| 290 | + <ProposalCard |
| 291 | + proposal={withReq({ provider: "slack", connected: false })} |
| 292 | + confirming={false} |
| 293 | + onConfirm={noop} |
| 294 | + onCancel={noop} |
| 295 | + onConnect={onConnect} |
| 296 | + />, |
| 297 | + ); |
| 298 | + fireEvent.click(screen.getByRole("button", { name: /Connect/ })); |
| 299 | + // The rejection is contained; the row leaves the busy state and stays usable. |
| 300 | + await waitFor(() => |
| 301 | + expect(screen.queryByRole("button", { name: /Connecting/ })).toBeNull(), |
| 302 | + ); |
| 303 | + expect( |
| 304 | + (screen.getByRole("button", { name: /Connect/ }) as HTMLButtonElement) |
| 305 | + .disabled, |
| 306 | + ).toBe(false); |
| 307 | + }); |
| 308 | + |
| 309 | + it("clears the busy state when the in-place handler throws synchronously", async () => { |
| 310 | + const onConnect = vi.fn(() => { |
| 311 | + throw new Error("sync boom"); |
| 312 | + }); |
| 313 | + render( |
| 314 | + <ProposalCard |
| 315 | + proposal={withReq({ provider: "slack", connected: false })} |
| 316 | + confirming={false} |
| 317 | + onConfirm={noop} |
| 318 | + onCancel={noop} |
| 319 | + onConnect={onConnect} |
| 320 | + />, |
| 321 | + ); |
| 322 | + fireEvent.click(screen.getByRole("button", { name: /Connect/ })); |
| 323 | + await waitFor(() => |
| 324 | + expect(screen.queryByRole("button", { name: /Connecting/ })).toBeNull(), |
| 325 | + ); |
| 326 | + expect(onConnect).toHaveBeenCalled(); |
| 327 | + expect( |
| 328 | + (screen.getByRole("button", { name: /Connect/ }) as HTMLButtonElement) |
| 329 | + .disabled, |
| 330 | + ).toBe(false); |
| 331 | + }); |
196 | 332 | }); |
0 commit comments