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

Confirmation Modal - show warning message when nothing has been changed in modal. #199523

Merged
merged 13 commits into from
Nov 12, 2024
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
@@ -0,0 +1,89 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React from 'react';
import { fireEvent, render, screen } from '@testing-library/react';
import { EntityStoreEnablementModal } from './enablement_modal';
import { useEntityEnginePrivileges } from '../hooks/use_entity_engine_privileges';
import { TestProviders } from '../../../../common/mock';

const mockToggle = jest.fn();
const mockEnableStore = jest.fn(() => jest.fn());
jest.mock('../hooks/use_entity_engine_privileges', () => ({
useEntityEnginePrivileges: jest.fn(),
}));

const defaultProps = {
visible: true,
toggle: mockToggle,
enableStore: mockEnableStore,
riskScore: { disabled: false, checked: false },
entityStore: { disabled: false, checked: false },
};

const renderComponent = (props = defaultProps) => {
return render(<EntityStoreEnablementModal {...props} />, { wrapper: TestProviders });
};

describe('EntityStoreEnablementModal', () => {
beforeEach(() => {
jest.clearAllMocks();
(useEntityEnginePrivileges as jest.Mock).mockReturnValue({
data: {
privileges: {
elasticsearch: {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not completely 100% on the contents of this required for the test

index: {},
},
kibana: {},
},
},
isLoading: false,
});
});

it('should render the modal when visible is true', () => {
renderComponent();
expect(screen.getByRole('dialog')).toBeInTheDocument();
});

it('should not render the modal when visible is false', () => {
renderComponent({ ...defaultProps, visible: false });
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
});

it('should call toggle function when cancel button is clicked', () => {
renderComponent();
fireEvent.click(screen.getByText('Cancel'));
expect(mockToggle).toHaveBeenCalledWith(false);
});

it('should call enableStore function when enable button is clicked', () => {
renderComponent({
...defaultProps,
riskScore: { ...defaultProps.riskScore, checked: true },
entityStore: { ...defaultProps.entityStore, checked: true },
});
fireEvent.click(screen.getByText('Enable'));
expect(mockEnableStore).toHaveBeenCalledWith({ riskScore: true, entityStore: true });
});

it('should display proceed warning when no enablement options are selected', () => {
renderComponent();
expect(screen.getByText('Please enable at least one option to proceed.')).toBeInTheDocument();
});

it('should disable the enable button when enablementOptions are false', () => {
renderComponent({
...defaultProps,
riskScore: { ...defaultProps.riskScore, checked: false },
entityStore: { ...defaultProps.entityStore, checked: false },
});

const enableButton = screen.getByRole('button', { name: /Enable/i });
expect(enableButton).toBeDisabled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ import {
EuiButtonEmpty,
EuiBetaBadge,
EuiToolTip,
EuiCallOut,
useEuiTheme,
} from '@elastic/eui';
import { css } from '@emotion/react';
import React, { useState } from 'react';
import { FormattedMessage } from '@kbn/i18n-react';
import { TECHNICAL_PREVIEW, TECHNICAL_PREVIEW_TOOLTIP } from '../../../../common/translations';
import {
ENABLEMENT_DESCRIPTION_RISK_ENGINE_ONLY,
ENABLEMENT_DESCRIPTION_ENTITY_STORE_ONLY,
ENABLEMENT_WARNING_SELECT_TO_PROCEED,
} from '../translations';
import { useEntityEnginePrivileges } from '../hooks/use_entity_engine_privileges';
import { MissingPrivilegesCallout } from './missing_privileges_callout';
Expand Down Expand Up @@ -57,15 +61,28 @@ export const EntityStoreEnablementModal: React.FC<EntityStoreEnablementModalProp
riskScore,
entityStore,
}) => {
const { euiTheme } = useEuiTheme();
const [enablements, setEnablements] = useState({
riskScore: !!riskScore.checked,
entityStore: !!entityStore.checked,
});
const { data: privileges, isLoading: isLoadingPrivileges } = useEntityEnginePrivileges();
const enablementOptions = enablements.riskScore || enablements.entityStore;

if (!visible) {
return null;
}
const proceedWarning = (
<EuiCallOut
size="s"
color="danger"
css={css`
border-radius: ${euiTheme.border.radius.medium};
`}
>
<p>{ENABLEMENT_WARNING_SELECT_TO_PROCEED}</p>
</EuiCallOut>
);
return (
<EuiModal onClose={() => toggle(false)}>
<EuiModalHeader>
Expand Down Expand Up @@ -131,13 +148,25 @@ export const EntityStoreEnablementModal: React.FC<EntityStoreEnablementModalProp
</EuiModalBody>

<EuiModalFooter>
<EuiButtonEmpty onClick={() => toggle(false)}>{'Cancel'}</EuiButtonEmpty>
<EuiButton onClick={enableStore(enablements)} fill>
<FormattedMessage
id="xpack.securitySolution.entityAnalytics.enablements.modal.enable"
defaultMessage="Enable"
/>
</EuiButton>
<EuiFlexGroup justifyContent="flexEnd" alignItems="center">
{!enablementOptions ? <EuiFlexItem>{proceedWarning}</EuiFlexItem> : null}
<EuiFlexItem grow={false}>
<EuiFlexGroup direction="row" justifyContent="flexEnd">
<EuiButtonEmpty onClick={() => toggle(false)}>{'Cancel'}</EuiButtonEmpty>
CAWilson94 marked this conversation as resolved.
Show resolved Hide resolved
<EuiButton
onClick={enableStore(enablements)}
fill
isDisabled={!enablementOptions}
aria-disabled={!enablementOptions}
>
<FormattedMessage
id="xpack.securitySolution.entityAnalytics.enablements.modal.enable"
defaultMessage="Enable"
/>
</EuiButton>
</EuiFlexGroup>
</EuiFlexItem>
</EuiFlexGroup>
</EuiModalFooter>
</EuiModal>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,10 @@ export const ENABLEMENT_DESCRIPTION_BOTH = i18n.translate(
'Your entity store is currently empty. Add information about your entities directly from your logs, or import them using a text file.',
}
);

export const ENABLEMENT_WARNING_SELECT_TO_PROCEED = i18n.translate(
'xpack.securitySolution.entityAnalytics.entityStore.enablement.description.enablementWarningMessage',
{
defaultMessage: 'Please enable at least one option to proceed.',
}
);