Skip to content

Commit b897b01

Browse files
author
Shreyas-Microsoft
committed
fix chat history list item test
1 parent 2b09be9 commit b897b01

File tree

1 file changed

+48
-18
lines changed

1 file changed

+48
-18
lines changed

src/frontend/src/components/ChatHistory/chatHistoryListItem.test.tsx

+48-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { renderWithContext, screen, waitFor, fireEvent, act, findByText, render } from '../../test/test.utils'
22
import { ChatHistoryListItemCell, ChatHistoryListItemGroups } from './ChatHistoryListItem'
33
import { Conversation } from '../../api/models'
4-
import { historyRename, historyDelete, historyList } from '../../api'
4+
import { historyRename, historyDelete, historyList, historyRead } from '../../api'
55
import React, { useEffect } from 'react'
66
import userEvent from '@testing-library/user-event'
77
import { 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
}))
1617
const mockGroupedChatHistory = [
@@ -164,17 +165,39 @@ describe('ChatHistoryListItemCell', () => {
164165
const titleElement = screen.getByText(/Test Chat/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(/Test Chat/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(/Test Chat/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

Comments
 (0)