Skip to content

Commit

Permalink
[Multiple DataSource] DataSourceSelectable support to render label by…
Browse files Browse the repository at this point in the history
… getting it from dataSourceOptions (#6358) (#6382)

* get label from dataSourceOptions

Signed-off-by: yujin-emma <yujin.emma.work@gmail.com>

* update dataSourceOptions

Signed-off-by: yujin-emma <yujin.emma.work@gmail.com>

* update changelog

Signed-off-by: yujin-emma <yujin.emma.work@gmail.com>

* fix failed test

Signed-off-by: yujin-emma <yujin.emma.work@gmail.com>

* address comments and fix test

Signed-off-by: yujin-emma <yujin.emma.work@gmail.com>

* update selected option checked status and udpate snapshot

Signed-off-by: yujin-emma <yujin.emma.work@gmail.com>

* update selectable test

Signed-off-by: yujin-emma <yujin.emma.work@gmail.com>

* revert example code

Signed-off-by: yujin-emma <yujin.emma.work@gmail.com>

* revern config file

Signed-off-by: yujin-emma <yujin.emma.work@gmail.com>

* push the utils

Signed-off-by: yujin-emma <yujin.emma.work@gmail.com>

* udpate snapshot

Signed-off-by: yujin-emma <yujin.emma.work@gmail.com>

* remove console log

Signed-off-by: yujin-emma <yujin.emma.work@gmail.com>

* udate default data source

Signed-off-by: yujin-emma <yujin.emma.work@gmail.com>

* remove unnessary check for empty input

Signed-off-by: yujin-emma <yujin.emma.work@gmail.com>

* fix failed test

Signed-off-by: yujin-emma <yujin.emma.work@gmail.com>

* fix failed test

Signed-off-by: yujin-emma <yujin.emma.work@gmail.com>

---------

Signed-off-by: yujin-emma <yujin.emma.work@gmail.com>
(cherry picked from commit 85df662)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

# Conflicts:
#	CHANGELOG.md

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 93525c2 commit 9066a21
Show file tree
Hide file tree
Showing 9 changed files with 375 additions and 124 deletions.

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

0 comments on commit 9066a21

Please sign in to comment.