Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Hailong Cui <[email protected]>
  • Loading branch information
Hailong-am committed Aug 10, 2023
1 parent 1613919 commit 5004ee0
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 4 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

126 changes: 126 additions & 0 deletions src/plugins/management_overview/public/application.test.tsx
Original file line number Diff line number Diff line change
@@ -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<string, PublicAppInfo>): jest.Mocked<ApplicationStart> => {
const currentAppId$ = new Subject<string | undefined>();

return {
applications$: new BehaviorSubject<Map<string, PublicAppInfo>>(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<string, PublicAppInfo>, overviewApps?: OverviewApp[]) {
return render(
<ManagementOverviewWrapper
application={applicationStartMock(apps)}
overviewApps={overviewApps}
/>
);
}

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<string, PublicAppInfo> = new Map<string, PublicAppInfo>();
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<string, PublicAppInfo>());
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<string, PublicAppInfo>(),
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<string, PublicAppInfo> = new Map<string, PublicAppInfo>();
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<string, PublicAppInfo> = new Map<string, PublicAppInfo>();
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();
});
});
9 changes: 5 additions & 4 deletions src/plugins/management_overview/public/application.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit 5004ee0

Please sign in to comment.