Skip to content

Commit

Permalink
test fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
stephmilovic committed Oct 18, 2024
1 parent 235ce61 commit 8d07e14
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,28 @@ import { defaultAssistantFeatures } from '@kbn/elastic-assistant-common';
const http = {
fetch: jest.fn().mockResolvedValue(defaultAssistantFeatures),
};
const onFetch = jest.fn();
const mockData = {
welcome_id: {
id: 'welcome_id',
title: 'Welcome',
category: 'assistant',
messages: [],
apiConfig: { connectorId: '123' },
replacements: {},
},
electric_sheep_id: {
id: 'electric_sheep_id',
category: 'assistant',
title: 'electric sheep',
messages: [],
apiConfig: { connectorId: '123' },
replacements: {},
},
};

const defaultProps = {
http,
onFetch,
baseConversations: {},
isAssistantEnabled: true,
} as unknown as UseFetchCurrentUserConversationsParams;

Expand All @@ -36,14 +53,20 @@ const createWrapper = () => {
};

describe('useFetchCurrentUserConversations', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it(`should make http request to fetch conversations`, async () => {
renderHook(() => useFetchCurrentUserConversations(defaultProps), {
wrapper: createWrapper(),
});

await act(async () => {
const { waitForNextUpdate } = renderHook(() =>
useFetchCurrentUserConversations(defaultProps)
const { result, waitForNextUpdate } = renderHook(
() => useFetchCurrentUserConversations(defaultProps),
{
wrapper: createWrapper(),
}
);
await waitForNextUpdate();
expect(defaultProps.http.fetch).toHaveBeenCalledWith(
Expand All @@ -52,14 +75,47 @@ describe('useFetchCurrentUserConversations', () => {
method: 'GET',
query: {
page: 1,
perPage: 100,
per_page: 5000,
fields: ['title', 'is_default', 'updated_at', 'api_config'],
sort_field: 'is_default',
sort_order: 'desc',
},
version: '2023-10-31',
signal: undefined,
}
);
expect(result.current.data).toEqual({});
});
});
it(`Combines baseConversations with result`, async () => {
renderHook(() => useFetchCurrentUserConversations(defaultProps), {
wrapper: createWrapper(),
});

expect(onFetch).toHaveBeenCalled();
await act(async () => {
const { result, waitForNextUpdate } = renderHook(
() => useFetchCurrentUserConversations({ ...defaultProps, baseConversations: mockData }),
{
wrapper: createWrapper(),
}
);
await waitForNextUpdate();
expect(defaultProps.http.fetch).toHaveBeenCalledWith(
'/api/security_ai_assistant/current_user/conversations/_find',
{
method: 'GET',
query: {
page: 1,
per_page: 5000,
fields: ['title', 'is_default', 'updated_at', 'api_config'],
sort_field: 'is_default',
sort_order: 'desc',
},
version: '2023-10-31',
signal: undefined,
}
);
expect(result.current.data).toEqual(mockData);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ describe('Assistant', () => {

describe('when selected conversation changes and some connectors are loaded', () => {
it('should persist the conversation id to local storage', async () => {
const getConversation = jest.fn().mockResolvedValue(mockData.electric_sheep_id);
const getConversation = jest
.fn()
.mockResolvedValueOnce(mockData.welcome_id)
.mockResolvedValue(mockData.electric_sheep_id);
(useConversation as jest.Mock).mockReturnValue({
...mockUseConversation,
getConversation,
Expand All @@ -167,7 +170,10 @@ describe('Assistant', () => {
...mockData.electric_sheep_id,
excludeFromLastConversationStorage: true,
};
const getConversation = jest.fn().mockResolvedValue(conversation);
const getConversation = jest
.fn()
.mockResolvedValueOnce(mockData.welcome_id)
.mockResolvedValue(conversation);
(useConversation as jest.Mock).mockReturnValue({
...mockUseConversation,
getConversation,
Expand Down

0 comments on commit 8d07e14

Please sign in to comment.