1
1
import { renderWithContext , screen , waitFor , fireEvent , act , findByText , render } from '../../test/test.utils'
2
2
import { ChatHistoryListItemCell , ChatHistoryListItemGroups } from './ChatHistoryListItem'
3
3
import { Conversation } from '../../api/models'
4
- import { historyRename , historyDelete , historyList } from '../../api'
4
+ import { historyRename , historyDelete , historyList , historyRead } from '../../api'
5
5
import React , { useEffect } from 'react'
6
6
import userEvent from '@testing-library/user-event'
7
7
import { AppStateContext } from '../../state/AppProvider'
@@ -11,6 +11,7 @@ jest.mock('../../api/api', () => ({
11
11
historyRename : jest . fn ( ) ,
12
12
historyDelete : jest . fn ( ) ,
13
13
historyList : jest . fn ( ) ,
14
+ historyRead : jest . fn ( ) ,
14
15
15
16
} ) )
16
17
const mockGroupedChatHistory = [
@@ -164,17 +165,39 @@ describe('ChatHistoryListItemCell', () => {
164
165
const titleElement = screen . getByText ( / T e s t C h a t / i)
165
166
expect ( titleElement ) . toBeInTheDocument ( )
166
167
} )
167
- test ( 'calls onSelect when a chat history item is clicked' , async ( ) => {
168
- renderWithContext ( < ChatHistoryListItemCell item = { conversation } onSelect = { mockOnSelect } /> , mockAppState )
169
- const titleElement = screen . getByText ( / T e s t C h a t / i)
170
- expect ( titleElement ) . toBeInTheDocument ( )
171
- // Simulate click on a chat item
172
- fireEvent . click ( titleElement )
168
+ // test('calls onSelect when a chat history item is clicked', async () => {
169
+ // renderWithContext(<ChatHistoryListItemCell item={conversation} onSelect={mockOnSelect} />, mockAppState)
170
+ // const titleElement = screen.getByText(/Test Chat/i)
171
+ // expect(titleElement).toBeInTheDocument()
172
+ // // Simulate click on a chat item
173
+ // fireEvent.click(titleElement)
174
+ // await waitFor(() => {
175
+ // // Ensure the onSelect handler is called with the correct item
176
+ // expect(mockOnSelect).toHaveBeenCalledWith(conversation)
177
+ // })
178
+ // })
179
+
180
+ // test('calls onSelect when clicked', async () => {
181
+ // renderWithContext(<ChatHistoryListItemCell item={conversation} onSelect={mockOnSelect} />, mockAppState);
182
+ // fireEvent.click(screen.getByText(/Test Chat/i));
183
+ // await waitFor(() => {
184
+ // expect(mockOnSelect).toHaveBeenCalledWith(conversation);
185
+ // });
186
+ // });
187
+
188
+ test ( 'calls onSelect with updated chat data when clicked' , async ( ) => {
189
+ // Mock historyRead to return some messages
190
+ const mockMessages = [ { id : 'msg1' , text : 'Hello' } ] ;
191
+ ( historyRead as jest . Mock ) . mockResolvedValue ( mockMessages ) ;
192
+
193
+ renderWithContext ( < ChatHistoryListItemCell item = { conversation } onSelect = { mockOnSelect } /> , mockAppState ) ;
194
+
195
+ fireEvent . click ( screen . getByText ( / T e s t C h a t / i) ) ;
196
+
173
197
await waitFor ( ( ) => {
174
- // Ensure the onSelect handler is called with the correct item
175
- expect ( mockOnSelect ) . toHaveBeenCalledWith ( conversation )
176
- } )
177
- } )
198
+ expect ( mockOnSelect ) . toHaveBeenCalledWith ( { ...conversation , messages : mockMessages } ) ;
199
+ } ) ;
200
+ } ) ;
178
201
179
202
test ( 'truncates long title' , ( ) => {
180
203
const longTitleConversation = {
@@ -188,13 +211,20 @@ describe('ChatHistoryListItemCell', () => {
188
211
expect ( truncatedTitle ) . toBeInTheDocument ( )
189
212
} )
190
213
191
- test ( 'calls onSelect when clicked' , ( ) => {
192
- renderWithContext ( < ChatHistoryListItemCell item = { conversation } onSelect = { mockOnSelect } /> , mockAppState )
193
-
194
- const item = screen . getByLabelText ( 'chat history item' )
195
- fireEvent . click ( item )
196
- expect ( mockOnSelect ) . toHaveBeenCalledWith ( conversation )
197
- } )
214
+ test ( 'calls onSelect when clicked' , async ( ) => {
215
+ renderWithContext ( < ChatHistoryListItemCell item = { conversation } onSelect = { mockOnSelect } /> , mockAppState ) ;
216
+
217
+ const item = screen . getByLabelText ( 'chat history item' ) ;
218
+ fireEvent . click ( item ) ;
219
+
220
+ await waitFor ( ( ) => {
221
+ expect ( mockOnSelect ) . toHaveBeenCalledWith ( expect . objectContaining ( {
222
+ id : conversation . id ,
223
+ title : conversation . title ,
224
+ date : conversation . date ,
225
+ } ) ) ;
226
+ } ) ;
227
+ } ) ;
198
228
199
229
test ( 'when null item is not passed' , ( ) => {
200
230
renderWithContext ( < ChatHistoryListItemCell onSelect = { mockOnSelect } /> , mockAppState )
0 commit comments