Skip to content

Commit

Permalink
test: update tests
Browse files Browse the repository at this point in the history
Signed-off-by: tygao <[email protected]>
  • Loading branch information
raintygao committed May 27, 2024
1 parent 6fe9a0f commit 6c622df
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 88 deletions.
25 changes: 23 additions & 2 deletions public/hooks/__tests__/use_conversations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,36 @@ import { useDeleteConversation, usePatchConversation } from '../use_conversation
import { renderHook, act } from '@testing-library/react-hooks';
import { useCore } from '../../contexts/core_context';
import { HttpHandler } from '../../../../../src/core/public';

import { DataSourceServiceMock } from '../../services/data_source_service.mock';
import { httpServiceMock } from '../../../../../src/core/public/mocks';
jest.mock('../../contexts/core_context');
const useCoreMocked = useCore as jest.MockedFunction<typeof useCore>;

const setup = () => {
const useCoreMocked = useCore as jest.MockedFunction<typeof useCore>;
const mockedDataSource = new DataSourceServiceMock();
const mockedHttp = httpServiceMock.createStartContract();
mockedHttp.delete = jest.fn(() => Promise.resolve());
mockedHttp.put = jest.fn(() => Promise.resolve());

useCoreMocked.mockReturnValue({
services: {
dataSource: mockedDataSource,
http: mockedHttp,
},
});

return useCoreMocked;
};

