Skip to content

Commit

Permalink
[Security Solution] Fix the status of timelines' bulk actions (#74560)
Browse files Browse the repository at this point in the history
* fix bulk actions

* fix lint error

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
angorayc and elasticmachine committed Aug 13, 2020
1 parent 290f9bf commit 1a09c87
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { EuiContextMenuPanel, EuiContextMenuItem, EuiBasicTable } from '@elastic/eui';
import React, { useCallback, useMemo } from 'react';

import { TimelineType, TimelineStatus } from '../../../../common/types/timeline';
import { TimelineType } from '../../../../common/types/timeline';

import * as i18n from './translations';
import { DeleteTimelines, OpenTimelineResult } from './types';
Expand Down Expand Up @@ -66,7 +66,7 @@ export const useEditTimelineBatchActions = ({

const getBatchItemsPopoverContent = useCallback(
(closePopover: () => void) => {
const disabled = selectedItems?.some((item) => item.status === TimelineStatus.immutable);
const disabled = selectedItems == null || selectedItems.length === 0;
return (
<>
<EditTimelineActions
Expand All @@ -87,6 +87,7 @@ export const useEditTimelineBatchActions = ({
<EuiContextMenuPanel
items={[
<EuiContextMenuItem
data-test-subj="export-timeline-action"
disabled={disabled}
icon="exportAction"
key="ExportItemKey"
Expand All @@ -95,6 +96,7 @@ export const useEditTimelineBatchActions = ({
{i18n.EXPORT_SELECTED}
</EuiContextMenuItem>,
<EuiContextMenuItem
data-test-subj="delete-timeline-action"
disabled={disabled}
icon="trash"
key="DeleteItemKey"
Expand All @@ -107,7 +109,6 @@ export const useEditTimelineBatchActions = ({
</>
);
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[
selectedItems,
deleteTimelines,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { cloneDeep } from 'lodash/fp';
import { mountWithIntl } from 'test_utils/enzyme_helpers';
import React from 'react';
import { ThemeProvider } from 'styled-components';
import { act } from '@testing-library/react';

import '../../../common/mock/match_media';
import { DEFAULT_SEARCH_RESULTS_PER_PAGE } from '../../pages/timelines_page';
Expand Down Expand Up @@ -51,7 +52,7 @@ describe('OpenTimeline', () => {
title,
timelineType: TimelineType.default,
timelineStatus: TimelineStatus.active,
templateTimelineFilter: [<div />],
templateTimelineFilter: [<div key="mock-a" />, <div key="mock-b" />],
totalSearchResultsCount: mockSearchResults.length,
});

Expand Down Expand Up @@ -279,6 +280,86 @@ describe('OpenTimeline', () => {
expect(wrapper.find('[data-test-subj="utility-bar-action"]').exists()).toEqual(true);
});

test('it should disable export-timeline if no timeline is selected', async () => {
const defaultProps = {
...getDefaultTestProps(mockResults),
timelineStatus: null,
selectedItems: [],
};
const wrapper = mountWithIntl(
<ThemeProvider theme={theme}>
<OpenTimeline {...defaultProps} />
</ThemeProvider>
);

wrapper.find('[data-test-subj="utility-bar-action"]').find('EuiLink').simulate('click');
await act(async () => {
expect(
wrapper.find('[data-test-subj="export-timeline-action"]').first().prop('disabled')
).toEqual(true);
});
});

test('it should disable delete timeline if no timeline is selected', async () => {
const defaultProps = {
...getDefaultTestProps(mockResults),
timelineStatus: null,
selectedItems: [],
};
const wrapper = mountWithIntl(
<ThemeProvider theme={theme}>
<OpenTimeline {...defaultProps} />
</ThemeProvider>
);

wrapper.find('[data-test-subj="utility-bar-action"]').find('EuiLink').simulate('click');
await act(async () => {
expect(
wrapper.find('[data-test-subj="delete-timeline-action"]').first().prop('disabled')
).toEqual(true);
});
});

test('it should enable export-timeline if a timeline is selected', async () => {
const defaultProps = {
...getDefaultTestProps(mockResults),
timelineStatus: null,
selectedItems: [{}],
};
const wrapper = mountWithIntl(
<ThemeProvider theme={theme}>
<OpenTimeline {...defaultProps} />
</ThemeProvider>
);

wrapper.find('[data-test-subj="utility-bar-action"]').find('EuiLink').simulate('click');
await act(async () => {
expect(
wrapper.find('[data-test-subj="export-timeline-action"]').first().prop('disabled')
).toEqual(false);
});
});

test('it should enable delete timeline if a timeline is selected', async () => {
const defaultProps = {
...getDefaultTestProps(mockResults),
timelineStatus: null,
selectedItems: [{}],
};
const wrapper = mountWithIntl(
<ThemeProvider theme={theme}>
<OpenTimeline {...defaultProps} />
</ThemeProvider>
);

wrapper.find('[data-test-subj="utility-bar-action"]').find('EuiLink').simulate('click');
await act(async () => {
expect(
wrapper.find('[data-test-subj="delete-timeline-action"]').first().prop('disabled')
).toEqual(false);
});
});

test("it should render a selectable timeline table if timelineStatus is active (selecting custom templates' tab)", () => {
const defaultProps = {
...getDefaultTestProps(mockResults),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ export const OpenTimeline = React.memo<OpenTimelineProps>(
}, [onDeleteSelected, deleteTimelines, timelineStatus]);

const SearchRowContent = useMemo(() => <>{templateTimelineFilter}</>, [templateTimelineFilter]);

return (
<>
<EditOneTimelineAction
Expand Down

0 comments on commit 1a09c87

Please sign in to comment.