Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / components / containers / contacts / merge / ContactMergingContent.test.tsx
blobfb5ee6688958980c77d6ecfa34c48aa279b66963
1 import { render, waitFor } from '@testing-library/react';
3 import { prepareVCardContact } from '@proton/shared/lib/contacts/encrypt';
5 import useApi from '../../../hooks/useApi';
6 import ContactMergingContent from './ContactMergingContent';
8 const encrypt = prepareVCardContact as jest.Mock;
10 jest.mock('../../../hooks/useApi', () => {
11     const apiMock = jest.fn(({ url, method }) => {
12         if (method === 'get') {
13             const parts = url.split('/');
14             return Promise.resolve({ Contact: { ID: parts[parts.length - 1] } });
15         }
16         if (method === 'post') {
17             return Promise.resolve({ Responses: [{ Response: { Code: 1000, Contact: { ID: 'id1' } } }] });
18         }
19         if (method === 'put') {
20             return Promise.resolve();
21         }
22     });
24     return () => apiMock;
25 });
27 jest.mock('@proton/account/userKeys/hooks', () => {
28     return {
29         useUserKeys: () => [[]],
30     };
31 });
33 jest.mock('@proton/shared/lib/contacts/decrypt', () => {
34     return {
35         prepareVCardContact: jest.fn(({ ID }) => {
36             return Promise.resolve({ vCardContact: { nickname: [{ field: 'nickname', value: ID }] }, errors: [] });
37         }),
38     };
39 });
41 jest.mock('@proton/shared/lib/contacts/encrypt', () => {
42     return {
43         prepareVCardContact: jest.fn(() => {
44             return Promise.resolve({ Cards: ['something encrypted'] });
45         }),
46     };
47 });
49 window.ResizeObserver =
50     window.ResizeObserver ||
51     jest.fn().mockImplementation(() => ({
52         disconnect: jest.fn(),
53         observe: jest.fn(),
54         unobserve: jest.fn(),
55     }));
57 describe('ContactMergingContent', () => {
58     const id1 = 'id1';
59     const id2 = 'id2';
61     it('should perform a simple merge', async () => {
62         render(
63             <ContactMergingContent
64                 // userKeysList={[]}
65                 mergeFinished={false}
66                 beMergedModel={{ [id1]: [id1, id2] }}
67                 beDeletedModel={{}}
68                 totalBeMerged={1}
69                 totalBeDeleted={1}
70                 onFinish={jest.fn()}
71             />
72         );
74         const apiMock = useApi() as jest.Mock;
76         await waitFor(() => {
77             expect(apiMock).toHaveBeenCalledTimes(4); // 2 gets, 1 update, 1 delete
78         });
80         const encryptedProps = encrypt.mock.calls[0][0];
82         expect(encryptedProps.nickname[0].field).toBe('nickname');
83         expect(encryptedProps.nickname[0].value).toBe(id1);
84         expect(encryptedProps.nickname[1].field).toBe('nickname');
85         expect(encryptedProps.nickname[1].value).toBe(id2);
87         const updateCall = apiMock.mock.calls[2];
88         expect(updateCall[0].data.Contacts[0].Cards[0]).toBe('something encrypted');
90         const deleteCall = apiMock.mock.calls[3];
91         expect(deleteCall[0].data.IDs).toEqual([id2]);
92     });
93 });