Skip to content

Commit

Permalink
test: add unit tests for chat history search list
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Wang <[email protected]>
  • Loading branch information
wanglam committed Dec 11, 2023
1 parent c6d291c commit 52e5ab5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
60 changes: 52 additions & 8 deletions public/tabs/history/__tests__/chat_history_search_list.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@ import { coreMock } from '../../../../../../src/core/public/mocks';
import * as chatContextExports from '../../../contexts/chat_context';
import * as coreContextExports from '../../../contexts/core_context';

import { ChatHistorySearchList } from '../chat_history_search_list';
import { ChatHistorySearchList, ChatHistorySearchListProps } from '../chat_history_search_list';

const setup = () => {
const setup = ({
loading = false,
histories = [{ id: '1', title: 'foo', updatedTimeMs: 0 }],
onSearchChange = jest.fn(),
onLoadChat = jest.fn(),
onRefresh = jest.fn(),
onHistoryDeleted = jest.fn(),
...restProps
}: Partial<ChatHistorySearchListProps> = {}) => {
const useChatContextMock = {
sessionId: '1',
setTitle: jest.fn(),
Expand All @@ -22,18 +30,20 @@ const setup = () => {
services: coreMock.createStart(),
};
useCoreMock.services.http.put.mockImplementation(() => Promise.resolve());
useCoreMock.services.http.delete.mockImplementation(() => Promise.resolve());
jest.spyOn(coreContextExports, 'useCore').mockReturnValue(useCoreMock);
jest.spyOn(chatContextExports, 'useChatContext').mockReturnValue(useChatContextMock);

const renderResult = render(
<I18nProvider>
<ChatHistorySearchList
loading={false}
histories={[{ id: '1', title: 'foo', updatedTimeMs: 0 }]}
onSearchChange={jest.fn()}
onLoadChat={jest.fn()}
onRefresh={jest.fn()}
onHistoryDeleted={jest.fn()}
loading={loading}
histories={histories}
onSearchChange={onSearchChange}
onLoadChat={onLoadChat}
onRefresh={onRefresh}
onHistoryDeleted={onHistoryDeleted}
{...restProps}
/>
</I18nProvider>
);
Expand Down Expand Up @@ -68,4 +78,38 @@ describe('<ChatHistorySearchList />', () => {
expect(useChatContextMock.setTitle).toHaveBeenLastCalledWith('bar');
});
});

it('should call onRefresh and onHistoryDeleted after conversation deleted', async () => {
const onRefreshMock = jest.fn();
const onHistoryDeletedMock = jest.fn();

const { renderResult } = setup({
onRefresh: onRefreshMock,
onHistoryDeleted: onHistoryDeletedMock,
});

act(() => {
fireEvent.click(renderResult.getByLabelText('Delete conversation'));
});

expect(onRefreshMock).not.toHaveBeenCalled();
expect(onHistoryDeletedMock).not.toHaveBeenCalled();

await waitFor(async () => {
fireEvent.click(renderResult.getByTestId('confirmModalConfirmButton'));
});

await waitFor(async () => {
expect(onRefreshMock).toHaveBeenCalled();
expect(onHistoryDeletedMock).toHaveBeenCalledWith('1');
});
});

it('should display empty panel', () => {
const { renderResult } = setup({
histories: [],
});

expect(renderResult.getByText('There were no results found.')).toBeInTheDocument();
});
});
2 changes: 1 addition & 1 deletion public/tabs/history/chat_history_search_list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { EditConversationNameModal } from '../../components/edit_conversation_na
import { DeleteConversationConfirmModal } from './delete_conversation_confirm_modal';
import { useChatContext } from '../../contexts';

interface ChatHistorySearchListProps
export interface ChatHistorySearchListProps
extends Pick<
EuiTablePaginationProps,
'activePage' | 'itemsPerPage' | 'onChangeItemsPerPage' | 'onChangePage' | 'pageCount'
Expand Down

0 comments on commit 52e5ab5

Please sign in to comment.