Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Security Solution][Timeline] fix wrong add to favorite within timeline guided tour #175993

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ jest.mock('react-redux', () => {
};
});

const renderAddFavoritesButton = () =>
const renderAddFavoritesButton = (isPartOfGuidedTour = false) =>
render(
<TestProviders>
<AddToFavoritesButton timelineId="timeline-1" />
<AddToFavoritesButton timelineId="timeline-1" isPartOfGuidedTour={isPartOfGuidedTour} />
</TestProviders>
);

Expand All @@ -49,6 +49,7 @@ describe('AddToFavoritesButton', () => {
const button = getByTestId('timeline-favorite-empty-star');

expect(button).toBeInTheDocument();
expect(button).toHaveProperty('id', '');
expect(button.firstChild).toHaveAttribute('data-euiicon-type', 'starEmpty');
expect(queryByTestId('timeline-favorite-filled-star')).not.toBeInTheDocument();
});
Expand Down Expand Up @@ -86,4 +87,17 @@ describe('AddToFavoritesButton', () => {
expect(getByTestId('timeline-favorite-filled-star')).toBeInTheDocument();
expect(queryByTestId('timeline-favorite-empty-star')).not.toBeInTheDocument();
});

it('should use id for guided tour if prop is true', () => {
mockGetState.mockReturnValue({
...mockTimelineModel,
status: TimelineStatus.active,
});

const { getByTestId } = renderAddFavoritesButton(true);

const button = getByTestId('timeline-favorite-empty-star');

expect(button).toHaveProperty('id', 'add-to-favorites');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -34,40 +34,46 @@ interface AddToFavoritesButtonProps {
* Id of the timeline to be displayed in the bottom bar and within the modal
*/
timelineId: string;
/**
* Whether the button is a step in the timeline guided tour
*/
isPartOfGuidedTour?: boolean;
}

/**
* This component renders the add to favorites button for timeline.
* It is used in the bottom bar as well as in the timeline modal's header.
*/
export const AddToFavoritesButton = React.memo<AddToFavoritesButtonProps>(({ timelineId }) => {
const dispatch = useDispatch();
const { isFavorite, status } = useSelector((state: State) =>
selectTimelineById(state, timelineId)
);
export const AddToFavoritesButton = React.memo<AddToFavoritesButtonProps>(
({ timelineId, isPartOfGuidedTour = false }) => {
const dispatch = useDispatch();
const { isFavorite, status } = useSelector((state: State) =>
selectTimelineById(state, timelineId)
);

const isTimelineDraftOrImmutable = status !== TimelineStatus.active;
const label = isFavorite ? REMOVE_FROM_FAVORITES : ADD_TO_FAVORITES;
const isTimelineDraftOrImmutable = status !== TimelineStatus.active;
const label = isFavorite ? REMOVE_FROM_FAVORITES : ADD_TO_FAVORITES;

const handleClick = useCallback(
() => dispatch(timelineActions.updateIsFavorite({ id: timelineId, isFavorite: !isFavorite })),
[dispatch, timelineId, isFavorite]
);
const handleClick = useCallback(
() => dispatch(timelineActions.updateIsFavorite({ id: timelineId, isFavorite: !isFavorite })),
[dispatch, timelineId, isFavorite]
);

return (
<EuiButtonIcon
id={TIMELINE_TOUR_CONFIG_ANCHORS.ADD_TO_FAVORITES}
iconType={isFavorite ? 'starFilled' : 'starEmpty'}
isSelected={isFavorite}
disabled={isTimelineDraftOrImmutable}
aria-label={label}
title={label}
data-test-subj={`timeline-favorite-${isFavorite ? 'filled' : 'empty'}-star`}
onClick={handleClick}
>
{label}
</EuiButtonIcon>
);
});
return (
<EuiButtonIcon
id={isPartOfGuidedTour ? TIMELINE_TOUR_CONFIG_ANCHORS.ADD_TO_FAVORITES : undefined}
iconType={isFavorite ? 'starFilled' : 'starEmpty'}
isSelected={isFavorite}
disabled={isTimelineDraftOrImmutable}
aria-label={label}
title={label}
data-test-subj={`timeline-favorite-${isFavorite ? 'filled' : 'empty'}-star`}
onClick={handleClick}
>
{label}
</EuiButtonIcon>
);
}
);

AddToFavoritesButton.displayName = 'AddToFavoritesButton';
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('TimelineBottomBar', () => {
expect(getByTestId('timeline-event-count-badge')).toBeInTheDocument();
expect(getByTestId('timeline-save-status')).toBeInTheDocument();
expect(getByTestId('timeline-favorite-empty-star')).toBeInTheDocument();
expect(getByTestId('timeline-favorite-empty-star')).toHaveProperty('id', '');
});

test('should not render the event count badge if timeline is open', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export const TimelineModalHeader = React.memo<FlyoutHeaderPanelProps>(({ timelin
<EuiFlexItem grow={false}>
<EuiFlexGroup gutterSize="xs" alignItems="center" responsive={false}>
<EuiFlexItem grow={false}>
<AddToFavoritesButton timelineId={timelineId} />
<AddToFavoritesButton timelineId={timelineId} isPartOfGuidedTour />
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiText
Expand Down