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] } });
16 if (method === 'post') {
17 return Promise.resolve({ Responses: [{ Response: { Code: 1000, Contact: { ID: 'id1' } } }] });
19 if (method === 'put') {
20 return Promise.resolve();
27 jest.mock('@proton/account/userKeys/hooks', () => {
29 useUserKeys: () => [[]],
33 jest.mock('@proton/shared/lib/contacts/decrypt', () => {
35 prepareVCardContact: jest.fn(({ ID }) => {
36 return Promise.resolve({ vCardContact: { nickname: [{ field: 'nickname', value: ID }] }, errors: [] });
41 jest.mock('@proton/shared/lib/contacts/encrypt', () => {
43 prepareVCardContact: jest.fn(() => {
44 return Promise.resolve({ Cards: ['something encrypted'] });
49 window.ResizeObserver =
50 window.ResizeObserver ||
51 jest.fn().mockImplementation(() => ({
52 disconnect: jest.fn(),
57 describe('ContactMergingContent', () => {
61 it('should perform a simple merge', async () => {
63 <ContactMergingContent
66 beMergedModel={{ [id1]: [id1, id2] }}
74 const apiMock = useApi() as jest.Mock;
77 expect(apiMock).toHaveBeenCalledTimes(4); // 2 gets, 1 update, 1 delete
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]);