From 89ca1b01c228d3c993007477b940e50611fcb58c Mon Sep 17 00:00:00 2001 From: Scotty Bollinger Date: Wed, 16 Sep 2020 18:06:34 -0500 Subject: [PATCH] [Enterprise Search] Add helpers for flash messages (#77677) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [Enterprise Search] Add a helper for success messages * Move method to separate file * Update test names * D R Y * I really need to slow down 🤦🏼‍♂️ * PR feedback * Typo --- .../shared/flash_messages/index.ts | 1 + .../set_message_helpers.test.ts | 53 +++++++++++++++++++ .../flash_messages/set_message_helpers.ts | 28 ++++++++++ 3 files changed, 82 insertions(+) create mode 100644 x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/set_message_helpers.test.ts create mode 100644 x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/set_message_helpers.ts diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/index.ts b/x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/index.ts index c03954179184e..c4daeb44420c8 100644 --- a/x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/index.ts +++ b/x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/index.ts @@ -13,3 +13,4 @@ export { } from './flash_messages_logic'; export { FlashMessagesProvider } from './flash_messages_provider'; export { flashAPIErrors } from './handle_api_errors'; +export { setSuccessMessage, setErrorMessage, setQueuedSuccessMessage } from './set_message_helpers'; diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/set_message_helpers.test.ts b/x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/set_message_helpers.test.ts new file mode 100644 index 0000000000000..c3c60d77f4577 --- /dev/null +++ b/x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/set_message_helpers.test.ts @@ -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 { + FlashMessagesLogic, + setSuccessMessage, + setErrorMessage, + setQueuedSuccessMessage, +} from './'; + +describe('Flash Message Helpers', () => { + const message = 'I am a message'; + + beforeEach(() => { + FlashMessagesLogic.mount(); + }); + + it('setSuccessMessage()', () => { + setSuccessMessage(message); + + expect(FlashMessagesLogic.values.messages).toEqual([ + { + message, + type: 'success', + }, + ]); + }); + + it('setErrorMessage()', () => { + setErrorMessage(message); + + expect(FlashMessagesLogic.values.messages).toEqual([ + { + message, + type: 'error', + }, + ]); + }); + + it('setQueuedSuccessMessage()', () => { + setQueuedSuccessMessage(message); + + expect(FlashMessagesLogic.values.queuedMessages).toEqual([ + { + message, + type: 'success', + }, + ]); + }); +}); diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/set_message_helpers.ts b/x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/set_message_helpers.ts new file mode 100644 index 0000000000000..6abb540b7c14b --- /dev/null +++ b/x-pack/plugins/enterprise_search/public/applications/shared/flash_messages/set_message_helpers.ts @@ -0,0 +1,28 @@ +/* + * 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 { FlashMessagesLogic } from './'; + +export const setSuccessMessage = (message: string) => { + FlashMessagesLogic.actions.setFlashMessages({ + type: 'success', + message, + }); +}; + +export const setErrorMessage = (message: string) => { + FlashMessagesLogic.actions.setFlashMessages({ + type: 'error', + message, + }); +}; + +export const setQueuedSuccessMessage = (message: string) => { + FlashMessagesLogic.actions.setQueuedMessages({ + type: 'success', + message, + }); +};