Skip to content

Commit 47ee05f

Browse files
committed
Improve api
1 parent 53b191f commit 47ee05f

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

x-pack/plugins/security_solution/public/cases/components/use_push_to_service/index.test.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import { usePushToService, ReturnUsePushToService, UsePushToService } from '.';
1010
import { TestProviders } from '../../../common/mock';
1111
import { usePostPushToService } from '../../containers/use_post_push_to_service';
1212
import { basicPush, actionLicenses } from '../../containers/mock';
13-
import * as i18n from './translations';
1413
import { useGetActionLicense } from '../../containers/use_get_action_license';
15-
import { getKibanaConfigError, getLicenseError } from './helpers';
1614
import { connectorsMock } from '../../containers/configure/mock';
1715

1816
jest.mock('react-router-dom', () => {

x-pack/plugins/security_solution/public/common/containers/local_storage/use_messages_storage.test.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,32 @@ describe('useLocalStorage', () => {
5454
expect(getMessages('case')).toEqual(['id-1', 'id-2']);
5555
});
5656
});
57+
58+
it('should remove a message', async () => {
59+
await act(async () => {
60+
const { result, waitForNextUpdate } = renderHook<string, UseMessagesStorage>(() =>
61+
useMessagesStorage()
62+
);
63+
await waitForNextUpdate();
64+
const { getMessages, addMessage, removeMessage } = result.current;
65+
addMessage('case', 'id-1');
66+
addMessage('case', 'id-2');
67+
removeMessage('case', 'id-2');
68+
expect(getMessages('case')).toEqual(['id-1']);
69+
});
70+
});
71+
72+
it('should clear all messages', async () => {
73+
await act(async () => {
74+
const { result, waitForNextUpdate } = renderHook<string, UseMessagesStorage>(() =>
75+
useMessagesStorage()
76+
);
77+
await waitForNextUpdate();
78+
const { getMessages, addMessage, clearAllMessages } = result.current;
79+
addMessage('case', 'id-1');
80+
addMessage('case', 'id-2');
81+
clearAllMessages('case');
82+
expect(getMessages('case')).toEqual([]);
83+
});
84+
});
5785
});

x-pack/plugins/security_solution/public/common/containers/local_storage/use_messages_storage.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import { useKibana } from '../../lib/kibana';
1010
export interface UseMessagesStorage {
1111
getMessages: (plugin: string) => string[];
1212
addMessage: (plugin: string, id: string) => void;
13+
removeMessage: (plugin: string, id: string) => void;
14+
clearAllMessages: (plugin: string) => void;
1315
}
1416

1517
export const useMessagesStorage = (): UseMessagesStorage => {
1618
const { storage } = useKibana().services;
1719

1820
const getMessages = useCallback(
19-
(plugin: string): string[] => {
20-
return storage.get(`${plugin}-messages`) ?? [];
21-
},
21+
(plugin: string): string[] => storage.get(`${plugin}-messages`) ?? [],
2222
[storage]
2323
);
2424

@@ -30,8 +30,23 @@ export const useMessagesStorage = (): UseMessagesStorage => {
3030
[storage]
3131
);
3232

33+
const removeMessage = useCallback(
34+
(plugin: string, id: string) => {
35+
const pluginStorage = storage.get(`${plugin}-messages`) ?? [];
36+
storage.set(`${plugin}-messages`, [...pluginStorage.filter((val: string) => val !== id)]);
37+
},
38+
[storage]
39+
);
40+
41+
const clearAllMessages = useCallback(
42+
(plugin: string): string[] => storage.remove(`${plugin}-messages`),
43+
[storage]
44+
);
45+
3346
return {
3447
getMessages,
3548
addMessage,
49+
clearAllMessages,
50+
removeMessage,
3651
};
3752
};

0 commit comments

Comments
 (0)