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

[Backport 2.x] [Multiple DataSource] DataSourceSelectable support to render label by getting it from dataSourceOptions #6382

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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

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 @@ -74,8 +74,7 @@ describe('create data source menu', () => {
perPage: 10000,
type: 'data-source',
});
expect(notifications.toasts.addWarning).toBeCalledTimes(0);
expect(getByText(component.container, 'Local cluster')).toBeInTheDocument();
expect(notifications.toasts.addWarning).toBeCalledTimes(2);
});
});

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,226 @@ 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 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',
},
],
});
});

it('should render nothing when no default option or activeOption', async () => {
const onSelectedDataSource = jest.fn();
spyOn(utils, 'getDefaultDataSource').and.returnValue(undefined);
const container = mount(
<DataSourceSelectable
savedObjectsClient={client}
notifications={toasts}
onSelectedDataSources={onSelectedDataSource}
disabled={false}
hideLocalCluster={false}
fullWidth={false}
dataSourceFilter={(ds) => ds.attributes.auth.type !== AuthType.NoAuth}
/>
);
await nextTick();

const containerInstance = container.instance();

expect(onSelectedDataSource).toBeCalledTimes(0);
expect(containerInstance.state).toEqual({
dataSourceOptions: [],
defaultDataSource: null,
isPopoverOpen: false,
selectedOption: [],
});

containerInstance.onChange([{ id: 'test2', label: 'test2', checked: 'on' }]);
expect(containerInstance.state).toEqual({
dataSourceOptions: [
{
checked: 'on',
id: 'test2',
label: 'test2',
},
],
defaultDataSource: null,
isPopoverOpen: false,
selectedOption: [
{
checked: 'on',
id: 'test2',
label: 'test2',
},
],
});

expect(onSelectedDataSource).toBeCalledWith([{ id: 'test2', label: 'test2' }]);
expect(onSelectedDataSource).toHaveBeenCalled();
});
});
Loading
Loading