Skip to content

Commit

Permalink
add unit test for search use case overview page
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 28, 2024
1 parent 81a179d commit 51ca3b1
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test('render list of cards', () => {
jest
.spyOn(embeddableStart, 'EmbeddablePanel')
.mockImplementation(() => <span>CardEmbeddablePanel</span>);
render(
const { container, queryAllByText } = render(
<CardList
embeddableServices={embeddableStart}
embeddable={
Expand All @@ -37,5 +37,37 @@ test('render list of cards', () => {
}
/>
);
expect(screen.queryAllByText('CardEmbeddablePanel')).toHaveLength(2);
expect(queryAllByText('CardEmbeddablePanel')).toHaveLength(2);
// verify container has div with class euiFlexGroup
expect(container.querySelector('.euiFlexGroup')).toBeInTheDocument();
});

test('render list of cards with grid', () => {
const embeddableStart = embeddablePluginMock.createStartContract();
jest
.spyOn(embeddableStart, 'EmbeddablePanel')
.mockImplementation(() => <span>CardEmbeddablePanel</span>);
const { container } = render(
<CardList
embeddableServices={embeddableStart}
embeddable={
new CardContainer(
{
id: 'card',
panels: {
'card-id-1': { type: CARD_EMBEDDABLE, explicitInput: { id: 'card-id-1' } },
'card-id-2': { type: CARD_EMBEDDABLE, explicitInput: { id: 'card-id-2' } },
'card-id-3': { type: CARD_EMBEDDABLE, explicitInput: { id: 'card-id-3' } },
'card-id-4': { type: CARD_EMBEDDABLE, explicitInput: { id: 'card-id-4' } },
},
grid: true,
columns: 2,
},
embeddableStart
)
}
/>
);
// verify container has div with class euiFlexGrid
expect(container.querySelector('.euiFlexGrid')).toBeInTheDocument();
});
1 change: 0 additions & 1 deletion src/plugins/content_management/public/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export const SEARCH_OVERVIEW_PAGE_ID = `${SEARCH_USE_CASE_ID}_overview`;
export const OBSERVABILITY_OVERVIEW_PAGE_ID = `${OBSERVABILITY_USE_CASE_ID}_overview`;
export const SECURITY_ANALYTICS_OVERVIEW_PAGE_ID = `${SECURITY_ANALYTICS_USE_CASE_ID}_overview`;
export const HOME_PAGE_ID = 'osd_homepage';
export const SEARCH_OVERVIEW_ID = 'search_overview';

// section ids
export enum SECTIONS {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('Sample data card', () => {
it('should call the getTargetArea function with the correct arguments', () => {
registerSampleDataCard(contentManagement, coreStart);
const call = registerContentProviderMock.mock.calls[0];
expect(call[0].getTargetArea()).toEqual(['analytics_overview/get_started']);
expect(call[0].getTargetArea()).toEqual('analytics_overview/get_started');
expect(call[0].getContent()).toMatchInlineSnapshot(`
Object {
"cardProps": Object {
Expand All @@ -39,5 +39,10 @@ describe('Sample data card', () => {
"title": "Try openSearch",
}
`);

// search use case overview
expect(registerContentProviderMock.mock.calls[1][0].getTargetArea()).toEqual(
'search_overview/get_started'
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { render } from '@testing-library/react';
import React from 'react';
import { coreMock } from '../../../../../../core/public/mocks';
import { OpenSearchDashboardsContextProvider } from '../../../../../opensearch_dashboards_react/public';
import { contentManagementPluginMocks } from '../../../../../content_management/public/mocks';
import { SearchUseCaseOverviewApp } from './search_use_case_app';
import { ContentManagementPluginStart } from '../../../../../content_management/public';
import { BehaviorSubject } from 'rxjs';
import { WorkspaceObject } from 'opensearch-dashboards/public';

describe('<SearchUseCaseOverviewApp />', () => {
const renderPageMock = jest.fn();
renderPageMock.mockReturnValue('dummy page');
const mock = {
...contentManagementPluginMocks.createStartContract(),
renderPage: renderPageMock,
};
const coreStartMocks = coreMock.createStart();

function renderSearchUseCaseOverviewApp(
contentManagement: ContentManagementPluginStart,
services = { ...coreStartMocks }
) {
return (
<OpenSearchDashboardsContextProvider services={services}>
<SearchUseCaseOverviewApp contentManagement={contentManagement} />
</OpenSearchDashboardsContextProvider>
);
}

beforeEach(() => {
jest.clearAllMocks();
});

it('render for workspace disabled case', () => {
const { container } = render(renderSearchUseCaseOverviewApp(mock, coreStartMocks));

expect(container).toMatchInlineSnapshot(`
<div>
dummy page
</div>
`);

expect(coreStartMocks.chrome.setBreadcrumbs).toHaveBeenCalledWith([
{ text: 'Search overview' },
]);
expect(mock.renderPage).toBeCalledWith('search_overview');
});

it('render for workspace enabled case', () => {
const coreStartMocksWithWorkspace = {
...coreStartMocks,
workspaces: {
...coreStartMocks.workspaces,
currentWorkspace$: new BehaviorSubject({
id: 'foo',
name: 'foo ws',
}),
},
};

const { container } = render(renderSearchUseCaseOverviewApp(mock, coreStartMocksWithWorkspace));

expect(container).toMatchInlineSnapshot(`
<div>
dummy page
</div>
`);

expect(coreStartMocks.chrome.setBreadcrumbs).toHaveBeenCalledWith([{ text: 'foo ws' }]);
expect(mock.renderPage).toBeCalledWith('search_overview');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { i18n } from '@osd/i18n';
import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public';
import {
ContentManagementPluginStart,
SEARCH_OVERVIEW_ID,
SEARCH_OVERVIEW_PAGE_ID,
} from '../../../../../content_management/public';

interface Props {
Expand All @@ -39,7 +39,7 @@ export const SearchUseCaseOverviewApp = ({ contentManagement }: Props) => {

return (
<I18nProvider>
{contentManagement ? contentManagement.renderPage(SEARCH_OVERVIEW_ID) : null}
{contentManagement ? contentManagement.renderPage(SEARCH_OVERVIEW_PAGE_ID) : null}
</I18nProvider>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { coreMock } from '../../../../../../core/public/mocks';
import { contentManagementPluginMocks } from '../../../../../content_management/public/mocks';
import { registerContentToSearchUseCasePage, setupSearchUseCase } from './search_use_case_setup';

describe('Search use case setup', () => {
const coreStart = coreMock.createStart();
const registerContentProviderMock = jest.fn();
const registerPageMock = jest.fn();

const contentManagementSetupMock = {
...contentManagementPluginMocks.createSetupContract(),
registerPage: registerPageMock,
};

const contentManagementStartMock = {
...contentManagementPluginMocks.createStartContract(),
registerContentProvider: registerContentProviderMock,
};

it('setupSearchUseCase', () => {
setupSearchUseCase(contentManagementSetupMock);
expect(registerPageMock).toHaveBeenCalledTimes(1);

const call = registerPageMock.mock.calls[0];
expect(call[0]).toMatchInlineSnapshot(`
Object {
"id": "search_overview",
"sections": Array [
Object {
"id": "get_started",
"kind": "card",
"order": 1000,
"title": "Set up search",
},
Object {
"columns": 2,
"grid": true,
"id": "different_search_types",
"kind": "card",
"order": 2000,
"title": "Try out different search techniques",
},
Object {
"columns": 2,
"grid": true,
"id": "config_evaluate_search",
"kind": "card",
"order": 3000,
"title": "Configure and evaluate search",
},
],
"title": "Overview",
}
`);
});

it('registerContentToSearchUseCasePage', () => {
registerContentToSearchUseCasePage(contentManagementStartMock, coreStart);

const call = registerContentProviderMock.mock.calls[0];
expect(call[0].getTargetArea()).toEqual('search_overview/get_started');
expect(call[0].getContent()).toMatchInlineSnapshot(`
Object {
"cardProps": Object {
"selectable": Object {
"children": <EuiI18n
default="Documentation"
token="home.searchOverview.setup.accessSearch.footer"
/>,
"isSelected": false,
"onClick": [Function],
},
},
"description": "You can run a search using REST API or language client. For experimentation, you can also run queries interactively.",
"id": "access_search_functionality",
"kind": "card",
"order": 10,
"title": "Access search functionality",
}
`);

// search type section
const searchTypesCall = registerContentProviderMock.mock.calls[2];
expect(searchTypesCall[0].getTargetArea()).toEqual('search_overview/different_search_types');
expect(searchTypesCall[0].getContent()).toMatchInlineSnapshot(`
Object {
"cardProps": Object {
"children": <div
className="euiCard__footer"
>
<EuiLink
external={true}
href="https://opensearch.org/docs/latest/query-dsl/full-text/query-string/"
target="_blank"
>
View Documentation
</EuiLink>
</div>,
"layout": "horizontal",
},
"description": "Lexical or keyword search matches documents based on exact words or phrases. Search the text using human-friendly query string query syntax or create complex, customizable queries using Query DSL—the OpenSearch query language.",
"getIcon": [Function],
"id": "text_search",
"kind": "card",
"order": 10,
"title": "Text search",
}
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import { i18n } from '@osd/i18n';
import {
ContentManagementPluginSetup,
ContentManagementPluginStart,
SEARCH_OVERVIEW_ID,
SEARCH_OVERVIEW_PAGE_ID,
SECTIONS,
SEARCH_OVERVIEW_CONTENT_AREAS,
} from '../../../../../content_management/public';

export const setupSearchUseCase = (contentManagement: ContentManagementPluginSetup) => {
contentManagement.registerPage({
id: SEARCH_OVERVIEW_ID,
id: SEARCH_OVERVIEW_PAGE_ID,
title: 'Overview',
sections: [
{
Expand Down

0 comments on commit 51ca3b1

Please sign in to comment.