Skip to content

Commit

Permalink
EPMRPP-96531 || added unit test for util function
Browse files Browse the repository at this point in the history
  • Loading branch information
maria-hambardzumian committed Nov 15, 2024
1 parent 5fa666c commit 1d613a9
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions app/src/common/utils/fieldTransformer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { separateFromIntoNameAndEmail, combineNameAndEmailToFrom } from './fieldTransformer';

describe('separateFromIntoNameAndEmail', () => {
it('should split "from" into "fromName" and "fromEmail" when valid format is provided', () => {
const input = { from: 'John Doe <[email protected]>' };
const result = separateFromIntoNameAndEmail(input);
expect(result).toEqual({
fromName: 'John Doe',
fromEmail: '[email protected]',
});
});

it('should set "fromName" and empty "fromEmail" when "from" does not include <email>', () => {
const input = { from: 'John Doe' };
const result = separateFromIntoNameAndEmail(input);
expect(result).toEqual({
fromName: 'John Doe',
fromEmail: '',
});
});

it('should set "fromName" and "fromEmail" to empty strings when "from" is not provided', () => {
const input = {};
const result = separateFromIntoNameAndEmail(input);
expect(result).toEqual({
fromName: '',
fromEmail: '',
});
});

it('should leave unrelated fields in the object unchanged', () => {
const input = { from: 'John Doe <[email protected]>', otherField: 'value' };
const result = separateFromIntoNameAndEmail(input);
expect(result).toEqual({
fromName: 'John Doe',
fromEmail: '[email protected]',
otherField: 'value',
});
});
});

describe('combineNameAndEmailToFrom', () => {
it('should combine "fromName" and "fromEmail" into "from"', () => {
const input = { fromName: 'John Doe', fromEmail: '[email protected]' };
const result = combineNameAndEmailToFrom(input);
expect(result).toEqual({
from: 'John Doe <[email protected]>',
});
});

it('should leave unrelated fields in the object unchanged', () => {
const input = { fromName: 'John Doe', fromEmail: '[email protected]', otherField: 'value' };
const result = combineNameAndEmailToFrom(input);
expect(result).toEqual({
from: 'John Doe <[email protected]>',
otherField: 'value',
});
});

it('should not modify the object if "fromName" or "fromEmail" is missing', () => {
const input1 = { fromName: 'John Doe' };
const input2 = { fromEmail: '[email protected]' };
const input3 = {};

const result1 = combineNameAndEmailToFrom(input1);
const result2 = combineNameAndEmailToFrom(input2);
const result3 = combineNameAndEmailToFrom(input3);

expect(result1).toEqual({ fromName: 'John Doe' });
expect(result2).toEqual({ fromEmail: '[email protected]' });
expect(result3).toEqual({});
});
});

0 comments on commit 1d613a9

Please sign in to comment.