diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/anonymization_fields/use_fetch_anonymization_fields.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/anonymization_fields/use_fetch_anonymization_fields.test.tsx index 5352e60bcea45..35bf94bc1d1b3 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/anonymization_fields/use_fetch_anonymization_fields.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/anonymization_fields/use_fetch_anonymization_fields.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { act, renderHook } from '@testing-library/react-hooks'; +import { waitFor, renderHook } from '@testing-library/react'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import type { ReactNode } from 'react'; @@ -41,9 +41,7 @@ describe('useFetchAnonymizationFields', () => { wrapper: createWrapper(), }); - await act(async () => { - const { waitForNextUpdate } = renderHook(() => useFetchAnonymizationFields()); - await waitForNextUpdate(); + await waitFor(() => { expect(http.fetch).toHaveBeenCalledWith( '/api/security_ai_assistant/anonymization_fields/_find', { diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/capabilities/use_capabilities.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/capabilities/use_capabilities.test.tsx index 6101782ae43b1..cc5df8d0a44b7 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/capabilities/use_capabilities.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/capabilities/use_capabilities.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import type { ReactNode } from 'react'; diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/conversations/conversations.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/conversations/conversations.test.tsx index 7e87ca79e88ab..50ae344a78671 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/conversations/conversations.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/conversations/conversations.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { act, renderHook } from '@testing-library/react-hooks'; +import { act, waitFor, renderHook } from '@testing-library/react'; import { DeleteConversationParams, @@ -32,18 +32,18 @@ describe('conversations api', () => { await act(async () => { const deleteProps = { http, toasts, id: 'test' } as unknown as DeleteConversationParams; - const { waitForNextUpdate } = renderHook(() => deleteConversation(deleteProps)); - await waitForNextUpdate(); - - expect(deleteProps.http.fetch).toHaveBeenCalledWith( - '/api/security_ai_assistant/current_user/conversations/test', - { - method: 'DELETE', - signal: undefined, - version: '2023-10-31', - } - ); - expect(toasts.addError).not.toHaveBeenCalled(); + renderHook(() => deleteConversation(deleteProps)); + await waitFor(() => { + expect(deleteProps.http.fetch).toHaveBeenCalledWith( + '/api/security_ai_assistant/current_user/conversations/test', + { + method: 'DELETE', + signal: undefined, + version: '2023-10-31', + } + ); + expect(toasts.addError).not.toHaveBeenCalled(); + }); }); }); @@ -58,18 +58,18 @@ describe('conversations api', () => { it('should call api to get conversation', async () => { await act(async () => { const getProps = { http, toasts, id: 'test' } as unknown as GetConversationByIdParams; - const { waitForNextUpdate } = renderHook(() => getConversationById(getProps)); - await waitForNextUpdate(); - - expect(getProps.http.fetch).toHaveBeenCalledWith( - '/api/security_ai_assistant/current_user/conversations/test', - { - method: 'GET', - signal: undefined, - version: '2023-10-31', - } - ); - expect(toasts.addError).not.toHaveBeenCalled(); + renderHook(() => getConversationById(getProps)); + await waitFor(() => { + expect(getProps.http.fetch).toHaveBeenCalledWith( + '/api/security_ai_assistant/current_user/conversations/test', + { + method: 'GET', + signal: undefined, + version: '2023-10-31', + } + ); + expect(toasts.addError).not.toHaveBeenCalled(); + }); }); }); diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/conversations/use_fetch_current_user_conversations.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/conversations/use_fetch_current_user_conversations.test.tsx index f10c7d07a35d6..cfe67c60324cc 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/conversations/use_fetch_current_user_conversations.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/conversations/use_fetch_current_user_conversations.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { act, renderHook } from '@testing-library/react-hooks'; +import { waitFor, renderHook } from '@testing-library/react'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import type { ReactNode } from 'react'; @@ -41,11 +41,7 @@ describe('useFetchCurrentUserConversations', () => { wrapper: createWrapper(), }); - await act(async () => { - const { waitForNextUpdate } = renderHook(() => - useFetchCurrentUserConversations(defaultProps) - ); - await waitForNextUpdate(); + await waitFor(() => { expect(defaultProps.http.fetch).toHaveBeenCalledWith( '/api/security_ai_assistant/current_user/conversations/_find', { diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/evaluate/use_perform_evaluation.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/evaluate/use_perform_evaluation.test.tsx index 6d1296fc9aa64..913dbaed3263b 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/evaluate/use_perform_evaluation.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/evaluate/use_perform_evaluation.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { act, renderHook } from '@testing-library/react-hooks'; +import { waitFor, renderHook } from '@testing-library/react'; import { usePerformEvaluation, UsePerformEvaluationParams } from './use_perform_evaluation'; import { postEvaluation as _postEvaluation } from './evaluate'; import { useMutation as _useMutation } from '@tanstack/react-query'; @@ -50,10 +50,8 @@ describe('usePerformEvaluation', () => { jest.clearAllMocks(); }); it('should call api with undefined evalParams', async () => { - await act(async () => { - const { waitForNextUpdate } = renderHook(() => usePerformEvaluation(defaultProps)); - await waitForNextUpdate(); - + renderHook(() => usePerformEvaluation(defaultProps)); + await waitFor(() => { expect(defaultProps.http.post).toHaveBeenCalledWith('/internal/elastic_assistant/evaluate', { body: undefined, headers: { @@ -80,10 +78,9 @@ describe('usePerformEvaluation', () => { opts.onError(e); } }); - await act(async () => { - const { waitForNextUpdate } = renderHook(() => usePerformEvaluation(defaultProps)); - await waitForNextUpdate(); + renderHook(() => usePerformEvaluation(defaultProps)); + await waitFor(() => expect(defaultProps.http.post).toHaveBeenCalledWith('/internal/elastic_assistant/evaluate', { body: '{"graphs":["d","c"],"datasetName":"kewl","connectorIds":["h","g"],"runName":"test run"}', headers: { @@ -91,26 +88,19 @@ describe('usePerformEvaluation', () => { }, signal: undefined, version: API_VERSIONS.internal.v1, - }); - }); + }) + ); }); it('should return evaluation response', async () => { - await act(async () => { - const { result, waitForNextUpdate } = renderHook(() => usePerformEvaluation(defaultProps)); - await waitForNextUpdate(); - - await expect(result.current).resolves.toStrictEqual(statusResponse); - }); + const { result } = renderHook(() => usePerformEvaluation(defaultProps)); + await waitFor(() => expect(result.current).resolves.toStrictEqual(statusResponse)); }); it('should display error toast when api throws error', async () => { postEvaluationMock.mockRejectedValue(new Error('this is an error')); - await act(async () => { - const { waitForNextUpdate } = renderHook(() => usePerformEvaluation(defaultProps)); - await waitForNextUpdate(); - expect(toasts.addError).toHaveBeenCalled(); - }); + renderHook(() => usePerformEvaluation(defaultProps)); + await waitFor(() => expect(toasts.addError).toHaveBeenCalled()); }); }); diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/knowledge_base/use_knowledge_base_indices.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/knowledge_base/use_knowledge_base_indices.test.tsx index 4f258aa3c1964..a77ab1d63aa6e 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/knowledge_base/use_knowledge_base_indices.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/knowledge_base/use_knowledge_base_indices.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { act, renderHook } from '@testing-library/react-hooks'; +import { waitFor, renderHook } from '@testing-library/react'; import { useKnowledgeBaseIndices, UseKnowledgeBaseIndicesParams, @@ -48,10 +48,8 @@ describe('useKnowledgeBaseIndices', () => { }); it('should call api to get knowledge base indices', async () => { - await act(async () => { - const { waitForNextUpdate } = renderHook(() => useKnowledgeBaseIndices(defaultProps)); - await waitForNextUpdate(); - + renderHook(() => useKnowledgeBaseIndices(defaultProps)); + await waitFor(() => { expect(defaultProps.http.fetch).toHaveBeenCalledWith( '/internal/elastic_assistant/knowledge_base/_indices', { @@ -65,20 +63,17 @@ describe('useKnowledgeBaseIndices', () => { }); it('should return indices response', async () => { - await act(async () => { - const { result, waitForNextUpdate } = renderHook(() => useKnowledgeBaseIndices(defaultProps)); - await waitForNextUpdate(); - - await expect(result.current).resolves.toStrictEqual(indicesResponse); + const { result } = renderHook(() => useKnowledgeBaseIndices(defaultProps)); + await waitFor(() => { + expect(result.current).resolves.toStrictEqual(indicesResponse); }); }); it('should display error toast when api throws error', async () => { getKnowledgeBaseIndicesMock.mockRejectedValue(new Error('this is an error')); - await act(async () => { - const { waitForNextUpdate } = renderHook(() => useKnowledgeBaseIndices(defaultProps)); - await waitForNextUpdate(); + renderHook(() => useKnowledgeBaseIndices(defaultProps)); + await waitFor(() => { expect(toasts.addError).toHaveBeenCalled(); }); }); diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/knowledge_base/use_knowledge_base_status.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/knowledge_base/use_knowledge_base_status.test.tsx index 83073b5770ba0..69fccba092ee9 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/knowledge_base/use_knowledge_base_status.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/knowledge_base/use_knowledge_base_status.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { act, renderHook } from '@testing-library/react-hooks'; +import { waitFor, renderHook } from '@testing-library/react'; import { useKnowledgeBaseStatus, UseKnowledgeBaseStatusParams } from './use_knowledge_base_status'; import { getKnowledgeBaseStatus as _getKnowledgeBaseStatus } from './api'; @@ -49,10 +49,8 @@ describe('useKnowledgeBaseStatus', () => { jest.clearAllMocks(); }); it('should call api to get knowledge base status without resource arg', async () => { - await act(async () => { - const { waitForNextUpdate } = renderHook(() => useKnowledgeBaseStatus(defaultProps)); - await waitForNextUpdate(); - + renderHook(() => useKnowledgeBaseStatus(defaultProps)); + await waitFor(() => { expect(defaultProps.http.fetch).toHaveBeenCalledWith( '/internal/elastic_assistant/knowledge_base/', { @@ -65,12 +63,8 @@ describe('useKnowledgeBaseStatus', () => { }); }); it('should call api to get knowledge base status with resource arg', async () => { - await act(async () => { - const { waitForNextUpdate } = renderHook(() => - useKnowledgeBaseStatus({ ...defaultProps, resource: 'something' }) - ); - await waitForNextUpdate(); - + renderHook(() => useKnowledgeBaseStatus({ ...defaultProps, resource: 'something' })); + await waitFor(() => expect(defaultProps.http.fetch).toHaveBeenCalledWith( '/internal/elastic_assistant/knowledge_base/something', { @@ -78,26 +72,18 @@ describe('useKnowledgeBaseStatus', () => { signal: undefined, version: '1', } - ); - }); + ) + ); }); it('should return status response', async () => { - await act(async () => { - const { result, waitForNextUpdate } = renderHook(() => useKnowledgeBaseStatus(defaultProps)); - await waitForNextUpdate(); - - await expect(result.current).resolves.toStrictEqual(statusResponse); - }); + const { result } = renderHook(() => useKnowledgeBaseStatus(defaultProps)); + await waitFor(() => expect(result.current).resolves.toStrictEqual(statusResponse)); }); it('should display error toast when api throws error', async () => { getKnowledgeBaseStatusMock.mockRejectedValue(new Error('this is an error')); - await act(async () => { - const { waitForNextUpdate } = renderHook(() => useKnowledgeBaseStatus(defaultProps)); - await waitForNextUpdate(); - - expect(toasts.addError).toHaveBeenCalled(); - }); + renderHook(() => useKnowledgeBaseStatus(defaultProps)); + await waitFor(() => expect(toasts.addError).toHaveBeenCalled()); }); }); diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/knowledge_base/use_setup_knowledge_base.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/knowledge_base/use_setup_knowledge_base.test.tsx index c08f6f93617fc..802ddf74bdf72 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/knowledge_base/use_setup_knowledge_base.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/knowledge_base/use_setup_knowledge_base.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { act, renderHook } from '@testing-library/react-hooks'; +import { waitFor, renderHook } from '@testing-library/react'; import { useSetupKnowledgeBase, UseSetupKnowledgeBaseParams } from './use_setup_knowledge_base'; import { postKnowledgeBase as _postKnowledgeBase } from './api'; import { useMutation as _useMutation } from '@tanstack/react-query'; @@ -50,10 +50,8 @@ describe('useSetupKnowledgeBase', () => { jest.clearAllMocks(); }); it('should call api to post knowledge base setup', async () => { - await act(async () => { - const { waitForNextUpdate } = renderHook(() => useSetupKnowledgeBase(defaultProps)); - await waitForNextUpdate(); - + renderHook(() => useSetupKnowledgeBase(defaultProps)); + await waitFor(() => { expect(defaultProps.http.fetch).toHaveBeenCalledWith( '/internal/elastic_assistant/knowledge_base/', { @@ -73,36 +71,27 @@ describe('useSetupKnowledgeBase', () => { opts.onError(e); } }); - await act(async () => { - const { waitForNextUpdate } = renderHook(() => useSetupKnowledgeBase(defaultProps)); - await waitForNextUpdate(); + renderHook(() => useSetupKnowledgeBase(defaultProps)); + await waitFor(() => expect(defaultProps.http.fetch).toHaveBeenCalledWith( '/internal/elastic_assistant/knowledge_base/something', { method: 'POST', version: '1', } - ); - }); + ) + ); }); it('should return setup response', async () => { - await act(async () => { - const { result, waitForNextUpdate } = renderHook(() => useSetupKnowledgeBase(defaultProps)); - await waitForNextUpdate(); - - await expect(result.current).resolves.toStrictEqual(statusResponse); - }); + const { result } = renderHook(() => useSetupKnowledgeBase(defaultProps)); + await waitFor(() => expect(result.current).resolves.toStrictEqual(statusResponse)); }); it('should display error toast when api throws error', async () => { postKnowledgeBaseMock.mockRejectedValue(new Error('this is an error')); - await act(async () => { - const { waitForNextUpdate } = renderHook(() => useSetupKnowledgeBase(defaultProps)); - await waitForNextUpdate(); - - expect(toasts.addError).toHaveBeenCalled(); - }); + renderHook(() => useSetupKnowledgeBase(defaultProps)); + await waitFor(() => expect(toasts.addError).toHaveBeenCalled()); }); }); diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/prompts/use_fetch_prompts.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/prompts/use_fetch_prompts.test.tsx index 8e228d2787a36..8fb1f808f8df5 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/prompts/use_fetch_prompts.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/api/prompts/use_fetch_prompts.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { act, renderHook } from '@testing-library/react-hooks'; +import { waitFor, renderHook } from '@testing-library/react'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import type { ReactNode } from 'react'; @@ -41,9 +41,7 @@ describe('useFetchPrompts', () => { wrapper: createWrapper(), }); - await act(async () => { - const { waitForNextUpdate } = renderHook(() => useFetchPrompts()); - await waitForNextUpdate(); + await waitFor(() => { expect(http.fetch).toHaveBeenCalledWith('/api/security_ai_assistant/prompts/_find', { method: 'GET', query: { diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/chat_send/use_chat_send.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant/chat_send/use_chat_send.test.tsx index f72f85892d379..54fc610d405de 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/chat_send/use_chat_send.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/chat_send/use_chat_send.test.tsx @@ -10,8 +10,7 @@ import { useSendMessage } from '../use_send_message'; import { useConversation } from '../use_conversation'; import { emptyWelcomeConvo, welcomeConvo } from '../../mock/conversation'; import { useChatSend, UseChatSendProps } from './use_chat_send'; -import { act, renderHook } from '@testing-library/react-hooks'; -import { waitFor } from '@testing-library/react'; +import { waitFor, renderHook, act } from '@testing-library/react'; import { TestProviders } from '../../mock/test_providers/test_providers'; import { useAssistantContext } from '../../..'; @@ -64,10 +63,10 @@ describe('use chat send', () => { }); it('handleOnChatCleared clears the conversation', async () => { (clearConversation as jest.Mock).mockReturnValueOnce(testProps.currentConversation); - const { result, waitForNextUpdate } = renderHook(() => useChatSend(testProps), { + const { result } = renderHook(() => useChatSend(testProps), { wrapper: TestProviders, }); - await waitForNextUpdate(); + await waitFor(() => new Promise((resolve) => resolve(null))); act(() => { result.current.handleOnChatCleared(); }); @@ -99,7 +98,7 @@ describe('use chat send', () => { }); }); it('handleRegenerateResponse removes the last message of the conversation, resends the convo to GenAI, and appends the message received', async () => { - const { result, waitForNextUpdate } = renderHook( + const { result } = renderHook( () => useChatSend({ ...testProps, currentConversation: { ...welcomeConvo, id: 'welcome-id' } }), { @@ -107,7 +106,7 @@ describe('use chat send', () => { } ); - await waitForNextUpdate(); + await waitFor(() => new Promise((resolve) => resolve(null))); act(() => { result.current.handleRegenerateResponse(); }); @@ -121,10 +120,10 @@ describe('use chat send', () => { }); it('sends telemetry events for both user and assistant', async () => { const promptText = 'prompt text'; - const { result, waitForNextUpdate } = renderHook(() => useChatSend(testProps), { + const { result } = renderHook(() => useChatSend(testProps), { wrapper: TestProviders, }); - await waitForNextUpdate(); + await waitFor(() => new Promise((resolve) => resolve(null))); act(() => { result.current.handleChatSend(promptText); }); diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/conversations/conversation_settings/use_conveersation_changed.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant/conversations/conversation_settings/use_conveersation_changed.test.tsx index 091c691d8e324..0e088533ba1d8 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/conversations/conversation_settings/use_conveersation_changed.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/conversations/conversation_settings/use_conveersation_changed.test.tsx @@ -4,7 +4,8 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import { renderHook, act } from '@testing-library/react-hooks'; + +import { renderHook, act } from '@testing-library/react'; import { useConversationChanged } from './use_conversation_changed'; import { customConvo } from '../../../mock/conversation'; import { mockConnectors } from '../../../mock/connectors'; diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/conversations/conversation_settings/use_conversation_deletex.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant/conversations/conversation_settings/use_conversation_deletex.test.tsx index f96de69b8ae7c..5210d76d05791 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/conversations/conversation_settings/use_conversation_deletex.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/conversations/conversation_settings/use_conversation_deletex.test.tsx @@ -4,7 +4,8 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import { renderHook, act } from '@testing-library/react-hooks'; + +import { renderHook, act } from '@testing-library/react'; import { useConversationDeleted } from './use_conversation_deleted'; import { customConvo, alertConvo, welcomeConvo } from '../../../mock/conversation'; import { Conversation, ConversationsBulkActions } from '../../../..'; diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/conversations/conversation_settings_management/use_conversations_table.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant/conversations/conversation_settings_management/use_conversations_table.test.tsx index 797bde3466223..5a98d86b1b4a2 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/conversations/conversation_settings_management/use_conversations_table.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/conversations/conversation_settings_management/use_conversations_table.test.tsx @@ -4,7 +4,8 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; + +import { renderHook } from '@testing-library/react'; import { useConversationsTable, GetConversationsListParams, diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/prompt_editor/system_prompt/system_prompt_modal/use_system_prompt_editor.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant/prompt_editor/system_prompt/system_prompt_modal/use_system_prompt_editor.test.tsx index 009ee6c5a83cd..507c6773dd398 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/prompt_editor/system_prompt/system_prompt_modal/use_system_prompt_editor.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/prompt_editor/system_prompt/system_prompt_modal/use_system_prompt_editor.test.tsx @@ -4,7 +4,8 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import { renderHook, act } from '@testing-library/react-hooks'; + +import { renderHook, act } from '@testing-library/react'; import { useSystemPromptEditor } from './use_system_prompt_editor'; import { mockSystemPrompt, diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/prompt_editor/system_prompt/system_prompt_settings_management/use_system_prompt_table.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant/prompt_editor/system_prompt/system_prompt_settings_management/use_system_prompt_table.test.tsx index 220e150ec8647..4fa17c80e5925 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/prompt_editor/system_prompt/system_prompt_settings_management/use_system_prompt_table.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/prompt_editor/system_prompt/system_prompt_settings_management/use_system_prompt_table.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { useSystemPromptTable } from './use_system_prompt_table'; import { Conversation } from '../../../../assistant_context/types'; import { AIConnector } from '../../../../connectorland/connector_selector'; diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/quick_prompts/quick_prompt_settings/use_quick_prompt_editor.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant/quick_prompts/quick_prompt_settings/use_quick_prompt_editor.test.tsx index 239b8a06293ed..3626249c9fe31 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/quick_prompts/quick_prompt_settings/use_quick_prompt_editor.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/quick_prompts/quick_prompt_settings/use_quick_prompt_editor.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook, act } from '@testing-library/react-hooks'; +import { renderHook, act } from '@testing-library/react'; import { useQuickPromptEditor } from './use_quick_prompt_editor'; import { mockAlertPromptContext } from '../../../mock/prompt_context'; import { MOCK_QUICK_PROMPTS } from '../../../mock/quick_prompt'; diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/quick_prompts/quick_prompt_settings_management/use_quick_prompt_table.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant/quick_prompts/quick_prompt_settings_management/use_quick_prompt_table.test.tsx index ae91a8bb971c6..9d957a77dd738 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/quick_prompts/quick_prompt_settings_management/use_quick_prompt_table.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/quick_prompts/quick_prompt_settings_management/use_quick_prompt_table.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { useQuickPromptTable } from './use_quick_prompt_table'; import { EuiTableActionsColumnType, EuiTableComputedColumnType } from '@elastic/eui'; import { MOCK_QUICK_PROMPTS } from '../../../mock/quick_prompt'; diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/settings/use_settings_updater/use_settings_updater.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant/settings/use_settings_updater/use_settings_updater.test.tsx index e3a6ab1fc75b5..8f68d99c3eaa5 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/settings/use_settings_updater/use_settings_updater.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/settings/use_settings_updater/use_settings_updater.test.tsx @@ -4,7 +4,8 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import { act, renderHook } from '@testing-library/react-hooks'; + +import { act, waitFor, renderHook } from '@testing-library/react'; import { DEFAULT_LATEST_ALERTS } from '../../../assistant_context/constants'; import { alertConvo, welcomeConvo } from '../../../mock/conversation'; @@ -98,96 +99,101 @@ describe('useSettingsUpdater', () => { jest.clearAllMocks(); }); it('should set all state variables to their initial values when resetSettings is called', async () => { - await act(async () => { - const { result, waitForNextUpdate } = renderHook(() => - useSettingsUpdater( - mockConversations, - { - data: [...mockSystemPrompts, ...mockQuickPrompts], - page: 1, - perPage: 100, - total: 10, - }, - conversationsLoaded, - promptsLoaded, - anonymizationFields - ) - ); - await waitForNextUpdate(); - const { - setConversationSettings, - setConversationsSettingsBulkActions, - setUpdatedKnowledgeBaseSettings, - setUpdatedAssistantStreamingEnabled, - resetSettings, - setPromptsBulkActions, - } = result.current; + const { result } = renderHook(() => + useSettingsUpdater( + mockConversations, + { + data: [...mockSystemPrompts, ...mockQuickPrompts], + page: 1, + perPage: 100, + total: 10, + }, + conversationsLoaded, + promptsLoaded, + anonymizationFields + ) + ); + await waitFor(() => new Promise((resolve) => resolve(null))); + const { + setConversationSettings, + setConversationsSettingsBulkActions, + setUpdatedKnowledgeBaseSettings, + setUpdatedAssistantStreamingEnabled, + resetSettings, + setPromptsBulkActions, + } = result.current; + act(() => { setConversationSettings(updatedValues.conversations); setConversationsSettingsBulkActions({}); setPromptsBulkActions({}); setUpdatedAnonymizationData(updatedValues.updatedAnonymizationData); setUpdatedKnowledgeBaseSettings(updatedValues.knowledgeBase); setUpdatedAssistantStreamingEnabled(updatedValues.assistantStreamingEnabled); + }); - expect(result.current.conversationSettings).toEqual(updatedValues.conversations); - expect(result.current.quickPromptSettings).toEqual(updatedValues.allQuickPrompts); - expect(result.current.systemPromptSettings).toEqual(updatedValues.allSystemPrompts); - expect(result.current.updatedAnonymizationData).toEqual(anonymizationFields); - expect(result.current.knowledgeBase).toEqual(updatedValues.knowledgeBase); - expect(result.current.assistantStreamingEnabled).toEqual( - updatedValues.assistantStreamingEnabled - ); + expect(result.current.conversationSettings).toEqual(updatedValues.conversations); + expect(result.current.quickPromptSettings).toEqual(updatedValues.allQuickPrompts); + expect(result.current.systemPromptSettings).toEqual(updatedValues.allSystemPrompts); + expect(result.current.updatedAnonymizationData).toEqual(anonymizationFields); + expect(result.current.knowledgeBase).toEqual(updatedValues.knowledgeBase); + expect(result.current.assistantStreamingEnabled).toEqual( + updatedValues.assistantStreamingEnabled + ); + act(() => { resetSettings(); - - expect(result.current.conversationSettings).toEqual(mockConversations); - expect(result.current.quickPromptSettings).toEqual(mockValues.allQuickPrompts); - expect(result.current.systemPromptSettings).toEqual(mockValues.allSystemPrompts); - expect(result.current.anonymizationFieldsBulkActions).toEqual( - mockValues.anonymizationFieldsBulkActions - ); - expect(result.current.knowledgeBase).toEqual(mockValues.knowledgeBase); - expect(result.current.assistantStreamingEnabled).toEqual( - mockValues.assistantStreamingEnabled - ); }); + + expect(result.current.conversationSettings).toEqual(mockConversations); + expect(result.current.quickPromptSettings).toEqual(mockValues.allQuickPrompts); + expect(result.current.systemPromptSettings).toEqual(mockValues.allSystemPrompts); + expect(result.current.anonymizationFieldsBulkActions).toEqual( + mockValues.anonymizationFieldsBulkActions + ); + expect(result.current.knowledgeBase).toEqual(mockValues.knowledgeBase); + expect(result.current.assistantStreamingEnabled).toEqual(mockValues.assistantStreamingEnabled); }); it('should update all state variables to their updated values when saveSettings is called', async () => { - await act(async () => { - const { result, waitForNextUpdate } = renderHook(() => - useSettingsUpdater( - mockConversations, - { - data: mockSystemPrompts, - page: 1, - perPage: 100, - total: 10, - }, - conversationsLoaded, - promptsLoaded, - anonymizationFields - ) - ); - await waitForNextUpdate(); - const { - setConversationSettings, - setConversationsSettingsBulkActions, - setAnonymizationFieldsBulkActions, - setUpdatedKnowledgeBaseSettings, - setPromptsBulkActions, - } = result.current; + const { result } = renderHook(() => + useSettingsUpdater( + mockConversations, + { + data: mockSystemPrompts, + page: 1, + perPage: 100, + total: 10, + }, + conversationsLoaded, + promptsLoaded, + anonymizationFields + ) + ); + await waitFor(() => new Promise((resolve) => resolve(null))); + + const { + setConversationSettings, + setConversationsSettingsBulkActions, + setAnonymizationFieldsBulkActions, + setUpdatedKnowledgeBaseSettings, + setPromptsBulkActions, + } = result.current; + act(() => { setConversationSettings(updatedValues.conversations); setConversationsSettingsBulkActions({ delete: { ids: ['1'] } }); setAnonymizationFieldsBulkActions({ delete: { ids: ['1'] } }); setPromptsBulkActions({}); setUpdatedAnonymizationData(updatedValues.updatedAnonymizationData); setUpdatedKnowledgeBaseSettings(updatedValues.knowledgeBase); + }); + await act(async () => { await result.current.saveSettings(); + }); + await waitFor(() => { expect(mockHttp.fetch).toHaveBeenCalledWith( '/internal/elastic_assistant/current_user/conversations/_bulk_action', { @@ -202,80 +208,99 @@ describe('useSettingsUpdater', () => { expect(setKnowledgeBaseMock).toHaveBeenCalledWith(updatedValues.knowledgeBase); }); }); + it('should track when alerts count is updated', async () => { - await act(async () => { - const { result, waitForNextUpdate } = renderHook(() => - useSettingsUpdater( - mockConversations, - { - data: mockSystemPrompts, - page: 1, - perPage: 100, - total: 10, - }, - conversationsLoaded, - promptsLoaded, - anonymizationFields - ) - ); - await waitForNextUpdate(); - const { setUpdatedKnowledgeBaseSettings } = result.current; + const { result } = renderHook(() => + useSettingsUpdater( + mockConversations, + { + data: mockSystemPrompts, + page: 1, + perPage: 100, + total: 10, + }, + conversationsLoaded, + promptsLoaded, + anonymizationFields + ) + ); + await waitFor(() => new Promise((resolve) => resolve(null))); + const { setUpdatedKnowledgeBaseSettings } = result.current; + act(() => { setUpdatedKnowledgeBaseSettings({ ...updatedValues.knowledgeBase, }); + }); + + await act(async () => { await result.current.saveSettings(); - expect(reportAssistantSettingToggled).toHaveBeenCalledWith({ alertsCountUpdated: true }); }); + + await waitFor(() => + expect(reportAssistantSettingToggled).toHaveBeenCalledWith({ alertsCountUpdated: true }) + ); }); + it('should track when streaming is updated', async () => { - await act(async () => { - const { result, waitForNextUpdate } = renderHook(() => - useSettingsUpdater( - mockConversations, - { - data: mockSystemPrompts, - page: 1, - perPage: 100, - total: 10, - }, - conversationsLoaded, - promptsLoaded, - anonymizationFields - ) - ); - await waitForNextUpdate(); - const { setUpdatedAssistantStreamingEnabled } = result.current; + const { result } = renderHook(() => + useSettingsUpdater( + mockConversations, + { + data: mockSystemPrompts, + page: 1, + perPage: 100, + total: 10, + }, + conversationsLoaded, + promptsLoaded, + anonymizationFields + ) + ); + await waitFor(() => new Promise((resolve) => resolve(null))); + + const { setUpdatedAssistantStreamingEnabled } = result.current; + + act(() => { setUpdatedAssistantStreamingEnabled(false); + }); + + await act(async () => { await result.current.saveSettings(); + }); + + await waitFor(() => expect(reportAssistantSettingToggled).toHaveBeenCalledWith({ assistantStreamingEnabled: false, - }); - }); + }) + ); }); + it('if no settings update, do not track anything', async () => { - await act(async () => { - const { result, waitForNextUpdate } = renderHook(() => - useSettingsUpdater( - mockConversations, - { - data: mockSystemPrompts, - page: 1, - perPage: 100, - total: 10, - }, - conversationsLoaded, - promptsLoaded, - anonymizationFields - ) - ); - await waitForNextUpdate(); - const { setUpdatedKnowledgeBaseSettings } = result.current; + const { result } = renderHook(() => + useSettingsUpdater( + mockConversations, + { + data: mockSystemPrompts, + page: 1, + perPage: 100, + total: 10, + }, + conversationsLoaded, + promptsLoaded, + anonymizationFields + ) + ); + + await waitFor(() => new Promise((resolve) => resolve(null))); + const { setUpdatedKnowledgeBaseSettings } = result.current; + await act(async () => { setUpdatedKnowledgeBaseSettings(mockValues.knowledgeBase); await result.current.saveSettings(); - expect(reportAssistantSettingToggled).not.toHaveBeenCalledWith(); }); + + await waitFor(() => expect(reportAssistantSettingToggled).not.toHaveBeenCalledWith()); }); }); diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/use_assistant_overlay/index.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant/use_assistant_overlay/index.test.tsx index 780ffcbd9a323..7a142710d19ec 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/use_assistant_overlay/index.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/use_assistant_overlay/index.test.tsx @@ -5,11 +5,10 @@ * 2.0. */ -import { act, renderHook } from '@testing-library/react-hooks'; import { DefinedUseQueryResult } from '@tanstack/react-query'; import { useAssistantOverlay } from '.'; -import { waitFor } from '@testing-library/react'; +import { waitFor, renderHook, act } from '@testing-library/react'; import { useFetchCurrentUserConversations } from '../api'; import { Conversation } from '../../assistant_context/types'; import { mockConnectors } from '../../mock/connectors'; diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/use_conversation/index.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant/use_conversation/index.test.tsx index 696a2e41b37c2..9e4fc2ab1a2c6 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/use_conversation/index.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/use_conversation/index.test.tsx @@ -6,7 +6,7 @@ */ import { useConversation } from '.'; -import { act, renderHook } from '@testing-library/react-hooks'; +import { act, waitFor, renderHook } from '@testing-library/react'; import { TestProviders } from '../../mock/test_providers/test_providers'; import React from 'react'; import { MessageRole } from '@kbn/elastic-assistant-common'; @@ -54,79 +54,86 @@ describe('useConversation', () => { }); it('should create a new conversation when called with valid conversationId and message', async () => { - await act(async () => { - const { result, waitForNextUpdate } = renderHook(() => useConversation(), { - wrapper: ({ children }: React.PropsWithChildren<{}>) => ( - {children} - ), - }); - await waitForNextUpdate(); - createConversation.mockResolvedValue(mockConvo); + const { result } = renderHook(() => useConversation(), { + wrapper: ({ children }: React.PropsWithChildren<{}>) => ( + {children} + ), + }); + + await waitFor(() => new Promise((resolve) => resolve(null))); - const createResult = await result.current.createConversation({ + createConversation.mockResolvedValue(mockConvo); + + let createResult; + + await act(async () => { + createResult = await result.current.createConversation({ ...mockConvo, replacements: {}, title: mockConvo.title, category: 'assistant', }); - - expect(createResult).toEqual(mockConvo); }); + + expect(createResult).toEqual(mockConvo); }); it('should delete an existing conversation when called with valid conversationId', async () => { - await act(async () => { - const { result, waitForNextUpdate } = renderHook(() => useConversation(), { - wrapper: ({ children }: React.PropsWithChildren<{}>) => ( - {children} - ), - }); - await waitForNextUpdate(); + const { result } = renderHook(() => useConversation(), { + wrapper: ({ children }: React.PropsWithChildren<{}>) => ( + {children} + ), + }); + + await waitFor(() => new Promise((resolve) => resolve(null))); + await act(async () => { await result.current.deleteConversation('new-convo'); + }); - expect(deleteConversation).toHaveBeenCalledWith({ - http: httpMock, - id: 'new-convo', - }); + expect(deleteConversation).toHaveBeenCalledWith({ + http: httpMock, + id: 'new-convo', }); }); it('should update the apiConfig for an existing conversation when called with a valid conversationId and apiConfig', async () => { - await act(async () => { - const { result, waitForNextUpdate } = renderHook(() => useConversation(), { - wrapper: ({ children }: React.PropsWithChildren<{}>) => ( - {children} - ), - }); - await waitForNextUpdate(); + const { result } = renderHook(() => useConversation(), { + wrapper: ({ children }: React.PropsWithChildren<{}>) => ( + {children} + ), + }); + await waitFor(() => new Promise((resolve) => resolve(null))); + await act(async () => { await result.current.setApiConfig({ conversation: WELCOME_CONVERSATION, apiConfig: mockConvo.apiConfig, }); + }); - expect(createConversation).toHaveBeenCalledWith({ - http: httpMock, - conversation: { ...WELCOME_CONVERSATION, apiConfig: mockConvo.apiConfig, id: '' }, - }); + expect(createConversation).toHaveBeenCalledWith({ + http: httpMock, + conversation: { ...WELCOME_CONVERSATION, apiConfig: mockConvo.apiConfig, id: '' }, }); }); it('should remove the last message from a conversation when called with valid conversationId', async () => { - await act(async () => { - const { result, waitForNextUpdate } = renderHook(() => useConversation(), { - wrapper: ({ children }: React.PropsWithChildren<{}>) => ( - {children} - ), - }); - await waitForNextUpdate(); + const { result } = renderHook(() => useConversation(), { + wrapper: ({ children }: React.PropsWithChildren<{}>) => ( + {children} + ), + }); + await waitFor(() => new Promise((resolve) => resolve(null))); - getConversationById.mockResolvedValue(mockConvo); + getConversationById.mockResolvedValue(mockConvo); - const removeResult = await result.current.removeLastMessage('new-convo'); + let removeResult; - expect(removeResult).toEqual([message]); + await act(async () => { + removeResult = await result.current.removeLastMessage('new-convo'); }); + + expect(removeResult).toEqual([message]); }); }); diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/use_current_conversation/index.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant/use_current_conversation/index.test.tsx index 0f04068a89ca2..f5ea35983dd80 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/use_current_conversation/index.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/use_current_conversation/index.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook, act } from '@testing-library/react-hooks'; +import { renderHook, act, waitFor } from '@testing-library/react'; import { useCurrentConversation, Props } from '.'; import { useConversation } from '../use_conversation'; import deepEqual from 'fast-deep-equal'; @@ -49,12 +49,15 @@ describe('useCurrentConversation', () => { }; beforeEach(() => { - jest.clearAllMocks(); (useConversation as jest.Mock).mockReturnValue(mockUseConversation); (deepEqual as jest.Mock).mockReturnValue(false); (find as jest.Mock).mockReturnValue(undefined); }); + afterEach(() => { + jest.clearAllMocks(); + }); + const defaultProps: Props = { // @ts-ignore not exact system prompt type, ok for test allSystemPrompts: [{ id: 'system-prompt-id' }, { id: 'something-crazy' }], @@ -321,16 +324,19 @@ describe('useCurrentConversation', () => { await act(async () => { await result.current.handleCreateConversation(); }); + const { defaultSystemPromptId: _, ...everythingExceptSystemPromptId } = mockData.welcome_id.apiConfig; - expect(mockUseConversation.createConversation).toHaveBeenCalledWith({ - apiConfig: { - ...everythingExceptSystemPromptId, - defaultSystemPromptId: 'LBOi3JEBy3uD9EGi1d_G', - }, - title: 'New chat', - }); + await waitFor(() => + expect(mockUseConversation.createConversation).toHaveBeenCalledWith({ + apiConfig: { + ...everythingExceptSystemPromptId, + defaultSystemPromptId: 'LBOi3JEBy3uD9EGi1d_G', + }, + title: 'New chat', + }) + ); }); it('should delete a conversation', async () => { diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant/use_current_conversation/index.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant/use_current_conversation/index.tsx index d599190ca5623..267c39c402a1c 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant/use_current_conversation/index.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant/use_current_conversation/index.tsx @@ -207,6 +207,11 @@ export const useCurrentConversation = ({ // if no Welcome convo exists, create one getDefaultConversation({ cTitle: WELCOME_CONVERSATION_TITLE }); + // on the off chance that the conversation is not found, return + if (!nextConversation) { + return; + } + if (nextConversation && nextConversation.id === '') { // This is a default conversation that has not yet been initialized const conversation = await initializeDefaultConversationWithConnector(nextConversation); diff --git a/x-pack/packages/kbn-elastic-assistant/impl/assistant_context/index.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/assistant_context/index.test.tsx index 4e877e1886fb4..94e7800450c5d 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/assistant_context/index.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/assistant_context/index.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { useAssistantContext } from '.'; import useLocalStorage from 'react-use/lib/useLocalStorage'; @@ -18,10 +18,8 @@ describe('AssistantContext', () => { beforeEach(() => jest.clearAllMocks()); test('it throws an error when useAssistantContext hook is used without a SecurityAssistantContext', () => { - const { result } = renderHook(useAssistantContext); - - expect(result.error).toEqual( - new Error('useAssistantContext must be used within a AssistantProvider') + expect(() => renderHook(useAssistantContext)).toThrow( + /useAssistantContext must be used within a AssistantProvider/ ); }); diff --git a/x-pack/packages/kbn-elastic-assistant/impl/connectorland/use_load_action_types/index.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/connectorland/use_load_action_types/index.test.tsx index 50d3a73797413..dd169c9e345e3 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/connectorland/use_load_action_types/index.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/connectorland/use_load_action_types/index.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { act, renderHook } from '@testing-library/react-hooks'; +import { act, waitFor, renderHook } from '@testing-library/react'; import { useLoadActionTypes, Props } from '.'; import { mockActionTypes } from '../../mock/connectors'; @@ -32,10 +32,8 @@ describe('useLoadActionTypes', () => { jest.clearAllMocks(); }); it('should call api to load action types', async () => { - await act(async () => { - const { waitForNextUpdate } = renderHook(() => useLoadActionTypes(defaultProps)); - await waitForNextUpdate(); - + renderHook(() => useLoadActionTypes(defaultProps)); + await waitFor(() => { expect(defaultProps.http.get).toHaveBeenCalledWith('/api/actions/connector_types', { query: { feature_id: 'generativeAIForSecurity' }, }); @@ -44,26 +42,20 @@ describe('useLoadActionTypes', () => { }); it('should return sorted action types', async () => { - await act(async () => { - const { result, waitForNextUpdate } = renderHook(() => useLoadActionTypes(defaultProps)); - await waitForNextUpdate(); - - await expect(result.current).resolves.toStrictEqual( + const { result } = renderHook(() => useLoadActionTypes(defaultProps)); + await waitFor(() => + expect(result.current).resolves.toStrictEqual( mockActionTypes.sort((a, b) => a.name.localeCompare(b.name)) - ); - }); + ) + ); }); it('should display error toast when api throws error', async () => { await act(async () => { const mockHttp = { get: jest.fn().mockRejectedValue(new Error('this is an error')), } as unknown as Props['http']; - const { waitForNextUpdate } = renderHook(() => - useLoadActionTypes({ ...defaultProps, http: mockHttp }) - ); - await waitForNextUpdate(); - - expect(toasts.addError).toHaveBeenCalled(); + renderHook(() => useLoadActionTypes({ ...defaultProps, http: mockHttp })); + await waitFor(() => expect(toasts.addError).toHaveBeenCalled()); }); }); }); diff --git a/x-pack/packages/kbn-elastic-assistant/impl/connectorland/use_load_connectors/index.test.tsx b/x-pack/packages/kbn-elastic-assistant/impl/connectorland/use_load_connectors/index.test.tsx index b29274efb19c0..685d01c988e0d 100644 --- a/x-pack/packages/kbn-elastic-assistant/impl/connectorland/use_load_connectors/index.test.tsx +++ b/x-pack/packages/kbn-elastic-assistant/impl/connectorland/use_load_connectors/index.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { act, renderHook } from '@testing-library/react-hooks'; +import { waitFor, renderHook } from '@testing-library/react'; import { useLoadConnectors, Props } from '.'; import { mockConnectors } from '../../mock/connectors'; @@ -68,37 +68,27 @@ describe('useLoadConnectors', () => { jest.clearAllMocks(); }); it('should call api to load action types', async () => { - await act(async () => { - const { waitForNextUpdate } = renderHook(() => useLoadConnectors(defaultProps)); - await waitForNextUpdate(); - + renderHook(() => useLoadConnectors(defaultProps)); + await waitFor(() => { expect(defaultProps.http.get).toHaveBeenCalledWith('/api/actions/connectors'); expect(toasts.addError).not.toHaveBeenCalled(); }); }); it('should return sorted action types, removing isMissingSecrets and wrong action type ids', async () => { - await act(async () => { - const { result, waitForNextUpdate } = renderHook(() => useLoadConnectors(defaultProps)); - await waitForNextUpdate(); - - await expect(result.current).resolves.toStrictEqual( + const { result } = renderHook(() => useLoadConnectors(defaultProps)); + await waitFor(() => { + expect(result.current).resolves.toStrictEqual( // @ts-ignore ts does not like config, but we define it in the mock data loadConnectorsResult.map((c) => ({ ...c, apiProvider: c.config.apiProvider })) ); }); }); it('should display error toast when api throws error', async () => { - await act(async () => { - const mockHttp = { - get: jest.fn().mockRejectedValue(new Error('this is an error')), - } as unknown as Props['http']; - const { waitForNextUpdate } = renderHook(() => - useLoadConnectors({ ...defaultProps, http: mockHttp }) - ); - await waitForNextUpdate(); - - expect(toasts.addError).toHaveBeenCalled(); - }); + const mockHttp = { + get: jest.fn().mockRejectedValue(new Error('this is an error')), + } as unknown as Props['http']; + renderHook(() => useLoadConnectors({ ...defaultProps, http: mockHttp })); + await waitFor(() => expect(toasts.addError).toHaveBeenCalled()); }); }); diff --git a/x-pack/plugins/security_solution/public/assistant/get_comments/stream/use_stream.test.tsx b/x-pack/plugins/security_solution/public/assistant/get_comments/stream/use_stream.test.tsx index 9aa3491218f88..1293ddff34f75 100644 --- a/x-pack/plugins/security_solution/public/assistant/get_comments/stream/use_stream.test.tsx +++ b/x-pack/plugins/security_solution/public/assistant/get_comments/stream/use_stream.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { waitFor, renderHook } from '@testing-library/react'; import { useStream } from './use_stream'; const refetchCurrentConversation = jest.fn(); @@ -49,7 +49,7 @@ describe.skip('useStream', () => { }); it('Should stream response. isLoading/isStreaming are true while streaming, isLoading/isStreaming are false when streaming completes', async () => { - const { result, waitFor } = renderHook(() => useStream(defaultProps)); + const { result } = renderHook(() => useStream(defaultProps)); expect(reader).toHaveBeenCalledTimes(1); await waitFor(() => { expect(result.current).toEqual({ @@ -107,7 +107,7 @@ describe.skip('useStream', () => { releaseLock: jest.fn(), closed: jest.fn().mockResolvedValue(true), } as unknown as ReadableStreamDefaultReader; - const { result, waitForNextUpdate } = renderHook(() => + const { result } = renderHook(() => useStream({ ...defaultProps, reader: errorReader, @@ -115,7 +115,7 @@ describe.skip('useStream', () => { ); expect(result.current.error).toBeUndefined(); - await waitForNextUpdate(); + await waitFor(() => new Promise((resolve) => resolve(null))); expect(result.current.error).toBe(errorMessage); expect(result.current.isLoading).toBe(false); diff --git a/x-pack/plugins/security_solution/public/assistant/overlay.test.tsx b/x-pack/plugins/security_solution/public/assistant/overlay.test.tsx index ca90d72b37157..eb9adf234e9fd 100644 --- a/x-pack/plugins/security_solution/public/assistant/overlay.test.tsx +++ b/x-pack/plugins/security_solution/public/assistant/overlay.test.tsx @@ -22,7 +22,14 @@ jest.mock('@kbn/elastic-assistant', () => ({ jest.mock('../common/hooks/use_experimental_features'); describe('AssistantOverlay', () => { - const queryClient = new QueryClient(); + const queryClient = new QueryClient({ + defaultOptions: { + queries: { + cacheTime: Infinity, + retry: false, + }, + }, + }); beforeEach(() => { jest.clearAllMocks(); diff --git a/x-pack/plugins/security_solution/public/assistant/provider.test.tsx b/x-pack/plugins/security_solution/public/assistant/provider.test.tsx index a96623b8567a8..5108fb259cfb2 100644 --- a/x-pack/plugins/security_solution/public/assistant/provider.test.tsx +++ b/x-pack/plugins/security_solution/public/assistant/provider.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { act, renderHook } from '@testing-library/react-hooks'; +import { waitFor, renderHook } from '@testing-library/react'; import { httpServiceMock, type HttpSetupMock } from '@kbn/core-http-browser-mocks'; import type { Storage } from '@kbn/kibana-utils-plugin/public'; import { createConversations } from './provider'; @@ -166,47 +166,43 @@ describe('createConversations', () => { }); it('should call bulk conversations with the transformed conversations from the local storage', async () => { - await act(async () => { - const { waitForNextUpdate } = renderHook(() => - createConversations( - coreMock.createStart().notifications, - http, - mockStorage as unknown as Storage - ) - ); - await waitForNextUpdate(); - expect(http.fetch.mock.calls[0][0]).toBe( - '/internal/elastic_assistant/current_user/conversations/_bulk_action' - ); - expect( - http.fetch.mock.calls[0].length > 1 - ? // eslint-disable-next-line @typescript-eslint/no-explicit-any - JSON.parse((http.fetch.mock.calls[0] as any[])[1]?.body).create.length - : 0 - ).toBe(2); - }); + renderHook(() => + createConversations( + coreMock.createStart().notifications, + http, + mockStorage as unknown as Storage + ) + ); + await waitFor(() => new Promise((resolve) => resolve(null))); + expect(http.fetch.mock.calls[0][0]).toBe( + '/internal/elastic_assistant/current_user/conversations/_bulk_action' + ); + expect( + http.fetch.mock.calls[0].length > 1 + ? // eslint-disable-next-line @typescript-eslint/no-explicit-any + JSON.parse((http.fetch.mock.calls[0] as any[])[1]?.body).create.length + : 0 + ).toBe(2); }); it('should add missing actionTypeId to apiConfig', async () => { - await act(async () => { - const { waitForNextUpdate } = renderHook(() => - createConversations( - coreMock.createStart().notifications, - http, - mockStorage as unknown as Storage - ) - ); - await waitForNextUpdate(); - expect(http.fetch.mock.calls[0][0]).toBe( - '/internal/elastic_assistant/current_user/conversations/_bulk_action' - ); - const createdConversations = - http.fetch.mock.calls[0].length > 1 - ? // eslint-disable-next-line @typescript-eslint/no-explicit-any - JSON.parse((http.fetch.mock.calls[0] as any[])[1]?.body)?.create - : []; - expect(createdConversations[0].apiConfig.actionTypeId).toEqual('.bedrock'); - expect(createdConversations[1].apiConfig.actionTypeId).toEqual('.gen-ai'); - }); + renderHook(() => + createConversations( + coreMock.createStart().notifications, + http, + mockStorage as unknown as Storage + ) + ); + await waitFor(() => new Promise((resolve) => resolve(null))); + expect(http.fetch.mock.calls[0][0]).toBe( + '/internal/elastic_assistant/current_user/conversations/_bulk_action' + ); + const createdConversations = + http.fetch.mock.calls[0].length > 1 + ? // eslint-disable-next-line @typescript-eslint/no-explicit-any + JSON.parse((http.fetch.mock.calls[0] as any[])[1]?.body)?.create + : []; + expect(createdConversations[0].apiConfig.actionTypeId).toEqual('.bedrock'); + expect(createdConversations[1].apiConfig.actionTypeId).toEqual('.gen-ai'); }); }); diff --git a/x-pack/plugins/security_solution/public/assistant/use_assistant_telemetry/index.test.tsx b/x-pack/plugins/security_solution/public/assistant/use_assistant_telemetry/index.test.tsx index b90db333742a4..269d09bfed0fc 100644 --- a/x-pack/plugins/security_solution/public/assistant/use_assistant_telemetry/index.test.tsx +++ b/x-pack/plugins/security_solution/public/assistant/use_assistant_telemetry/index.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { useAssistantTelemetry } from '.'; import { BASE_SECURITY_CONVERSATIONS } from '../content/conversations'; import { createTelemetryServiceMock } from '../../common/lib/telemetry/telemetry_service.mock'; diff --git a/x-pack/plugins/security_solution/public/assistant/use_conversation_store/index.test.tsx b/x-pack/plugins/security_solution/public/assistant/use_conversation_store/index.test.tsx index 99836af47a7a8..d93430e1552df 100644 --- a/x-pack/plugins/security_solution/public/assistant/use_conversation_store/index.test.tsx +++ b/x-pack/plugins/security_solution/public/assistant/use_conversation_store/index.test.tsx @@ -4,7 +4,8 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; + +import { renderHook } from '@testing-library/react'; import { useBaseConversations } from '.'; import { useLinkAuthorized } from '../../common/links'; import { useKibana as mockUseKibana } from '../../common/lib/kibana/__mocks__'; diff --git a/x-pack/plugins/security_solution/public/attack_discovery/pages/results/attack_discovery_panel/actions/use_add_to_case/index.test.tsx b/x-pack/plugins/security_solution/public/attack_discovery/pages/results/attack_discovery_panel/actions/use_add_to_case/index.test.tsx index 7ecb824739679..b9e5aadb018ad 100644 --- a/x-pack/plugins/security_solution/public/attack_discovery/pages/results/attack_discovery_panel/actions/use_add_to_case/index.test.tsx +++ b/x-pack/plugins/security_solution/public/attack_discovery/pages/results/attack_discovery_panel/actions/use_add_to_case/index.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { act, renderHook } from '@testing-library/react-hooks'; +import { renderHook, act } from '@testing-library/react'; import { useAddToNewCase } from '.'; import { TestProviders } from '../../../../../../common/mock'; diff --git a/x-pack/plugins/security_solution/public/attack_discovery/pages/results/attack_discovery_panel/actions/use_add_to_existing_case/index.test.tsx b/x-pack/plugins/security_solution/public/attack_discovery/pages/results/attack_discovery_panel/actions/use_add_to_existing_case/index.test.tsx index e84b7973208f7..fd91ff132f204 100644 --- a/x-pack/plugins/security_solution/public/attack_discovery/pages/results/attack_discovery_panel/actions/use_add_to_existing_case/index.test.tsx +++ b/x-pack/plugins/security_solution/public/attack_discovery/pages/results/attack_discovery_panel/actions/use_add_to_existing_case/index.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { act, renderHook } from '@testing-library/react-hooks'; +import { renderHook, act } from '@testing-library/react'; import { useAddToExistingCase } from '.'; import { useKibana } from '../../../../../../common/lib/kibana'; diff --git a/x-pack/plugins/security_solution/public/attack_discovery/pages/results/attack_discovery_panel/view_in_ai_assistant/use_view_in_ai_assistant.test.ts b/x-pack/plugins/security_solution/public/attack_discovery/pages/results/attack_discovery_panel/view_in_ai_assistant/use_view_in_ai_assistant.test.ts index 86f243e9445f4..4d7d7316a9c9b 100644 --- a/x-pack/plugins/security_solution/public/attack_discovery/pages/results/attack_discovery_panel/view_in_ai_assistant/use_view_in_ai_assistant.test.ts +++ b/x-pack/plugins/security_solution/public/attack_discovery/pages/results/attack_discovery_panel/view_in_ai_assistant/use_view_in_ai_assistant.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { useAssistantOverlay } from '@kbn/elastic-assistant'; import { useAssistantAvailability } from '../../../../../assistant/use_assistant_availability'; diff --git a/x-pack/plugins/security_solution/public/attack_discovery/pages/use_attack_discovery/index.test.tsx b/x-pack/plugins/security_solution/public/attack_discovery/pages/use_attack_discovery/index.test.tsx index a476aead19036..a21ff89827d4c 100644 --- a/x-pack/plugins/security_solution/public/attack_discovery/pages/use_attack_discovery/index.test.tsx +++ b/x-pack/plugins/security_solution/public/attack_discovery/pages/use_attack_discovery/index.test.tsx @@ -7,7 +7,7 @@ import { useLoadConnectors } from '@kbn/elastic-assistant'; import { useFetchAnonymizationFields } from '@kbn/elastic-assistant/impl/assistant/api/anonymization_fields/use_fetch_anonymization_fields'; -import { renderHook, act } from '@testing-library/react-hooks'; +import { renderHook, act } from '@testing-library/react'; import React from 'react'; import { useKibana } from '../../../common/lib/kibana'; diff --git a/x-pack/plugins/security_solution/public/attack_discovery/pages/use_attack_discovery/use_poll_api/use_poll_api.test.tsx b/x-pack/plugins/security_solution/public/attack_discovery/pages/use_attack_discovery/use_poll_api/use_poll_api.test.tsx index c243138d9ef5c..8d161657ae040 100644 --- a/x-pack/plugins/security_solution/public/attack_discovery/pages/use_attack_discovery/use_poll_api/use_poll_api.test.tsx +++ b/x-pack/plugins/security_solution/public/attack_discovery/pages/use_attack_discovery/use_poll_api/use_poll_api.test.tsx @@ -7,7 +7,7 @@ import type { HttpSetupMock } from '@kbn/core-http-browser-mocks'; import { coreMock } from '@kbn/core/public/mocks'; -import { act, renderHook } from '@testing-library/react-hooks'; +import { renderHook, act } from '@testing-library/react'; import { attackDiscoveryStatus, usePollApi } from './use_poll_api'; import moment from 'moment/moment'; import { kibanaMock } from '../../../../common/mock';