Skip to content
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 @@ -10,27 +10,57 @@ import type { UserProfileWithAvatar } from '@kbn/user-profile-components';
import { act, fireEvent, render } from '@testing-library/react';
import React from 'react';
import { TestProviders } from '../../../mock';
import { useGetUserProfiles } from '../../../../detections/containers/detection_engine/alerts/use_get_user_profiles';
import { useSuggestUsers } from '../../../../detections/containers/detection_engine/alerts/use_suggest_users';

import { BulkAlertAssigneesPanel } from './alert_bulk_assignees';
import { ALERT_WORKFLOW_ASSIGNEE_IDS } from '@kbn/rule-data-utils';

jest.mock('../../../../detections/containers/detection_engine/alerts/use_get_user_profiles');
jest.mock('../../../../detections/containers/detection_engine/alerts/use_suggest_users');

const mockUserProfiles: UserProfileWithAvatar[] = [
{ uid: 'default-test-assignee-id-1', enabled: true, user: { username: 'user1' }, data: {} },
{ uid: 'default-test-assignee-id-2', enabled: true, user: { username: 'user2' }, data: {} },
{ uid: 'user-id-1', enabled: true, user: { username: 'user1' }, data: {} },
{ uid: 'user-id-2', enabled: true, user: { username: 'user2' }, data: {} },
];

