From ebd9a24cc0a410d03c8d19daca147b143e5f4cc2 Mon Sep 17 00:00:00 2001 From: Hailong Cui Date: Mon, 31 Jul 2023 14:03:48 +0800 Subject: [PATCH] Add unit test Signed-off-by: Hailong Cui --- .../__snapshots__/application.test.tsx.snap | 109 +++++++++++++++ .../public/application.test.tsx | 126 ++++++++++++++++++ .../public/application.tsx | 9 +- 3 files changed, 240 insertions(+), 4 deletions(-) create mode 100644 src/plugins/management_overview/public/__snapshots__/application.test.tsx.snap create mode 100644 src/plugins/management_overview/public/application.test.tsx diff --git a/src/plugins/management_overview/public/__snapshots__/application.test.tsx.snap b/src/plugins/management_overview/public/__snapshots__/application.test.tsx.snap new file mode 100644 index 000000000000..27e649cbf1e7 --- /dev/null +++ b/src/plugins/management_overview/public/__snapshots__/application.test.tsx.snap @@ -0,0 +1,109 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Overview page rendering should render normally 1`] = ` +
+
+

+ + Overview + +

+
+
+
+
+
+

+ +

+
+

+ dev tools description +

+
+
+
+
+
+
+
+`; + +exports[`Overview page rendering should render normally when no application available 1`] = ` +
+
+

+ + Overview + +

+
+
+
+
+`; + +exports[`Overview page rendering should render normally when no overview app 1`] = ` +
+
+

+ + Overview + +

+
+
+
+
+`; diff --git a/src/plugins/management_overview/public/application.test.tsx b/src/plugins/management_overview/public/application.test.tsx new file mode 100644 index 000000000000..bbd33a6f86a1 --- /dev/null +++ b/src/plugins/management_overview/public/application.test.tsx @@ -0,0 +1,126 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { render } from '@testing-library/react'; +import { ManagementOverviewWrapper } from './application'; +import React from 'react'; +import { ApplicationStart, PublicAppInfo } from 'opensearch-dashboards/public'; +import { BehaviorSubject, Subject } from 'rxjs'; +import { deepFreeze } from '@osd/std'; +import { OverviewApp } from './overview_app'; +import { AppNavLinkStatus, AppStatus } from '../../../core/public'; + +const applicationStartMock = (apps: Map): jest.Mocked => { + const currentAppId$ = new Subject(); + + return { + applications$: new BehaviorSubject>(apps), + currentAppId$: currentAppId$.asObservable(), + capabilities: deepFreeze({ + catalogue: {}, + management: {}, + navLinks: {}, + }), + navigateToApp: jest.fn(), + navigateToUrl: jest.fn(), + getUrlForApp: jest.fn(), + registerMountContext: jest.fn(), + }; +}; + +function renderOverviewPage(apps: Map, overviewApps?: OverviewApp[]) { + return render( + + ); +} + +describe('Overview page rendering', () => { + it('should render normally', () => { + const overviewApps: OverviewApp[] = [ + { + id: 'dev_tools', + title: 'Dev Tools', + description: 'dev tools description', + order: 0, + }, + ]; + + const apps: Map = new Map(); + apps.set('dev_tools', { + status: AppStatus.accessible, + navLinkStatus: AppNavLinkStatus.default, + appRoute: '/app/console', + } as PublicAppInfo); + const { container, queryByText } = renderOverviewPage(apps, overviewApps); + expect(container.firstChild).toMatchSnapshot(); + expect(queryByText('Dev Tools')).not.toBeNull(); + }); + + it('should render normally when no overview app', () => { + const { container, queryByText } = renderOverviewPage(new Map()); + expect(container.firstChild).toMatchSnapshot(); + expect(queryByText('Overview')).not.toBeNull(); + }); + + it('should render normally when no application available', () => { + const overviewApps: OverviewApp[] = [ + { + id: 'dev_tools', + title: 'Dev Tools', + description: 'dev tools description', + order: 0, + }, + ]; + const { container, queryByText } = renderOverviewPage( + new Map(), + overviewApps + ); + expect(container.firstChild).toMatchSnapshot(); + expect(queryByText('Dev Tools')).toBeNull(); + }); + + it('should overview app not display when nav link status is hidden', () => { + const overviewApps: OverviewApp[] = [ + { + id: 'dev_tools', + title: 'Dev Tools', + description: 'dev tools description', + order: 0, + }, + ]; + + const apps: Map = new Map(); + apps.set('dev_tools', { + status: AppStatus.accessible, + navLinkStatus: AppNavLinkStatus.hidden, + appRoute: '/app/console', + } as PublicAppInfo); + const { queryByText } = renderOverviewPage(apps, overviewApps); + expect(queryByText('Dev Tools')).toBeNull(); + }); + + it('should overview app not display when it is invalid app', () => { + const overviewApps: OverviewApp[] = [ + { + id: 'invalid_app_id', + title: 'Dev Tools', + description: 'dev tools description', + order: 0, + }, + ]; + + const apps: Map = new Map(); + apps.set('dev_tools', { + status: AppStatus.accessible, + navLinkStatus: AppNavLinkStatus.hidden, + appRoute: '/app/console', + } as PublicAppInfo); + const { queryByText } = renderOverviewPage(apps, overviewApps); + expect(queryByText('Dev Tools')).toBeNull(); + }); +}); diff --git a/src/plugins/management_overview/public/application.tsx b/src/plugins/management_overview/public/application.tsx index 4252f295ff40..805c43081fdd 100644 --- a/src/plugins/management_overview/public/application.tsx +++ b/src/plugins/management_overview/public/application.tsx @@ -17,14 +17,15 @@ export interface ManagementOverviewProps { overviewApps?: OverviewApp[]; } -function ManagementOverviewWrapper(props: ManagementOverviewProps) { +export function ManagementOverviewWrapper(props: ManagementOverviewProps) { const { application, overviewApps } = props; const applications = useObservable(application.applications$); const availableApps = useMemo(() => { - return overviewApps?.filter( - (overviewApp) => applications?.get(overviewApp.id)?.navLinkStatus !== AppNavLinkStatus.hidden - ); + return overviewApps?.filter((overviewApp) => { + const app = applications?.get(overviewApp.id); + return app && app.navLinkStatus !== AppNavLinkStatus.hidden; + }); }, [applications, overviewApps]); return (