Skip to content

Commit

Permalink
fix failed test
Browse files Browse the repository at this point in the history
Signed-off-by: yujin-emma <yujin.emma.work@gmail.com>
  • Loading branch information
yujin-emma committed Apr 7, 2024
1 parent 83e0780 commit 0c51068
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 34 deletions.

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

39 changes: 16 additions & 23 deletions src/plugins/data_source_management/public/components/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
mockUiSettingsCalls,
getSingleDataSourceResponse,
getDataSource,
getDataSourceOptions,
} from '../mocks';
import {
AuthType,
Expand Down Expand Up @@ -518,64 +519,56 @@ describe('DataSourceManagement: Utils.ts', () => {

const result = getFilteredDataSources(dataSources);

expect(result).toEqual(dataSources);
expect(result).toEqual([
{
id: '1',
label: 'DataSource 1',
},
]);
});

test('should return filtered data sources when a filter is provided', () => {
const filter = (dataSource: SavedObject<DataSourceAttributes>) => dataSource.id === '2';
const result = getFilteredDataSources(getDataSource, filter);

expect(result).toEqual([
{
id: '2',
type: '',
references: [],
attributes: {
title: 'DataSource 2',
endpoint: '',
auth: { type: AuthType.NoAuth, credentials: undefined },
name: AuthType.NoAuth,
},
label: 'DataSource 2',
},
]);
});
});
describe('getDefaultDataSource', () => {
const LocalCluster = { id: 'local', label: 'Local Cluster' };
const hideLocalCluster = false;
const defaultOption = [{ id: '2', label: 'Default Option' }];
const defaultOption = [{ id: '2', label: 'DataSource 2' }];

it('should return the default option if it exists in the data sources', () => {
mockUiSettingsCalls(uiSettings, 'get', '2');
const result = getDefaultDataSource(
getDataSource,
getDataSourceOptions,
LocalCluster,
uiSettings,
hideLocalCluster,
defaultOption
'2',
hideLocalCluster
);
expect(result).toEqual([defaultOption[0]]);
});

it('should return local cluster if it exists and no default options in the data sources', () => {
mockUiSettingsCalls(uiSettings, 'get', null);
const result = getDefaultDataSource(
getDataSource,
LocalCluster,
uiSettings,
hideLocalCluster
);
const result = getDefaultDataSource(getDataSource, LocalCluster, null, hideLocalCluster);
expect(result).toEqual([LocalCluster]);
});

it('should return the default datasource if hideLocalCluster is false', () => {
mockUiSettingsCalls(uiSettings, 'get', '2');
const result = getDefaultDataSource(getDataSource, LocalCluster, uiSettings, true);
const result = getDefaultDataSource(getDataSourceOptions, LocalCluster, '2', false);
expect(result).toEqual([{ id: '2', label: 'DataSource 2' }]);
});

it('should return the first data source if no default option, hideLocalCluster is ture and no default datasource', () => {
mockUiSettingsCalls(uiSettings, 'get', null);
const result = getDefaultDataSource(getDataSource, LocalCluster, uiSettings, true);
const result = getDefaultDataSource(getDataSourceOptions, LocalCluster, uiSettings, true);
expect(result).toEqual([{ id: '1', label: 'DataSource 1' }]);
});
});
Expand Down
15 changes: 15 additions & 0 deletions src/plugins/data_source_management/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ export const getDataSource = [
},
];

export const getDataSourceOptions = [
{
id: '1',
label: 'DataSource 1',
},
{
id: '2',
label: 'DataSource 2',
},
{
id: '3',
label: 'DataSource 1',
},
];

/* Mock data responses - JSON*/
export const getDataSourcesResponse = {
savedObjects: [
Expand Down

0 comments on commit 0c51068

Please sign in to comment.