CommandOutputViewer.test.tsx 2.2 KB

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