Remove payments API routing initialization
[ProtonMail-WebClient.git] / packages / components / containers / contacts / modals / ContactExportingModal.test.tsx
blobd3d00e61b115a0cdbdad74d0431588fe52df0702
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', () => {
14     return jest.fn();
15 });
17 describe('ContactExportingModal', () => {
18     const props: ContactExportingProps = {
19         contactGroupID: 'contactGroupID',
20         onSave: jest.fn(),
21     };
23     const contact1 = {
24         ID: 'ContactID1',
25         LabelIDs: [props.contactGroupID],
26     } as Contact;
28     const vcard1 = `BEGIN:VCARD
29 VERSION:4.0
30 UID:urn:uuid:4fbe8971-0bc3-424c-9c26-36c3e1eff6b1
31 FN:J. Doe
32 EMAIL:jdoe@example.com
33 TEL:testtel
34 END:VCARD`;
36     const contact2 = {
37         ID: 'ContactID2',
38         LabelIDs: [props.contactGroupID],
39     } as Contact;
41     const vcard2 = `BEGIN:VCARD
42 VERSION:4.0
43 UID:urn:uuid:4fbe8971-0bc3-424c-9c26-36c3e1eff6b2
44 FN:Jane Doe
45 EMAIL:janedoe@example.com
46 TEL:testteltt
47 END:VCARD`;
49     beforeAll(() => {
50         CryptoProxy.setEndpoint(mockedCryptoApi);
51     });
53     beforeEach(clearAll);
55     afterAll(async () => {
56         await CryptoProxy.releaseEndpoint();
57     });
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', () => {
64             return {
65                 Contacts: [
66                     { ID: contact1.ID, Cards: Cards1 },
67                     { ID: contact2.ID, Cards: Cards2 },
68                 ],
69             };
70         });
72         minimalCache();
74         const { findByText, getByText } = renderWithProviders(<ContactExportingModal open={true} {...props} />, {
75             preloadedState: {
76                 contacts: getModelState([contact1, contact2]),
77             },
78         });
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);
93         });
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);
98     });
99 });