-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6aa4457
commit afc8e07
Showing
9 changed files
with
1,335 additions
and
341 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
ClientAdvisor/App/frontend/src/components/ChatHistory/ChatHistoryList.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React from 'react' | ||
import { renderWithContext, screen, waitFor, fireEvent, act } from '../../test/test.utils'; | ||
import { ChatHistoryList } from './ChatHistoryList' | ||
import {groupByMonth} from '../../helpers/helpers'; | ||
|
||
// Mock the groupByMonth function | ||
jest.mock('../../helpers/helpers', () => ({ | ||
groupByMonth: jest.fn(), | ||
})); | ||
|
||
// Mock ChatHistoryListItemGroups component | ||
jest.mock('./ChatHistoryListItem', () => ({ | ||
ChatHistoryListItemGroups: jest.fn(() => <div>Mocked ChatHistoryListItemGroups</div>), | ||
})); | ||
|
||
describe('ChatHistoryList', () => { | ||
|
||
beforeEach(() => { | ||
global.fetch = jest.fn(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should display "No chat history." when chatHistory is empty', () => { | ||
renderWithContext(<ChatHistoryList />); | ||
|
||
expect(screen.getByText('No chat history.')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should call groupByMonth with chatHistory when chatHistory is present', () => { | ||
const mockstate = { | ||
chatHistory : [{ | ||
id: '1', | ||
title: 'Sample chat message', | ||
messages:[], | ||
date:new Date().toISOString(), | ||
updatedAt: new Date().toISOString(), | ||
}] | ||
}; | ||
(groupByMonth as jest.Mock).mockReturnValue([]); | ||
renderWithContext(<ChatHistoryList /> , mockstate); | ||
|
||
expect(groupByMonth).toHaveBeenCalledWith(mockstate.chatHistory); | ||
}); | ||
|
||
it('should render ChatHistoryListItemGroups with grouped chat history when chatHistory is present', () => { | ||
const mockstate = { | ||
chatHistory : [{ | ||
id: '1', | ||
title: 'Sample chat message', | ||
messages:[], | ||
date:new Date().toISOString(), | ||
updatedAt: new Date().toISOString(), | ||
}] | ||
}; | ||
(groupByMonth as jest.Mock).mockReturnValue([]); | ||
renderWithContext(<ChatHistoryList /> , mockstate); | ||
|
||
expect(screen.getByText('Mocked ChatHistoryListItemGroups')).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
ClientAdvisor/App/frontend/src/components/ChatHistory/ChatHistoryListItem.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { renderWithContext, screen, waitFor, fireEvent, act } from '../../test/test.utils'; | ||
import { ChatHistoryListItemGroups } from './ChatHistoryListItem'; | ||
import { historyList } from '../../api'; | ||
|
||
jest.mock('../../api', () => ({ | ||
historyList: jest.fn(), | ||
})); | ||
|
||
const mockDispatch = jest.fn(); | ||
const handleFetchHistory = jest.fn(); | ||
|
||
// Mock the ChatHistoryListItemCell component | ||
jest.mock('./ChatHistoryListItemCell', () => ({ | ||
ChatHistoryListItemCell: jest.fn(({ item, onSelect }) => ( | ||
<div data-testid={`mock-cell-${item.id}`} onClick={() => onSelect(item)}> | ||
{item?.title} | ||
</div> | ||
)), | ||
})); | ||
|
||
const mockGroupedChatHistory = [ | ||
{ | ||
month: '2023-09', | ||
entries: [ | ||
{ id: '1', title: 'Chat 1', messages: [], date: new Date().toISOString(), updatedAt: new Date().toISOString() }, | ||
{ id: '2', title: 'Chat 2', messages: [], date: new Date().toISOString(), updatedAt: new Date().toISOString() }, | ||
], | ||
}, | ||
{ | ||
month: '2023-08', | ||
entries: [ | ||
{ id: '3', title: 'Chat 3', messages: [], date: new Date().toISOString(), updatedAt: new Date().toISOString() }, | ||
], | ||
}, | ||
]; | ||
|
||
describe('ChatHistoryListItemGroups Component', () => { | ||
beforeEach(() => { | ||
global.fetch = jest.fn(); | ||
|
||
jest.spyOn(console, 'error').mockImplementation(() => { }); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
//(console.error as jest.Mock).mockRestore(); | ||
}); | ||
|
||
it('should call handleFetchHistory with the correct offset when the observer is triggered', async () => { | ||
const responseMock = [{ id: '4', title: 'Chat 4', messages: [], date: new Date().toISOString(), updatedAt: new Date().toISOString() }]; | ||
(historyList as jest.Mock).mockResolvedValue([...responseMock]); | ||
await act(async () => { | ||
renderWithContext(<ChatHistoryListItemGroups groupedChatHistory={mockGroupedChatHistory} />); | ||
}); | ||
|
||
const scrollElms = await screen.findAllByRole('scrollDiv'); | ||
const lastElem = scrollElms[scrollElms.length - 1]; | ||
|
||
await act(async () => { | ||
fireEvent.scroll(lastElem, { target: { scrollY: 100 } }); | ||
//await waitFor(() => expect(historyList).toHaveBeenCalled()); | ||
}); | ||
|
||
await act(async () => { | ||
await waitFor(() => { | ||
expect(historyList).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('displays spinner while loading more history', async () => { | ||
const responseMock = [{ id: '4', title: 'Chat 4', messages: [], date: new Date().toISOString(), updatedAt: new Date().toISOString() }]; | ||
(historyList as jest.Mock).mockResolvedValue([...responseMock]); | ||
await act(async () => { | ||
renderWithContext(<ChatHistoryListItemGroups groupedChatHistory={mockGroupedChatHistory} />); | ||
}); | ||
|
||
const scrollElms = await screen.findAllByRole('scrollDiv'); | ||
const lastElem = scrollElms[scrollElms.length - 1]; | ||
|
||
await act(async () => { | ||
fireEvent.scroll(lastElem, { target: { scrollY: 100 } }); | ||
}); | ||
|
||
await act(async () => { | ||
await waitFor(() => { | ||
expect(screen.queryByLabelText(/loading/i)).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should render the grouped chat history', () => { | ||
renderWithContext(<ChatHistoryListItemGroups groupedChatHistory={mockGroupedChatHistory} />); | ||
|
||
// Check if each group is rendered | ||
expect(screen.getByText('2023-09')).toBeInTheDocument(); | ||
expect(screen.getByText('2023-08')).toBeInTheDocument(); | ||
|
||
// Check if entries are rendered | ||
expect(screen.getByText('Chat 1')).toBeInTheDocument(); | ||
expect(screen.getByText('Chat 2')).toBeInTheDocument(); | ||
expect(screen.getByText('Chat 3')).toBeInTheDocument(); | ||
}); | ||
|
||
it('calls onSelect with the correct item when a ChatHistoryListItemCell is clicked', async () => { | ||
const handleSelectMock = jest.fn(); | ||
|
||
// Render the component | ||
renderWithContext(<ChatHistoryListItemGroups groupedChatHistory={mockGroupedChatHistory} />); | ||
|
||
// Simulate clicks on each ChatHistoryListItemCell | ||
const cells = screen.getAllByTestId(/mock-cell-/); | ||
|
||
// Click on the first cell | ||
fireEvent.click(cells[0]); | ||
|
||
// Wait for the mock function to be called with the correct item | ||
// await waitFor(() => { | ||
// expect(handleSelectMock).toHaveBeenCalledWith(mockGroupedChatHistory[0].entries[0]); | ||
// }); | ||
|
||
}); | ||
|
||
it('handles API failure gracefully', async () => { | ||
// Mock the API to reject with an error | ||
(historyList as jest.Mock).mockResolvedValue(undefined); | ||
|
||
renderWithContext(<ChatHistoryListItemGroups groupedChatHistory={mockGroupedChatHistory} />); | ||
|
||
// Simulate triggering the scroll event that loads more history | ||
const scrollElms = await screen.findAllByRole('scrollDiv'); | ||
const lastElem = scrollElms[scrollElms.length - 1]; | ||
|
||
await act(async () => { | ||
fireEvent.scroll(lastElem, { target: { scrollY: 100 } }); | ||
}); | ||
// Check that the spinner is hidden after the API call | ||
await waitFor(() => { | ||
expect(screen.queryByLabelText(/loading/i)).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
}); |
Oops, something went wrong.