diff --git a/x-pack/packages/kbn-ai-assistant/src/hooks/use_conversation.test.tsx b/x-pack/packages/kbn-ai-assistant/src/hooks/use_conversation.test.tsx index dffca3addd34e..196553ce55066 100644 --- a/x-pack/packages/kbn-ai-assistant/src/hooks/use_conversation.test.tsx +++ b/x-pack/packages/kbn-ai-assistant/src/hooks/use_conversation.test.tsx @@ -5,12 +5,7 @@ * 2.0. */ import type { DeeplyMockedKeys } from '@kbn/utility-types-jest'; -import { - act, - renderHook, - type RenderHookResult, - type WrapperComponent, -} from '@testing-library/react-hooks'; +import { renderHook, act, type RenderHookResult } from '@testing-library/react'; import { merge } from 'lodash'; import React, { PropsWithChildren } from 'react'; import { BehaviorSubject, Observable, of, Subject } from 'rxjs'; @@ -33,7 +28,7 @@ import type { NotificationsStart } from '@kbn/core/public'; import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public'; import { AssistantScope } from '@kbn/ai-assistant-common'; -let hookResult: RenderHookResult; +let hookResult: RenderHookResult; type MockedService = DeeplyMockedKeys> & { conversations: DeeplyMockedKeys< @@ -81,34 +76,32 @@ const useKibanaMockServices = { }; describe('useConversation', () => { - let wrapper: WrapperComponent>; + const wrapper = ({ children }: PropsWithChildren) => ( + {children} + ); - beforeEach(() => { + afterEach(() => { jest.clearAllMocks(); - wrapper = ({ children }: PropsWithChildren) => ( - {children} - ); }); describe('with initial messages and a conversation id', () => { - beforeEach(() => { - hookResult = renderHook(useConversation, { - initialProps: { - chatService: mockChatService, - connectorId: 'my-connector', - initialMessages: [ - { - '@timestamp': new Date().toISOString(), - message: { content: '', role: MessageRole.User }, - }, - ], - initialConversationId: 'foo', - }, - wrapper, - }); - }); it('throws an error', () => { - expect(hookResult.result.error).toBeTruthy(); + expect(() => + renderHook(useConversation, { + initialProps: { + chatService: mockChatService, + connectorId: 'my-connector', + initialMessages: [ + { + '@timestamp': new Date().toISOString(), + message: { content: '', role: MessageRole.User }, + }, + ], + initialConversationId: 'foo', + }, + wrapper, + }) + ).toThrow(/Cannot set initialMessages if initialConversationId is set/); }); }); @@ -434,25 +427,29 @@ describe('useConversation', () => { describe('when the title is updated', () => { describe('without a stored conversation', () => { - beforeEach(() => { - hookResult = renderHook(useConversation, { - initialProps: { - chatService: mockChatService, - connectorId: 'my-connector', - initialMessages: [ - { - '@timestamp': new Date().toISOString(), - message: { content: '', role: MessageRole.User }, - }, - ], - initialConversationId: 'foo', - }, - wrapper, - }); - }); + it('throws an error', (done) => { + try { + const { result } = renderHook(useConversation, { + initialProps: { + chatService: mockChatService, + connectorId: 'my-connector', + initialMessages: [ + { + '@timestamp': new Date().toISOString(), + message: { content: '', role: MessageRole.User }, + }, + ], + initialConversationId: 'foo', + }, + wrapper, + }); - it('throws an error', () => { - expect(() => hookResult.result.current.saveTitle('my-new-title')).toThrow(); + result.current.saveTitle('my-new-title'); + } catch (e) { + expect(e).toBeInstanceOf(Error); + expect(e.message).toBe('Cannot set initialMessages if initialConversationId is set'); + done(); + } }); }); diff --git a/x-pack/packages/kbn-ai-assistant/src/hooks/use_local_storage.test.ts b/x-pack/packages/kbn-ai-assistant/src/hooks/use_local_storage.test.ts index ab1d00392fdb9..ea4ed05e36b66 100644 --- a/x-pack/packages/kbn-ai-assistant/src/hooks/use_local_storage.test.ts +++ b/x-pack/packages/kbn-ai-assistant/src/hooks/use_local_storage.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook, act } from '@testing-library/react-hooks'; +import { renderHook, act } from '@testing-library/react'; import { useLocalStorage } from './use_local_storage'; describe('useLocalStorage', () => { diff --git a/x-pack/plugins/enterprise_search/public/applications/applications/components/search_application/docs_explorer/document_context.test.tsx b/x-pack/plugins/enterprise_search/public/applications/applications/components/search_application/docs_explorer/document_context.test.tsx index 76a65da6d3d6c..9e6607c278f4f 100644 --- a/x-pack/plugins/enterprise_search/public/applications/applications/components/search_application/docs_explorer/document_context.test.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/applications/components/search_application/docs_explorer/document_context.test.tsx @@ -7,7 +7,7 @@ import React from 'react'; -import { act, renderHook } from '@testing-library/react-hooks'; +import { renderHook, act } from '@testing-library/react'; import { DocumentProvider, useSelectedDocument } from './document_context'; diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/layout/nav.test.tsx b/x-pack/plugins/enterprise_search/public/applications/shared/layout/nav.test.tsx index 21b399892269d..19a0fea68969b 100644 --- a/x-pack/plugins/enterprise_search/public/applications/shared/layout/nav.test.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/shared/layout/nav.test.tsx @@ -15,7 +15,7 @@ jest.mock('../../enterprise_search_content/components/search_index/indices/indic import { setMockValues, mockKibanaValues } from '../../__mocks__/kea_logic'; -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { EuiSideNavItemType } from '@elastic/eui'; diff --git a/x-pack/plugins/search_inference_endpoints/public/hooks/use_delete_endpoint.test.tsx b/x-pack/plugins/search_inference_endpoints/public/hooks/use_delete_endpoint.test.tsx index 7f2c2157a3609..553b648013600 100644 --- a/x-pack/plugins/search_inference_endpoints/public/hooks/use_delete_endpoint.test.tsx +++ b/x-pack/plugins/search_inference_endpoints/public/hooks/use_delete_endpoint.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { waitFor, renderHook } from '@testing-library/react'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import { useKibana } from './use_kibana'; import { useDeleteEndpoint } from './use_delete_endpoint'; @@ -43,7 +43,7 @@ describe('useDeleteEndpoint', () => { }; it('should call delete endpoint and show success toast on success', async () => { - const { result, waitFor } = renderHook(() => useDeleteEndpoint(), { wrapper }); + const { result } = renderHook(() => useDeleteEndpoint(), { wrapper }); result.current.mutate({ type: 'text_embedding', id: 'in-1' }); @@ -60,7 +60,7 @@ describe('useDeleteEndpoint', () => { it('should show error toast on failure', async () => { const error = { body: { message: 'error' } }; mockDelete.mockRejectedValue(error); - const { result, waitFor } = renderHook(() => useDeleteEndpoint(), { wrapper }); + const { result } = renderHook(() => useDeleteEndpoint(), { wrapper }); result.current.mutate({ type: 'model', id: '123' }); diff --git a/x-pack/plugins/search_inference_endpoints/public/hooks/use_scan_usage.test.tsx b/x-pack/plugins/search_inference_endpoints/public/hooks/use_scan_usage.test.tsx index 5f1766b7a6fa4..f243f5f7a20d0 100644 --- a/x-pack/plugins/search_inference_endpoints/public/hooks/use_scan_usage.test.tsx +++ b/x-pack/plugins/search_inference_endpoints/public/hooks/use_scan_usage.test.tsx @@ -7,7 +7,7 @@ import React from 'react'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; -import { renderHook } from '@testing-library/react-hooks'; +import { waitFor, renderHook } from '@testing-library/react'; import { useScanUsage } from './use_scan_usage'; import { useKibana } from './use_kibana'; @@ -43,7 +43,7 @@ describe('useScanUsage', () => { }); test('should call API endpoint with the correct parameters and return response', async () => { - const { result, waitForNextUpdate } = renderHook( + const { result } = renderHook( () => useScanUsage({ type: 'text_embedding', @@ -52,18 +52,18 @@ describe('useScanUsage', () => { { wrapper } ); - await waitForNextUpdate(); + await waitFor(() => { + expect(mockDelete).toHaveBeenCalledWith( + '/internal/inference_endpoint/endpoints/text_embedding/in-1', + { query: { scanUsage: true } } + ); - expect(mockDelete).toHaveBeenCalledWith( - '/internal/inference_endpoint/endpoints/text_embedding/in-1', - { query: { scanUsage: true } } - ); - - expect(result.current.data).toEqual({ - acknowledge: true, - error_message: 'inference id is being used', - indexes: ['index1', 'index2'], - pipelines: ['pipeline1', 'pipeline2'], + expect(result.current.data).toEqual({ + acknowledge: true, + error_message: 'inference id is being used', + indexes: ['index1', 'index2'], + pipelines: ['pipeline1', 'pipeline2'], + }); }); }); }); diff --git a/x-pack/plugins/search_inference_endpoints/public/hooks/use_table_data.test.tsx b/x-pack/plugins/search_inference_endpoints/public/hooks/use_table_data.test.tsx index b16fb5f7675cb..c5d3cf15f1407 100644 --- a/x-pack/plugins/search_inference_endpoints/public/hooks/use_table_data.test.tsx +++ b/x-pack/plugins/search_inference_endpoints/public/hooks/use_table_data.test.tsx @@ -6,7 +6,7 @@ */ import { InferenceAPIConfigResponse } from '@kbn/ml-trained-models-utils'; -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { QueryParams } from '../components/all_inference_endpoints/types'; import { useTableData } from './use_table_data'; import { INFERENCE_ENDPOINTS_TABLE_PER_PAGE_VALUES } from '../components/all_inference_endpoints/types'; diff --git a/x-pack/plugins/search_playground/public/hooks/use_llms_models.test.ts b/x-pack/plugins/search_playground/public/hooks/use_llms_models.test.ts index c529a9d4b9aa6..be215e0f9e15a 100644 --- a/x-pack/plugins/search_playground/public/hooks/use_llms_models.test.ts +++ b/x-pack/plugins/search_playground/public/hooks/use_llms_models.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { useLoadConnectors } from './use_load_connectors'; import { useLLMsModels } from './use_llms_models'; import { LLMs } from '../types'; diff --git a/x-pack/plugins/search_playground/public/hooks/use_load_connectors.test.ts b/x-pack/plugins/search_playground/public/hooks/use_load_connectors.test.ts index eb2f36eb62e5f..57020cbcd6d3c 100644 --- a/x-pack/plugins/search_playground/public/hooks/use_load_connectors.test.ts +++ b/x-pack/plugins/search_playground/public/hooks/use_load_connectors.test.ts @@ -8,7 +8,7 @@ import { loadAllActions as loadConnectors } from '@kbn/triggers-actions-ui-plugin/public/common/constants'; import { useLoadConnectors } from './use_load_connectors'; import { useKibana } from './use_kibana'; -import { act, renderHook } from '@testing-library/react-hooks'; +import { waitFor, renderHook } from '@testing-library/react'; import { OpenAiProviderType } from '@kbn/stack-connectors-plugin/common/openai/constants'; const mockedLoadConnectors = loadConnectors as jest.Mock; @@ -80,11 +80,9 @@ describe('useLoadConnectors', () => { ]; mockedLoadConnectors.mockResolvedValue(connectors); - await act(async () => { - const { result, waitForNextUpdate } = renderHook(() => useLoadConnectors()); - await waitForNextUpdate(); - - await expect(result.current).resolves.toStrictEqual([ + const { result } = renderHook(() => useLoadConnectors()); + await waitFor(() => + expect(result.current).resolves.toStrictEqual([ { actionTypeId: '.gen-ai', config: { @@ -122,8 +120,8 @@ describe('useLoadConnectors', () => { title: 'OpenAI Other', type: 'openai_other', }, - ]); - }); + ]) + ); }); it('handles pre-configured connectors', async () => { @@ -149,11 +147,9 @@ describe('useLoadConnectors', () => { ]; mockedLoadConnectors.mockResolvedValue(connectors); - await act(async () => { - const { result, waitForNextUpdate } = renderHook(() => useLoadConnectors()); - await waitForNextUpdate(); - - await expect(result.current).resolves.toStrictEqual([ + const { result } = renderHook(() => useLoadConnectors()); + await waitFor(() => + expect(result.current).resolves.toStrictEqual([ { actionTypeId: '.gen-ai', id: '1', @@ -171,22 +167,20 @@ describe('useLoadConnectors', () => { title: 'OpenAI Azure', type: 'openai_azure', }, - ]); - }); + ]) + ); }); it('handles errors correctly', async () => { const error = new Error('Test Error'); mockedLoadConnectors.mockRejectedValue(error); - await act(async () => { - const { waitForNextUpdate } = renderHook(() => useLoadConnectors()); - await waitForNextUpdate(); - + renderHook(() => useLoadConnectors()); + await waitFor(() => expect(mockedUseKibana().services.notifications.toasts.addError).toHaveBeenCalledWith( error, expect.any(Object) - ); - }); + ) + ); }); }); diff --git a/x-pack/plugins/search_playground/public/hooks/use_load_fields_by_indices.test.ts b/x-pack/plugins/search_playground/public/hooks/use_load_fields_by_indices.test.ts index 62f6a1acb6f64..c5c70cbdfc9d9 100644 --- a/x-pack/plugins/search_playground/public/hooks/use_load_fields_by_indices.test.ts +++ b/x-pack/plugins/search_playground/public/hooks/use_load_fields_by_indices.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { useLoadFieldsByIndices } from './use_load_fields_by_indices'; import { useUsageTracker } from './use_usage_tracker'; import { useIndicesFields } from './use_indices_fields'; diff --git a/x-pack/plugins/search_playground/public/hooks/use_management_link.test.ts b/x-pack/plugins/search_playground/public/hooks/use_management_link.test.ts index ed566bbaee9f5..8093dfcea061a 100644 --- a/x-pack/plugins/search_playground/public/hooks/use_management_link.test.ts +++ b/x-pack/plugins/search_playground/public/hooks/use_management_link.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { waitFor, renderHook } from '@testing-library/react'; import { useManagementLink } from './use_management_link'; import { useKibana } from './use_kibana'; @@ -41,13 +41,13 @@ describe('useManagementLink Hook', () => { 'http://localhost:5601/app/management/insightsAndAlerting/triggersActionsConnectors'; mockGetUrl.mockResolvedValue(expectedUrl); const connectorId = 'test-connector-id'; - const { result, waitForNextUpdate } = renderHook(() => useManagementLink(connectorId)); - await waitForNextUpdate(); - - expect(result.current).toBe(expectedUrl); - expect(mockGetUrl).toHaveBeenCalledWith({ - sectionId: 'insightsAndAlerting', - appId: 'triggersActionsConnectors/connectors/test-connector-id', + const { result } = renderHook(() => useManagementLink(connectorId)); + await waitFor(() => { + expect(result.current).toBe(expectedUrl); + expect(mockGetUrl).toHaveBeenCalledWith({ + sectionId: 'insightsAndAlerting', + appId: 'triggersActionsConnectors/connectors/test-connector-id', + }); }); }); diff --git a/x-pack/plugins/search_playground/public/hooks/use_query_indices.test.ts b/x-pack/plugins/search_playground/public/hooks/use_query_indices.test.ts index f94bccba2bcbd..9e38efcf696b6 100644 --- a/x-pack/plugins/search_playground/public/hooks/use_query_indices.test.ts +++ b/x-pack/plugins/search_playground/public/hooks/use_query_indices.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { useQueryIndices } from './use_query_indices'; jest.mock('./use_kibana', () => ({ diff --git a/x-pack/plugins/search_playground/public/hooks/use_source_indices_fields.test.tsx b/x-pack/plugins/search_playground/public/hooks/use_source_indices_fields.test.tsx index 4adf3d18ea92b..1ae008fe39bec 100644 --- a/x-pack/plugins/search_playground/public/hooks/use_source_indices_fields.test.tsx +++ b/x-pack/plugins/search_playground/public/hooks/use_source_indices_fields.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook, act } from '@testing-library/react-hooks'; +import { renderHook, act } from '@testing-library/react'; import { useUsageTracker } from './use_usage_tracker'; import { useController } from 'react-hook-form'; import { useIndicesFields } from './use_indices_fields'; diff --git a/x-pack/plugins/search_playground/public/hooks/use_usage_tracker.test.ts b/x-pack/plugins/search_playground/public/hooks/use_usage_tracker.test.ts index 9fb74674504f8..7f0202a820894 100644 --- a/x-pack/plugins/search_playground/public/hooks/use_usage_tracker.test.ts +++ b/x-pack/plugins/search_playground/public/hooks/use_usage_tracker.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { useKibana } from './use_kibana'; import { useUsageTracker } from './use_usage_tracker';