The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { type EachTestingContext } from 'twenty-shared/testing';
import { type ObjectRecordDiff } from 'twenty-shared/database-events';
import { computeChangedAdditionalEmails } from 'src/modules/contact-creation-manager/utils/compute-changed-additional-emails';
import { type PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
type ComputeChangedAdditionalEmailsTestCase = EachTestingContext<{
diff: Partial<ObjectRecordDiff<PersonWorkspaceEntity>>;
expected: {
addedAdditionalEmails: string[];
removedAdditionalEmails: string[];
};
}>;
const testCases: ComputeChangedAdditionalEmailsTestCase[] = [
{
title:
'should return added and removed emails when both before and after are valid arrays',
context: {
diff: {
emails: {
before: {
primaryEmail: '[email protected]',
additionalEmails: ['[email protected]', '[email protected]'],
},
after: {
primaryEmail: '[email protected]',
additionalEmails: ['[email protected]', '[email protected]'],
},
},
},
expected: {
addedAdditionalEmails: ['[email protected]'],
removedAdditionalEmails: ['[email protected]'],
},
},
},
{
title:
'should return all emails as added when before is empty and after has emails',
context: {
diff: {
emails: {
before: {
primaryEmail: '[email protected]',
additionalEmails: [],
},
after: {
primaryEmail: '[email protected]',
additionalEmails: ['[email protected]', '[email protected]'],
},
},
},
expected: {
addedAdditionalEmails: ['[email protected]', '[email protected]'],
removedAdditionalEmails: [],
},
},
},
{
title:
'should return all emails as removed when before has emails and after is empty',
context: {
diff: {
emails: {
before: {
primaryEmail: '[email protected]',
additionalEmails: ['[email protected]', '[email protected]'],
},
after: {
primaryEmail: '[email protected]',
additionalEmails: [],
},
},
},
expected: {
addedAdditionalEmails: [],
removedAdditionalEmails: ['[email protected]', '[email protected]'],
},
},
},
{
title: 'should return empty arrays when both before and after are empty',
context: {
diff: {
emails: {
before: {
primaryEmail: '[email protected]',
additionalEmails: [],
},
after: {
primaryEmail: '[email protected]',
additionalEmails: [],
},
},
},
expected: {
addedAdditionalEmails: [],
removedAdditionalEmails: [],
},
},
},
{
title:
'should return empty arrays when both before and after have the same emails',
context: {
diff: {
emails: {
before: {
primaryEmail: '[email protected]',
additionalEmails: ['[email protected]', '[email protected]'],
},
after: {
primaryEmail: '[email protected]',
additionalEmails: ['[email protected]', '[email protected]'],
},
},
},
expected: {
addedAdditionalEmails: [],
removedAdditionalEmails: [],
},
},
},
{
title: 'should handle case when before additionalEmails is not an array',
context: {
diff: {
emails: {
before: {
primaryEmail: '[email protected]',
additionalEmails: null as any,
},
after: {
primaryEmail: '[email protected]',
additionalEmails: ['[email protected]'],
},
},
},
expected: {
addedAdditionalEmails: [],
removedAdditionalEmails: [],
},
},
},
{
title: 'should handle case when after additionalEmails is not an array',
context: {
diff: {
emails: {
before: {
primaryEmail: '[email protected]',
additionalEmails: ['[email protected]'],
},
after: {
primaryEmail: '[email protected]',
additionalEmails: null as any,
},
},
},
expected: {
addedAdditionalEmails: [],
removedAdditionalEmails: [],
},
},
},
{
title:
'should handle case when both before and after additionalEmails are not arrays',
context: {
diff: {
emails: {
before: {
primaryEmail: '[email protected]',
additionalEmails: null as any,
},
after: {
primaryEmail: '[email protected]',
additionalEmails: undefined as any,
},
},
},
expected: {
addedAdditionalEmails: [],
removedAdditionalEmails: [],
},
},
},
{
title: 'should handle case when emails diff is undefined',
context: {
diff: {},
expected: {
addedAdditionalEmails: [],
removedAdditionalEmails: [],
},
},
},
{
title:
'should handle complex scenario with multiple additions and removals',
context: {
diff: {
emails: {
before: {
primaryEmail: '[email protected]',
additionalEmails: [
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
],
},
after: {
primaryEmail: '[email protected]',
additionalEmails: [
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
],
},
},
},
expected: {
addedAdditionalEmails: ['[email protected]', '[email protected]'],
removedAdditionalEmails: ['[email protected]', '[email protected]'],
},
},
},
{
title: 'should not be case sensitive when comparing emails',
context: {
diff: {
emails: {
before: {
primaryEmail: '[email protected]',
additionalEmails: ['[email protected]'],
},
after: {
primaryEmail: '[email protected]',
additionalEmails: ['[email protected]'],
},
},
},
expected: {
addedAdditionalEmails: [],
removedAdditionalEmails: [],
},
},
},
];
describe('computeChangedAdditionalEmails', () => {
test.each(testCases)('$title', ({ context: { diff, expected } }) => {
const result = computeChangedAdditionalEmails(diff);
expect(result).toEqual(expected);
});
});