From ff16c79f02976f8aa04eb0a29c3df017e3cfcfe0 Mon Sep 17 00:00:00 2001 From: tygao Date: Wed, 27 Mar 2024 16:24:43 +0800 Subject: [PATCH] add util test Signed-off-by: tygao --- src/plugins/workspace/public/hooks.ts | 2 +- src/plugins/workspace/public/utils.test.ts | 54 +++++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/plugins/workspace/public/hooks.ts b/src/plugins/workspace/public/hooks.ts index c2c08438121d..a63dc8f83d3d 100644 --- a/src/plugins/workspace/public/hooks.ts +++ b/src/plugins/workspace/public/hooks.ts @@ -12,7 +12,7 @@ export function useApplications(application?: ApplicationStart) { const applications = useObservable(application?.applications$ ?? of(new Map()), new Map()); return useMemo(() => { const apps: PublicAppInfo[] = []; - applications?.forEach((app) => { + applications.forEach((app) => { apps.push(app); }); return apps; diff --git a/src/plugins/workspace/public/utils.test.ts b/src/plugins/workspace/public/utils.test.ts index 510a775cd745..17bfc65ad04a 100644 --- a/src/plugins/workspace/public/utils.test.ts +++ b/src/plugins/workspace/public/utils.test.ts @@ -3,7 +3,8 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { featureMatchesConfig } from './utils'; +import { featureMatchesConfig, getSelectedFeatureQuantities } from './utils'; +import { PublicAppInfo } from '../../../core/public'; describe('workspace utils: featureMatchesConfig', () => { it('feature configured with `*` should match any features', () => { @@ -91,3 +92,54 @@ describe('workspace utils: featureMatchesConfig', () => { ); }); }); + +describe('workspace utils: getSelectedFeatureQuantities', () => { + const defaultApplications = [ + { + appRoute: '/app/dashboards', + id: 'dashboards', + title: 'Dashboards', + category: { + id: 'opensearchDashboards', + label: 'OpenSearch Dashboards', + euiIconType: 'inputOutput', + order: 1000, + }, + status: 0, + navLinkStatus: 1, + }, + { + appRoute: '/app/dev_tools', + id: 'dev_tools', + title: 'Dev Tools', + category: { + id: 'management', + label: 'Management', + order: 5000, + euiIconType: 'managementApp', + }, + status: 0, + navLinkStatus: 1, + }, + ] as PublicAppInfo[]; + it('should support * rules', () => { + const { total, selected } = getSelectedFeatureQuantities(['*'], defaultApplications); + expect(total).toBe(2); + expect(selected).toBe(2); + }); + + it('should support @ and exclude rule', () => { + const { total, selected } = getSelectedFeatureQuantities(['!@management'], defaultApplications); + expect(total).toBe(2); + expect(selected).toBe(0); + }); + + it('should get quantity normally', () => { + const { total, selected } = getSelectedFeatureQuantities( + ['!@management', 'dev_tools'], + defaultApplications + ); + expect(total).toBe(2); + expect(selected).toBe(1); + }); +});