From e28687ab9b894384333570f4a9fc9ba7201d5fb4 Mon Sep 17 00:00:00 2001 From: Jan Monschke Date: Tue, 4 Oct 2022 18:11:10 +0200 Subject: [PATCH] fix: handle the `undefined` case correctly (#142580) --- ...elated_alerts_by_process_ancestry.test.tsx | 21 ++++++++++++++++++- .../related_alerts_by_process_ancestry.tsx | 9 ++++---- .../use_alert_prevalence_from_process_tree.ts | 2 +- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/x-pack/plugins/security_solution/public/common/components/event_details/insights/related_alerts_by_process_ancestry.test.tsx b/x-pack/plugins/security_solution/public/common/components/event_details/insights/related_alerts_by_process_ancestry.test.tsx index f95bf9234cc16..bd91f55d704da 100644 --- a/x-pack/plugins/security_solution/public/common/components/event_details/insights/related_alerts_by_process_ancestry.test.tsx +++ b/x-pack/plugins/security_solution/public/common/components/event_details/insights/related_alerts_by_process_ancestry.test.tsx @@ -116,7 +116,7 @@ describe('RelatedAlertsByProcessAncestry', () => { }); }); - it('renders a special message when there are no alerts to display', async () => { + it('renders a special message when there are no alerts to display (empty response)', async () => { mockUseAlertPrevalenceFromProcessTree.mockReturnValue({ loading: false, error: false, @@ -134,4 +134,23 @@ describe('RelatedAlertsByProcessAncestry', () => { expect(screen.getByText(PROCESS_ANCESTRY_EMPTY)).toBeInTheDocument(); }); }); + + it('renders a special message when there are no alerts to display (undefined case)', async () => { + mockUseAlertPrevalenceFromProcessTree.mockReturnValue({ + loading: false, + error: false, + alertIds: undefined, + }); + + render( + + + + ); + + userEvent.click(screen.getByText(PROCESS_ANCESTRY)); + await waitFor(() => { + expect(screen.getByText(PROCESS_ANCESTRY_EMPTY)).toBeInTheDocument(); + }); + }); }); diff --git a/x-pack/plugins/security_solution/public/common/components/event_details/insights/related_alerts_by_process_ancestry.tsx b/x-pack/plugins/security_solution/public/common/components/event_details/insights/related_alerts_by_process_ancestry.tsx index 330cb7ae113b3..28737c60f4e07 100644 --- a/x-pack/plugins/security_solution/public/common/components/event_details/insights/related_alerts_by_process_ancestry.tsx +++ b/x-pack/plugins/security_solution/public/common/components/event_details/insights/related_alerts_by_process_ancestry.tsx @@ -70,15 +70,12 @@ export const RelatedAlertsByProcessAncestry = React.memo( const [cache, setCache] = useState>({}); const onToggle = useCallback((isOpen: boolean) => setShowContent(isOpen), []); - const isEmpty = !!cache.alertIds && cache.alertIds.length === 0; // Makes sure the component is not fetching data before the accordion // has been openend. const renderContent = useCallback(() => { if (!showContent) { return null; - } else if (isEmpty) { - return PROCESS_ANCESTRY_EMPTY; } else if (cache.alertIds) { return ( ( onCacheLoad={setCache} /> ); - }, [showContent, cache, data, eventId, timelineId, index, originalDocumentId, isEmpty]); + }, [showContent, cache, data, eventId, timelineId, index, originalDocumentId]); return ( { - if (alertIds) { + if (alertIds && alertIds.length !== 0) { onCacheLoad({ alertIds }); } }, [alertIds, onCacheLoad]); @@ -152,6 +149,8 @@ const FetchAndNotifyCachedAlertsByProcessAncestry: React.FC<{ return ; } else if (error) { return <>{PROCESS_ANCESTRY_ERROR}; + } else if (!alertIds || alertIds.length === 0) { + return <>{PROCESS_ANCESTRY_EMPTY}; } return null; diff --git a/x-pack/plugins/security_solution/public/common/containers/alerts/use_alert_prevalence_from_process_tree.ts b/x-pack/plugins/security_solution/public/common/containers/alerts/use_alert_prevalence_from_process_tree.ts index 1a59271614c57..e3bc22ec2decb 100644 --- a/x-pack/plugins/security_solution/public/common/containers/alerts/use_alert_prevalence_from_process_tree.ts +++ b/x-pack/plugins/security_solution/public/common/containers/alerts/use_alert_prevalence_from_process_tree.ts @@ -18,7 +18,7 @@ interface UserAlertPrevalenceFromProcessTreeResult { } interface ProcessTreeAlertPrevalenceResponse { - alertIds: string[]; + alertIds: string[] | undefined; } interface EntityResponse {