From 9a0ef27d52ac7a18b63cb5717ad6a38f43f004e8 Mon Sep 17 00:00:00 2001 From: Oyelola Victoria Date: Sat, 21 Sep 2024 12:40:11 +0000 Subject: [PATCH] add test and refactor `getFormattedEmailOptions` Signed-off-by: Oyelola Victoria --- .../email/email_params.test.tsx | 46 +++++++++++++++++++ .../connector_types/email/email_params.tsx | 26 +++++++---- 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/x-pack/plugins/stack_connectors/public/connector_types/email/email_params.test.tsx b/x-pack/plugins/stack_connectors/public/connector_types/email/email_params.test.tsx index 76cc3b136455a..676c6ef85a308 100644 --- a/x-pack/plugins/stack_connectors/public/connector_types/email/email_params.test.tsx +++ b/x-pack/plugins/stack_connectors/public/connector_types/email/email_params.test.tsx @@ -13,6 +13,7 @@ import { useKibana } from '@kbn/kibana-react-plugin/public'; import { triggersActionsUiMock } from '@kbn/triggers-actions-ui-plugin/public/mocks'; import EmailParamsFields from './email_params'; import { getIsExperimentalFeatureEnabled } from '../../common/get_experimental_features'; +import { getFormattedEmailOptions } from './email_params'; jest.mock('@kbn/kibana-react-plugin/public', () => ({ useKibana: jest.fn(), @@ -234,3 +235,48 @@ describe('EmailParamsFields renders', () => { expect(editAction).not.toHaveBeenCalled(); }); }); + +describe('getFormattedEmailOptions', () => { + test('should return new options added to previous options', () => { + const searchValue = 'test@test.com, other@test.com'; + const previousOptions = [{ label: 'existing@test.com' }]; + const newOptions = getFormattedEmailOptions(searchValue, previousOptions); + + expect(newOptions).toEqual([ + { label: 'existing@test.com' }, + { label: 'test@test.com' }, + { label: 'other@test.com' }, + ]); + }); + + test('should trim extra spaces in search value', () => { + const searchValue = ' test@test.com , other@test.com , '; + const previousOptions: Array<{ label: string }> = []; + const newOptions = getFormattedEmailOptions(searchValue, previousOptions); + + expect(newOptions).toEqual([{ label: 'test@test.com' }, { label: 'other@test.com' }]); + }); + + test('should prevent duplicate email addresses', () => { + const searchValue = 'duplicate@test.com, duplicate@test.com'; + const previousOptions = [{ label: 'existing@test.com' }, { label: 'duplicate@test.com' }]; + const newOptions = getFormattedEmailOptions(searchValue, previousOptions); + + expect(newOptions).toEqual([{ label: 'existing@test.com' }, { label: 'duplicate@test.com' }]); + }); + + test('should return previous options if search value is empty', () => { + const searchValue = ''; + const previousOptions = [{ label: 'existing@test.com' }]; + const newOptions = getFormattedEmailOptions(searchValue, previousOptions); + expect(newOptions).toEqual([{ label: 'existing@test.com' }]); + }); + + test('should handle single email without comma', () => { + const searchValue = 'single@test.com'; + const previousOptions = [{ label: 'existing@test.com' }]; + const newOptions = getFormattedEmailOptions(searchValue, previousOptions); + + expect(newOptions).toEqual([{ label: 'existing@test.com' }, { label: 'single@test.com' }]); + }); +}); diff --git a/x-pack/plugins/stack_connectors/public/connector_types/email/email_params.tsx b/x-pack/plugins/stack_connectors/public/connector_types/email/email_params.tsx index 47bf8b18bab8d..9e4525234129c 100644 --- a/x-pack/plugins/stack_connectors/public/connector_types/email/email_params.tsx +++ b/x-pack/plugins/stack_connectors/public/connector_types/email/email_params.tsx @@ -18,6 +18,22 @@ import { EmailActionParams } from '../types'; const noop = () => {}; +export const getFormattedEmailOptions = ( + searchValue: string, + previousOptions: Array<{ label: string }> +): Array<{ label: string }> => { + if (!searchValue.trim()) return previousOptions; + const previousEmails: string[] = previousOptions.map((option) => option.label); + const newEmails: string[] = searchValue.split(',').reduce((result, email) => { + const trimmedEmail: string = email.trim(); + if (trimmedEmail) result.push(trimmedEmail); + return result; + }, [] as string[]); + const allUniqueEmails: string[] = Array.from(new Set([...previousEmails, ...newEmails])); + const formattedOptions = allUniqueEmails.map((email) => ({ label: email })); + return formattedOptions; +}; + export const EmailParamsFields = ({ actionParams, editAction, @@ -64,16 +80,6 @@ export const EmailParamsFields = ({ const isBCCInvalid: boolean = errors.bcc !== undefined && Number(errors.bcc.length) > 0 && bcc !== undefined; - const getFormattedEmailOptions = ( - searchValue: string, - previousOptions: Array<{ label: string }> - ): Array<{ label: string }> => { - const formattedOptions: Array<{ label: string }> = searchValue - .split(',') - .map((value) => ({ label: value.trim() })); - return [...previousOptions, ...formattedOptions]; - }; - return ( <>