11// npx jest src/components/chat/__tests__/CommandExecution.test.tsx
22
33import React from "react"
4- import { render , screen } from "@testing-library/react"
4+ import { render , screen , fireEvent } from "@testing-library/react"
55
66import { ExtensionStateContextProvider } from "@src/context/ExtensionStateContext"
77
88import { CommandExecution } from "../CommandExecution"
99
10+ jest . mock ( "../../../components/common/CodeBlock" )
11+
1012jest . mock ( "@src/lib/utils" , ( ) => ( {
1113 cn : ( ...inputs : any [ ] ) => inputs . filter ( Boolean ) . join ( " " ) ,
1214} ) )
1315
14- jest . mock ( "lucide-react" , ( ) => ( {
15- ChevronDown : ( ) => < div data-testid = "chevron-down" > ChevronDown</ div > ,
16- } ) )
17-
18- jest . mock ( "react-virtuoso" , ( ) => ( {
19- Virtuoso : React . forwardRef ( ( { totalCount, itemContent } : any , ref : any ) => (
20- < div ref = { ref } data-testid = "virtuoso-container" >
21- { Array . from ( { length : totalCount } ) . map ( ( _ , index ) => (
22- < div key = { index } data-testid = { `virtuoso-item-${ index } ` } >
23- { itemContent ( index ) }
24- </ div >
25- ) ) }
26- </ div >
27- ) ) ,
28- VirtuosoHandle : jest . fn ( ) ,
29- } ) )
30-
3116describe ( "CommandExecution" , ( ) => {
3217 const renderComponent = ( command : string , output : string ) => {
3318 return render (
@@ -40,24 +25,34 @@ describe("CommandExecution", () => {
4025 it ( "renders command output with virtualized list" , ( ) => {
4126 const testOutput = "Line 1\nLine 2\nLine 3"
4227 renderComponent ( "ls" , testOutput )
43- expect ( screen . getByTestId ( "virtuoso-container" ) ) . toBeInTheDocument ( )
44- expect ( screen . getByText ( "Line 1" ) ) . toBeInTheDocument ( )
45- expect ( screen . getByText ( "Line 2" ) ) . toBeInTheDocument ( )
46- expect ( screen . getByText ( "Line 3" ) ) . toBeInTheDocument ( )
28+ const codeBlock = screen . getByTestId ( "mock-code-block" )
29+ expect ( codeBlock ) . toHaveTextContent ( "ls" )
30+
31+ fireEvent . click ( screen . getByText ( "commandOutput" ) )
32+ const outputBlock = screen . getAllByTestId ( "mock-code-block" ) [ 1 ]
33+
34+ expect ( outputBlock ) . toHaveTextContent ( "Line 1" )
35+ expect ( outputBlock ) . toHaveTextContent ( "Line 2" )
36+ expect ( outputBlock ) . toHaveTextContent ( "Line 3" )
4737 } )
4838
4939 it ( "handles empty output" , ( ) => {
5040 renderComponent ( "ls" , "" )
51- expect ( screen . getByTestId ( "virtuoso-container" ) ) . toBeInTheDocument ( )
52- expect ( screen . getByTestId ( "virtuoso-item-0" ) ) . toBeInTheDocument ( )
53- expect ( screen . queryByTestId ( "virtuoso-item-1" ) ) . not . toBeInTheDocument ( )
41+ const codeBlock = screen . getByTestId ( "mock-code-block" )
42+ expect ( codeBlock ) . toHaveTextContent ( "ls" )
43+ expect ( screen . queryByText ( "commandOutput" ) ) . not . toBeInTheDocument ( )
44+ expect ( screen . queryAllByTestId ( "mock-code-block" ) ) . toHaveLength ( 1 )
5445 } )
5546
5647 it ( "handles large output" , ( ) => {
5748 const largeOutput = Array . from ( { length : 1000 } , ( _ , i ) => `Line ${ i + 1 } ` ) . join ( "\n" )
5849 renderComponent ( "ls" , largeOutput )
59- expect ( screen . getByTestId ( "virtuoso-container" ) ) . toBeInTheDocument ( )
60- expect ( screen . getByText ( "Line 1" ) ) . toBeInTheDocument ( )
61- expect ( screen . getByText ( "Line 1000" ) ) . toBeInTheDocument ( )
50+ const codeBlock = screen . getByTestId ( "mock-code-block" )
51+ expect ( codeBlock ) . toHaveTextContent ( "ls" )
52+
53+ fireEvent . click ( screen . getByText ( "commandOutput" ) )
54+ const outputBlock = screen . getAllByTestId ( "mock-code-block" ) [ 1 ]
55+ expect ( outputBlock ) . toHaveTextContent ( "Line 1" )
56+ expect ( outputBlock ) . toHaveTextContent ( "Line 1000" )
6257 } )
6358} )
0 commit comments