|
| 1 | +import React from "react" |
| 2 | +import { render, screen } from "@testing-library/react" |
| 3 | +import CommandOutputViewer from "../../../components/common/CommandOutputViewer" |
| 4 | + |
| 5 | +// Mock the cn utility function |
| 6 | +jest.mock("../../../lib/utils", () => ({ |
| 7 | + cn: (...inputs: any[]) => inputs.filter(Boolean).join(" "), |
| 8 | +})) |
| 9 | + |
| 10 | +// Mock the Virtuoso component |
| 11 | +jest.mock("react-virtuoso", () => ({ |
| 12 | + Virtuoso: React.forwardRef(({ totalCount, itemContent }: any, ref: any) => ( |
| 13 | + <div ref={ref} data-testid="virtuoso-container"> |
| 14 | + {Array.from({ length: totalCount }).map((_, index) => ( |
| 15 | + <div key={index} data-testid={`virtuoso-item-${index}`}> |
| 16 | + {itemContent(index)} |
| 17 | + </div> |
| 18 | + ))} |
| 19 | + </div> |
| 20 | + )), |
| 21 | + VirtuosoHandle: jest.fn(), |
| 22 | +})) |
| 23 | + |
| 24 | +describe("CommandOutputViewer", () => { |
| 25 | + it("renders command output with virtualized list", () => { |
| 26 | + const testOutput = "Line 1\nLine 2\nLine 3" |
| 27 | + |
| 28 | + render(<CommandOutputViewer output={testOutput} />) |
| 29 | + |
| 30 | + // Check if Virtuoso container is rendered |
| 31 | + expect(screen.getByTestId("virtuoso-container")).toBeInTheDocument() |
| 32 | + |
| 33 | + // Check if all lines are rendered |
| 34 | + expect(screen.getByText("Line 1")).toBeInTheDocument() |
| 35 | + expect(screen.getByText("Line 2")).toBeInTheDocument() |
| 36 | + expect(screen.getByText("Line 3")).toBeInTheDocument() |
| 37 | + }) |
| 38 | + |
| 39 | + it("handles empty output", () => { |
| 40 | + render(<CommandOutputViewer output="" />) |
| 41 | + |
| 42 | + // Should still render the container but with no items |
| 43 | + expect(screen.getByTestId("virtuoso-container")).toBeInTheDocument() |
| 44 | + |
| 45 | + // No virtuoso items should be rendered for empty string (which creates one empty line) |
| 46 | + expect(screen.getByTestId("virtuoso-item-0")).toBeInTheDocument() |
| 47 | + expect(screen.queryByTestId("virtuoso-item-1")).not.toBeInTheDocument() |
| 48 | + }) |
| 49 | + |
| 50 | + it("handles large output", () => { |
| 51 | + // Create a large output with 1000 lines |
| 52 | + const largeOutput = Array.from({ length: 1000 }, (_, i) => `Line ${i + 1}`).join("\n") |
| 53 | + |
| 54 | + render(<CommandOutputViewer output={largeOutput} />) |
| 55 | + |
| 56 | + // Check if Virtuoso container is rendered |
| 57 | + expect(screen.getByTestId("virtuoso-container")).toBeInTheDocument() |
| 58 | + |
| 59 | + // Check if first and last lines are rendered |
| 60 | + expect(screen.getByText("Line 1")).toBeInTheDocument() |
| 61 | + expect(screen.getByText("Line 1000")).toBeInTheDocument() |
| 62 | + }) |
| 63 | +}) |
0 commit comments