From a80bf7447ae0e066330023c5617081545739e39c Mon Sep 17 00:00:00 2001 From: Eyo Okon Eyo Date: Wed, 18 Dec 2024 11:32:32 +0000 Subject: [PATCH] rewrite tests to remove hack for waitForNextUpdate --- .../ml/anomaly/use_anomalies_search.test.ts | 11 +++++----- .../use_messages_storage.test.tsx | 22 +++++++++---------- .../common/containers/source/index.test.tsx | 8 ++----- .../containers/use_full_screen/index.test.tsx | 3 +-- .../components/user_info/index.test.tsx | 1 - .../alerts/use_query.test.tsx | 4 +--- .../use_cell_actions.test.tsx | 10 ++++----- 7 files changed, 25 insertions(+), 34 deletions(-) diff --git a/x-pack/solutions/security/plugins/security_solution/public/common/components/ml/anomaly/use_anomalies_search.test.ts b/x-pack/solutions/security/plugins/security_solution/public/common/components/ml/anomaly/use_anomalies_search.test.ts index a12ecc13e9c9b..993f7c0b47b59 100644 --- a/x-pack/solutions/security/plugins/security_solution/public/common/components/ml/anomaly/use_anomalies_search.test.ts +++ b/x-pack/solutions/security/plugins/security_solution/public/common/components/ml/anomaly/use_anomalies_search.test.ts @@ -75,8 +75,6 @@ describe('useAggregatedAnomaliesByJob', () => { wrapper: TestProviders, }); - await waitFor(() => new Promise((resolve) => resolve(null))); - act(() => { result.current.refetch(); }); @@ -145,12 +143,13 @@ describe('useAggregatedAnomaliesByJob', () => { const { result } = renderHook(() => useAggregatedAnomaliesByJob({ skip: false, from, to }), { wrapper: TestProviders, }); - await waitFor(() => new Promise((resolve) => resolve(null))); - const names = result.current.data.map(({ name }) => name); + await waitFor(() => { + const names = result.current.data.map(({ name }) => name); - expect(names[0]).toEqual(firstJobSecurityName); - expect(names[1]).toEqual(secondJobSecurityName); + expect(names[0]).toEqual(firstJobSecurityName); + expect(names[1]).toEqual(secondJobSecurityName); + }); }); it('does not throw error when aggregations is undefined', async () => { diff --git a/x-pack/solutions/security/plugins/security_solution/public/common/containers/local_storage/use_messages_storage.test.tsx b/x-pack/solutions/security/plugins/security_solution/public/common/containers/local_storage/use_messages_storage.test.tsx index 008ec6b0bf005..c9452309fcfe9 100644 --- a/x-pack/solutions/security/plugins/security_solution/public/common/containers/local_storage/use_messages_storage.test.tsx +++ b/x-pack/solutions/security/plugins/security_solution/public/common/containers/local_storage/use_messages_storage.test.tsx @@ -18,56 +18,57 @@ describe('useLocalStorage', () => { it('should return an empty array when there is no messages', async () => { const { result } = renderHook(() => useMessagesStorage()); - await waitFor(() => new Promise((resolve) => resolve(null))); const { getMessages } = result.current; - act(() => { + await waitFor(() => { expect(getMessages('case')).toEqual([]); }); }); it('should add a message', async () => { const { result } = renderHook(() => useMessagesStorage()); - await waitFor(() => new Promise((resolve) => resolve(null))); const { getMessages, addMessage } = result.current; act(() => { addMessage('case', 'id-1'); - expect(getMessages('case')).toEqual(['id-1']); }); + + await waitFor(() => expect(getMessages('case')).toEqual(['id-1'])); }); it('should add multiple messages', async () => { const { result } = renderHook(() => useMessagesStorage()); - await waitFor(() => new Promise((resolve) => resolve(null))); const { getMessages, addMessage } = result.current; act(() => { addMessage('case', 'id-1'); addMessage('case', 'id-2'); - expect(getMessages('case')).toEqual(['id-1', 'id-2']); }); + + await waitFor(() => expect(getMessages('case')).toEqual(['id-1', 'id-2'])); }); it('should remove a message', async () => { const { result } = renderHook(() => useMessagesStorage()); - await waitFor(() => new Promise((resolve) => resolve(null))); const { getMessages, addMessage, removeMessage } = result.current; await act(async () => { addMessage('case', 'id-1'); addMessage('case', 'id-2'); removeMessage('case', 'id-2'); - expect(getMessages('case')).toEqual(['id-1']); }); + + await waitFor(() => expect(getMessages('case')).toEqual(['id-1'])); }); it('should return presence of a message', async () => { const { result } = renderHook(() => useMessagesStorage()); - await waitFor(() => new Promise((resolve) => resolve(null))); const { hasMessage, addMessage, removeMessage } = result.current; await act(async () => { addMessage('case', 'id-1'); addMessage('case', 'id-2'); removeMessage('case', 'id-2'); + }); + + await waitFor(() => { expect(hasMessage('case', 'id-1')).toEqual(true); expect(hasMessage('case', 'id-2')).toEqual(false); }); @@ -75,13 +76,12 @@ describe('useLocalStorage', () => { it('should clear all messages', async () => { const { result } = renderHook(() => useMessagesStorage()); - await waitFor(() => new Promise((resolve) => resolve(null))); const { getMessages, addMessage, clearAllMessages } = result.current; await act(async () => { addMessage('case', 'id-1'); addMessage('case', 'id-2'); clearAllMessages('case'); - expect(getMessages('case')).toEqual([]); }); + await waitFor(() => expect(getMessages('case')).toEqual([])); }); }); diff --git a/x-pack/solutions/security/plugins/security_solution/public/common/containers/source/index.test.tsx b/x-pack/solutions/security/plugins/security_solution/public/common/containers/source/index.test.tsx index ffc25a30f8985..3f59b65a8fc53 100644 --- a/x-pack/solutions/security/plugins/security_solution/public/common/containers/source/index.test.tsx +++ b/x-pack/solutions/security/plugins/security_solution/public/common/containers/source/index.test.tsx @@ -9,7 +9,7 @@ import type { IndexFieldSearch } from './use_data_view'; import { useDataView } from './use_data_view'; import { mocksSource } from './mock'; import { mockGlobalState, TestProviders } from '../../mock'; -import { act, waitFor, renderHook } from '@testing-library/react'; +import { act, renderHook } from '@testing-library/react'; import { useKibana } from '../../lib/kibana'; const mockDispatch = jest.fn(); @@ -87,8 +87,6 @@ describe('source/index.tsx', () => { wrapper: TestProviders, }); - await waitFor(() => new Promise((resolve) => resolve(null))); - await act(async () => { await result.current.indexFieldsSearch({ dataViewId: 'neato' }); }); @@ -108,8 +106,6 @@ describe('source/index.tsx', () => { wrapper: TestProviders, }); - await waitFor(() => new Promise((resolve) => resolve(null))); - await act(async () => { indexFieldsSearch = result.current.indexFieldsSearch; }); @@ -135,7 +131,7 @@ describe('source/index.tsx', () => { const { result } = renderHook(() => useDataView(), { wrapper: TestProviders, }); - await waitFor(() => new Promise((resolve) => resolve(null))); + await act(async () => { indexFieldsSearch = result.current.indexFieldsSearch; }); diff --git a/x-pack/solutions/security/plugins/security_solution/public/common/containers/use_full_screen/index.test.tsx b/x-pack/solutions/security/plugins/security_solution/public/common/containers/use_full_screen/index.test.tsx index 0e0ffba4933b3..7013bdcc55550 100644 --- a/x-pack/solutions/security/plugins/security_solution/public/common/containers/use_full_screen/index.test.tsx +++ b/x-pack/solutions/security/plugins/security_solution/public/common/containers/use_full_screen/index.test.tsx @@ -58,7 +58,6 @@ describe('useFullScreen', () => { wrapper: WrapperContainer, })); - await waitFor(() => new Promise((resolve) => resolve(null))); act(() => { result.current.setGlobalFullScreen(true); }); @@ -78,7 +77,7 @@ describe('useFullScreen', () => { ({ result } = renderHook(() => useGlobalFullScreen(), { wrapper: WrapperContainer, })); - await waitFor(() => new Promise((resolve) => resolve(null))); + act(() => { result.current.setGlobalFullScreen(false); }); diff --git a/x-pack/solutions/security/plugins/security_solution/public/detections/components/user_info/index.test.tsx b/x-pack/solutions/security/plugins/security_solution/public/detections/components/user_info/index.test.tsx index 862736dec3fa7..8e84d1a5e4f0b 100644 --- a/x-pack/solutions/security/plugins/security_solution/public/detections/components/user_info/index.test.tsx +++ b/x-pack/solutions/security/plugins/security_solution/public/detections/components/user_info/index.test.tsx @@ -41,7 +41,6 @@ describe('useUserInfo', () => { const { result } = renderHook(() => useUserInfo(), { wrapper: TestProviders, }); - await waitFor(() => new Promise((resolve) => resolve(null))); expect(result.current).toEqual({ canUserCRUD: null, diff --git a/x-pack/solutions/security/plugins/security_solution/public/detections/containers/detection_engine/alerts/use_query.test.tsx b/x-pack/solutions/security/plugins/security_solution/public/detections/containers/detection_engine/alerts/use_query.test.tsx index 4640256eace3c..b799410e3a5e7 100644 --- a/x-pack/solutions/security/plugins/security_solution/public/detections/containers/detection_engine/alerts/use_query.test.tsx +++ b/x-pack/solutions/security/plugins/security_solution/public/detections/containers/detection_engine/alerts/use_query.test.tsx @@ -73,7 +73,7 @@ describe('useQueryAlerts', () => { initialProps: [mockAlertsQuery, indexName], } ); - await waitFor(() => new Promise((resolve) => resolve(null))); + rerender([mockAlertsQuery, 'new-mock-index-name']); await waitFor(() => expect(spyOnfetchRules).toHaveBeenCalledTimes(2)); }); @@ -116,8 +116,6 @@ describe('useQueryAlerts', () => { const localProps = { ...defaultProps, skip: false }; const { rerender } = renderHook(() => useQueryAlerts(localProps)); - await waitFor(() => new Promise((resolve) => resolve(null))); - localProps.skip = true; rerender(); rerender(); diff --git a/x-pack/solutions/security/plugins/security_solution/public/detections/hooks/trigger_actions_alert_table/use_cell_actions.test.tsx b/x-pack/solutions/security/plugins/security_solution/public/detections/hooks/trigger_actions_alert_table/use_cell_actions.test.tsx index f025dd6a971ab..618c1b1ba2cc0 100644 --- a/x-pack/solutions/security/plugins/security_solution/public/detections/hooks/trigger_actions_alert_table/use_cell_actions.test.tsx +++ b/x-pack/solutions/security/plugins/security_solution/public/detections/hooks/trigger_actions_alert_table/use_cell_actions.test.tsx @@ -100,13 +100,13 @@ describe('getUseCellActionsHook', () => { } ); - await waitFor(() => new Promise((resolve) => resolve(null))); + await waitFor(() => { + const cellAction = result.current.getCellActions('host.name', 0)[0]; - const cellAction = result.current.getCellActions('host.name', 0)[0]; + renderCellAction(cellAction); - renderCellAction(cellAction); - - expect(screen.getByTestId('dataGridColumnCellAction-action1')).toBeInTheDocument(); + expect(screen.getByTestId('dataGridColumnCellAction-action1')).toBeInTheDocument(); + }); }); it('should not render cell actions correctly for eventRendered view', async () => {