Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammad-ammar committed Sep 22, 2023
1 parent 00f2dd6 commit f180315
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/data/validation/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,16 @@ const returnValidatedEmails = (formData) => {
// Return an object of email and id if non-empty email and id are present
const sanitize = (row) => {
const data = row.split(',');
// only consider those rows where we have 2 columns
if (data.length === 2) {
const email = data[0].trim();
const id = data[1].trim();
// only consider email and id if both are non-empty
if (email && id) {
return { email, id };
return {
id,
email: email.toLowerCase(),
};
}
}
return null;
Expand All @@ -186,15 +191,16 @@ const sanitize = (row) => {
* haveSFIDs (bool): Whether the formData contains Salesforce ids or not
*/
const extractEmailAndIds = (formData) => {
const emails = []; const
ids = [];
const emails = [];
const ids = [];

// TODO: TBD: Most probably we will remove this and will not handle emails + salesforce ids in textarea
if (formData[EMAIL_ADDRESS_TEXT_FORM_DATA] && formData[EMAIL_ADDRESS_TEXT_FORM_DATA].length) {
const rows = formData[EMAIL_ADDRESS_TEXT_FORM_DATA].split(/\r\n|\n/);
rows.forEach((row) => {
const data = sanitize(row);
if (data) {
// do not add duplicate emails
if (data && emails.includes(data.email) === false) {
emails.push(data.email);
ids.push(data.id);
}
Expand All @@ -205,7 +211,8 @@ const extractEmailAndIds = (formData) => {
if (formData[EMAIL_ADDRESS_CSV_FORM_DATA] && formData[EMAIL_ADDRESS_CSV_FORM_DATA].length) {
formData[EMAIL_ADDRESS_CSV_FORM_DATA].forEach((row) => {
const data = sanitize(row);
if (data) {
// do not add duplicate emails
if (data && emails.includes(data.email) === false) {
emails.push(data.email);
ids.push(data.id);
}
Expand Down
18 changes: 18 additions & 0 deletions src/data/validation/email.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from 'lodash';
import {
extractEmailAndIds,
validateEmailAddresses,
validateEmailAddressesFields,
validateEmailTemplateFields,
Expand Down Expand Up @@ -233,4 +234,21 @@ describe('email validation', () => {
);
});
});

describe('validate emails and ids extraction', () => {
it('extracted correct emails and ids', () => {
const formData = new FormData();
formData[EMAIL_ADDRESS_CSV_FORM_DATA] = [
'[email protected],000000000000ABCABC',
'[email protected],000000000000XYZXYZ',
'[email protected],000000000000ABCDDD',
'[email protected],',
];

const data = extractEmailAndIds(formData);
expect(data.emails.sort()).toEqual(['[email protected]', '[email protected]'].sort());
expect(data.ids).toEqual(['000000000000ABCABC', '000000000000XYZXYZ']);
expect(data.haveSFIDs).toBeTruthy();
});
});
});

0 comments on commit f180315

Please sign in to comment.