From 31d7558a1ff8884e7cfd881d09cd0fbd2d86032e Mon Sep 17 00:00:00 2001 From: Eyo Okon Eyo Date: Tue, 31 Dec 2024 11:06:34 +0100 Subject: [PATCH 1/3] remove instance of react hooks testing library --- .../hooks/use_find_alerts_query.test.tsx | 6 ++-- .../public/components/context/index.test.tsx | 10 ++---- .../use_create_knowledge_base_entry.test.tsx | 2 +- ...use_delete_knowledge_base_entries.test.tsx | 2 +- .../use_knowledge_base_entries.test.ts | 32 +++++++++---------- ...use_update_knowledge_base_entries.test.tsx | 2 +- .../status/use_should_disable_status.test.tsx | 2 +- .../case_view/use_case_observables.test.ts | 2 +- .../use_user_permissions.test.tsx | 2 +- .../use_delete_observables.test.tsx | 6 ++-- .../containers/use_get_similar_cases.test.tsx | 20 ++++-------- .../containers/use_patch_observables.test.tsx | 6 ++-- .../containers/use_post_observables.test.tsx | 28 +++++++--------- .../use_bulk_untrack_alerts_by_query.test.ts | 4 +-- .../public/app_plugin/app_helpers.test.ts | 2 +- .../http_status_badge.test.tsx | 2 +- .../infra/public/hooks/use_time_range.test.ts | 2 +- .../hooks/use_monitor_filters.test.ts | 2 +- .../src/hooks/use_fetch_graph_data.test.tsx | 2 +- ...e_navigate_to_graph_visualization.test.tsx | 2 +- .../hooks/use_navigate_to_left_panel.test.tsx | 2 +- .../use_navigate_to_host_details.test.ts | 2 +- .../use_navigate_to_user_details.test.ts | 2 +- 23 files changed, 63 insertions(+), 79 deletions(-) diff --git a/src/platform/packages/shared/kbn-alerts-ui-shared/src/common/hooks/use_find_alerts_query.test.tsx b/src/platform/packages/shared/kbn-alerts-ui-shared/src/common/hooks/use_find_alerts_query.test.tsx index 4fe37fd7a6035..025b23d1ea5ff 100644 --- a/src/platform/packages/shared/kbn-alerts-ui-shared/src/common/hooks/use_find_alerts_query.test.tsx +++ b/src/platform/packages/shared/kbn-alerts-ui-shared/src/common/hooks/use_find_alerts_query.test.tsx @@ -8,7 +8,7 @@ */ import React from 'react'; -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook, waitFor } from '@testing-library/react'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import { testQueryClientConfig } from '../test_utils/test_query_client_config'; import { httpServiceMock } from '@kbn/core-http-browser-mocks'; @@ -32,7 +32,7 @@ describe('useFindAlertsQuery', () => { }); it('calls the api correctly', async () => { - const { result, waitForValueToChange } = renderHook( + const { result } = renderHook( () => useFindAlertsQuery({ ...mockServices, @@ -43,7 +43,7 @@ describe('useFindAlertsQuery', () => { } ); - await waitForValueToChange(() => result.current.isLoading, { timeout: 5000 }); + await waitFor(() => expect(result.current.isLoading).toBe(true), { timeout: 5000 }); expect(mockServices.http.post).toHaveBeenCalledTimes(1); expect(mockServices.http.post).toBeCalledWith('/internal/rac/alerts/find', { diff --git a/src/platform/plugins/shared/share/public/components/context/index.test.tsx b/src/platform/plugins/shared/share/public/components/context/index.test.tsx index 162518e505b1d..3e0dde735ad06 100644 --- a/src/platform/plugins/shared/share/public/components/context/index.test.tsx +++ b/src/platform/plugins/shared/share/public/components/context/index.test.tsx @@ -7,18 +7,14 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { useShareTabsContext } from '.'; describe('share menu context', () => { describe('useShareTabsContext', () => { it('throws an error if used outside of ShareMenuProvider tree', () => { - const { result } = renderHook(() => useShareTabsContext()); - - expect(result.error?.message).toEqual( - expect.stringContaining( - 'Failed to call `useShareTabsContext` because the context from ShareMenuProvider is missing.' - ) + expect(() => renderHook(() => useShareTabsContext())).toThrow( + /^Failed to call `useShareTabsContext` because the context from ShareMenuProvider is missing./ ); }); }); diff --git a/x-pack/platform/packages/shared/kbn-elastic-assistant/impl/assistant/api/knowledge_base/entries/use_create_knowledge_base_entry.test.tsx b/x-pack/platform/packages/shared/kbn-elastic-assistant/impl/assistant/api/knowledge_base/entries/use_create_knowledge_base_entry.test.tsx index 73d0ddb976861..077f992e4124c 100644 --- a/x-pack/platform/packages/shared/kbn-elastic-assistant/impl/assistant/api/knowledge_base/entries/use_create_knowledge_base_entry.test.tsx +++ b/x-pack/platform/packages/shared/kbn-elastic-assistant/impl/assistant/api/knowledge_base/entries/use_create_knowledge_base_entry.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { act, renderHook } from '@testing-library/react-hooks'; +import { renderHook, act } from '@testing-library/react'; import { useCreateKnowledgeBaseEntry, UseCreateKnowledgeBaseEntryParams, diff --git a/x-pack/platform/packages/shared/kbn-elastic-assistant/impl/assistant/api/knowledge_base/entries/use_delete_knowledge_base_entries.test.tsx b/x-pack/platform/packages/shared/kbn-elastic-assistant/impl/assistant/api/knowledge_base/entries/use_delete_knowledge_base_entries.test.tsx index 6003b1f81f435..439460862e6d4 100644 --- a/x-pack/platform/packages/shared/kbn-elastic-assistant/impl/assistant/api/knowledge_base/entries/use_delete_knowledge_base_entries.test.tsx +++ b/x-pack/platform/packages/shared/kbn-elastic-assistant/impl/assistant/api/knowledge_base/entries/use_delete_knowledge_base_entries.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { act, renderHook } from '@testing-library/react-hooks'; +import { renderHook, act } from '@testing-library/react'; import { useDeleteKnowledgeBaseEntries, UseDeleteKnowledgeEntriesParams, diff --git a/x-pack/platform/packages/shared/kbn-elastic-assistant/impl/assistant/api/knowledge_base/entries/use_knowledge_base_entries.test.ts b/x-pack/platform/packages/shared/kbn-elastic-assistant/impl/assistant/api/knowledge_base/entries/use_knowledge_base_entries.test.ts index f298258800d35..b2fcafe00031e 100644 --- a/x-pack/platform/packages/shared/kbn-elastic-assistant/impl/assistant/api/knowledge_base/entries/use_knowledge_base_entries.test.ts +++ b/x-pack/platform/packages/shared/kbn-elastic-assistant/impl/assistant/api/knowledge_base/entries/use_knowledge_base_entries.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook, waitFor } from '@testing-library/react'; import { useKnowledgeBaseEntries } from './use_knowledge_base_entries'; import { HttpSetup } from '@kbn/core/public'; import { IToasts } from '@kbn/core-notifications-browser'; @@ -27,7 +27,7 @@ describe('useKnowledgeBaseEntries', () => { data: [{ id: '1', title: 'Entry 1' }], }); - const { result, waitForNextUpdate } = renderHook( + const { result } = renderHook( () => useKnowledgeBaseEntries({ http: httpMock, enabled: true }), { wrapper: TestProviders, @@ -35,32 +35,32 @@ describe('useKnowledgeBaseEntries', () => { ); expect(result.current.fetchStatus).toEqual('fetching'); - await waitForNextUpdate(); - - expect(result.current.data).toEqual({ - page: 1, - perPage: 100, - total: 1, - data: [{ id: '1', title: 'Entry 1' }], - }); + await waitFor(() => + expect(result.current.data).toEqual({ + page: 1, + perPage: 100, + total: 1, + data: [{ id: '1', title: 'Entry 1' }], + }) + ); }); it('handles fetch error', async () => { const error = new Error('Fetch error'); (httpMock.fetch as jest.Mock).mockRejectedValue(error); - const { waitForNextUpdate } = renderHook( + renderHook( () => useKnowledgeBaseEntries({ http: httpMock, toasts: toastsMock, enabled: true }), { wrapper: TestProviders, } ); - await waitForNextUpdate(); - - expect(toastsMock.addError).toHaveBeenCalledWith(error, { - title: 'Error fetching Knowledge Base entries', - }); + await waitFor(() => + expect(toastsMock.addError).toHaveBeenCalledWith(error, { + title: 'Error fetching Knowledge Base entries', + }) + ); }); it('does not fetch when disabled', async () => { diff --git a/x-pack/platform/packages/shared/kbn-elastic-assistant/impl/assistant/api/knowledge_base/entries/use_update_knowledge_base_entries.test.tsx b/x-pack/platform/packages/shared/kbn-elastic-assistant/impl/assistant/api/knowledge_base/entries/use_update_knowledge_base_entries.test.tsx index 0c35727846a01..141ea5b47a8b1 100644 --- a/x-pack/platform/packages/shared/kbn-elastic-assistant/impl/assistant/api/knowledge_base/entries/use_update_knowledge_base_entries.test.tsx +++ b/x-pack/platform/packages/shared/kbn-elastic-assistant/impl/assistant/api/knowledge_base/entries/use_update_knowledge_base_entries.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { act, renderHook } from '@testing-library/react-hooks'; +import { renderHook, act } from '@testing-library/react'; import { useUpdateKnowledgeBaseEntries, UseUpdateKnowledgeBaseEntriesParams, diff --git a/x-pack/platform/plugins/shared/cases/public/components/actions/status/use_should_disable_status.test.tsx b/x-pack/platform/plugins/shared/cases/public/components/actions/status/use_should_disable_status.test.tsx index 37957c9fe1f8e..f8360e64a533b 100644 --- a/x-pack/platform/plugins/shared/cases/public/components/actions/status/use_should_disable_status.test.tsx +++ b/x-pack/platform/plugins/shared/cases/public/components/actions/status/use_should_disable_status.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { CaseStatuses } from '../../../../common/types/domain'; import { useUserPermissions } from '../../user_actions/use_user_permissions'; import { useShouldDisableStatus } from './use_should_disable_status'; diff --git a/x-pack/platform/plugins/shared/cases/public/components/case_view/use_case_observables.test.ts b/x-pack/platform/plugins/shared/cases/public/components/case_view/use_case_observables.test.ts index 183619786deea..8598660670bb7 100644 --- a/x-pack/platform/plugins/shared/cases/public/components/case_view/use_case_observables.test.ts +++ b/x-pack/platform/plugins/shared/cases/public/components/case_view/use_case_observables.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { useCaseObservables } from './use_case_observables'; import { useGetCaseConfiguration } from '../../containers/configure/use_get_case_configuration'; import { OBSERVABLE_TYPES_BUILTIN_KEYS } from '../../../common/constants'; diff --git a/x-pack/platform/plugins/shared/cases/public/components/user_actions/use_user_permissions.test.tsx b/x-pack/platform/plugins/shared/cases/public/components/user_actions/use_user_permissions.test.tsx index e7c712b0df590..bb4c2eed838d2 100644 --- a/x-pack/platform/plugins/shared/cases/public/components/user_actions/use_user_permissions.test.tsx +++ b/x-pack/platform/plugins/shared/cases/public/components/user_actions/use_user_permissions.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { useCasesContext } from '../cases_context/use_cases_context'; import { useUserPermissions } from './use_user_permissions'; import type { UserActivityParams } from '../user_actions_activity_bar/types'; diff --git a/x-pack/platform/plugins/shared/cases/public/containers/use_delete_observables.test.tsx b/x-pack/platform/plugins/shared/cases/public/containers/use_delete_observables.test.tsx index adf0921820e72..ee241a1cce4ed 100644 --- a/x-pack/platform/plugins/shared/cases/public/containers/use_delete_observables.test.tsx +++ b/x-pack/platform/plugins/shared/cases/public/containers/use_delete_observables.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 { useDeleteObservable } from './use_delete_observables'; import { deleteObservable } from './api'; import { useCasesToast } from '../common/use_cases_toast'; @@ -35,7 +35,7 @@ describe('useDeleteObservable', () => { it('should call deleteObservable and show success toast on success', async () => { (deleteObservable as jest.Mock).mockResolvedValue({}); - const { result, waitFor } = renderHook(() => useDeleteObservable(caseId, observableId), { + const { result } = renderHook(() => useDeleteObservable(caseId, observableId), { wrapper: appMockRender.AppWrapper, }); @@ -52,7 +52,7 @@ describe('useDeleteObservable', () => { const error = new Error('Failed to delete observable'); (deleteObservable as jest.Mock).mockRejectedValue(error); - const { result, waitFor } = renderHook(() => useDeleteObservable(caseId, observableId), { + const { result } = renderHook(() => useDeleteObservable(caseId, observableId), { wrapper: appMockRender.AppWrapper, }); diff --git a/x-pack/platform/plugins/shared/cases/public/containers/use_get_similar_cases.test.tsx b/x-pack/platform/plugins/shared/cases/public/containers/use_get_similar_cases.test.tsx index 25bcadec2e7af..c934d691b5aa9 100644 --- a/x-pack/platform/plugins/shared/cases/public/containers/use_get_similar_cases.test.tsx +++ b/x-pack/platform/plugins/shared/cases/public/containers/use_get_similar_cases.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook, waitFor } from '@testing-library/react'; import * as api from './api'; import type { AppMockRenderer } from '../common/mock'; import { createAppMockRenderer } from '../common/mock'; @@ -30,12 +30,9 @@ describe('useGetSimilarCases', () => { it('calls getSimilarCases with correct arguments', async () => { const spyOnGetCases = jest.spyOn(api, 'getSimilarCases'); - const { waitFor } = renderHook( - () => useGetSimilarCases({ caseId: mockCase.id, perPage: 10, page: 0 }), - { - wrapper: appMockRender.AppWrapper, - } - ); + renderHook(() => useGetSimilarCases({ caseId: mockCase.id, perPage: 10, page: 0 }), { + wrapper: appMockRender.AppWrapper, + }); await waitFor(() => { expect(spyOnGetCases).toBeCalled(); @@ -58,12 +55,9 @@ describe('useGetSimilarCases', () => { const addError = jest.fn(); (useToasts as jest.Mock).mockReturnValue({ addSuccess, addError }); - const { waitFor } = renderHook( - () => useGetSimilarCases({ caseId: mockCase.id, perPage: 10, page: 0 }), - { - wrapper: appMockRender.AppWrapper, - } - ); + renderHook(() => useGetSimilarCases({ caseId: mockCase.id, perPage: 10, page: 0 }), { + wrapper: appMockRender.AppWrapper, + }); await waitFor(() => { expect(addError).toHaveBeenCalled(); diff --git a/x-pack/platform/plugins/shared/cases/public/containers/use_patch_observables.test.tsx b/x-pack/platform/plugins/shared/cases/public/containers/use_patch_observables.test.tsx index 98f1d50e77658..bcf3f88bf3914 100644 --- a/x-pack/platform/plugins/shared/cases/public/containers/use_patch_observables.test.tsx +++ b/x-pack/platform/plugins/shared/cases/public/containers/use_patch_observables.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 { usePatchObservable } from './use_patch_observables'; import { patchObservable } from './api'; import { useCasesToast } from '../common/use_cases_toast'; @@ -41,7 +41,7 @@ describe('usePatchObservable', () => { it('should call patchObservable and show success toast on success', async () => { (patchObservable as jest.Mock).mockResolvedValue({}); - const { result, waitFor } = renderHook(() => usePatchObservable(caseId, observableId), { + const { result } = renderHook(() => usePatchObservable(caseId, observableId), { wrapper: appMockRender.AppWrapper, }); @@ -60,7 +60,7 @@ describe('usePatchObservable', () => { const error = new Error('Failed to patch observable'); (patchObservable as jest.Mock).mockRejectedValue(error); - const { result, waitFor } = renderHook(() => usePatchObservable(caseId, observableId), { + const { result } = renderHook(() => usePatchObservable(caseId, observableId), { wrapper: appMockRender.AppWrapper, }); diff --git a/x-pack/platform/plugins/shared/cases/public/containers/use_post_observables.test.tsx b/x-pack/platform/plugins/shared/cases/public/containers/use_post_observables.test.tsx index 177b18d6b36de..1bd78f16d6dd6 100644 --- a/x-pack/platform/plugins/shared/cases/public/containers/use_post_observables.test.tsx +++ b/x-pack/platform/plugins/shared/cases/public/containers/use_post_observables.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 * as api from './api'; import { useToasts } from '../common/lib/kibana'; import type { AppMockRenderer } from '../common/mock'; @@ -41,7 +41,7 @@ describe('usePostObservables', () => { it('calls the api when invoked with the correct parameters', async () => { const spy = jest.spyOn(api, 'postObservable'); - const { waitForNextUpdate, result } = renderHook(() => usePostObservable(mockCase.id), { + const { result } = renderHook(() => usePostObservable(mockCase.id), { wrapper: appMockRender.AppWrapper, }); @@ -49,14 +49,14 @@ describe('usePostObservables', () => { result.current.mutate(observableMock); }); - await waitForNextUpdate(); - - expect(spy).toHaveBeenCalledWith({ observable: observableMock.observable }, mockCase.id); + await waitFor(() => + expect(spy).toHaveBeenCalledWith({ observable: observableMock.observable }, mockCase.id) + ); }); it('invalidates the queries correctly', async () => { const queryClientSpy = jest.spyOn(appMockRender.queryClient, 'invalidateQueries'); - const { waitForNextUpdate, result } = renderHook(() => usePostObservable(mockCase.id), { + const { result } = renderHook(() => usePostObservable(mockCase.id), { wrapper: appMockRender.AppWrapper, }); @@ -64,13 +64,11 @@ describe('usePostObservables', () => { result.current.mutate(observableMock); }); - await waitForNextUpdate(); - - expect(queryClientSpy).toHaveBeenCalledWith(casesQueriesKeys.caseView()); + await waitFor(() => expect(queryClientSpy).toHaveBeenCalledWith(casesQueriesKeys.caseView())); }); it('does shows a success toaster', async () => { - const { waitForNextUpdate, result } = renderHook(() => usePostObservable(mockCase.id), { + const { result } = renderHook(() => usePostObservable(mockCase.id), { wrapper: appMockRender.AppWrapper, }); @@ -78,9 +76,7 @@ describe('usePostObservables', () => { result.current.mutate(observableMock); }); - await waitForNextUpdate(); - - expect(addSuccess).toHaveBeenCalled(); + await waitFor(() => expect(addSuccess).toHaveBeenCalled()); }); it('shows a toast error when the api return an error', async () => { @@ -88,7 +84,7 @@ describe('usePostObservables', () => { .spyOn(api, 'postObservable') .mockRejectedValue(new Error('usePostObservables: Test error')); - const { waitForNextUpdate, result } = renderHook(() => usePostObservable(mockCase.id), { + const { result } = renderHook(() => usePostObservable(mockCase.id), { wrapper: appMockRender.AppWrapper, }); @@ -96,8 +92,6 @@ describe('usePostObservables', () => { result.current.mutate(observableMock); }); - await waitForNextUpdate(); - - expect(addError).toHaveBeenCalled(); + await waitFor(() => expect(addError).toHaveBeenCalled()); }); }); diff --git a/x-pack/platform/plugins/shared/triggers_actions_ui/public/application/sections/alerts_table/hooks/use_bulk_untrack_alerts_by_query.test.ts b/x-pack/platform/plugins/shared/triggers_actions_ui/public/application/sections/alerts_table/hooks/use_bulk_untrack_alerts_by_query.test.ts index 5de16dd70ce52..118d18f3820d9 100644 --- a/x-pack/platform/plugins/shared/triggers_actions_ui/public/application/sections/alerts_table/hooks/use_bulk_untrack_alerts_by_query.test.ts +++ b/x-pack/platform/plugins/shared/triggers_actions_ui/public/application/sections/alerts_table/hooks/use_bulk_untrack_alerts_by_query.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { act, renderHook } from '@testing-library/react-hooks'; +import { renderHook, act, waitFor } from '@testing-library/react'; import { AppMockRenderer, createAppMockRenderer } from '../../test_utils'; import { AlertsQueryContext } from '@kbn/alerts-ui-shared/src/common/contexts/alerts_query_context'; import { useBulkUntrackAlertsByQuery } from './use_bulk_untrack_alerts_by_query'; @@ -35,7 +35,7 @@ describe('useBulkUntrackAlertsByQuery', () => { it('calls the api when invoked with the correct parameters', async () => { httpMock.mockResolvedValue(response); - const { result, waitFor } = renderHook(() => useBulkUntrackAlertsByQuery(), { + const { result } = renderHook(() => useBulkUntrackAlertsByQuery(), { wrapper: appMockRender.AppWrapper, }); diff --git a/x-pack/plugins/lens/public/app_plugin/app_helpers.test.ts b/x-pack/plugins/lens/public/app_plugin/app_helpers.test.ts index 773276710f48d..15dc932ee8bfc 100644 --- a/x-pack/plugins/lens/public/app_plugin/app_helpers.test.ts +++ b/x-pack/plugins/lens/public/app_plugin/app_helpers.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook, act } from '@testing-library/react-hooks'; +import { renderHook, act } from '@testing-library/react'; import { faker } from '@faker-js/faker'; import { UseNavigateBackToAppProps, useNavigateBackToApp } from './app_helpers'; import { defaultDoc, makeDefaultServices } from '../mocks/services_mock'; diff --git a/x-pack/solutions/observability/plugins/apm/public/components/shared/summary/http_status_badge/http_status_badge.test.tsx b/x-pack/solutions/observability/plugins/apm/public/components/shared/summary/http_status_badge/http_status_badge.test.tsx index e7d1d388a9579..4b4913dc74c22 100644 --- a/x-pack/solutions/observability/plugins/apm/public/components/shared/summary/http_status_badge/http_status_badge.test.tsx +++ b/x-pack/solutions/observability/plugins/apm/public/components/shared/summary/http_status_badge/http_status_badge.test.tsx @@ -8,7 +8,7 @@ import React from 'react'; import { mount } from 'enzyme'; import { HttpStatusBadge } from '.'; -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { useEuiTheme } from '@elastic/eui'; describe('HttpStatusBadge', () => { diff --git a/x-pack/solutions/observability/plugins/infra/public/hooks/use_time_range.test.ts b/x-pack/solutions/observability/plugins/infra/public/hooks/use_time_range.test.ts index 03227d4bbf65e..4989f734440f3 100644 --- a/x-pack/solutions/observability/plugins/infra/public/hooks/use_time_range.test.ts +++ b/x-pack/solutions/observability/plugins/infra/public/hooks/use_time_range.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { useTimeRange } from './use_time_range'; import * as datemath from '../utils/datemath'; diff --git a/x-pack/solutions/observability/plugins/synthetics/public/apps/synthetics/components/monitors_page/hooks/use_monitor_filters.test.ts b/x-pack/solutions/observability/plugins/synthetics/public/apps/synthetics/components/monitors_page/hooks/use_monitor_filters.test.ts index 830e2bce119ce..70ca977ce1b59 100644 --- a/x-pack/solutions/observability/plugins/synthetics/public/apps/synthetics/components/monitors_page/hooks/use_monitor_filters.test.ts +++ b/x-pack/solutions/observability/plugins/synthetics/public/apps/synthetics/components/monitors_page/hooks/use_monitor_filters.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import * as spaceHook from '../../../../../hooks/use_kibana_space'; import * as paramHook from '../../../hooks/use_url_params'; import * as redux from 'react-redux'; diff --git a/x-pack/solutions/security/packages/kbn-cloud-security-posture/graph/src/hooks/use_fetch_graph_data.test.tsx b/x-pack/solutions/security/packages/kbn-cloud-security-posture/graph/src/hooks/use_fetch_graph_data.test.tsx index da5eaee9bfbf9..94327b66a3754 100644 --- a/x-pack/solutions/security/packages/kbn-cloud-security-posture/graph/src/hooks/use_fetch_graph_data.test.tsx +++ b/x-pack/solutions/security/packages/kbn-cloud-security-posture/graph/src/hooks/use_fetch_graph_data.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { useFetchGraphData } from './use_fetch_graph_data'; const mockUseQuery = jest.fn(); diff --git a/x-pack/solutions/security/plugins/security_solution/public/flyout/document_details/shared/hooks/use_navigate_to_graph_visualization.test.tsx b/x-pack/solutions/security/plugins/security_solution/public/flyout/document_details/shared/hooks/use_navigate_to_graph_visualization.test.tsx index 929dc208f3b38..d8c7ff66d9787 100644 --- a/x-pack/solutions/security/plugins/security_solution/public/flyout/document_details/shared/hooks/use_navigate_to_graph_visualization.test.tsx +++ b/x-pack/solutions/security/plugins/security_solution/public/flyout/document_details/shared/hooks/use_navigate_to_graph_visualization.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { useExpandableFlyoutApi } from '@kbn/expandable-flyout'; import { mockFlyoutApi } from '../mocks/mock_flyout_context'; import { useWhichFlyout } from './use_which_flyout'; diff --git a/x-pack/solutions/security/plugins/security_solution/public/flyout/document_details/shared/hooks/use_navigate_to_left_panel.test.tsx b/x-pack/solutions/security/plugins/security_solution/public/flyout/document_details/shared/hooks/use_navigate_to_left_panel.test.tsx index 41c0d85fa660f..29e34b3f83220 100644 --- a/x-pack/solutions/security/plugins/security_solution/public/flyout/document_details/shared/hooks/use_navigate_to_left_panel.test.tsx +++ b/x-pack/solutions/security/plugins/security_solution/public/flyout/document_details/shared/hooks/use_navigate_to_left_panel.test.tsx @@ -8,7 +8,7 @@ import { useNavigateToLeftPanel } from './use_navigate_to_left_panel'; import { useIsExperimentalFeatureEnabled } from '../../../../common/hooks/use_experimental_features'; import { useExpandableFlyoutApi } from '@kbn/expandable-flyout'; -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { useDocumentDetailsContext } from '../context'; import { mockFlyoutApi } from '../mocks/mock_flyout_context'; import { DocumentDetailsRightPanelKey, DocumentDetailsLeftPanelKey } from '../constants/panel_keys'; diff --git a/x-pack/solutions/security/plugins/security_solution/public/flyout/entity_details/host_right/hooks/use_navigate_to_host_details.test.ts b/x-pack/solutions/security/plugins/security_solution/public/flyout/entity_details/host_right/hooks/use_navigate_to_host_details.test.ts index 62b6c0ebe217b..aeeed8b030585 100644 --- a/x-pack/solutions/security/plugins/security_solution/public/flyout/entity_details/host_right/hooks/use_navigate_to_host_details.test.ts +++ b/x-pack/solutions/security/plugins/security_solution/public/flyout/entity_details/host_right/hooks/use_navigate_to_host_details.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { useNavigateToHostDetails } from './use_navigate_to_host_details'; import { useIsExperimentalFeatureEnabled } from '../../../../common/hooks/use_experimental_features'; import { useExpandableFlyoutApi } from '@kbn/expandable-flyout'; diff --git a/x-pack/solutions/security/plugins/security_solution/public/flyout/entity_details/user_right/hooks/use_navigate_to_user_details.test.ts b/x-pack/solutions/security/plugins/security_solution/public/flyout/entity_details/user_right/hooks/use_navigate_to_user_details.test.ts index 58f9860389d69..be5617eed96ad 100644 --- a/x-pack/solutions/security/plugins/security_solution/public/flyout/entity_details/user_right/hooks/use_navigate_to_user_details.test.ts +++ b/x-pack/solutions/security/plugins/security_solution/public/flyout/entity_details/user_right/hooks/use_navigate_to_user_details.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { renderHook } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react'; import { useNavigateToUserDetails } from './use_navigate_to_user_details'; import { useIsExperimentalFeatureEnabled } from '../../../../common/hooks/use_experimental_features'; import { useExpandableFlyoutApi } from '@kbn/expandable-flyout'; From 89ee0d3a3ffa28e8782fb2409e628f0d0e37795e Mon Sep 17 00:00:00 2001 From: Eyo Okon Eyo Date: Tue, 31 Dec 2024 15:39:19 +0100 Subject: [PATCH 2/3] add rule to prevent usage of the react-hooks testing library --- .eslintrc.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.eslintrc.js b/.eslintrc.js index 0e51464096200..804a45645b201 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -271,6 +271,10 @@ const RESTRICTED_IMPORTS = [ message: 'Please, use rxjs instead: rxjs/operators is just a subset, unnecessarily duplicating the package import.', }, + { + name: '@testing-library/react-hooks', + message: 'Please use @testing-library/react instead', + }, ]; module.exports = { From 72636c859b3916a02558633390e5e8681f8ac55d Mon Sep 17 00:00:00 2001 From: Eyo Okon Eyo Date: Thu, 2 Jan 2025 10:18:01 +0100 Subject: [PATCH 3/3] fix mock --- .../graph/src/hooks/use_fetch_graph_data.test.tsx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/x-pack/solutions/security/packages/kbn-cloud-security-posture/graph/src/hooks/use_fetch_graph_data.test.tsx b/x-pack/solutions/security/packages/kbn-cloud-security-posture/graph/src/hooks/use_fetch_graph_data.test.tsx index 94327b66a3754..7a3b010aca1d7 100644 --- a/x-pack/solutions/security/packages/kbn-cloud-security-posture/graph/src/hooks/use_fetch_graph_data.test.tsx +++ b/x-pack/solutions/security/packages/kbn-cloud-security-posture/graph/src/hooks/use_fetch_graph_data.test.tsx @@ -8,7 +8,12 @@ import { renderHook } from '@testing-library/react'; import { useFetchGraphData } from './use_fetch_graph_data'; -const mockUseQuery = jest.fn(); +const mockUseQuery = jest.fn((...args: unknown[]) => ({ + isLoading: true, + data: null, + isError: false, + isFetching: true, +})); jest.mock('@tanstack/react-query', () => { return {