Skip to content

Commit

Permalink
Handle set as default
Browse files Browse the repository at this point in the history
Signed-off-by: Yuanqi(Ella) Zhu <[email protected]>
  • Loading branch information
zhyuanqi committed Mar 26, 2024
1 parent be5a16a commit 0cd6a68
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('Datasource Management: Create Datasource Wizard', () => {

test('should create datasource successfully', async () => {
spyOn(utils, 'createSingleDataSource').and.returnValue({});
spyOn(utils, 'handleSetDefaultDatasourceDuringCreation').and.returnValue({});
spyOn(utils, 'handleSetDefaultDatasource').and.returnValue({});
await act(async () => {
// @ts-ignore
await component.find(formIdentifier).first().prop('handleSubmit')(
Expand All @@ -62,7 +62,7 @@ describe('Datasource Management: Create Datasource Wizard', () => {
});
expect(utils.createSingleDataSource).toHaveBeenCalled();
expect(history.push).toBeCalledWith('');
expect(utils.handleSetDefaultDatasourceDuringCreation).toHaveBeenCalled();
expect(utils.handleSetDefaultDatasource).toHaveBeenCalled();
});

test('should fail to create datasource', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
getDataSources,
testConnection,
fetchDataSourceVersion,
handleSetDefaultDatasourceDuringCreation,
handleSetDefaultDatasource,
} from '../utils';
import { LoadingMask } from '../loading_mask';

Expand Down Expand Up @@ -79,7 +79,7 @@ export const CreateDataSourceWizard: React.FunctionComponent<CreateDataSourceWiz
attributes.dataSourceVersion = version.dataSourceVersion;
await createSingleDataSource(savedObjects.client, attributes);
// Set the first create data source as default data source.
await handleSetDefaultDatasourceDuringCreation(savedObjects.client, uiSettings);
await handleSetDefaultDatasource(savedObjects.client, uiSettings);
props.history.push('');
} catch (e) {
setIsLoading(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe('DataSourceTable', () => {

it('should delete confirm modal confirm button work normally', async () => {
spyOn(utils, 'deleteMultipleDataSources').and.returnValue(Promise.resolve({}));
spyOn(utils, 'handleSetDefaultDatasourceAfterDeletion').and.returnValue({});
spyOn(utils, 'setFirstDataSourceAsDefault').and.returnValue({});
act(() => {
// @ts-ignore
component.find(tableIdentifier).props().selection.onSelectionChange(getMappedDataSources);
Expand All @@ -143,12 +143,12 @@ describe('DataSourceTable', () => {
});
component.update();
expect(component.find(confirmModalIdentifier).exists()).toBe(false);
expect(utils.handleSetDefaultDatasourceAfterDeletion).toHaveBeenCalled();
expect(utils.setFirstDataSourceAsDefault).toHaveBeenCalled();
});

it('should delete datasources & fail', async () => {
spyOn(utils, 'deleteMultipleDataSources').and.returnValue(Promise.reject({}));
spyOn(utils, 'handleSetDefaultDatasourceAfterDeletion').and.returnValue({});
spyOn(utils, 'setFirstDataSourceAsDefault').and.returnValue({});
act(() => {
// @ts-ignore
component.find(tableIdentifier).props().selection.onSelectionChange(getMappedDataSources);
Expand All @@ -164,7 +164,7 @@ describe('DataSourceTable', () => {
});
component.update();
expect(utils.deleteMultipleDataSources).toHaveBeenCalled();
expect(utils.handleSetDefaultDatasourceAfterDeletion).not.toHaveBeenCalled();
expect(utils.setFirstDataSourceAsDefault).not.toHaveBeenCalled();
// @ts-ignore
expect(component.find(confirmModalIdentifier).exists()).toBe(false);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ import {
} from '../../../../opensearch_dashboards_react/public';
import { DataSourceManagementContext, DataSourceTableItem, ToastMessageItem } from '../../types';
import { CreateButton } from '../create_button';
import {
deleteMultipleDataSources,
getDataSources,
handleSetDefaultDatasourceAfterDeletion,
} from '../utils';
import { deleteMultipleDataSources, getDataSources, setFirstDataSourceAsDefault } from '../utils';
import { LoadingMask } from '../loading_mask';

/* Table config */
Expand Down Expand Up @@ -256,7 +252,7 @@ export const DataSourceTable = ({ history }: RouteComponentProps) => {
try {
for (const dataSource of selectedDataSources) {
if (uiSettings.get('defaultDataSource') === dataSource.id) {
await handleSetDefaultDatasourceAfterDeletion(savedObjects.client, uiSettings);
await setFirstDataSourceAsDefault(savedObjects.client, uiSettings, true);
}
}
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe('Datasource Management: Edit Datasource Wizard', () => {
});
test('should delete datasource successfully', async () => {
spyOn(utils, 'deleteDataSourceById').and.returnValue({});
spyOn(utils, 'handleSetDefaultDatasourceAfterDeletion').and.returnValue({});
spyOn(utils, 'setFirstDataSourceAsDefault').and.returnValue({});
spyOn(uiSettings, 'get').and.returnValue('test1');
await act(async () => {
// @ts-ignore
Expand All @@ -148,11 +148,11 @@ describe('Datasource Management: Edit Datasource Wizard', () => {
});
expect(utils.deleteDataSourceById).toHaveBeenCalled();
expect(history.push).toBeCalledWith('');
expect(utils.handleSetDefaultDatasourceAfterDeletion).toHaveBeenCalled();
expect(utils.setFirstDataSourceAsDefault).toHaveBeenCalled();
});
test('should fail to delete datasource', async () => {
spyOn(utils, 'deleteDataSourceById').and.throwError('error');
spyOn(utils, 'handleSetDefaultDatasourceAfterDeletion').and.returnValue({});
spyOn(utils, 'setFirstDataSourceAsDefault').and.returnValue({});
spyOn(uiSettings, 'get').and.returnValue('test1');
await act(async () => {
// @ts-ignore
Expand All @@ -162,7 +162,7 @@ describe('Datasource Management: Edit Datasource Wizard', () => {
});
component.update();
expect(utils.deleteDataSourceById).toHaveBeenCalled();
expect(utils.handleSetDefaultDatasourceAfterDeletion).not.toHaveBeenCalled();
expect(utils.setFirstDataSourceAsDefault).not.toHaveBeenCalled();
});
test('should test connection', () => {
spyOn(utils, 'testConnection');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
getDataSources,
testConnection,
updateDataSourceById,
handleSetDefaultDatasourceAfterDeletion,
setFirstDataSourceAsDefault,
} from '../utils';
import { getEditBreadcrumbs } from '../breadcrumbs';
import { EditDataSourceForm } from './components/edit_form/edit_data_source_form';
Expand Down Expand Up @@ -127,7 +127,7 @@ export const EditDataSource: React.FunctionComponent<RouteComponentProps<{ id: s
const setDefaultDataSource = async () => {
try {
if (uiSettings.get('defaultDataSource') === dataSourceID) {
await handleSetDefaultDatasourceAfterDeletion(savedObjects.client, uiSettings);
await setFirstDataSourceAsDefault(savedObjects.client, uiSettings, true);
}
} catch (e) {
handleDisplayToastMessage({

Check warning on line 133 in src/plugins/data_source_management/public/components/edit_data_source/edit_data_source.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_source_management/public/components/edit_data_source/edit_data_source.tsx#L133

Added line #L133 was not covered by tests
Expand Down
44 changes: 32 additions & 12 deletions src/plugins/data_source_management/public/components/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
isValidUrl,
testConnection,
updateDataSourceById,
handleSetDefaultDatasourceAfterDeletion,
handleSetDefaultDatasourceDuringCreation,
handleSetDefaultDatasource,
setFirstDataSourceAsDefault,
} from './utils';
import { coreMock } from '../../../../core/public/mocks';
import {
Expand Down Expand Up @@ -279,29 +279,49 @@ describe('DataSourceManagement: Utils.ts', () => {
expect(getDefaultAuthMethod(authenticationMethodRegistry)?.name).toBe(AuthType.NoAuth);
});
});
describe('handleSetDefaultDatasourceAfterDeletion', () => {
test('should remove defaultDataSource setting and set new defaultDataSource if data sources exist', async () => {
describe('handle set default datasource', () => {
beforeEach(() => {
jest.clearAllMocks(); // Reset all mock calls before each test
});
test('should set default datasource when it does not have default datasource ', async () => {
mockUiSettingsCalls(uiSettings, 'get', null);
mockResponseForSavedObjectsCalls(savedObjects.client, 'find', getDataSourcesResponse);
await handleSetDefaultDatasource(savedObjects.client, uiSettings);
expect(uiSettings.set).toHaveBeenCalled();
});
test('should not set default datasource when it has default datasouce', async () => {
mockUiSettingsCalls(uiSettings, 'get', 'test');

await handleSetDefaultDatasourceAfterDeletion(savedObjects.client, uiSettings);
expect(uiSettings.set).toHaveBeenCalledWith('defaultDataSource', 'test');
mockResponseForSavedObjectsCalls(savedObjects.client, 'find', getDataSourcesResponse);
await handleSetDefaultDatasource(savedObjects.client, uiSettings);
expect(uiSettings.set).not.toHaveBeenCalled();
});
});
describe('handleSetDefaultDatasourceDuringCreation', () => {
describe('set first aataSource as default', () => {
beforeEach(() => {
jest.clearAllMocks(); // Reset all mock calls before each test
});
test('should not set defaultDataSource if more than one data source exists', async () => {
test('should set defaultDataSource if more than one data source exists', async () => {
mockResponseForSavedObjectsCalls(savedObjects.client, 'find', getDataSourcesResponse);
await handleSetDefaultDatasourceDuringCreation(savedObjects.client, uiSettings);
expect(uiSettings.set).not.toHaveBeenCalled();
await setFirstDataSourceAsDefault(savedObjects.client, uiSettings, true);
expect(uiSettings.set).toHaveBeenCalled();
});
test('should set defaultDataSource if only one data source exists', async () => {
mockResponseForSavedObjectsCalls(savedObjects.client, 'find', getSingleDataSourceResponse);
await handleSetDefaultDatasourceDuringCreation(savedObjects.client, uiSettings);
await setFirstDataSourceAsDefault(savedObjects.client, uiSettings, true);
expect(uiSettings.set).toHaveBeenCalled();
});
test('should not set defaultDataSource if no data source exists', async () => {
mockResponseForSavedObjectsCalls(savedObjects.client, 'find', { savedObjects: [] });
await setFirstDataSourceAsDefault(savedObjects.client, uiSettings, true);
expect(uiSettings.remove).toHaveBeenCalled();
expect(uiSettings.set).not.toHaveBeenCalled();
});
test('should not set defaultDataSource if no data source exists and no default datasouce', async () => {
mockResponseForSavedObjectsCalls(savedObjects.client, 'find', { savedObjects: [] });
await setFirstDataSourceAsDefault(savedObjects.client, uiSettings, false);
expect(uiSettings.remove).not.toHaveBeenCalled();
expect(uiSettings.set).not.toHaveBeenCalled();
});
});
describe('Check extractRegisteredAuthTypeCredentials method', () => {
test('Should extract credential field successfully', () => {
Expand Down
21 changes: 11 additions & 10 deletions src/plugins/data_source_management/public/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,27 @@ export async function getDataSourcesWithFields(
return response?.savedObjects;
}

export async function handleSetDefaultDatasourceAfterDeletion(
export async function handleSetDefaultDatasource(
savedObjectsClient: SavedObjectsClientContract,
uiSettings: IUiSettingsClient
) {
uiSettings.remove('defaultDataSource');
const listOfDataSources: DataSourceTableItem[] = await getDataSources(savedObjectsClient);
if (Array.isArray(listOfDataSources) && listOfDataSources.length >= 1) {
const datasourceId = listOfDataSources[0].id;
await uiSettings.set('defaultDataSource', datasourceId);
if (uiSettings.get('defaultDataSource', null) === null) {
return await setFirstDataSourceAsDefault(savedObjectsClient, uiSettings, false);
}
}

export async function handleSetDefaultDatasourceDuringCreation(
export async function setFirstDataSourceAsDefault(
savedObjectsClient: SavedObjectsClientContract,
uiSettings: IUiSettingsClient
uiSettings: IUiSettingsClient,
exists: boolean
) {
if (exists) {
uiSettings.remove('defaultDataSource');
}
const listOfDataSources: DataSourceTableItem[] = await getDataSources(savedObjectsClient);
if (Array.isArray(listOfDataSources) && listOfDataSources.length === 1) {
if (Array.isArray(listOfDataSources) && listOfDataSources.length >= 1) {
const datasourceId = listOfDataSources[0].id;
await uiSettings.set('defaultDataSource', datasourceId);
return await uiSettings.set('defaultDataSource', datasourceId);
}
}

Expand Down

0 comments on commit 0cd6a68

Please sign in to comment.