From 3c665d72f35e5b30c57ff80d11d6126a2bd21bbb Mon Sep 17 00:00:00 2001 From: Charlotte Alexandra Wilson Date: Tue, 12 Nov 2024 18:21:50 +0000 Subject: [PATCH] Confirmation Modal - show warning message when nothing has been changed in modal. (#199523) (cherry picked from commit 5d0b62ce9eb19a23cc8abfd6506be3f0f687ee80) --- .../components/enablement_modal.test.tsx | 89 +++++++++++++++++++ .../components/enablement_modal.tsx | 43 +++++++-- .../components/entity_store/translations.ts | 7 ++ 3 files changed, 132 insertions(+), 7 deletions(-) create mode 100644 x-pack/plugins/security_solution/public/entity_analytics/components/entity_store/components/enablement_modal.test.tsx diff --git a/x-pack/plugins/security_solution/public/entity_analytics/components/entity_store/components/enablement_modal.test.tsx b/x-pack/plugins/security_solution/public/entity_analytics/components/entity_store/components/enablement_modal.test.tsx new file mode 100644 index 0000000000000..360e28ec41518 --- /dev/null +++ b/x-pack/plugins/security_solution/public/entity_analytics/components/entity_store/components/enablement_modal.test.tsx @@ -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(, { wrapper: TestProviders }); +}; + +describe('EntityStoreEnablementModal', () => { + beforeEach(() => { + jest.clearAllMocks(); + (useEntityEnginePrivileges as jest.Mock).mockReturnValue({ + data: { + privileges: { + elasticsearch: { + 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(); + }); +}); diff --git a/x-pack/plugins/security_solution/public/entity_analytics/components/entity_store/components/enablement_modal.tsx b/x-pack/plugins/security_solution/public/entity_analytics/components/entity_store/components/enablement_modal.tsx index 6c2528620eb4c..73d65176d116b 100644 --- a/x-pack/plugins/security_solution/public/entity_analytics/components/entity_store/components/enablement_modal.tsx +++ b/x-pack/plugins/security_solution/public/entity_analytics/components/entity_store/components/enablement_modal.tsx @@ -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'; @@ -57,15 +61,28 @@ export const EntityStoreEnablementModal: React.FC { + 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 = ( + +

{ENABLEMENT_WARNING_SELECT_TO_PROCEED}

+
+ ); return ( toggle(false)}> @@ -131,13 +148,25 @@ export const EntityStoreEnablementModal: React.FC - toggle(false)}>{'Cancel'} - - - + + {!enablementOptions ? {proceedWarning} : null} + + + toggle(false)}>{'Cancel'} + + + + + + ); diff --git a/x-pack/plugins/security_solution/public/entity_analytics/components/entity_store/translations.ts b/x-pack/plugins/security_solution/public/entity_analytics/components/entity_store/translations.ts index 4c113dd533acb..f2920612543d9 100644 --- a/x-pack/plugins/security_solution/public/entity_analytics/components/entity_store/translations.ts +++ b/x-pack/plugins/security_solution/public/entity_analytics/components/entity_store/translations.ts @@ -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.', + } +);