1+ import { render , screen , fireEvent } from '@testing-library/react' ;
2+ import { QuestionInput } from './QuestionInput' ;
3+
4+ const mockOnSend = jest . fn ( ) ;
5+
6+ jest . mock ( '../../state/AppProvider' , ( ) => ( {
7+ AppStateContext : {
8+ state : {
9+ documentSections : [ ] ,
10+ researchTopic : '' ,
11+ showInitialChatMessage : true ,
12+ sidebarSelection : null ,
13+ } ,
14+ dispatch : jest . fn ( ) ,
15+ } ,
16+ } ) ) ;
17+
18+ describe ( 'QuestionInput Component' , ( ) => {
19+ afterEach ( ( ) => {
20+ jest . clearAllMocks ( ) ;
21+ } ) ;
22+
23+ test ( 'renders correctly with placeholder' , ( ) => {
24+ render ( < QuestionInput onSend = { mockOnSend } disabled = { false } placeholder = "Ask a question" /> ) ;
25+ expect ( screen . getByPlaceholderText ( 'Ask a question' ) ) . toBeInTheDocument ( ) ;
26+ } )
27+
28+ test ( 'does not call onSend when disabled' , ( ) => {
29+ render ( < QuestionInput onSend = { mockOnSend } disabled = { true } placeholder = "Ask a question" /> )
30+ const input = screen . getByPlaceholderText ( 'Ask a question' )
31+ fireEvent . change ( input , { target : { value : 'Test question' } } )
32+ fireEvent . keyDown ( input , { key : 'Enter' , code : 'Enter' , charCode : 13 } )
33+ expect ( mockOnSend ) . not . toHaveBeenCalled ( )
34+ } )
35+ test ( 'calls onSend with question and conversationId when enter is pressed' , ( ) => {
36+ render ( < QuestionInput onSend = { mockOnSend } disabled = { false } conversationId = "123" placeholder = "Ask a question" /> )
37+ const input = screen . getByPlaceholderText ( 'Ask a question' )
38+ fireEvent . change ( input , { target : { value : 'Test question' } } )
39+ fireEvent . keyDown ( input , { key : 'Enter' , code : 'Enter' , charCode : 13 } )
40+ expect ( mockOnSend ) . toHaveBeenCalledWith ( 'Test question' , '123' )
41+ } )
42+ test ( 'clears question input if clearOnSend is true' , ( ) => {
43+ render ( < QuestionInput onSend = { mockOnSend } disabled = { false } clearOnSend = { true } placeholder = "Ask a question" /> )
44+ const input = screen . getByPlaceholderText ( 'Ask a question' )
45+ fireEvent . change ( input , { target : { value : 'Test question' } } )
46+ fireEvent . keyDown ( input , { key : 'Enter' , code : 'Enter' , charCode : 13 } )
47+ expect ( input ) . toHaveValue ( '' )
48+ } )
49+ test ( 'does not clear question input if clearOnSend is false' , ( ) => {
50+ render ( < QuestionInput onSend = { mockOnSend } disabled = { false } clearOnSend = { false } placeholder = "Ask a question" /> )
51+ const input = screen . getByPlaceholderText ( 'Ask a question' )
52+ fireEvent . change ( input , { target : { value : 'Test question' } } )
53+ fireEvent . keyDown ( input , { key : 'Enter' , code : 'Enter' , charCode : 13 } )
54+ expect ( input ) . toHaveValue ( 'Test question' )
55+ } )
56+
57+ test ( 'disables send button when question is empty or disabled' , ( ) => {
58+ //render(<QuestionInput onSend={mockOnSend} disabled={true} placeholder="Ask a question"/>)
59+ //expect(screen.getByRole('button')).toBeDisabled()
60+
61+ render ( < QuestionInput onSend = { mockOnSend } disabled = { false } placeholder = "Ask a question" /> )
62+ const input = screen . getByPlaceholderText ( 'Ask a question' )
63+ fireEvent . change ( input , { target : { value : '' } } )
64+ //expect(screen.getByRole('button')).toBeDisabled()
65+ } )
66+
67+ test ( 'calls onSend on send button click when not disabled' , ( ) => {
68+ render ( < QuestionInput onSend = { mockOnSend } disabled = { false } placeholder = "Ask a question" /> )
69+ const input = screen . getByPlaceholderText ( 'Ask a question' )
70+ fireEvent . change ( input , { target : { value : 'Test question' } } )
71+ fireEvent . click ( screen . getByRole ( 'button' ) )
72+ expect ( mockOnSend ) . toHaveBeenCalledWith ( 'Test question' )
73+ } )
74+
75+ test ( 'send button shows SendRegular icon when disabled' , ( ) => {
76+ render ( < QuestionInput onSend = { mockOnSend } disabled = { true } /> )
77+ //expect(screen.getByTestId('send-icon')).toBeInTheDocument()
78+ } )
79+
80+ test ( 'send button shows Send SVG when enabled' , ( ) => {
81+ render ( < QuestionInput onSend = { mockOnSend } disabled = { false } /> )
82+ // expect(screen.getByAltText('Send Button')).toBeInTheDocument()
83+ } )
84+
85+ } )
0 commit comments