const mockAssigneeItems = [
const mockSuggestedUserProfiles: UserProfileWithAvatar[] = [
...mockUserProfiles,
{ uid: 'user-id-3', enabled: true, user: { username: 'user3' }, data: {} },
{ uid: 'user-id-4', enabled: true, user: { username: 'user4' }, data: {} },
];

const mockAlertsWithAssignees = [
{
_id: 'test-id',
data: [{ field: ALERT_WORKFLOW_ASSIGNEE_IDS, value: ['assignee-id-1', 'assignee-id-2'] }],
data: [
{
field: ALERT_WORKFLOW_ASSIGNEE_IDS,
value: ['user-id-1', 'user-id-2'],
},
],
ecs: { _id: 'test-id' },
},
{
_id: 'test-id',
data: [
{
field: ALERT_WORKFLOW_ASSIGNEE_IDS,
value: ['user-id-1', 'user-id-2'],
},
],
ecs: { _id: 'test-id' },
},
];

(useSuggestUsers as jest.Mock).mockReturnValue({ loading: false, userProfiles: mockUserProfiles });
(useGetUserProfiles as jest.Mock).mockReturnValue({
loading: false,
userProfiles: mockUserProfiles,
});
(useSuggestUsers as jest.Mock).mockReturnValue({
loading: false,
userProfiles: mockSuggestedUserProfiles,
});

const renderAssigneesMenu = (
items: TimelineItem[],
Expand All @@ -56,7 +86,7 @@ describe('BulkAlertAssigneesPanel', () => {
});

test('it renders', () => {
const wrapper = renderAssigneesMenu(mockAssigneeItems);
const wrapper = renderAssigneesMenu(mockAlertsWithAssignees);

expect(wrapper.getByTestId('alert-assignees-update-button')).toBeInTheDocument();
expect(useSuggestUsers).toHaveBeenCalled();
Expand All @@ -67,25 +97,8 @@ describe('BulkAlertAssigneesPanel', () => {
const mockedOnSubmit = jest.fn();
const mockedSetIsLoading = jest.fn();

const mockAssignees = [
{
_id: 'test-id',
data: [{ field: ALERT_WORKFLOW_ASSIGNEE_IDS, value: ['default-test-assignee-id-1'] }],
ecs: { _id: 'test-id' },
},
{
_id: 'test-id',
data: [
{
field: ALERT_WORKFLOW_ASSIGNEE_IDS,
value: ['default-test-assignee-id-1', 'default-test-assignee-id-2'],
},
],
ecs: { _id: 'test-id' },
},
];
const wrapper = renderAssigneesMenu(
mockAssignees,
mockAlertsWithAssignees,
mockedClosePopover,
mockedOnSubmit,
mockedSetIsLoading
Expand All @@ -100,66 +113,41 @@ describe('BulkAlertAssigneesPanel', () => {
});

test('it updates state correctly', () => {
const mockAssignees = [
{
_id: 'test-id',
data: [{ field: ALERT_WORKFLOW_ASSIGNEE_IDS, value: ['default-test-assignee-id-1'] }],
ecs: { _id: 'test-id' },
},
{
_id: 'test-id',
data: [
{
field: ALERT_WORKFLOW_ASSIGNEE_IDS,
value: ['default-test-assignee-id-1', 'default-test-assignee-id-2'],
},
],
ecs: { _id: 'test-id' },
},
];
const wrapper = renderAssigneesMenu(mockAssignees);

expect(wrapper.getAllByRole('option')[0]).toHaveAttribute('title', 'user1');
expect(wrapper.getAllByRole('option')[0]).toBeChecked();
act(() => {
fireEvent.click(wrapper.getByText('user1'));
});
expect(wrapper.getAllByRole('option')[0]).toHaveAttribute('title', 'user1');
expect(wrapper.getAllByRole('option')[0]).not.toBeChecked();

expect(wrapper.getAllByRole('option')[1]).toHaveAttribute('title', 'user2');
expect(wrapper.getAllByRole('option')[1]).not.toBeChecked();
act(() => {
fireEvent.click(wrapper.getByText('user2'));
});
expect(wrapper.getAllByRole('option')[1]).toHaveAttribute('title', 'user2');
expect(wrapper.getAllByRole('option')[1]).toBeChecked();
const wrapper = renderAssigneesMenu(mockAlertsWithAssignees);

const deselectUser = (userName: string, index: number) => {
expect(wrapper.getAllByRole('option')[index]).toHaveAttribute('title', userName);
expect(wrapper.getAllByRole('option')[index]).toBeChecked();
act(() => {
fireEvent.click(wrapper.getByText(userName));
});
expect(wrapper.getAllByRole('option')[index]).toHaveAttribute('title', userName);
expect(wrapper.getAllByRole('option')[index]).not.toBeChecked();
};

const selectUser = (userName: string, index = 0) => {
expect(wrapper.getAllByRole('option')[index]).toHaveAttribute('title', userName);
expect(wrapper.getAllByRole('option')[index]).not.toBeChecked();
act(() => {
fireEvent.click(wrapper.getByText(userName));
});
expect(wrapper.getAllByRole('option')[index]).toHaveAttribute('title', userName);
expect(wrapper.getAllByRole('option')[index]).toBeChecked();
};

deselectUser('user1', 0);
deselectUser('user2', 1);
selectUser('user3', 2);
selectUser('user4', 3);
});

test('it calls expected functions on submit when alerts have changed', () => {
const mockedClosePopover = jest.fn();
const mockedOnSubmit = jest.fn();
const mockedSetIsLoading = jest.fn();

const mockAssignees = [
{
_id: 'test-id',
data: [{ field: ALERT_WORKFLOW_ASSIGNEE_IDS, value: ['default-test-assignee-id-1'] }],
ecs: { _id: 'test-id' },
},
{
_id: 'test-id',
data: [
{
field: ALERT_WORKFLOW_ASSIGNEE_IDS,
value: ['default-test-assignee-id-1', 'default-test-assignee-id-2'],
},
],
ecs: { _id: 'test-id' },
},
];
const wrapper = renderAssigneesMenu(
mockAssignees,
mockAlertsWithAssignees,
mockedClosePopover,
mockedOnSubmit,
mockedSetIsLoading
Expand All @@ -170,6 +158,12 @@ describe('BulkAlertAssigneesPanel', () => {
act(() => {
fireEvent.click(wrapper.getByText('user2'));
});
act(() => {
fireEvent.click(wrapper.getByText('user3'));
});
act(() => {
fireEvent.click(wrapper.getByText('user4'));
});

act(() => {
fireEvent.click(wrapper.getByTestId('alert-assignees-update-button'));
Expand All @@ -178,8 +172,8 @@ describe('BulkAlertAssigneesPanel', () => {
expect(mockedOnSubmit).toHaveBeenCalled();
expect(mockedOnSubmit).toHaveBeenCalledWith(
{
assignees_to_add: ['default-test-assignee-id-2'],
assignees_to_remove: ['default-test-assignee-id-1'],
assignees_to_add: ['user-id-4', 'user-id-3'],
assignees_to_remove: ['user-id-1', 'user-id-2'],
},
['test-id', 'test-id'],
expect.anything(), // An anonymous callback defined in the onSubmit function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import type {
} from './use_bulk_alert_assignees_items';
import { useBulkAlertAssigneesItems } from './use_bulk_alert_assignees_items';
import { useSetAlertAssignees } from './use_set_alert_assignees';
import { useGetUserProfiles } from '../../../../detections/containers/detection_engine/alerts/use_get_user_profiles';
import { useSuggestUsers } from '../../../../detections/containers/detection_engine/alerts/use_suggest_users';

jest.mock('./use_set_alert_assignees');
jest.mock('../../../../detections/containers/detection_engine/alerts/use_get_user_profiles');
jest.mock('../../../../detections/containers/detection_engine/alerts/use_suggest_users');

const mockUserProfiles: UserProfileWithAvatar[] = [
Expand Down Expand Up @@ -50,6 +52,10 @@ const renderPanel = (panel: UseBulkAlertAssigneesPanel) => {
describe('useBulkAlertAssigneesItems', () => {
beforeEach(() => {
(useSetAlertAssignees as jest.Mock).mockReturnValue(jest.fn());
(useGetUserProfiles as jest.Mock).mockReturnValue({
loading: false,
userProfiles: mockUserProfiles,
});
(useSuggestUsers as jest.Mock).mockReturnValue({
loading: false,
userProfiles: mockUserProfiles,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import React from 'react';
import type { EuiContextMenuPanelDescriptor } from '@elastic/eui';
import { EuiPopover, EuiContextMenu } from '@elastic/eui';
import { useSetAlertAssignees } from '../../../../common/components/toolbar/bulk_actions/use_set_alert_assignees';
import { useGetUserProfiles } from '../../../containers/detection_engine/alerts/use_get_user_profiles';
import { useSuggestUsers } from '../../../containers/detection_engine/alerts/use_suggest_users';

jest.mock('../../../containers/detection_engine/alerts/use_alerts_privileges');
jest.mock('../../../../common/components/toolbar/bulk_actions/use_set_alert_assignees');
jest.mock('../../../containers/detection_engine/alerts/use_get_user_profiles');
jest.mock('../../../containers/detection_engine/alerts/use_suggest_users');

const mockUserProfiles: UserProfileWithAvatar[] = [
Expand Down Expand Up @@ -160,6 +162,10 @@ describe('useAlertAssigneesActions', () => {

it('should render the nested panel', async () => {
(useSetAlertAssignees as jest.Mock).mockReturnValue(jest.fn());
(useGetUserProfiles as jest.Mock).mockReturnValue({
loading: false,
userProfiles: mockUserProfiles,
});
(useSuggestUsers as jest.Mock).mockReturnValue({
loading: false,
userProfiles: mockUserProfiles,
Expand Down