Skip to content

Commit

Permalink
add test and refactor getFormattedEmailOptions
Browse files Browse the repository at this point in the history
Signed-off-by: Oyelola Victoria <[email protected]>
  • Loading branch information
VriaA committed Sep 29, 2024
1 parent 877c51d commit 9a0ef27
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -234,3 +235,48 @@ describe('EmailParamsFields renders', () => {
expect(editAction).not.toHaveBeenCalled();
});
});

describe('getFormattedEmailOptions', () => {
test('should return new options added to previous options', () => {
const searchValue = '[email protected], [email protected]';
const previousOptions = [{ label: '[email protected]' }];
const newOptions = getFormattedEmailOptions(searchValue, previousOptions);

expect(newOptions).toEqual([
{ label: '[email protected]' },
{ label: '[email protected]' },
{ label: '[email protected]' },
]);
});

test('should trim extra spaces in search value', () => {
const searchValue = ' [email protected] , [email protected] , ';
const previousOptions: Array<{ label: string }> = [];
const newOptions = getFormattedEmailOptions(searchValue, previousOptions);

expect(newOptions).toEqual([{ label: '[email protected]' }, { label: '[email protected]' }]);
});

test('should prevent duplicate email addresses', () => {
const searchValue = '[email protected], [email protected]';
const previousOptions = [{ label: '[email protected]' }, { label: '[email protected]' }];
const newOptions = getFormattedEmailOptions(searchValue, previousOptions);

expect(newOptions).toEqual([{ label: '[email protected]' }, { label: '[email protected]' }]);
});

test('should return previous options if search value is empty', () => {
const searchValue = '';
const previousOptions = [{ label: '[email protected]' }];
const newOptions = getFormattedEmailOptions(searchValue, previousOptions);
expect(newOptions).toEqual([{ label: '[email protected]' }]);
});

test('should handle single email without comma', () => {
const searchValue = '[email protected]';
const previousOptions = [{ label: '[email protected]' }];
const newOptions = getFormattedEmailOptions(searchValue, previousOptions);

expect(newOptions).toEqual([{ label: '[email protected]' }, { label: '[email protected]' }]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 (
<>
<EuiFormRow
Expand Down

0 comments on commit 9a0ef27

Please sign in to comment.