|
| 1 | +/** |
| 2 | + * Unit tests for ContinuePrompt component |
| 3 | + * |
| 4 | + * Tests cover: |
| 5 | + * - Renders the continue dialog correctly |
| 6 | + * - Continue button triggers onContinue callback |
| 7 | + * - X dismiss button triggers onDismiss callback |
| 8 | + * - Dialog displays correct title and description text |
| 9 | + */ |
| 10 | + |
| 11 | +import React from "react"; |
| 12 | +import { render, screen, fireEvent } from "@testing-library/react"; |
| 13 | +import { describe, it, expect, vi } from "vitest"; |
| 14 | +import ContinuePrompt from "./ContinuePrompt"; |
| 15 | + |
| 16 | +describe("ContinuePrompt", () => { |
| 17 | + it("renders the continue dialog with correct title", () => { |
| 18 | + render(<ContinuePrompt onContinue={vi.fn()} onDismiss={vi.fn()} />); |
| 19 | + |
| 20 | + expect(screen.getByText("Session interrupted")).toBeInTheDocument(); |
| 21 | + }); |
| 22 | + |
| 23 | + it("renders the description text", () => { |
| 24 | + render(<ContinuePrompt onContinue={vi.fn()} onDismiss={vi.fn()} />); |
| 25 | + |
| 26 | + expect( |
| 27 | + screen.getByText("The previous operation was interrupted. Do you want to continue the conversation?") |
| 28 | + ).toBeInTheDocument(); |
| 29 | + }); |
| 30 | + |
| 31 | + it("renders a Continue button", () => { |
| 32 | + render(<ContinuePrompt onContinue={vi.fn()} onDismiss={vi.fn()} />); |
| 33 | + |
| 34 | + const continueButton = screen.getByRole("button", { name: /continue/i }); |
| 35 | + expect(continueButton).toBeInTheDocument(); |
| 36 | + }); |
| 37 | + |
| 38 | + it("calls onContinue when Continue button is clicked", () => { |
| 39 | + const onContinue = vi.fn(); |
| 40 | + const onDismiss = vi.fn(); |
| 41 | + |
| 42 | + render(<ContinuePrompt onContinue={onContinue} onDismiss={onDismiss} />); |
| 43 | + |
| 44 | + const continueButton = screen.getByRole("button", { name: /continue/i }); |
| 45 | + fireEvent.click(continueButton); |
| 46 | + |
| 47 | + expect(onContinue).toHaveBeenCalledTimes(1); |
| 48 | + expect(onDismiss).not.toHaveBeenCalled(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("calls onDismiss when X close button is clicked", () => { |
| 52 | + const onContinue = vi.fn(); |
| 53 | + const onDismiss = vi.fn(); |
| 54 | + |
| 55 | + render(<ContinuePrompt onContinue={onContinue} onDismiss={onDismiss} />); |
| 56 | + |
| 57 | + // The X button has title="Dismiss" |
| 58 | + const dismissButton = screen.getByTitle("Dismiss"); |
| 59 | + fireEvent.click(dismissButton); |
| 60 | + |
| 61 | + expect(onDismiss).toHaveBeenCalledTimes(1); |
| 62 | + expect(onContinue).not.toHaveBeenCalled(); |
| 63 | + }); |
| 64 | + |
| 65 | + it("renders the dialog within a bordered container", () => { |
| 66 | + const { container } = render(<ContinuePrompt onContinue={vi.fn()} onDismiss={vi.fn()} />); |
| 67 | + |
| 68 | + // The dialog should have the rounded border class |
| 69 | + const dialogContainer = container.querySelector(".rounded-md.border"); |
| 70 | + expect(dialogContainer).toBeInTheDocument(); |
| 71 | + }); |
| 72 | + |
| 73 | + it("renders with correct layout structure", () => { |
| 74 | + const { container } = render(<ContinuePrompt onContinue={vi.fn()} onDismiss={vi.fn()} />); |
| 75 | + |
| 76 | + // Should have flex layout for title and close button |
| 77 | + const headerRow = container.querySelector(".flex.items-center.justify-between"); |
| 78 | + expect(headerRow).toBeInTheDocument(); |
| 79 | + |
| 80 | + // Should have a button group |
| 81 | + const buttonGroup = container.querySelector(".flex.gap-2"); |
| 82 | + expect(buttonGroup).toBeInTheDocument(); |
| 83 | + }); |
| 84 | + |
| 85 | + it("can be called multiple times with different callbacks", () => { |
| 86 | + const onContinue1 = vi.fn(); |
| 87 | + const onDismiss1 = vi.fn(); |
| 88 | + const onContinue2 = vi.fn(); |
| 89 | + const onDismiss2 = vi.fn(); |
| 90 | + |
| 91 | + const { rerender } = render(<ContinuePrompt onContinue={onContinue1} onDismiss={onDismiss1} />); |
| 92 | + |
| 93 | + fireEvent.click(screen.getByRole("button", { name: /continue/i })); |
| 94 | + expect(onContinue1).toHaveBeenCalledTimes(1); |
| 95 | + |
| 96 | + rerender(<ContinuePrompt onContinue={onContinue2} onDismiss={onDismiss2} />); |
| 97 | + |
| 98 | + fireEvent.click(screen.getByRole("button", { name: /continue/i })); |
| 99 | + expect(onContinue2).toHaveBeenCalledTimes(1); |
| 100 | + expect(onContinue1).toHaveBeenCalledTimes(1); |
| 101 | + }); |
| 102 | +}); |
0 commit comments