Skip to content

Commit

Permalink
change component name and add test
Browse files Browse the repository at this point in the history
Signed-off-by: Lu Yu <[email protected]>
  • Loading branch information
BionIT committed Feb 3, 2024
1 parent 3db3d74 commit 006450c
Show file tree
Hide file tree
Showing 12 changed files with 292 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"requiredPlugins": ["management", "dataSource", "indexPatternManagement"],
"optionalPlugins": [],
"requiredBundles": ["opensearchDashboardsReact"],
"extraPublicDirs": ["public/components/utils", "public/components/data_source_picker/data_source_picker"]
"extraPublicDirs": ["public/components/utils"]
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { ShallowWrapper, shallow } from 'enzyme';
import { ClusterSelector } from './cluster_selector';
import { SavedObjectsClientContract } from '../../../../../core/public';
import { notificationServiceMock } from '../../../../../core/public/mocks';
import React from 'react';

describe('ClusterSelector', () => {
let component: ShallowWrapper<any, Readonly<{}>, React.Component<{}, {}, any>>;

let client: SavedObjectsClientContract;
const { toasts } = notificationServiceMock.createStartContract();

beforeEach(() => {
client = {
find: jest.fn().mockResolvedValue([]),
} as any;
component = shallow(
<ClusterSelector
savedObjectsClient={client}
notifications={toasts}
onSelectedDataSource={jest.fn()}
disabled={false}
fullWidth={false}
/>
);
});

it('should render normally', () => {
expect(component).toMatchSnapshot();
expect(client.find).toBeCalledWith({
fields: ['id', 'description', 'title'],
perPage: 10000,
type: 'data-source',
});
expect(toasts.addWarning).toBeCalledTimes(0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,44 @@
*/

import React from 'react';
import { getDataSources } from '../utils';
import { i18n } from '@osd/i18n';
import { EuiComboBox } from '@elastic/eui';
import { SavedObjectsClientContract, ToastsStart } from 'opensearch-dashboards/public';
import { getDataSources } from '../utils';

export const LocalCluster = {
export const LocalCluster: ClusterOption = {
label: i18n.translate('dataSource.localCluster', {
defaultMessage: 'Local cluster',
}),
id: '',
};

export class DataSourcePicker extends React.Component {
constructor(props) {
interface ClusterSelectorProps {
savedObjectsClient: SavedObjectsClientContract;
notifications: ToastsStart;
onSelectedDataSource: (clusterOption: ClusterOption[]) => void;
disabled: boolean;
fullWidth: boolean;
}

interface ClusterSelectorState {
clusterOptions: ClusterOption[];
selectedOption: ClusterOption[];
}

export interface ClusterOption {
label: string;
id: string;
}

export class ClusterSelector extends React.Component<ClusterSelectorProps, ClusterSelectorState> {
private _isMounted: boolean = false;

constructor(props: ClusterSelectorProps) {
super(props);

this.state = {
dataSources: [],
clusterOptions: [],
selectedOption: [LocalCluster],
};
}
Expand All @@ -34,17 +55,17 @@ export class DataSourcePicker extends React.Component {
getDataSources(this.props.savedObjectsClient)
.then((fetchedDataSources) => {
if (fetchedDataSources?.length) {
const dataSourceOptions = fetchedDataSources.map((dataSource) => ({
const clusterOptions = fetchedDataSources.map((dataSource) => ({
id: dataSource.id,
label: dataSource.title,
}));

dataSourceOptions.push(LocalCluster);
clusterOptions.push(LocalCluster);

if (!this._isMounted) return;
this.setState({
...this.state,
dataSources: dataSourceOptions,
clusterOptions,
});
}
})
Expand Down Expand Up @@ -75,7 +96,7 @@ export class DataSourcePicker extends React.Component {
defaultMessage: 'Select a data source',
})}
singleSelection={{ asPlainText: true }}
options={this.state.dataSources}
options={this.state.clusterOptions}
selectedOptions={this.state.selectedOption}
onChange={(e) => this.onChange(e)}
prepend={i18n.translate('dataSourceComboBoxPrepend', {
Expand All @@ -84,6 +105,7 @@ export class DataSourcePicker extends React.Component {
compressed
isDisabled={this.props.disabled}
fullWidth={this.props.fullWidth || false}
data-test-subj={'clusterSelectorComboBox'}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

export { ClusterSelector } from './cluster_selector';
1 change: 1 addition & 0 deletions src/plugins/data_source_management/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export function plugin() {
return new DataSourceManagementPlugin();
}
export { DataSourceManagementPluginStart } from './types';
export { ClusterSelector } from './components/cluster_selector';
8 changes: 4 additions & 4 deletions src/plugins/dev_tools/public/application.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ import {
ScopedHistory,
} from 'src/core/public';

// eslint-disable-next-line @osd/eslint/no-restricted-paths
import { DataSourcePicker } from '../../data_source_management/public/components/data_source_picker/data_source_picker';
import { ClusterSelector } from 'src/plugins/data_source_management/public';
import { DevToolApp } from './dev_tool';
import { DevToolsSetupDependencies } from './plugin';
import { addHelpMenuToAppChrome } from './utils/util';
Expand Down Expand Up @@ -131,12 +130,13 @@ function DevToolsWrapper({
</EuiToolTip>
))}
{dataSourceEnabled ? (
<div className="devAppDataSourcePicker">
<DataSourcePicker
<div className="devAppClusterSelector">
<ClusterSelector
savedObjectsClient={savedObjects.client}
notifications={toasts}
onSelectedDataSource={onChange}
disabled={!dataSourceEnabled}
fullWidth={false}
/>
</div>
) : null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ import PropTypes from 'prop-types';
import { Synopsis } from './synopsis';
import { SampleDataSetCards } from './sample_data_set_cards';
import { getServices } from '../opensearch_dashboards_services';
// eslint-disable-next-line @osd/eslint/no-restricted-paths
import { DataSourcePicker } from '../../../../data_source_management/public/components/data_source_picker/data_source_picker';

import {
EuiPage,
Expand All @@ -53,6 +51,7 @@ import {
import { getTutorials } from '../load_tutorials';
import { injectI18n, FormattedMessage } from '@osd/i18n/react';
import { i18n } from '@osd/i18n';
import { ClusterSelector } from '../../../../data_source_management/public';

const ALL_TAB_ID = 'all';
const SAMPLE_DATA_TAB_ID = 'sampleData';
Expand Down Expand Up @@ -228,8 +227,8 @@ class TutorialDirectoryUi extends React.Component {
const { isDataSourceEnabled } = this.state;

return isDataSourceEnabled ? (
<div className="sampledataSourcePicker">
<DataSourcePicker
<div className="sampleDataClusterSelector">
<ClusterSelector
savedObjectsClient={getServices().savedObjectsClient}
notifications={getServices().toastNotifications}
onSelectedDataSource={this.onSelectedDataSourceChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
"dataSource"
],
"extraPublicDirs": ["public/lib"],
"requiredBundles": ["opensearchDashboardsReact", "home", "dataSourceManagement"]
"requiredBundles": ["opensearchDashboardsReact", "home"]
}
Loading

0 comments on commit 006450c

Please sign in to comment.