Skip to content

Commit

Permalink
Fix unit tests after getDataSourceQuery change
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Wang <[email protected]>
  • Loading branch information
wanglam committed May 29, 2024
1 parent 47e476c commit 344c316
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 30 deletions.
11 changes: 10 additions & 1 deletion public/hooks/__tests__/use_conversations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { useDeleteConversation, usePatchConversation } from '../use_conversations';
import { renderHook, act } from '@testing-library/react-hooks';

import { useCore } from '../../contexts/core_context';
import { HttpHandler } from '../../../../../src/core/public';
import { useDeleteConversation, usePatchConversation } from '../use_conversations';

jest.mock('../../contexts/core_context');
const useCoreMocked = useCore as jest.MockedFunction<typeof useCore>;
Expand Down Expand Up @@ -76,6 +77,10 @@ describe('useDeleteConversation', () => {
deleteConversationPromise = result.current.deleteConversation('foo');
});

await waitFor(() => {
expect(useCoreMocked.mock.results[0].value.services.http.delete).toHaveBeenCalled();
});

let deleteConversationError;
await act(async () => {
result.current.abort();
Expand Down Expand Up @@ -160,6 +165,10 @@ describe('usePatchConversation', () => {
patchConversationPromise = result.current.patchConversation('foo', 'new-title');
});

await waitFor(() => {
expect(useCoreMocked.mock.results[0].value.services.http.put).toHaveBeenCalled();
});

let patchConversationError;
await act(async () => {
result.current.abort();
Expand Down
5 changes: 4 additions & 1 deletion public/services/__tests__/conversation_load_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ describe('ConversationLoadService', () => {
const { conversationLoad, http } = setup();

conversationLoad.load('foo');
expect(conversationLoad.status$.getValue()).toBe('loading');
await waitFor(() => {
expect(http.get).toHaveBeenCalledWith(
'/api/assistant/conversation/foo',
expect.objectContaining({
signal: expect.anything(),
})
);
expect(conversationLoad.status$.getValue()).toBe('loading');
});
});

Expand All @@ -57,6 +57,9 @@ describe('ConversationLoadService', () => {
});
}) as HttpHandler);
const loadResult = conversationLoad.load('foo');
await waitFor(() => {
expect(http.get).toHaveBeenCalled();
});
conversationLoad.abortController?.abort();

await loadResult;
Expand Down
62 changes: 34 additions & 28 deletions public/services/__tests__/conversations_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('ConversationsService', () => {
const { conversations, http } = setup();

conversations.load();
expect(conversations.status$.getValue()).toBe('loading');
await waitFor(() => {
expect(http.get).toHaveBeenCalledWith(
'/api/assistant/conversations',
Expand All @@ -34,12 +35,10 @@ describe('ConversationsService', () => {
})
);
});

expect(conversations.status$.getValue()).toBe('loading');
});

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

expect(conversations.options).toBeFalsy();
conversations.load({
Expand All @@ -50,17 +49,19 @@ describe('ConversationsService', () => {
page: 1,
perPage: 10,
});
expect(http.get).toHaveBeenCalledWith(
'/api/assistant/conversations',
expect.objectContaining({
query: {
page: 1,
perPage: 10,
dataSourceId: dataSource.getDataSourceQuery()?.dataSourceId,
},
signal: expect.anything(),
})
);
await waitFor(() => {
expect(http.get).toHaveBeenCalledWith(
'/api/assistant/conversations',
expect.objectContaining({
query: {
page: 1,
perPage: 10,
dataSourceId: '',
},
signal: expect.anything(),
})
);
});
});

it('should emit latest conversations and "idle" status', async () => {
Expand All @@ -77,7 +78,7 @@ describe('ConversationsService', () => {
});

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

await conversations.load({
page: 1,
Expand All @@ -86,18 +87,20 @@ describe('ConversationsService', () => {
http.get.mockClear();

conversations.reload();
expect(http.get).toHaveBeenCalledTimes(1);
expect(http.get).toHaveBeenCalledWith(
'/api/assistant/conversations',
expect.objectContaining({
query: {
page: 1,
perPage: 10,
dataSourceId: dataSource.getDataSourceQuery()?.dataSourceId,
},
signal: expect.anything(),
})
);
await waitFor(() => {
expect(http.get).toHaveBeenCalledTimes(1);
expect(http.get).toHaveBeenCalledWith(
'/api/assistant/conversations',
expect.objectContaining({
query: {
page: 1,
perPage: 10,
dataSourceId: '',
},
signal: expect.anything(),
})
);
});
});

it('should emit error after loading aborted', async () => {
Expand All @@ -113,6 +116,9 @@ describe('ConversationsService', () => {
});
}) as HttpHandler);
const loadResult = conversations.load();
await waitFor(() => {
expect(http.get).toHaveBeenCalled();
});
conversations.abortController?.abort();

await loadResult;
Expand Down

0 comments on commit 344c316

Please sign in to comment.