forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[7.8] [SIEM][CASE] Persist callout when dismissed (elastic#68372) (el…
…astic#70150) # Conflicts: # x-pack/plugins/security_solution/package.json # x-pack/plugins/security_solution/public/alerts/components/no_write_alerts_callout/translations.ts # x-pack/plugins/security_solution/public/cases/components/callout/index.test.tsx # x-pack/plugins/security_solution/public/cases/components/callout/translations.ts # x-pack/plugins/security_solution/public/cases/components/use_push_to_service/helpers.tsx # x-pack/plugins/security_solution/public/cases/components/use_push_to_service/index.test.tsx # x-pack/plugins/security_solution/public/cases/components/use_push_to_service/index.tsx # x-pack/plugins/security_solution/public/cases/pages/case.tsx # x-pack/plugins/security_solution/public/cases/pages/case_details.tsx # x-pack/plugins/security_solution/public/common/mock/kibana_react.ts # x-pack/plugins/security_solution/public/timelines/components/timeline/header/translations.ts # x-pack/plugins/siem/public/containers/local_storage/use_messages_storage.test.tsx # x-pack/plugins/siem/public/containers/local_storage/use_messages_storage.tsx # x-pack/plugins/siem/public/pages/case/components/callout/callout.test.tsx # x-pack/plugins/siem/public/pages/case/components/callout/callout.tsx # x-pack/plugins/siem/public/pages/case/components/callout/helpers.test.tsx # x-pack/plugins/siem/public/pages/case/components/callout/types.ts
- Loading branch information
Showing
22 changed files
with
654 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
x-pack/plugins/siem/public/containers/local_storage/use_messages_storage.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { useKibana } from '../../lib/kibana'; | ||
import { createUseKibanaMock } from '../../mock/kibana_react'; | ||
import { useMessagesStorage, UseMessagesStorage } from './use_messages_storage'; | ||
|
||
jest.mock('../../lib/kibana'); | ||
const useKibanaMock = useKibana as jest.Mock; | ||
|
||
describe('useLocalStorage', () => { | ||
beforeEach(() => { | ||
const services = { ...createUseKibanaMock()().services }; | ||
useKibanaMock.mockImplementation(() => ({ services })); | ||
services.storage.store.clear(); | ||
}); | ||
|
||
it('should return an empty array when there is no messages', async () => { | ||
await act(async () => { | ||
const { result, waitForNextUpdate } = renderHook<string, UseMessagesStorage>(() => | ||
useMessagesStorage() | ||
); | ||
await waitForNextUpdate(); | ||
const { getMessages } = result.current; | ||
expect(getMessages('case')).toEqual([]); | ||
}); | ||
}); | ||
|
||
it('should add a message', async () => { | ||
await act(async () => { | ||
const { result, waitForNextUpdate } = renderHook<string, UseMessagesStorage>(() => | ||
useMessagesStorage() | ||
); | ||
await waitForNextUpdate(); | ||
const { getMessages, addMessage } = result.current; | ||
addMessage('case', 'id-1'); | ||
expect(getMessages('case')).toEqual(['id-1']); | ||
}); | ||
}); | ||
|
||
it('should add multiple messages', async () => { | ||
await act(async () => { | ||
const { result, waitForNextUpdate } = renderHook<string, UseMessagesStorage>(() => | ||
useMessagesStorage() | ||
); | ||
await waitForNextUpdate(); | ||
const { getMessages, addMessage } = result.current; | ||
addMessage('case', 'id-1'); | ||
addMessage('case', 'id-2'); | ||
expect(getMessages('case')).toEqual(['id-1', 'id-2']); | ||
}); | ||
}); | ||
|
||
it('should remove a message', async () => { | ||
await act(async () => { | ||
const { result, waitForNextUpdate } = renderHook<string, UseMessagesStorage>(() => | ||
useMessagesStorage() | ||
); | ||
await waitForNextUpdate(); | ||
const { getMessages, addMessage, removeMessage } = result.current; | ||
addMessage('case', 'id-1'); | ||
addMessage('case', 'id-2'); | ||
removeMessage('case', 'id-2'); | ||
expect(getMessages('case')).toEqual(['id-1']); | ||
}); | ||
}); | ||
|
||
it('should clear all messages', async () => { | ||
await act(async () => { | ||
const { result, waitForNextUpdate } = renderHook<string, UseMessagesStorage>(() => | ||
useMessagesStorage() | ||
); | ||
await waitForNextUpdate(); | ||
const { getMessages, addMessage, clearAllMessages } = result.current; | ||
addMessage('case', 'id-1'); | ||
addMessage('case', 'id-2'); | ||
clearAllMessages('case'); | ||
expect(getMessages('case')).toEqual([]); | ||
}); | ||
}); | ||
}); |
52 changes: 52 additions & 0 deletions
52
x-pack/plugins/siem/public/containers/local_storage/use_messages_storage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { useCallback } from 'react'; | ||
import { useKibana } from '../../lib/kibana'; | ||
|
||
export interface UseMessagesStorage { | ||
getMessages: (plugin: string) => string[]; | ||
addMessage: (plugin: string, id: string) => void; | ||
removeMessage: (plugin: string, id: string) => void; | ||
clearAllMessages: (plugin: string) => void; | ||
} | ||
|
||
export const useMessagesStorage = (): UseMessagesStorage => { | ||
const { storage } = useKibana().services; | ||
|
||
const getMessages = useCallback( | ||
(plugin: string): string[] => storage.get(`${plugin}-messages`) ?? [], | ||
[storage] | ||
); | ||
|
||
const addMessage = useCallback( | ||
(plugin: string, id: string) => { | ||
const pluginStorage = storage.get(`${plugin}-messages`) ?? []; | ||
storage.set(`${plugin}-messages`, [...pluginStorage, id]); | ||
}, | ||
[storage] | ||
); | ||
|
||
const removeMessage = useCallback( | ||
(plugin: string, id: string) => { | ||
const pluginStorage = storage.get(`${plugin}-messages`) ?? []; | ||
storage.set(`${plugin}-messages`, [...pluginStorage.filter((val: string) => val !== id)]); | ||
}, | ||
[storage] | ||
); | ||
|
||
const clearAllMessages = useCallback( | ||
(plugin: string): string[] => storage.remove(`${plugin}-messages`), | ||
[storage] | ||
); | ||
|
||
return { | ||
getMessages, | ||
addMessage, | ||
clearAllMessages, | ||
removeMessage, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { IStorage, Storage } from '../../../../../src/plugins/kibana_utils/public'; | ||
|
||
export const localStorageMock = (): IStorage => { | ||
let store: Record<string, unknown> = {}; | ||
|
||
return { | ||
getItem: (key: string) => { | ||
return store[key] || null; | ||
}, | ||
setItem: (key: string, value: unknown) => { | ||
store[key] = value; | ||
}, | ||
clear() { | ||
store = {}; | ||
}, | ||
removeItem(key: string) { | ||
delete store[key]; | ||
}, | ||
}; | ||
}; | ||
|
||
export const createSIEMStorageMock = () => { | ||
const localStorage = localStorageMock(); | ||
return { | ||
localStorage, | ||
storage: new Storage(localStorage), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
x-pack/plugins/siem/public/pages/case/components/callout/callout.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
|
||
import { CallOut, CallOutProps } from './callout'; | ||
|
||
describe('Callout', () => { | ||
const defaultProps: CallOutProps = { | ||
id: 'md5-hex', | ||
type: 'primary', | ||
title: 'a tittle', | ||
messages: [ | ||
{ | ||
id: 'generic-error', | ||
title: 'message-one', | ||
description: <p>{'error'}</p>, | ||
}, | ||
], | ||
showCallOut: true, | ||
handleDismissCallout: jest.fn(), | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('It renders the callout', () => { | ||
const wrapper = mount(<CallOut {...defaultProps} />); | ||
expect(wrapper.find(`[data-test-subj="case-callout-md5-hex"]`).exists()).toBeTruthy(); | ||
expect(wrapper.find(`[data-test-subj="callout-messages-md5-hex"]`).exists()).toBeTruthy(); | ||
expect(wrapper.find(`[data-test-subj="callout-dismiss-md5-hex"]`).exists()).toBeTruthy(); | ||
}); | ||
|
||
it('hides the callout', () => { | ||
const wrapper = mount(<CallOut {...defaultProps} showCallOut={false} />); | ||
expect(wrapper.find(`[data-test-subj="case-callout-md5-hex"]`).exists()).toBeFalsy(); | ||
}); | ||
|
||
it('does not shows any messages when the list is empty', () => { | ||
const wrapper = mount(<CallOut {...defaultProps} messages={[]} />); | ||
expect(wrapper.find(`[data-test-subj="callout-messages-md5-hex"]`).exists()).toBeFalsy(); | ||
}); | ||
|
||
it('transform the button color correctly - primary', () => { | ||
const wrapper = mount(<CallOut {...defaultProps} />); | ||
const className = | ||
wrapper.find(`button[data-test-subj="callout-dismiss-md5-hex"]`).first().prop('className') ?? | ||
''; | ||
expect(className.includes('euiButton--primary')).toBeTruthy(); | ||
}); | ||
|
||
it('transform the button color correctly - success', () => { | ||
const wrapper = mount(<CallOut {...defaultProps} type={'success'} />); | ||
const className = | ||
wrapper.find(`button[data-test-subj="callout-dismiss-md5-hex"]`).first().prop('className') ?? | ||
''; | ||
expect(className.includes('euiButton--secondary')).toBeTruthy(); | ||
}); | ||
|
||
it('transform the button color correctly - warning', () => { | ||
const wrapper = mount(<CallOut {...defaultProps} type={'warning'} />); | ||
const className = | ||
wrapper.find(`button[data-test-subj="callout-dismiss-md5-hex"]`).first().prop('className') ?? | ||
''; | ||
expect(className.includes('euiButton--warning')).toBeTruthy(); | ||
}); | ||
|
||
it('transform the button color correctly - danger', () => { | ||
const wrapper = mount(<CallOut {...defaultProps} type={'danger'} />); | ||
const className = | ||
wrapper.find(`button[data-test-subj="callout-dismiss-md5-hex"]`).first().prop('className') ?? | ||
''; | ||
expect(className.includes('euiButton--danger')).toBeTruthy(); | ||
}); | ||
|
||
it('dismiss the callout correctly', () => { | ||
const wrapper = mount(<CallOut {...defaultProps} messages={[]} />); | ||
expect(wrapper.find(`[data-test-subj="callout-dismiss-md5-hex"]`).exists()).toBeTruthy(); | ||
wrapper.find(`button[data-test-subj="callout-dismiss-md5-hex"]`).simulate('click'); | ||
wrapper.update(); | ||
|
||
expect(defaultProps.handleDismissCallout).toHaveBeenCalledWith('md5-hex', 'primary'); | ||
}); | ||
}); |
53 changes: 53 additions & 0 deletions
53
x-pack/plugins/siem/public/pages/case/components/callout/callout.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { EuiCallOut, EuiButton, EuiDescriptionList } from '@elastic/eui'; | ||
import { isEmpty } from 'lodash/fp'; | ||
import React, { memo, useCallback } from 'react'; | ||
|
||
import { ErrorMessage } from './types'; | ||
import * as i18n from './translations'; | ||
|
||
export interface CallOutProps { | ||
id: string; | ||
type: NonNullable<ErrorMessage['errorType']>; | ||
title: string; | ||
messages: ErrorMessage[]; | ||
showCallOut: boolean; | ||
handleDismissCallout: (id: string, type: NonNullable<ErrorMessage['errorType']>) => void; | ||
} | ||
|
||
const CallOutComponent = ({ | ||
id, | ||
type, | ||
title, | ||
messages, | ||
showCallOut, | ||
handleDismissCallout, | ||
}: CallOutProps) => { | ||
const handleCallOut = useCallback(() => handleDismissCallout(id, type), [ | ||
handleDismissCallout, | ||
id, | ||
type, | ||
]); | ||
|
||
return showCallOut ? ( | ||
<EuiCallOut title={title} color={type} iconType="gear" data-test-subj={`case-callout-${id}`}> | ||
{!isEmpty(messages) && ( | ||
<EuiDescriptionList data-test-subj={`callout-messages-${id}`} listItems={messages} /> | ||
)} | ||
<EuiButton | ||
data-test-subj={`callout-dismiss-${id}`} | ||
color={type === 'success' ? 'secondary' : type} | ||
onClick={handleCallOut} | ||
> | ||
{i18n.DISMISS_CALLOUT} | ||
</EuiButton> | ||
</EuiCallOut> | ||
) : null; | ||
}; | ||
|
||
export const CallOut = memo(CallOutComponent); |
Oops, something went wrong.