Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Multiple DataSource] DataSourceSelectable support to render label by getting it from dataSourceOptions #6358

Merged
merged 18 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [Multiple Datasource] Enhanced data source selector with default datasource shows as first choice ([#6293](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6293))
- [Multiple Datasource] Add multi data source support to sample vega visualizations ([#6218](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6218))
- [Multiple Datasource] Fetch data source title for DataSourceView when only id is provided ([#6315](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6315)
- [Multiple Datasource] Get data source label when only id is provided in DataSourceSelectable ([#6358](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6358)
- [Workspace] Add permission control logic ([#6052](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6052))
- [Multiple Datasource] Add default icon for selectable component and make sure the default datasource shows automatically ([#6327](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6327))
- [Multiple Datasource] Pass selected data sources to plugin consumers when the multi-select component initially loads ([#6333](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6333))
Expand Down

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
Expand Up @@ -10,7 +10,7 @@ import React from 'react';
import { DataSourceSelectable } from './data_source_selectable';
import { AuthType } from '../../types';
import { getDataSourcesWithFieldsResponse, mockResponseForSavedObjectsCalls } from '../../mocks';
import { render } from '@testing-library/react';
import { getByRole, render } from '@testing-library/react';
import * as utils from '../utils';

describe('DataSourceSelectable', () => {
Expand Down Expand Up @@ -169,4 +169,194 @@ describe('DataSourceSelectable', () => {
expect(onSelectedDataSource).toHaveBeenCalled();
expect(utils.getDefaultDataSource).toHaveBeenCalled();
});

it('should display selected option label normally', async () => {
const onSelectedDataSource = jest.fn();
const container = render(
<DataSourceSelectable
savedObjectsClient={client}
notifications={toasts}
onSelectedDataSources={onSelectedDataSource}
disabled={false}
hideLocalCluster={false}
fullWidth={false}
selectedOption={[{ id: 'test2', label: 'test2' }]}
dataSourceFilter={(ds) => ds.attributes.auth.type !== AuthType.NoAuth}
/>
);

await nextTick();
const button = await container.findByTestId('dataSourceSelectableContextMenuHeaderLink');
expect(button).toHaveTextContent('test2');
});

it('should display Local Cluster when not provide selectedOption', async () => {
const onSelectedDataSource = jest.fn();
const container = render(
<DataSourceSelectable
savedObjectsClient={client}
notifications={toasts}
onSelectedDataSources={onSelectedDataSource}
disabled={false}
hideLocalCluster={false}
fullWidth={false}
dataSourceFilter={(ds) => ds.attributes.auth.type !== AuthType.NoAuth}
/>
);
await nextTick();
const button = await container.findByTestId('dataSourceSelectableContextMenuHeaderLink');
expect(button).toHaveTextContent('Local cluster');
});

it('should render normally even only provide dataSourceId', async () => {
const onSelectedDataSource = jest.fn();
const container = render(
<DataSourceSelectable
savedObjectsClient={client}
notifications={toasts}
onSelectedDataSources={onSelectedDataSource}
disabled={false}
hideLocalCluster={false}
fullWidth={false}
selectedOption={[{ id: 'test2' }]}
dataSourceFilter={(ds) => ds.attributes.auth.type !== AuthType.NoAuth}
/>
);
await nextTick();
const button = await container.findByTestId('dataSourceSelectableContextMenuHeaderLink');
expect(button).toHaveTextContent('test2');
});

it('should render warning if provide undefined dataSourceId', async () => {
const onSelectedDataSource = jest.fn();
const container = render(
<DataSourceSelectable
savedObjectsClient={client}
notifications={toasts}
onSelectedDataSources={onSelectedDataSource}
disabled={false}
hideLocalCluster={false}
fullWidth={false}
selectedOption={[{ id: undefined }]}
dataSourceFilter={(ds) => ds.attributes.auth.type !== AuthType.NoAuth}
/>
);
await nextTick();
const button = await container.findByTestId('dataSourceSelectableContextMenuHeaderLink');
expect(button).toHaveTextContent('');
expect(toasts.addWarning).toBeCalledWith('Data source with id: undefined is not available');
});

it('should render warning if provide empty object', async () => {
const onSelectedDataSource = jest.fn();
const container = render(
<DataSourceSelectable
savedObjectsClient={client}
notifications={toasts}
onSelectedDataSources={onSelectedDataSource}
disabled={false}
hideLocalCluster={false}
fullWidth={false}
selectedOption={[{}]}
dataSourceFilter={(ds) => ds.attributes.auth.type !== AuthType.NoAuth}
/>
);
await nextTick();
const button = await container.findByTestId('dataSourceSelectableContextMenuHeaderLink');
expect(button).toHaveTextContent('');
expect(toasts.addWarning).toBeCalledWith('Data source with id: undefined is not available');
});
it('should warning if only provide label', async () => {
const onSelectedDataSource = jest.fn();
const container = render(
<DataSourceSelectable
savedObjectsClient={client}
notifications={toasts}
onSelectedDataSources={onSelectedDataSource}
disabled={false}
hideLocalCluster={false}
fullWidth={false}
selectedOption={[{ label: 'test-label' }]}
dataSourceFilter={(ds) => ds.attributes.auth.type !== AuthType.NoAuth}
/>
);
await nextTick();
expect(toasts.addWarning).toBeCalledWith('Data source with id: undefined is not available');
});
it('should warning if only provide empty label', async () => {
const onSelectedDataSource = jest.fn();
const container = render(
<DataSourceSelectable
savedObjectsClient={client}
notifications={toasts}
onSelectedDataSources={onSelectedDataSource}
disabled={false}
hideLocalCluster={false}
fullWidth={false}
selectedOption={[{ label: '' }]}
dataSourceFilter={(ds) => ds.attributes.auth.type !== AuthType.NoAuth}
/>
);
await nextTick();
expect(toasts.addWarning).toBeCalledWith('Data source with id: undefined is not available');
});

it('should warning if only provide empty array', async () => {
const onSelectedDataSource = jest.fn();
const container = render(
<DataSourceSelectable
savedObjectsClient={client}
notifications={toasts}
onSelectedDataSources={onSelectedDataSource}
disabled={false}
hideLocalCluster={true}
fullWidth={false}
selectedOption={[]}
/>
);
await nextTick();
expect(toasts.addWarning).toBeCalledWith('Data source with id: undefined is not available');
});

it('should render the selected option when pass in the valid dataSourceId', async () => {
const onSelectedDataSource = jest.fn();
const container = mount(
<DataSourceSelectable
savedObjectsClient={client}
notifications={toasts}
onSelectedDataSources={onSelectedDataSource}
disabled={false}
hideLocalCluster={true}
fullWidth={false}
selectedOption={[{ id: 'test2' }]}
/>
);
await nextTick();
const containerInstance = container.instance();
expect(containerInstance.state).toEqual({
dataSourceOptions: [
{
id: 'test1',
label: 'test1',
},
{
checked: 'on',
id: 'test2',
label: 'test2',
},
{
id: 'test3',
label: 'test3',
},
],
defaultDataSource: null,
isPopoverOpen: false,
selectedOption: [
{
id: 'test2',
label: 'test2',
},
],
});
});
});
Loading
Loading