describe('useDeleteConversation', () => {
const useCoreMocked = setup();
it('should call delete with path and signal', async () => {
const { result } = renderHook(() => useDeleteConversation());

await act(async () => {
await result.current.deleteConversation('foo');
});

expect(useCoreMocked.mock.results[0].value.services.http.delete).toHaveBeenCalledWith(
'/api/assistant/conversation/foo',
expect.objectContaining({
Expand Down Expand Up @@ -96,6 +115,8 @@ describe('useDeleteConversation', () => {
});

describe('usePatchConversation', () => {
const useCoreMocked = setup();

it('should call put with path, query and signal', async () => {
const { result } = renderHook(() => usePatchConversation());

Expand Down
12 changes: 10 additions & 2 deletions public/hooks/use_chat_actions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ConversationLoadService } from '../services/conversation_load_service';
import * as chatStateHookExports from './use_chat_state';
import { ASSISTANT_API } from '../../common/constants/llm';
import { IMessage } from 'common/types/chat_saved_object_attributes';
import { DataSourceServiceMock } from '../services/data_source_service.mock';

jest.mock('../services/conversations_service', () => {
return {
Expand Down Expand Up @@ -60,6 +61,7 @@ describe('useChatActions hook', () => {
const setSelectedTabIdMock = jest.fn();
const pplVisualizationRenderMock = jest.fn();
const setInteractionIdMock = jest.fn();
const dataSourceServiceMock = new DataSourceServiceMock();

const chatContextMock: chatContextHookExports.IChatContext = {
selectedTabId: 'chat',
Expand Down Expand Up @@ -88,8 +90,9 @@ describe('useChatActions hook', () => {
jest.spyOn(coreHookExports, 'useCore').mockReturnValue({
services: {
http: httpMock,
conversations: new ConversationsService(httpMock),
conversationLoad: new ConversationLoadService(httpMock),
conversations: new ConversationsService(httpMock, dataSourceServiceMock),
conversationLoad: new ConversationLoadService(httpMock, dataSourceServiceMock),
dataSource: dataSourceServiceMock,
},
});

Expand Down Expand Up @@ -125,6 +128,7 @@ describe('useChatActions hook', () => {
messages: [SEND_MESSAGE_RESPONSE.messages[0]],
input: INPUT_MESSAGE,
}),
query: dataSourceServiceMock.getDataSourceQuery(),
});

// it should send dispatch `receive` action to remove the message without messageId
Expand Down Expand Up @@ -199,6 +203,7 @@ describe('useChatActions hook', () => {
messages: [],
input: { type: 'input', content: 'message that send as input', contentType: 'text' },
}),
query: dataSourceServiceMock.getDataSourceQuery(),
});
});

Expand Down Expand Up @@ -261,6 +266,7 @@ describe('useChatActions hook', () => {
expect(chatStateDispatchMock).toHaveBeenCalledWith({ type: 'abort' });
expect(httpMock.post).toHaveBeenCalledWith(ASSISTANT_API.ABORT_AGENT_EXECUTION, {
body: JSON.stringify({ conversationId: 'conversation_id_to_abort' }),
query: dataSourceServiceMock.getDataSourceQuery(),
});
});

Expand Down Expand Up @@ -288,6 +294,7 @@ describe('useChatActions hook', () => {
conversationId: 'conversation_id_mock',
interactionId: 'interaction_id_mock',
}),
query: dataSourceServiceMock.getDataSourceQuery(),
});
expect(chatStateDispatchMock).toHaveBeenCalledWith(
expect.objectContaining({ type: 'receive', payload: { messages: [], interactions: [] } })
Expand Down Expand Up @@ -323,6 +330,7 @@ describe('useChatActions hook', () => {
conversationId: 'conversation_id_mock',
interactionId: 'interaction_id_mock',
}),
query: dataSourceServiceMock.getDataSourceQuery(),
});
expect(chatStateDispatchMock).not.toHaveBeenCalledWith(
expect.objectContaining({ type: 'receive' })
Expand Down
6 changes: 5 additions & 1 deletion public/hooks/use_feed_back.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import { httpServiceMock } from '../../../../src/core/public/mocks';
import * as chatContextHookExports from '../contexts/chat_context';
import { Interaction, IOutput, IMessage } from '../../common/types/chat_saved_object_attributes';
import { ASSISTANT_API } from '../../common/constants/llm';
import { DataSourceServiceMock } from '../services/data_source_service.mock';

describe('useFeedback hook', () => {
const httpMock = httpServiceMock.createStartContract();
const chatStateDispatchMock = jest.fn();

const dataSourceMock = new DataSourceServiceMock();
const chatContextMock = {
rootAgentId: 'root_agent_id_mock',
selectedTabId: 'chat',
Expand All @@ -29,6 +30,7 @@ describe('useFeedback hook', () => {
jest.spyOn(coreHookExports, 'useCore').mockReturnValue({
services: {
http: httpMock,
dataSource: dataSourceMock,
},
});
jest.spyOn(chatContextHookExports, 'useChatContext').mockReturnValue(chatContextMock);
Expand Down Expand Up @@ -82,6 +84,7 @@ describe('useFeedback hook', () => {
body: JSON.stringify({
satisfaction: true,
}),
query: dataSourceMock.getDataSourceQuery(),
}
);
expect(result.current.feedbackResult).toBe(true);
Expand Down Expand Up @@ -116,6 +119,7 @@ describe('useFeedback hook', () => {
body: JSON.stringify({
satisfaction: true,
}),
query: dataSourceMock.getDataSourceQuery(),
}
);
expect(result.current.feedbackResult).toBe(undefined);
Expand Down
3 changes: 2 additions & 1 deletion public/services/__tests__/conversation_load_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
import { HttpHandler } from '../../../../../src/core/public';
import { httpServiceMock } from '../../../../../src/core/public/mocks';
import { ConversationLoadService } from '../conversation_load_service';
import { DataSourceServiceMock } from '../data_source_service.mock';

const setup = () => {
const http = httpServiceMock.createSetupContract();
const conversationLoad = new ConversationLoadService(http);
const conversationLoad = new ConversationLoadService(http, new DataSourceServiceMock());

return {
conversationLoad,
Expand Down
11 changes: 8 additions & 3 deletions public/services/__tests__/conversations_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
import { HttpHandler } from '../../../../../src/core/public';
import { httpServiceMock } from '../../../../../src/core/public/mocks';
import { ConversationsService } from '../conversations_service';
import { DataSourceServiceMock } from '../data_source_service.mock';

const setup = () => {
const http = httpServiceMock.createSetupContract();
const conversations = new ConversationsService(http);
const dataSourceServiceMock = new DataSourceServiceMock();
const conversations = new ConversationsService(http, dataSourceServiceMock);

return {
conversations,
http,
dataSource: dataSourceServiceMock,
};
};

Expand All @@ -32,7 +35,7 @@ describe('ConversationsService', () => {
});

it('should update options property and call get with passed query', () => {
const { conversations, http } = setup();
const { conversations, http, dataSource } = setup();

expect(conversations.options).toBeFalsy();
conversations.load({
Expand All @@ -49,6 +52,7 @@ describe('ConversationsService', () => {
query: {
page: 1,
perPage: 10,
dataSourceId: dataSource.getDataSourceQuery()?.dataSourceId,
},
signal: expect.anything(),
})
Expand All @@ -69,7 +73,7 @@ describe('ConversationsService', () => {
});

it('should call get with same query again after reload called', async () => {
const { conversations, http } = setup();
const { conversations, http, dataSource } = setup();

await conversations.load({
page: 1,
Expand All @@ -85,6 +89,7 @@ describe('ConversationsService', () => {
query: {
page: 1,
perPage: 10,
dataSourceId: dataSource.getDataSourceQuery()?.dataSourceId,
},
signal: expect.anything(),
})
Expand Down
12 changes: 12 additions & 0 deletions public/services/data_source_service.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

export class DataSourceServiceMock {
constructor() {}

getDataSourceQuery() {
return { dataSourceId: '' };
}
}
18 changes: 1 addition & 17 deletions public/services/data_source_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,10 @@ import { BehaviorSubject } from 'rxjs';
import type { DataSourceManagementPluginSetup } from '../../../../src/plugins/data_source_management/public/plugin';

export class DataSourceService {
dataSourceId$ = new BehaviorSubject<string | null>(null);
private dataSourceManagement: DataSourceManagementPluginSetup | undefined | null = null;

constructor() {}

getDataSourceQuery() {
if (!this.dataSourceManagement) {
return {};
}
// TODO: use new handle logic to update
const dataSourceId = this.dataSourceManagement.dataSourceSelection
.getSelectionValue()
?.values()
.next().value;
if (dataSourceId === null) {
throw new Error('No data source id');
}
if (dataSourceId === '') {
return {};
}
return { dataSourceId };
return { dataSourceId: '' };
}
}
3 changes: 2 additions & 1 deletion public/tabs/history/__tests__/chat_history_page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as useChatStateExports from '../../../hooks/use_chat_state';
import * as chatContextExports from '../../../contexts/chat_context';
import * as coreContextExports from '../../../contexts/core_context';
import { ConversationsService } from '../../../services/conversations_service';
import { DataSourceServiceMock } from '../../../services/data_source_service.mock';

import { ChatHistoryPage } from '../chat_history_page';

Expand Down Expand Up @@ -42,7 +43,7 @@ const setup = ({
services: {
...coreMock.createStart(),
http,
conversations: new ConversationsService(http),
conversations: new ConversationsService(http, new DataSourceServiceMock()),
conversationLoad: {},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as chatContextExports from '../../../contexts/chat_context';
import * as coreContextExports from '../../../contexts/core_context';

import { ChatHistorySearchList, ChatHistorySearchListProps } from '../chat_history_search_list';
import { DataSourceServiceMock } from '../../../services/data_source_service.mock';

const setup = ({
loading = false,
Expand All @@ -26,8 +27,9 @@ const setup = ({
conversationId: '1',
setTitle: jest.fn(),
};
const dataSourceServiceMock = new DataSourceServiceMock();
const useCoreMock = {
services: coreMock.createStart(),
services: { ...coreMock.createStart(), dataSource: dataSourceServiceMock },
};
useCoreMock.services.http.put.mockImplementation(() => Promise.resolve());
useCoreMock.services.http.delete.mockImplementation(() => Promise.resolve());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ import {
DeleteConversationConfirmModalProps,
} from '../delete_conversation_confirm_modal';
import { HttpHandler } from '../../../../../../src/core/public';
import { DataSourceServiceMock } from '../../../services/data_source_service.mock';

const setup = ({ onClose, conversationId }: DeleteConversationConfirmModalProps) => {
const dataSourceServiceMock = new DataSourceServiceMock();

const useCoreMock = {
services: coreMock.createStart(),
services: { ...coreMock.createStart(), dataSource: dataSourceServiceMock },
};
jest.spyOn(coreContextExports, 'useCore').mockReturnValue(useCoreMock);

Expand Down
Loading

0 comments on commit 6c622df

Please sign in to comment.