11import { renderWithContext , screen , waitFor , fireEvent , act , findByText , render } from '../../test/test.utils'
22import { ChatHistoryListItemCell , ChatHistoryListItemGroups } from './ChatHistoryListItem'
33import { Conversation } from '../../api/models'
4- import { historyRename , historyDelete , historyList } from '../../api'
4+ import { historyRename , historyDelete , historyList , historyRead } from '../../api'
55import React , { useEffect } from 'react'
66import userEvent from '@testing-library/user-event'
77import { AppStateContext } from '../../state/AppProvider'
@@ -11,6 +11,7 @@ jest.mock('../../api/api', () => ({
1111 historyRename : jest . fn ( ) ,
1212 historyDelete : jest . fn ( ) ,
1313 historyList : jest . fn ( ) ,
14+ historyRead : jest . fn ( ) ,
1415
1516} ) )
1617const mockGroupedChatHistory = [
@@ -164,17 +165,39 @@ describe('ChatHistoryListItemCell', () => {
164165 const titleElement = screen . getByText ( / T e s t C h a t / i)
165166 expect ( titleElement ) . toBeInTheDocument ( )
166167 } )
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+
173197 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+ } ) ;
178201
179202 test ( 'truncates long title' , ( ) => {
180203 const longTitleConversation = {
@@ -188,13 +211,20 @@ describe('ChatHistoryListItemCell', () => {
188211 expect ( truncatedTitle ) . toBeInTheDocument ( )
189212 } )
190213
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+ } ) ;
198228
199229 test ( 'when null item is not passed' , ( ) => {
200230 renderWithContext ( < ChatHistoryListItemCell onSelect = { mockOnSelect } /> , mockAppState )
0 commit comments