From 061683457ef6427994becd3b3024639be3cc6541 Mon Sep 17 00:00:00 2001 From: nishant23122000 Date: Fri, 4 Nov 2022 16:36:33 +0530 Subject: [PATCH 1/5] fix: disable workspace for user having livechat-manager permission --- .../AdministrationList/AdministrationList.tsx | 9 +++++- .../AdministrationModelList.tsx | 31 ++++++++++--------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/apps/meteor/client/components/AdministrationList/AdministrationList.tsx b/apps/meteor/client/components/AdministrationList/AdministrationList.tsx index 468a78f6d93c..b864ba838c77 100644 --- a/apps/meteor/client/components/AdministrationList/AdministrationList.tsx +++ b/apps/meteor/client/components/AdministrationList/AdministrationList.tsx @@ -31,7 +31,14 @@ const AdministrationList: FC = ({ const showAdmin = hasAdminPermission || !!adminBoxItems.length; const list = [ - showAdmin && , + showAdmin && ( + + ), showManageApps && , showAudit && , ]; diff --git a/apps/meteor/client/components/AdministrationList/AdministrationModelList.tsx b/apps/meteor/client/components/AdministrationList/AdministrationModelList.tsx index b2f03fd3941c..4fb1fc4c42ea 100644 --- a/apps/meteor/client/components/AdministrationList/AdministrationModelList.tsx +++ b/apps/meteor/client/components/AdministrationList/AdministrationModelList.tsx @@ -13,13 +13,14 @@ import ListItem from '../Sidebar/ListItem'; type AdministrationModelListProps = { accountBoxItems: AccountBoxItem[]; + hasAdminPermission: boolean; showAdmin: boolean; closeList: () => void; }; const INFO_PERMISSIONS = ['view-statistics']; -const AdministrationModelList: FC = ({ accountBoxItems, showAdmin, closeList }) => { +const AdministrationModelList: FC = ({ accountBoxItems, hasAdminPermission, showAdmin, closeList }) => { const t = useTranslation(); const { tabType, trialEndDate, isLoading } = useUpgradeTabParams(); const shouldShowEmoji = isFullyFeature(tabType); @@ -51,20 +52,22 @@ const AdministrationModelList: FC = ({ accountBoxI }} /> )} - { - if (hasInfoPermission) { - infoRoute.push(); - closeList(); - return; - } + {hasAdminPermission && ( + { + if (hasInfoPermission) { + infoRoute.push(); + closeList(); + return; + } - adminRoute.push({ context: '/' }); - closeList(); - }} - /> + adminRoute.push({ context: '/' }); + closeList(); + }} + /> + )} )} {accountBoxItems.length > 0 && ( From 6b8c55a3f85c46203948b9135d1e9cf167fb04ec Mon Sep 17 00:00:00 2001 From: Filipe Marins Date: Mon, 26 Dec 2022 17:17:17 -0300 Subject: [PATCH 2/5] chore: update unit tests --- .../AdministrationModelList.spec.tsx | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/apps/meteor/tests/unit/client/components/AdministrationList/AdministrationModelList.spec.tsx b/apps/meteor/tests/unit/client/components/AdministrationList/AdministrationModelList.spec.tsx index a9db32cad707..7626f730c8e8 100644 --- a/apps/meteor/tests/unit/client/components/AdministrationList/AdministrationModelList.spec.tsx +++ b/apps/meteor/tests/unit/client/components/AdministrationList/AdministrationModelList.spec.tsx @@ -37,13 +37,22 @@ const defaultConfig = { describe('components/AdministrationList/AdministrationModelList', () => { it('should render administration', async () => { const AdministrationModelList = proxyquire.load(COMPONENT_PATH, defaultConfig).default; - render( null} accountBoxItems={[]} showAdmin={true} />); + render( null} accountBoxItems={[]} showAdmin={true} hasAdminPermission={true} />); expect(screen.getByText('Administration')).to.exist; expect(screen.getByText('Workspace')).to.exist; expect(screen.getByText('Upgrade')).to.exist; }); + it('should not render workspace', async () => { + const AdministrationModelList = proxyquire.load(COMPONENT_PATH, defaultConfig).default; + render( null} accountBoxItems={[]} showAdmin={true} hasAdminPermission={false} />); + + expect(screen.getByText('Administration')).to.exist; + expect(screen.queryByText('Workspace')).to.not.exist; + expect(screen.getByText('Upgrade')).to.exist; + }); + it('should not render workspace and upgrade when does not have permission', async () => { const AdministrationModelList = proxyquire.load(COMPONENT_PATH, defaultConfig).default; render( null} accountBoxItems={[]} showAdmin={false} />); @@ -60,7 +69,7 @@ describe('components/AdministrationList/AdministrationModelList', () => { const AdministrationModelList = proxyquire.load(COMPONENT_PATH, defaultConfig).default; render( - + , ); const button = screen.getByText('Workspace'); @@ -82,7 +91,7 @@ describe('components/AdministrationList/AdministrationModelList', () => { }).default; render( - + , ); const button = screen.getByText('Workspace'); From ad74dcb7247cb4c9b7bead5b305a9ecd055cdb4e Mon Sep 17 00:00:00 2001 From: Filipe Marins Date: Mon, 26 Dec 2022 18:10:08 -0300 Subject: [PATCH 3/5] chore: rename prop --- .../components/AdministrationList/AdministrationList.tsx | 8 ++------ .../AdministrationList/AdministrationModelList.tsx | 6 +++--- .../AdministrationList/AdministrationModelList.spec.tsx | 8 ++++---- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/apps/meteor/client/components/AdministrationList/AdministrationList.tsx b/apps/meteor/client/components/AdministrationList/AdministrationList.tsx index b864ba838c77..785b3c0a1064 100644 --- a/apps/meteor/client/components/AdministrationList/AdministrationList.tsx +++ b/apps/meteor/client/components/AdministrationList/AdministrationList.tsx @@ -29,15 +29,11 @@ const AdministrationList: FC = ({ const showAudit = hasAuditPermission || hasAuditLogPermission; const showManageApps = hasManageApps || !!appBoxItems.length; const showAdmin = hasAdminPermission || !!adminBoxItems.length; + const showWorkspace = hasAdminPermission; const list = [ showAdmin && ( - + ), showManageApps && , showAudit && , diff --git a/apps/meteor/client/components/AdministrationList/AdministrationModelList.tsx b/apps/meteor/client/components/AdministrationList/AdministrationModelList.tsx index 4fb1fc4c42ea..2529e10ab0a5 100644 --- a/apps/meteor/client/components/AdministrationList/AdministrationModelList.tsx +++ b/apps/meteor/client/components/AdministrationList/AdministrationModelList.tsx @@ -13,14 +13,14 @@ import ListItem from '../Sidebar/ListItem'; type AdministrationModelListProps = { accountBoxItems: AccountBoxItem[]; - hasAdminPermission: boolean; + showWorkspace: boolean; showAdmin: boolean; closeList: () => void; }; const INFO_PERMISSIONS = ['view-statistics']; -const AdministrationModelList: FC = ({ accountBoxItems, hasAdminPermission, showAdmin, closeList }) => { +const AdministrationModelList: FC = ({ accountBoxItems, showWorkspace, showAdmin, closeList }) => { const t = useTranslation(); const { tabType, trialEndDate, isLoading } = useUpgradeTabParams(); const shouldShowEmoji = isFullyFeature(tabType); @@ -52,7 +52,7 @@ const AdministrationModelList: FC = ({ accountBoxI }} /> )} - {hasAdminPermission && ( + {showWorkspace && ( { it('should render administration', async () => { const AdministrationModelList = proxyquire.load(COMPONENT_PATH, defaultConfig).default; - render( null} accountBoxItems={[]} showAdmin={true} hasAdminPermission={true} />); + render( null} accountBoxItems={[]} showAdmin={true} showWorkspace={true} />); expect(screen.getByText('Administration')).to.exist; expect(screen.getByText('Workspace')).to.exist; @@ -46,7 +46,7 @@ describe('components/AdministrationList/AdministrationModelList', () => { it('should not render workspace', async () => { const AdministrationModelList = proxyquire.load(COMPONENT_PATH, defaultConfig).default; - render( null} accountBoxItems={[]} showAdmin={true} hasAdminPermission={false} />); + render( null} accountBoxItems={[]} showAdmin={true} showWorkspace={false} />); expect(screen.getByText('Administration')).to.exist; expect(screen.queryByText('Workspace')).to.not.exist; @@ -69,7 +69,7 @@ describe('components/AdministrationList/AdministrationModelList', () => { const AdministrationModelList = proxyquire.load(COMPONENT_PATH, defaultConfig).default; render( - + , ); const button = screen.getByText('Workspace'); @@ -91,7 +91,7 @@ describe('components/AdministrationList/AdministrationModelList', () => { }).default; render( - + , ); const button = screen.getByText('Workspace'); From 7947c0d820d070fe12e033ad090871b450bf82f9 Mon Sep 17 00:00:00 2001 From: Tasso Evangelista Date: Mon, 2 Jan 2023 11:53:56 -0300 Subject: [PATCH 4/5] Remove rampant prop drilling --- .../AdministrationList/AdministrationList.tsx | 12 ++-- .../AdministrationModelList.tsx | 67 +++++++++---------- .../AdministrationList/AppsModelList.tsx | 43 ++++++------ .../AdministrationList/AuditModelList.tsx | 8 +-- .../sidebar/header/actions/Administration.tsx | 2 +- .../AuditModelList.spec.tsx | 8 +-- 6 files changed, 64 insertions(+), 76 deletions(-) diff --git a/apps/meteor/client/components/AdministrationList/AdministrationList.tsx b/apps/meteor/client/components/AdministrationList/AdministrationList.tsx index 38c31fed8ede..81527bfc8e2a 100644 --- a/apps/meteor/client/components/AdministrationList/AdministrationList.tsx +++ b/apps/meteor/client/components/AdministrationList/AdministrationList.tsx @@ -10,7 +10,7 @@ import AuditModelList from './AuditModelList'; type AdministrationListProps = { accountBoxItems: (IAppAccountBoxItem | AccountBoxItem)[]; - closeList: () => void; + onDismiss: () => void; hasAdminPermission: boolean; hasAuditLicense: boolean; hasAuditPermission: boolean; @@ -24,7 +24,7 @@ const AdministrationList: FC = ({ hasAuditLogPermission, hasManageApps, hasAdminPermission, - closeList, + onDismiss, }) => { const appBoxItems = accountBoxItems.filter((item): item is IAppAccountBoxItem => isAppAccountBoxItem(item)); const adminBoxItems = accountBoxItems.filter((item): item is AccountBoxItem => !isAppAccountBoxItem(item)); @@ -34,11 +34,9 @@ const AdministrationList: FC = ({ const showWorkspace = hasAdminPermission; const list = [ - showAdmin && ( - - ), - showManageApps && , - showAudit && , + showAdmin && , + showManageApps && , + showAudit && , ]; return ( diff --git a/apps/meteor/client/components/AdministrationList/AdministrationModelList.tsx b/apps/meteor/client/components/AdministrationList/AdministrationModelList.tsx index c6d3f8a370a1..f047cde8f03f 100644 --- a/apps/meteor/client/components/AdministrationList/AdministrationModelList.tsx +++ b/apps/meteor/client/components/AdministrationList/AdministrationModelList.tsx @@ -14,13 +14,12 @@ import ListItem from '../Sidebar/ListItem'; type AdministrationModelListProps = { accountBoxItems: AccountBoxItem[]; showWorkspace: boolean; - showAdmin: boolean; - closeList: () => void; + onDismiss: () => void; }; const INFO_PERMISSIONS = ['view-statistics']; -const AdministrationModelList: FC = ({ accountBoxItems, showWorkspace, showAdmin, closeList }) => { +const AdministrationModelList: FC = ({ accountBoxItems, showWorkspace, onDismiss }) => { const t = useTranslation(); const { tabType, trialEndDate, isLoading } = useUpgradeTabParams(); const shouldShowEmoji = isFullyFeature(tabType); @@ -36,39 +35,35 @@ const AdministrationModelList: FC = ({ accountBoxI <> {t('Administration')}
    - {showAdmin && ( - <> - {showUpgradeItem && ( - - {t(label)} {shouldShowEmoji && } - - } - action={(): void => { - upgradeRoute.push({ type: tabType }, trialEndDate ? { trialEndDate } : undefined); - closeList(); - }} - /> - )} - {showWorkspace && ( - { - if (hasInfoPermission) { - infoRoute.push(); - closeList(); - return; - } + {showUpgradeItem && ( + + {t(label)} {shouldShowEmoji && } + + } + action={(): void => { + upgradeRoute.push({ type: tabType }, trialEndDate ? { trialEndDate } : undefined); + onDismiss(); + }} + /> + )} + {showWorkspace && ( + { + if (hasInfoPermission) { + infoRoute.push(); + onDismiss(); + return; + } - adminRoute.push({ context: '/' }); - closeList(); - }} - /> - )} - + adminRoute.push({ context: '/' }); + onDismiss(); + }} + /> )} {accountBoxItems.length > 0 && ( <> @@ -77,7 +72,7 @@ const AdministrationModelList: FC = ({ accountBoxI if (item.href) { FlowRouter.go(item.href); } - closeList(); + onDismiss(); }; return ; diff --git a/apps/meteor/client/components/AdministrationList/AppsModelList.tsx b/apps/meteor/client/components/AdministrationList/AppsModelList.tsx index a62468317c6e..27c45110020c 100644 --- a/apps/meteor/client/components/AdministrationList/AppsModelList.tsx +++ b/apps/meteor/client/components/AdministrationList/AppsModelList.tsx @@ -9,11 +9,10 @@ import ListItem from '../Sidebar/ListItem'; type AppsModelListProps = { appBoxItems: IAppAccountBoxItem[]; - showManageApps: boolean; - closeList: () => void; + onDismiss: () => void; }; -const AppsModelList = ({ appBoxItems, showManageApps, closeList }: AppsModelListProps): ReactElement => { +const AppsModelList = ({ appBoxItems, onDismiss }: AppsModelListProps): ReactElement => { const t = useTranslation(); const marketplaceRoute = useRoute('admin-marketplace'); const page = 'list'; @@ -22,26 +21,22 @@ const AppsModelList = ({ appBoxItems, showManageApps, closeList }: AppsModelList <> {t('Apps')}
      - {showManageApps && ( - <> - { - marketplaceRoute.push({ context: 'all', page }); - closeList(); - }} - /> - { - marketplaceRoute.push({ context: 'installed', page }); - closeList(); - }} - /> - - )} + { + marketplaceRoute.push({ context: 'all', page }); + onDismiss(); + }} + /> + { + marketplaceRoute.push({ context: 'installed', page }); + onDismiss(); + }} + /> {appBoxItems.length > 0 && ( <> {appBoxItems.map((item, key) => { @@ -53,7 +48,7 @@ const AppsModelList = ({ appBoxItems, showManageApps, closeList }: AppsModelList appId: item.appId, payload: { context: item.context }, }); - closeList(); + onDismiss(); }; return ; })} diff --git a/apps/meteor/client/components/AdministrationList/AuditModelList.tsx b/apps/meteor/client/components/AdministrationList/AuditModelList.tsx index 0b28a7030a3e..75df229e0624 100644 --- a/apps/meteor/client/components/AdministrationList/AuditModelList.tsx +++ b/apps/meteor/client/components/AdministrationList/AuditModelList.tsx @@ -6,12 +6,12 @@ import React from 'react'; import ListItem from '../Sidebar/ListItem'; type AuditModelListProps = { - closeList: () => void; + onDismiss: () => void; showAudit: boolean; showAuditLog: boolean; }; -const AuditModelList: FC = ({ showAudit, showAuditLog, closeList }) => { +const AuditModelList: FC = ({ showAudit, showAuditLog, onDismiss }) => { const t = useTranslation(); const auditHomeRoute = useRoute('audit-home'); @@ -27,7 +27,7 @@ const AuditModelList: FC = ({ showAudit, showAuditLog, clos text={t('Messages')} action={(): void => { auditHomeRoute.push(); - closeList(); + onDismiss(); }} /> )} @@ -37,7 +37,7 @@ const AuditModelList: FC = ({ showAudit, showAuditLog, clos text={t('Logs')} action={(): void => { auditSettingsRoute.push(); - closeList(); + onDismiss(); }} /> )} diff --git a/apps/meteor/client/sidebar/header/actions/Administration.tsx b/apps/meteor/client/sidebar/header/actions/Administration.tsx index 4202021b265f..3516743dd5d8 100644 --- a/apps/meteor/client/sidebar/header/actions/Administration.tsx +++ b/apps/meteor/client/sidebar/header/actions/Administration.tsx @@ -58,7 +58,7 @@ const Administration: VFC, 'is'>> = (props) => toggle(false)} + onDismiss={(): void => toggle(false)} hasAdminPermission={hasAdminPermission} hasAuditLicense={hasAuditLicense} hasAuditPermission={hasAuditPermission} diff --git a/apps/meteor/tests/unit/client/components/AdministrationList/AuditModelList.spec.tsx b/apps/meteor/tests/unit/client/components/AdministrationList/AuditModelList.spec.tsx index 2109a65d3564..9807313c949b 100644 --- a/apps/meteor/tests/unit/client/components/AdministrationList/AuditModelList.spec.tsx +++ b/apps/meteor/tests/unit/client/components/AdministrationList/AuditModelList.spec.tsx @@ -8,7 +8,7 @@ import RouterContextMock from '../../../../mocks/client/RouterContextMock'; describe('components/AdministrationList/AuditModelList', () => { it('should render audit', async () => { - render( null} />); + render( null} />); expect(screen.getByText('Audit')).to.exist; expect(screen.getByText('Messages')).to.exist; @@ -16,7 +16,7 @@ describe('components/AdministrationList/AuditModelList', () => { }); it('should not render messages and log when does not have permission', async () => { - render( null} />); + render( null} />); expect(screen.getByText('Audit')).to.exist; expect(screen.queryByText('Messages')).to.not.exist; @@ -29,7 +29,7 @@ describe('components/AdministrationList/AuditModelList', () => { const closeList = spy(); render( - + , ); const button = screen.getByText('Messages'); @@ -44,7 +44,7 @@ describe('components/AdministrationList/AuditModelList', () => { const closeList = spy(); render( - + , ); const button = screen.getByText('Logs'); From a835f52baa4f02dd0c9fa8035a22c67b48af0b4f Mon Sep 17 00:00:00 2001 From: Tasso Evangelista Date: Mon, 2 Jan 2023 17:21:10 -0300 Subject: [PATCH 5/5] Update unit tests --- .../AdministrationList/AdministrationList.tsx | 2 +- .../AdministrationList/AppsModelList.tsx | 39 +++--- .../AdministrationList.spec.tsx | 85 ++++++++---- .../AdministrationModelList.spec.tsx | 122 +++++++++--------- .../AdministrationList/AppsModelList.spec.tsx | 49 +++---- .../AuditModelList.spec.tsx | 12 +- 6 files changed, 172 insertions(+), 137 deletions(-) diff --git a/apps/meteor/client/components/AdministrationList/AdministrationList.tsx b/apps/meteor/client/components/AdministrationList/AdministrationList.tsx index 81527bfc8e2a..e59d6e159f7b 100644 --- a/apps/meteor/client/components/AdministrationList/AdministrationList.tsx +++ b/apps/meteor/client/components/AdministrationList/AdministrationList.tsx @@ -35,7 +35,7 @@ const AdministrationList: FC = ({ const list = [ showAdmin && , - showManageApps && , + showManageApps && , showAudit && , ]; diff --git a/apps/meteor/client/components/AdministrationList/AppsModelList.tsx b/apps/meteor/client/components/AdministrationList/AppsModelList.tsx index 27c45110020c..c5dea2dfe18b 100644 --- a/apps/meteor/client/components/AdministrationList/AppsModelList.tsx +++ b/apps/meteor/client/components/AdministrationList/AppsModelList.tsx @@ -9,10 +9,11 @@ import ListItem from '../Sidebar/ListItem'; type AppsModelListProps = { appBoxItems: IAppAccountBoxItem[]; + appsManagementAllowed: boolean; onDismiss: () => void; }; -const AppsModelList = ({ appBoxItems, onDismiss }: AppsModelListProps): ReactElement => { +const AppsModelList = ({ appBoxItems, appsManagementAllowed, onDismiss }: AppsModelListProps): ReactElement => { const t = useTranslation(); const marketplaceRoute = useRoute('admin-marketplace'); const page = 'list'; @@ -21,22 +22,26 @@ const AppsModelList = ({ appBoxItems, onDismiss }: AppsModelListProps): ReactEle <> {t('Apps')}
        - { - marketplaceRoute.push({ context: 'all', page }); - onDismiss(); - }} - /> - { - marketplaceRoute.push({ context: 'installed', page }); - onDismiss(); - }} - /> + {appsManagementAllowed && ( + <> + { + marketplaceRoute.push({ context: 'all', page }); + onDismiss(); + }} + /> + { + marketplaceRoute.push({ context: 'installed', page }); + onDismiss(); + }} + /> + + )} {appBoxItems.length > 0 && ( <> {appBoxItems.map((item, key) => { diff --git a/apps/meteor/tests/unit/client/components/AdministrationList/AdministrationList.spec.tsx b/apps/meteor/tests/unit/client/components/AdministrationList/AdministrationList.spec.tsx index 337d02969fa5..11031ecef67c 100644 --- a/apps/meteor/tests/unit/client/components/AdministrationList/AdministrationList.spec.tsx +++ b/apps/meteor/tests/unit/client/components/AdministrationList/AdministrationList.spec.tsx @@ -3,34 +3,38 @@ import { expect } from 'chai'; import proxyquire from 'proxyquire'; import React from 'react'; -const COMPONENT_PATH = '../../../../../client/components/AdministrationList/AdministrationList'; -const defaultConfig = { - '@rocket.chat/ui-contexts': { - useAtLeastOnePermission: () => true, - }, - '../../../app/ui-utils/client/lib/AccountBox': { - AccountBoxItem: {}, - isAppAccountBoxItem: () => false, - }, - '../../../ee/client/hooks/useHasLicenseModule': { - useHasLicenseModule: () => true, - }, - './AdministrationModelList': () =>

        Administration Model List

        , - './AppsModelList': () =>

        Apps Model List

        , - './AuditModelList': () =>

        Audit Model List

        , +const mockAdministrationListModule = (stubs = {}) => { + return proxyquire.noCallThru().load('../../../../../client/components/AdministrationList/AdministrationList', { + '@rocket.chat/ui-contexts': { + useAtLeastOnePermission: () => true, + }, + '../../../app/ui-utils/client/lib/AccountBox': { + AccountBoxItem: {}, + isAppAccountBoxItem: () => false, + }, + '../../../ee/client/hooks/useHasLicenseModule': { + useHasLicenseModule: () => true, + }, + './AdministrationModelList': () =>

        Administration Model List

        , + './AppsModelList': () =>

        Apps Model List

        , + './AuditModelList': () =>

        Audit Model List

        , + ...stubs, + // eslint-disable-next-line @typescript-eslint/consistent-type-imports + }) as typeof import('../../../../../client/components/AdministrationList/AdministrationList'); }; describe('components/AdministrationList/AdministrationList', () => { it('should render all model list', async () => { - const AdministrationList = proxyquire.noCallThru().load(COMPONENT_PATH, defaultConfig).default; + const AdministrationList = mockAdministrationListModule().default; render( null} - accountBoxItems={[{}]} + accountBoxItems={[{} as any]} hasAuditPermission={true} hasAuditLogPermission={true} hasManageApps={true} hasAdminPermission={true} + hasAuditLicense={false} + onDismiss={() => null} />, ); @@ -40,13 +44,22 @@ describe('components/AdministrationList/AdministrationList', () => { }); it('should render nothing when no permission', async () => { - const AdministrationList = proxyquire.noCallThru().load(COMPONENT_PATH, { - ...defaultConfig, + const AdministrationList = mockAdministrationListModule({ '@rocket.chat/ui-contexts': { useAtLeastOnePermission: () => false, }, }).default; - render( null} accountBoxItems={[]} />); + render( + null} + />, + ); expect(screen.queryByText('Administration Model List')).to.not.exist; expect(screen.queryByText('Apps Model List')).to.not.exist; @@ -54,13 +67,22 @@ describe('components/AdministrationList/AdministrationList', () => { }); it('should render administration model list when has account box item', async () => { - const AdministrationList = proxyquire.noCallThru().load(COMPONENT_PATH, { - ...defaultConfig, + const AdministrationList = mockAdministrationListModule({ '@rocket.chat/ui-contexts': { useAtLeastOnePermission: () => false, }, }).default; - render( null} accountBoxItems={[{}]} />); + render( + null} + />, + ); expect(screen.getByText('Administration Model List')).to.exist; expect(screen.queryByText('Apps Model List')).to.not.exist; @@ -68,8 +90,7 @@ describe('components/AdministrationList/AdministrationList', () => { }); it('should render apps model list when has app account box item', async () => { - const AdministrationList = proxyquire.noCallThru().load(COMPONENT_PATH, { - ...defaultConfig, + const AdministrationList = mockAdministrationListModule({ '../../../app/ui-utils/client/lib/AccountBox': { 'AccountBoxItem': {}, 'isAppAccountBoxItem': () => true, @@ -79,7 +100,17 @@ describe('components/AdministrationList/AdministrationList', () => { useAtLeastOnePermission: () => false, }, }).default; - render( null} accountBoxItems={[{}]} />); + render( + null} + />, + ); expect(screen.getByText('Apps Model List')).to.exist; expect(screen.queryByText('Administration Model List')).to.not.exist; diff --git a/apps/meteor/tests/unit/client/components/AdministrationList/AdministrationModelList.spec.tsx b/apps/meteor/tests/unit/client/components/AdministrationList/AdministrationModelList.spec.tsx index bc9bfad7c131..5595abbccfe3 100644 --- a/apps/meteor/tests/unit/client/components/AdministrationList/AdministrationModelList.spec.tsx +++ b/apps/meteor/tests/unit/client/components/AdministrationList/AdministrationModelList.spec.tsx @@ -6,37 +6,40 @@ import React from 'react'; import RouterContextMock from '../../../../mocks/client/RouterContextMock'; -const COMPONENT_PATH = '../../../../../client/components/AdministrationList/AdministrationModelList'; -const defaultConfig = { - '../../../app/ui-utils/client': { - '@noCallThru': true, - }, - 'meteor/kadira:flow-router': { - 'FlowRouter': {}, - '@noCallThru': true, - }, - '../../views/hooks/useUpgradeTabParams': { - 'useUpgradeTabParams': () => ({ - isLoading: false, - tabType: 'Upgrade', - trialEndDate: '2020-01-01', - }), - '@noCallThru': true, - }, - '../../../lib/upgradeTab': { - getUpgradeTabLabel: () => 'Upgrade', - isFullyFeature: () => true, - }, - '../../../app/authorization/client': { - 'userHasAllPermission': () => true, - '@noCallThru': true, - }, +const mockAdministrationModelListModule = (stubs = {}) => { + return proxyquire.load('../../../../../client/components/AdministrationList/AdministrationModelList', { + '../../../app/ui-utils/client': { + '@noCallThru': true, + }, + 'meteor/kadira:flow-router': { + 'FlowRouter': {}, + '@noCallThru': true, + }, + '../../views/hooks/useUpgradeTabParams': { + 'useUpgradeTabParams': () => ({ + isLoading: false, + tabType: 'Upgrade', + trialEndDate: '2020-01-01', + }), + '@noCallThru': true, + }, + '../../../lib/upgradeTab': { + getUpgradeTabLabel: () => 'Upgrade', + isFullyFeature: () => true, + }, + '../../../app/authorization/client': { + 'userHasAllPermission': () => true, + '@noCallThru': true, + }, + ...stubs, + // eslint-disable-next-line @typescript-eslint/consistent-type-imports + }) as typeof import('../../../../../client/components/AdministrationList/AdministrationModelList'); }; describe('components/AdministrationList/AdministrationModelList', () => { it('should render administration', async () => { - const AdministrationModelList = proxyquire.load(COMPONENT_PATH, defaultConfig).default; - render( null} accountBoxItems={[]} showAdmin={true} showWorkspace={true} />); + const AdministrationModelList = mockAdministrationModelListModule().default; + render( null} />); expect(screen.getByText('Administration')).to.exist; expect(screen.getByText('Workspace')).to.exist; @@ -44,45 +47,35 @@ describe('components/AdministrationList/AdministrationModelList', () => { }); it('should not render workspace', async () => { - const AdministrationModelList = proxyquire.load(COMPONENT_PATH, defaultConfig).default; - render( null} accountBoxItems={[]} showAdmin={true} showWorkspace={false} />); + const AdministrationModelList = mockAdministrationModelListModule().default; + render( null} />); expect(screen.getByText('Administration')).to.exist; expect(screen.queryByText('Workspace')).to.not.exist; expect(screen.getByText('Upgrade')).to.exist; }); - it('should not render workspace and upgrade when does not have permission', async () => { - const AdministrationModelList = proxyquire.load(COMPONENT_PATH, defaultConfig).default; - render( null} accountBoxItems={[]} showAdmin={false} />); - - expect(screen.getByText('Administration')).to.exist; - expect(screen.queryByText('Workspace')).to.not.exist; - expect(screen.queryByText('Upgrade')).to.not.exist; - }); - context('when clicked', () => { it('should go to admin info', async () => { const pushRoute = spy(); - const closeList = spy(); - const AdministrationModelList = proxyquire.load(COMPONENT_PATH, defaultConfig).default; + const handleDismiss = spy(); + const AdministrationModelList = mockAdministrationModelListModule().default; render( - + , ); const button = screen.getByText('Workspace'); userEvent.click(button); await waitFor(() => expect(pushRoute).to.have.been.called.with('admin-info')); - await waitFor(() => expect(closeList).to.have.been.called()); + await waitFor(() => expect(handleDismiss).to.have.been.called()); }); it('should go to admin index if no permission', async () => { const pushRoute = spy(); - const closeList = spy(); - const AdministrationModelList = proxyquire.load(COMPONENT_PATH, { - ...defaultConfig, + const handleDismiss = spy(); + const AdministrationModelList = mockAdministrationModelListModule({ '../../../app/authorization/client': { 'userHasAllPermission': () => false, '@noCallThru': true, @@ -90,23 +83,23 @@ describe('components/AdministrationList/AdministrationModelList', () => { }).default; render( - + , ); const button = screen.getByText('Workspace'); userEvent.click(button); await waitFor(() => expect(pushRoute).to.have.been.called.with('admin-index')); - await waitFor(() => expect(closeList).to.have.been.called()); + await waitFor(() => expect(handleDismiss).to.have.been.called()); }); it('should call upgrade route', async () => { - const closeList = spy(); + const handleDismiss = spy(); const pushRoute = spy(); - const AdministrationModelList = proxyquire.load(COMPONENT_PATH, defaultConfig).default; + const AdministrationModelList = mockAdministrationModelListModule().default; render( - + , ); const button = screen.getByText('Upgrade'); @@ -114,14 +107,13 @@ describe('components/AdministrationList/AdministrationModelList', () => { userEvent.click(button); await waitFor(() => expect(pushRoute).to.have.been.called.with('upgrade', { type: 'Upgrade' }, { trialEndDate: '2020-01-01' })); - await waitFor(() => expect(closeList).to.have.been.called()); + await waitFor(() => expect(handleDismiss).to.have.been.called()); }); it('should render admin box and call router', async () => { const router = spy(); - const closeList = spy(); - const AdministrationModelList = proxyquire.load(COMPONENT_PATH, { - ...defaultConfig, + const handleDismiss = spy(); + const AdministrationModelList = mockAdministrationModelListModule({ 'meteor/kadira:flow-router': { 'FlowRouter': { go: router, @@ -131,28 +123,34 @@ describe('components/AdministrationList/AdministrationModelList', () => { }).default; render( - , + , ); const button = screen.getByText('Admin Item'); userEvent.click(button); await waitFor(() => expect(router).to.have.been.called.with('admin-item')); - await waitFor(() => expect(closeList).to.have.been.called()); + await waitFor(() => expect(handleDismiss).to.have.been.called()); }); it('should render admin box and call sidenav', async () => { - const closeList = spy(); - const AdministrationModelList = proxyquire.load(COMPONENT_PATH, { - ...defaultConfig, - }).default; + const handleDismiss = spy(); + const AdministrationModelList = mockAdministrationModelListModule().default; render( - , + , ); const button = screen.getByText('Admin Item'); userEvent.click(button); - await waitFor(() => expect(closeList).to.have.been.called()); + await waitFor(() => expect(handleDismiss).to.have.been.called()); }); }); }); diff --git a/apps/meteor/tests/unit/client/components/AdministrationList/AppsModelList.spec.tsx b/apps/meteor/tests/unit/client/components/AdministrationList/AppsModelList.spec.tsx index a15af26cb14d..bda2d3b913d1 100644 --- a/apps/meteor/tests/unit/client/components/AdministrationList/AppsModelList.spec.tsx +++ b/apps/meteor/tests/unit/client/components/AdministrationList/AppsModelList.spec.tsx @@ -6,18 +6,21 @@ import React from 'react'; import RouterContextMock from '../../../../mocks/client/RouterContextMock'; -const COMPONENT_PATH = '../../../../../client/components/AdministrationList/AppsModelList'; -const defaultConfig = { - '../../../app/ui-message/client/ActionManager': { - 'triggerActionButtonAction': {}, - '@noCallThru': true, - }, +const mockAppsModelListModule = (stubs = {}) => { + return proxyquire.load('../../../../../client/components/AdministrationList/AppsModelList', { + '../../../app/ui-message/client/ActionManager': { + 'triggerActionButtonAction': {}, + '@noCallThru': true, + }, + ...stubs, + // eslint-disable-next-line @typescript-eslint/consistent-type-imports + }) as typeof import('../../../../../client/components/AdministrationList/AppsModelList'); }; describe('components/AdministrationList/AppsModelList', () => { it('should render apps', async () => { - const AppsModelList = proxyquire.load(COMPONENT_PATH, defaultConfig).default; - render( null} appBoxItems={[]} />); + const AppsModelList = mockAppsModelListModule().default; + render( null} appBoxItems={[]} />); expect(screen.getByText('Apps')).to.exist; expect(screen.getByText('Marketplace')).to.exist; @@ -25,13 +28,12 @@ describe('components/AdministrationList/AppsModelList', () => { }); it('should not render marketplace and installed when does not have permission', async () => { - const AppsModelList = proxyquire.load(COMPONENT_PATH, { - ...defaultConfig, + const AppsModelList = mockAppsModelListModule({ '@rocket.chat/ui-contexts': { useAtLeastOnePermission: (): boolean => false, }, }).default; - render( null} appBoxItems={[]} />); + render( null} appBoxItems={[]} />); expect(screen.getByText('Apps')).to.exist; expect(screen.queryByText('Marketplace')).to.not.exist; @@ -41,42 +43,41 @@ describe('components/AdministrationList/AppsModelList', () => { context('when clicked', () => { it('should go to admin marketplace', async () => { const pushRoute = spy(); - const closeList = spy(); - const AppsModelList = proxyquire.load(COMPONENT_PATH, defaultConfig).default; + const handleDismiss = spy(); + const AppsModelList = mockAppsModelListModule().default; render( - + , ); const button = screen.getByText('Marketplace'); userEvent.click(button); await waitFor(() => expect(pushRoute).to.have.been.called.with('admin-marketplace')); - await waitFor(() => expect(closeList).to.have.been.called()); + await waitFor(() => expect(handleDismiss).to.have.been.called()); }); it('should go to installed', async () => { const pushRoute = spy(); - const closeList = spy(); - const AppsModelList = proxyquire.load(COMPONENT_PATH, defaultConfig).default; + const handleDismiss = spy(); + const AppsModelList = mockAppsModelListModule().default; render( - + , ); const button = screen.getByText('Installed'); userEvent.click(button); await waitFor(() => expect(pushRoute).to.have.been.called.with('admin-marketplace', { context: 'installed', page: 'list' })); - await waitFor(() => expect(closeList).to.have.been.called()); + await waitFor(() => expect(handleDismiss).to.have.been.called()); }); it('should render apps and trigger action', async () => { const pushRoute = spy(); - const closeList = spy(); + const handleDismiss = spy(); const triggerActionButtonAction = spy(); - const AppsModelList = proxyquire.load(COMPONENT_PATH, { - ...defaultConfig, + const AppsModelList = mockAppsModelListModule({ '../../../app/ui-message/client/ActionManager': { triggerActionButtonAction, '@noCallThru': true, @@ -84,14 +85,14 @@ describe('components/AdministrationList/AppsModelList', () => { }).default; render( - + , ); const button = screen.getByText('Custom App'); userEvent.click(button); await waitFor(() => expect(triggerActionButtonAction).to.have.been.called()); - await waitFor(() => expect(closeList).to.have.been.called()); + await waitFor(() => expect(handleDismiss).to.have.been.called()); }); }); }); diff --git a/apps/meteor/tests/unit/client/components/AdministrationList/AuditModelList.spec.tsx b/apps/meteor/tests/unit/client/components/AdministrationList/AuditModelList.spec.tsx index 9807313c949b..e7acc9118554 100644 --- a/apps/meteor/tests/unit/client/components/AdministrationList/AuditModelList.spec.tsx +++ b/apps/meteor/tests/unit/client/components/AdministrationList/AuditModelList.spec.tsx @@ -26,32 +26,32 @@ describe('components/AdministrationList/AuditModelList', () => { context('when clicked', () => { it('should go to audit home', async () => { const pushRoute = spy(); - const closeList = spy(); + const handleDismiss = spy(); render( - + , ); const button = screen.getByText('Messages'); userEvent.click(button); await waitFor(() => expect(pushRoute).to.have.been.called.with('audit-home')); - await waitFor(() => expect(closeList).to.have.been.called()); + await waitFor(() => expect(handleDismiss).to.have.been.called()); }); it('should go to audit log', async () => { const pushRoute = spy(); - const closeList = spy(); + const handleDismiss = spy(); render( - + , ); const button = screen.getByText('Logs'); userEvent.click(button); await waitFor(() => expect(pushRoute).to.have.been.called.with('audit-log')); - await waitFor(() => expect(closeList).to.have.been.called()); + await waitFor(() => expect(handleDismiss).to.have.been.called()); }); }); });