Skip to content

Commit

Permalink
chore: minor refactor and render tests
Browse files Browse the repository at this point in the history
  • Loading branch information
keshavbhatt committed Nov 12, 2024
1 parent ccd68d6 commit 2e26b38
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/views/sidebar/accordion-custom-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,30 @@ const AccordionCustomComponent: FC<{ item: Folder }> = ({ item }) => {

const onDragEnterAction = useCallback(
(data: OnDropActionProps): DragEnterAction => {

Check failure on line 83 in src/views/sidebar/accordion-custom-component.tsx

View check run for this annotation

Sonarqube Zextras / carbonio-mails-ui Sonarqube Results

src/views/sidebar/accordion-custom-component.tsx#L83

Refactor this function to reduce its Cognitive Complexity from 18 to the 15 allowed.
if (data.type === 'conversation' || data.type === 'message') {
const { type, data: dragData } = data;
const { parentFolderId } = dragData;
const itemId = Number(item.id);

if (type === 'conversation' || type === 'message') {
if (
data.data.parentFolderId === item.id || // same folder not allowed
(data.data.parentFolderId === FOLDERS.INBOX && [5, 6].includes(Number(item.id))) || // from inbox not allowed in draft and sent
(data.data.parentFolderId === FOLDERS.DRAFTS && ![3].includes(Number(item.id))) || // from draft only allowed in Trash
(item.id === FOLDERS.DRAFTS && data.data.parentFolderId !== FOLDERS.TRASH) || // only from Trash can move in Draft
(item.isLink && item.perm?.indexOf('w') === -1) || // only if shared folder have write permission
parentFolderId === item.id ||
(parentFolderId === FOLDERS.INBOX && [5, 6].includes(itemId)) ||
(parentFolderId === FOLDERS.DRAFTS && ![3].includes(itemId)) ||
(item.id === FOLDERS.DRAFTS && parentFolderId !== FOLDERS.TRASH) ||
(item.isLink && item.perm?.indexOf('w') === -1) ||
item.id === FOLDERS.USER_ROOT ||
(item.isLink && item.oname === ROOT_NAME)
) {
return { success: false };
}
}
if (data.type === 'folder') {
if (
item.id === data.data.id || // same folder not allowed
item.isLink || // shared folder not allowed
isDraft(item.id) ||
isSpam(item.id) // cannot be moved inside Draft and Spam
)

if (type === 'folder') {
if (item.id === dragData.id || item.isLink || isDraft(item.id) || isSpam(item.id)) {
return { success: false };
}
}

return undefined;
},
[item]
Expand Down
63 changes: 63 additions & 0 deletions src/views/sidebar/tests/accordion-custom-component.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com>
*
* SPDX-License-Identifier: AGPL-3.0-only
*/

import React from 'react';

import { faker } from '@faker-js/faker';
import { screen } from '@testing-library/react';

import { FOLDERS } from '../../../carbonio-ui-commons/constants/folders';
import { setupTest } from '../../../carbonio-ui-commons/test/test-setup';
import { Folder, FolderView } from '../../../carbonio-ui-commons/types';
import { generateStore } from '../../../tests/generators/store';
import AccordionCustomComponent from '../accordion-custom-component';

describe('AccordionCustomComponent', () => {
const store = generateStore();
const folder: Folder = {
id: '106',
uuid: faker.string.uuid(),
name: 'Confluence',
absFolderPath: '/Inbox/Confluence',
l: FOLDERS.INBOX,
luuid: faker.string.uuid(),
checked: false,
f: 'u',
u: 25,
view: 'message' as FolderView,
rev: 27896,
ms: 27896,
n: 37,
s: 5550022,
i4ms: 33607,
i4next: 17183,
activesyncdisabled: false,
webOfflineSyncDays: 0,
recursive: false,
deletable: true,
isLink: false,
children: [],
parent: undefined,
depth: 2
};

it('renders the accordion item with correct label and icon', () => {
setupTest(<AccordionCustomComponent item={folder} />, { store });

expect(screen.getByTestId('icon: FolderOutline')).toBeInTheDocument();
expect(screen.getByTestId(`accordion-folder-item-${folder.id}`)).toBeInTheDocument();
expect(screen.getByText('Confluence')).toBeInTheDocument();
expect(screen.getByText('25')).toBeInTheDocument();
});

it('does not render the component if the folder is broken', () => {
const brokenFolder = { ...folder, isLink: true, broken: true, reminder: true };

setupTest(<AccordionCustomComponent item={brokenFolder} />, { store });

expect(screen.queryByTestId(`accordion-folder-item-${folder.id}`)).not.toBeInTheDocument();
});
});

0 comments on commit 2e26b38

Please sign in to comment.