|
| 1 | +import { renderWithContext, screen, waitFor, fireEvent, act } from '../../test/test.utils'; |
| 2 | +import { ChatHistoryListItemGroups } from './ChatHistoryListItem'; |
| 3 | +import { historyList } from '../../api'; |
| 4 | + |
| 5 | +jest.mock('../../api', () => ({ |
| 6 | + historyList: jest.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +const mockDispatch = jest.fn(); |
| 10 | +const handleFetchHistory = jest.fn(); |
| 11 | + |
| 12 | +// Mock the ChatHistoryListItemCell component |
| 13 | +jest.mock('./ChatHistoryListItemCell', () => ({ |
| 14 | + ChatHistoryListItemCell: jest.fn(({ item, onSelect }) => ( |
| 15 | + <div data-testid={`mock-cell-${item.id}`} onClick={() => onSelect(item)}> |
| 16 | + {item?.title} |
| 17 | + </div> |
| 18 | + )), |
| 19 | +})); |
| 20 | + |
| 21 | +const mockGroupedChatHistory = [ |
| 22 | + { |
| 23 | + month: '2023-09', |
| 24 | + entries: [ |
| 25 | + { id: '1', title: 'Chat 1', messages: [], date: new Date().toISOString(), updatedAt: new Date().toISOString() }, |
| 26 | + { id: '2', title: 'Chat 2', messages: [], date: new Date().toISOString(), updatedAt: new Date().toISOString() }, |
| 27 | + ], |
| 28 | + }, |
| 29 | + { |
| 30 | + month: '2023-08', |
| 31 | + entries: [ |
| 32 | + { id: '3', title: 'Chat 3', messages: [], date: new Date().toISOString(), updatedAt: new Date().toISOString() }, |
| 33 | + ], |
| 34 | + }, |
| 35 | +]; |
| 36 | + |
| 37 | +describe('ChatHistoryListItemGroups Component', () => { |
| 38 | + beforeEach(() => { |
| 39 | + global.fetch = jest.fn(); |
| 40 | + |
| 41 | + jest.spyOn(console, 'error').mockImplementation(() => { }); |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(() => { |
| 45 | + jest.clearAllMocks(); |
| 46 | + //(console.error as jest.Mock).mockRestore(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should call handleFetchHistory with the correct offset when the observer is triggered', async () => { |
| 50 | + const responseMock = [{ id: '4', title: 'Chat 4', messages: [], date: new Date().toISOString(), updatedAt: new Date().toISOString() }]; |
| 51 | + (historyList as jest.Mock).mockResolvedValue([...responseMock]); |
| 52 | + await act(async () => { |
| 53 | + renderWithContext(<ChatHistoryListItemGroups groupedChatHistory={mockGroupedChatHistory} />); |
| 54 | + }); |
| 55 | + |
| 56 | + const scrollElms = await screen.findAllByRole('scrollDiv'); |
| 57 | + const lastElem = scrollElms[scrollElms.length - 1]; |
| 58 | + |
| 59 | + await act(async () => { |
| 60 | + fireEvent.scroll(lastElem, { target: { scrollY: 100 } }); |
| 61 | + //await waitFor(() => expect(historyList).toHaveBeenCalled()); |
| 62 | + }); |
| 63 | + |
| 64 | + await act(async () => { |
| 65 | + await waitFor(() => { |
| 66 | + expect(historyList).toHaveBeenCalled(); |
| 67 | + }); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('displays spinner while loading more history', async () => { |
| 72 | + const responseMock = [{ id: '4', title: 'Chat 4', messages: [], date: new Date().toISOString(), updatedAt: new Date().toISOString() }]; |
| 73 | + (historyList as jest.Mock).mockResolvedValue([...responseMock]); |
| 74 | + await act(async () => { |
| 75 | + renderWithContext(<ChatHistoryListItemGroups groupedChatHistory={mockGroupedChatHistory} />); |
| 76 | + }); |
| 77 | + |
| 78 | + const scrollElms = await screen.findAllByRole('scrollDiv'); |
| 79 | + const lastElem = scrollElms[scrollElms.length - 1]; |
| 80 | + |
| 81 | + await act(async () => { |
| 82 | + fireEvent.scroll(lastElem, { target: { scrollY: 100 } }); |
| 83 | + }); |
| 84 | + |
| 85 | + await act(async () => { |
| 86 | + await waitFor(() => { |
| 87 | + expect(screen.queryByLabelText(/loading/i)).not.toBeInTheDocument(); |
| 88 | + }); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should render the grouped chat history', () => { |
| 93 | + renderWithContext(<ChatHistoryListItemGroups groupedChatHistory={mockGroupedChatHistory} />); |
| 94 | + |
| 95 | + // Check if each group is rendered |
| 96 | + expect(screen.getByText('2023-09')).toBeInTheDocument(); |
| 97 | + expect(screen.getByText('2023-08')).toBeInTheDocument(); |
| 98 | + |
| 99 | + // Check if entries are rendered |
| 100 | + expect(screen.getByText('Chat 1')).toBeInTheDocument(); |
| 101 | + expect(screen.getByText('Chat 2')).toBeInTheDocument(); |
| 102 | + expect(screen.getByText('Chat 3')).toBeInTheDocument(); |
| 103 | + }); |
| 104 | + |
| 105 | + it('calls onSelect with the correct item when a ChatHistoryListItemCell is clicked', async () => { |
| 106 | + const handleSelectMock = jest.fn(); |
| 107 | + |
| 108 | + // Render the component |
| 109 | + renderWithContext(<ChatHistoryListItemGroups groupedChatHistory={mockGroupedChatHistory} />); |
| 110 | + |
| 111 | + // Simulate clicks on each ChatHistoryListItemCell |
| 112 | + const cells = screen.getAllByTestId(/mock-cell-/); |
| 113 | + |
| 114 | + // Click on the first cell |
| 115 | + fireEvent.click(cells[0]); |
| 116 | + |
| 117 | + // Wait for the mock function to be called with the correct item |
| 118 | + // await waitFor(() => { |
| 119 | + // expect(handleSelectMock).toHaveBeenCalledWith(mockGroupedChatHistory[0].entries[0]); |
| 120 | + // }); |
| 121 | + |
| 122 | + }); |
| 123 | + |
| 124 | + it('handles API failure gracefully', async () => { |
| 125 | + // Mock the API to reject with an error |
| 126 | + (historyList as jest.Mock).mockResolvedValue(undefined); |
| 127 | + |
| 128 | + renderWithContext(<ChatHistoryListItemGroups groupedChatHistory={mockGroupedChatHistory} />); |
| 129 | + |
| 130 | + // Simulate triggering the scroll event that loads more history |
| 131 | + const scrollElms = await screen.findAllByRole('scrollDiv'); |
| 132 | + const lastElem = scrollElms[scrollElms.length - 1]; |
| 133 | + |
| 134 | + await act(async () => { |
| 135 | + fireEvent.scroll(lastElem, { target: { scrollY: 100 } }); |
| 136 | + }); |
| 137 | + // Check that the spinner is hidden after the API call |
| 138 | + await waitFor(() => { |
| 139 | + expect(screen.queryByLabelText(/loading/i)).not.toBeInTheDocument(); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | +}); |
0 commit comments