Skip to content

Commit

Permalink
update mock context
Browse files Browse the repository at this point in the history
Signed-off-by: tygao <tygao@amazon.com>
  • Loading branch information
raintygao committed May 28, 2024
1 parent 6c622df commit d1f9ce8
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 27 deletions.
4 changes: 3 additions & 1 deletion public/chat_flyout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ export const ChatFlyout = (props: ChatFlyoutProps) => {
const chatContext = useChatContext();
const chatHistoryPageLoadedRef = useRef(false);
const core = useCore();
const defaultDataSourceSelection$ = new BehaviorSubject(new Map());

// TODO: use DataSourceService to replace
const selectedDS = useObservable(
core.services.setupDeps?.dataSourceManagement?.dataSourceSelection?.getSelection$() ??
new BehaviorSubject(new Map())
defaultDataSourceSelection$
);
const currentDS = useMemo(() => {
return selectedDS?.values().next().value;
Expand Down
5 changes: 5 additions & 0 deletions public/contexts/__mocks__/core_context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export const useCore = jest.fn(() => {
load: jest.fn(),
},
conversationLoad: {},
dataSource: {
getDataSourceQuery() {
return { dataSourceId: '' };
},
},
},
};
useCoreMock.services.http.delete.mockReturnValue(Promise.resolve());
Expand Down
25 changes: 2 additions & 23 deletions public/hooks/__tests__/use_conversations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,17 @@ 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 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;
};
jest.mock('../../contexts/core_context');
const useCoreMocked = useCore as jest.MockedFunction<typeof useCore>;

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 @@ -115,8 +96,6 @@ describe('useDeleteConversation', () => {
});

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

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

Expand Down
3 changes: 0 additions & 3 deletions public/services/data_source_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { BehaviorSubject } from 'rxjs';
import type { DataSourceManagementPluginSetup } from '../../../../src/plugins/data_source_management/public/plugin';

export class DataSourceService {
constructor() {}

Expand Down

0 comments on commit d1f9ce8

Please sign in to comment.