1 import { fireEvent } from '@testing-library/react';
3 import { getModelState } from '@proton/account/test';
4 import { CryptoProxy } from '@proton/crypto';
5 import downloadFile from '@proton/shared/lib/helpers/downloadFile';
6 import type { Contact } from '@proton/shared/lib/interfaces/contacts';
7 import { addApiMock } from '@proton/testing/lib/api';
9 import { clearAll, minimalCache, mockedCryptoApi, prepareContact, renderWithProviders } from '../tests/render';
10 import type { ContactExportingProps } from './ContactExportingModal';
11 import ContactExportingModal from './ContactExportingModal';
13 jest.mock('@proton/shared/lib/helpers/downloadFile', () => {
17 describe('ContactExportingModal', () => {
18 const props: ContactExportingProps = {
19 contactGroupID: 'contactGroupID',
25 LabelIDs: [props.contactGroupID],
28 const vcard1 = `BEGIN:VCARD
30 UID:urn:uuid:4fbe8971-0bc3-424c-9c26-36c3e1eff6b1
32 EMAIL:jdoe@example.com
38 LabelIDs: [props.contactGroupID],
41 const vcard2 = `BEGIN:VCARD
43 UID:urn:uuid:4fbe8971-0bc3-424c-9c26-36c3e1eff6b2
45 EMAIL:janedoe@example.com
50 CryptoProxy.setEndpoint(mockedCryptoApi);
55 afterAll(async () => {
56 await CryptoProxy.releaseEndpoint();
59 it('should export two contacts', async () => {
60 const { Cards: Cards1 } = await prepareContact(vcard1);
61 const { Cards: Cards2 } = await prepareContact(vcard2);
63 addApiMock('contacts/v4/contacts/export', () => {
66 { ID: contact1.ID, Cards: Cards1 },
67 { ID: contact2.ID, Cards: Cards2 },
74 const { findByText, getByText } = renderWithProviders(<ContactExportingModal open={true} {...props} />, {
76 contacts: getModelState([contact1, contact2]),
80 await findByText('2 out of 2', { exact: false });
82 const saveButton = getByText('Save');
83 fireEvent.click(saveButton);
85 expect(downloadFile).toHaveBeenCalled();
87 const args = (downloadFile as jest.Mock).mock.calls[0];
88 const blob = args[0] as Blob;
89 const content = await new Promise((resolve) => {
90 var reader = new FileReader();
91 reader.onload = () => resolve(reader.result);
92 reader.readAsText(blob);
95 const expected = `BEGIN:VCARD\r\nVERSION:4.0\r\nFN;PREF=1:J. Doe\r\nTEL;PREF=1:testtel\r\nUID:urn:uuid:4fbe8971-0bc3-424c-9c26-36c3e1eff6b1\r\nITEM1.EMAIL;PREF=1:jdoe@example.com\r\nEND:VCARD\r\nBEGIN:VCARD\r\nVERSION:4.0\r\nFN;PREF=1:Jane Doe\r\nTEL;PREF=1:testteltt\r\nUID:urn:uuid:4fbe8971-0bc3-424c-9c26-36c3e1eff6b2\r\nITEM1.EMAIL;PREF=1:janedoe@example.com\r\nEND:VCARD`;
97 expect(content).toBe(expected);