Skip to content

Commit

Permalink
fix: handle the undefined case correctly (elastic#142580)
Browse files Browse the repository at this point in the history
  • Loading branch information
janmonschke authored Oct 4, 2022
1 parent 4c18c0a commit 2a1df2d
Showing 3 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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(
<TestProviders>
<RelatedAlertsByProcessAncestry {...props} />
</TestProviders>
);

userEvent.click(screen.getByText(PROCESS_ANCESTRY));
await waitFor(() => {
expect(screen.getByText(PROCESS_ANCESTRY_EMPTY)).toBeInTheDocument();
});
});
});
Original file line number Diff line number Diff line change
@@ -70,15 +70,12 @@ export const RelatedAlertsByProcessAncestry = React.memo<Props>(
const [cache, setCache] = useState<Partial<Cache>>({});

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 (
<ActualRelatedAlertsByProcessAncestry
@@ -98,7 +95,7 @@ export const RelatedAlertsByProcessAncestry = React.memo<Props>(
onCacheLoad={setCache}
/>
);
}, [showContent, cache, data, eventId, timelineId, index, originalDocumentId, isEmpty]);
}, [showContent, cache, data, eventId, timelineId, index, originalDocumentId]);

return (
<InsightAccordion
@@ -143,7 +140,7 @@ const FetchAndNotifyCachedAlertsByProcessAncestry: React.FC<{
});

useEffect(() => {
if (alertIds) {
if (alertIds && alertIds.length !== 0) {
onCacheLoad({ alertIds });
}
}, [alertIds, onCacheLoad]);
@@ -152,6 +149,8 @@ const FetchAndNotifyCachedAlertsByProcessAncestry: React.FC<{
return <EuiLoadingSpinner />;
} else if (error) {
return <>{PROCESS_ANCESTRY_ERROR}</>;
} else if (!alertIds || alertIds.length === 0) {
return <>{PROCESS_ANCESTRY_EMPTY}</>;
}

return null;
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ interface UserAlertPrevalenceFromProcessTreeResult {
}

interface ProcessTreeAlertPrevalenceResponse {
alertIds: string[];
alertIds: string[] | undefined;
}

interface EntityResponse {

0 comments on commit 2a1df2d

Please sign in to comment.