1 import { toVCardContacts } from '@proton/shared/lib/contacts/helpers/csv';
2 import { fromVCardProperties, getVCardProperties } from '@proton/shared/lib/contacts/properties';
3 import type { PreVcardProperty, PreVcardsContact } from '@proton/shared/lib/interfaces/contacts';
4 import type { VCardContact, VCardProperty } from '@proton/shared/lib/interfaces/contacts/VCard';
6 const excludeFieldsForVerification = (contacts: VCardContact[]) => {
7 return contacts.map((contact) => {
8 const properties = getVCardProperties(contact).map(({ uid, params, ...property }) => property);
9 return fromVCardProperties(properties as VCardProperty[]);
13 describe('csv', () => {
14 describe('toVCardContacts', () => {
15 it('should convert to a vCardContact', () => {
16 const contact1: PreVcardsContact = [
17 [{ header: 'Given name', value: 'Contact Name', field: 'fn', checked: true } as PreVcardProperty],
18 [{ header: 'Email', value: 'contactemail@pm.me', field: 'email', checked: true } as PreVcardProperty],
21 const contact2: PreVcardsContact = [
22 [{ header: 'Given name', value: 'Contact Name 2', field: 'fn', checked: true } as PreVcardProperty],
23 [{ header: 'Email', value: 'contactemail2@pm.me', field: 'email', checked: true } as PreVcardProperty],
26 const prevCardsContacts: PreVcardsContact[] = [contact1, contact2];
28 const expected: VCardContact[] = [
30 fn: [{ field: 'fn', value: 'Contact Name', uid: '' }],
31 email: [{ field: 'email', value: 'contactemail@pm.me', group: 'item1', uid: '' }],
34 fn: [{ field: 'fn', value: 'Contact Name 2', uid: '' }],
35 email: [{ field: 'email', value: 'contactemail2@pm.me', group: 'item1', uid: '' }],
39 const res = excludeFieldsForVerification(toVCardContacts(prevCardsContacts).rest);
41 expect(res).toEqual(excludeFieldsForVerification(expected));
44 it('should convert to a vCardContact and add email as FN when no FN', () => {
45 const contact1: PreVcardsContact = [
46 [{ header: 'Email', value: 'contactemail@pm.me', field: 'email', checked: true } as PreVcardProperty],
49 const contact2: PreVcardsContact = [
50 [{ header: 'Given name', value: 'Contact Name 2', field: 'fn', checked: true } as PreVcardProperty],
51 [{ header: 'Email', value: 'contactemail2@pm.me', field: 'email', checked: true } as PreVcardProperty],
54 const prevCardsContacts: PreVcardsContact[] = [contact1, contact2];
56 const expected: VCardContact[] = [
58 fn: [{ field: 'fn', value: 'contactemail@pm.me', uid: '' }],
59 email: [{ field: 'email', value: 'contactemail@pm.me', group: 'item1', uid: '' }],
62 fn: [{ field: 'fn', value: 'Contact Name 2', uid: '' }],
63 email: [{ field: 'email', value: 'contactemail2@pm.me', group: 'item1', uid: '' }],
67 const res = excludeFieldsForVerification(toVCardContacts(prevCardsContacts).rest);
69 expect(res).toEqual(excludeFieldsForVerification(expected));
72 it('should throw an error when contact has no FN and no email, and import the rest', () => {
73 const contact1: PreVcardsContact = [];
75 const contact2: PreVcardsContact = [
76 [{ header: 'Given name', value: 'Contact Name 2', field: 'fn', checked: true } as PreVcardProperty],
77 [{ header: 'Email', value: 'contactemail2@pm.me', field: 'email', checked: true } as PreVcardProperty],
80 const prevCardsContacts: PreVcardsContact[] = [contact1, contact2];
82 const expected: VCardContact[] = [
84 fn: [{ field: 'fn', value: 'Contact Name 2', uid: '' }],
85 email: [{ field: 'email', value: 'contactemail2@pm.me', group: 'item1', uid: '' }],
89 const { errors, rest } = toVCardContacts(prevCardsContacts);
90 const res = excludeFieldsForVerification(rest);
92 expect(res).toEqual(excludeFieldsForVerification(expected));
93 expect(errors.length).toEqual(1);