Skip to content

Commit

Permalink
test: add tests for service
Browse files Browse the repository at this point in the history
Signed-off-by: tygao <tygao@amazon.com>
  • Loading branch information
raintygao committed May 24, 2024
1 parent 33c970c commit ee737c5
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { CoreStart, MountPoint } from 'opensearch-dashboards/public';
import {
DataSourceManagementPluginSetup,
DataSourceViewConfig,
DataSourceSelection,
} from 'src/plugins/data_source_management/public';
import { ComponentProp } from './types';
import { COLUMNS } from './constants';
Expand Down Expand Up @@ -88,6 +89,7 @@ export const DataSourceViewExample = ({
setSelectedDataSources(ds);
},
}}
dataSourceSelection={new DataSourceSelection()}
/>
);
}, [setActionMenu, notifications, savedObjects]);
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data_source_management/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export {
DataSourceMultiSelectableConfig,
createDataSourceMenu,
} from './components/data_source_menu';
export { DataSourceSelection } from './service/data_source_selection_service';
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { DataSourceSelection } from './data_source_selection_service';
import { generateComponentId } from '../components/utils';

describe('DataSourceSelection service', () => {
it('basic set, get and remove', async () => {
const dataSourceSelection = new DataSourceSelection();
const id = generateComponentId();
const dataSource = { id: 'id', label: 'label' };
expect(dataSourceSelection.getSelectionValue().get(id)).toBe(undefined);
dataSourceSelection.selectDataSource(id, [dataSource]);
expect(dataSourceSelection.getSelectionValue().get(id)).toStrictEqual([dataSource]);
dataSourceSelection.remove(id);
expect(dataSourceSelection.getSelectionValue().get(id)).toBe(undefined);
});

it('multiple set and get', async () => {
const dataSourceSelection = new DataSourceSelection();
const id1 = generateComponentId();
const id2 = generateComponentId();

const dataSource = { id: 'id', label: 'label' };
expect(dataSourceSelection.getSelectionValue().get(id1)).toBe(undefined);
expect(dataSourceSelection.getSelectionValue().get(id2)).toBe(undefined);
dataSourceSelection.selectDataSource(id1, [dataSource]);
dataSourceSelection.selectDataSource(id2, [dataSource]);
expect(dataSourceSelection.getSelectionValue().get(id1)).toStrictEqual([dataSource]);
expect(dataSourceSelection.getSelectionValue().get(id2)).toStrictEqual([dataSource]);
dataSourceSelection.remove(id1);
expect(dataSourceSelection.getSelectionValue().get(id1)).toBe(undefined);
expect(dataSourceSelection.getSelectionValue().get(id2)).toStrictEqual([dataSource]);
});

it('support subscribing selected observable', (done) => {
const dataSourceSelection = new DataSourceSelection();
const selectedDataSource$ = dataSourceSelection.getSelection$();
const id = generateComponentId();
const dataSource = { id: 'id', label: 'label' };
dataSourceSelection.selectDataSource(id, [dataSource]);
selectedDataSource$.subscribe((newValue) => {
expect(newValue.get(id)).toStrictEqual([dataSource]);
done();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* SPDX-License-Identifier: Apache-2.0
*/

import uuid from 'uuid';
import { BehaviorSubject } from 'rxjs';
import { DataSourceOption } from '../components/data_source_menu/types';

Expand All @@ -30,8 +29,4 @@ export class DataSourceSelection {
public getSelection$ = () => {
return this.selectedDataSource$;
};

public generateComponentId = () => {
return uuid.v4();
};
}

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 @@ -36,6 +36,7 @@ import { TopNavMenu } from './top_nav_menu';
import { TopNavMenuData } from './top_nav_menu_data';
import { shallowWithIntl, mountWithIntl } from 'test_utils/enzyme_helpers';
import * as testUtils from '../../../data_source_management/public/components/utils';
import { DataSourceSelection } from '../../../data_source_management/public/service/data_source_selection_service';

const dataShim = {
ui: {
Expand Down Expand Up @@ -63,6 +64,7 @@ describe('TopNavMenu', () => {
run: jest.fn(),
},
];
const dataSourceSelection = new DataSourceSelection();

it('Should render nothing when no config is provided', () => {
const component = shallowWithIntl(<TopNavMenu appName={'test'} />);
Expand Down Expand Up @@ -122,6 +124,7 @@ describe('TopNavMenu', () => {
spyOn(testUtils, 'getApplication').and.returnValue({ id: 'test2' });
spyOn(testUtils, 'getUiSettings').and.returnValue({ id: 'test2' });
spyOn(testUtils, 'getHideLocalCluster').and.returnValue(true);
spyOn(testUtils, 'getDataSourceSelection').and.returnValue(dataSourceSelection);
const component = shallowWithIntl(
<TopNavMenu
appName={'test'}
Expand All @@ -133,6 +136,7 @@ describe('TopNavMenu', () => {
fullWidth: true,
activeOption: [{ label: 'what', id: '1' }],
},
dataSourceSelection,
}}
/>
);
Expand All @@ -144,6 +148,7 @@ describe('TopNavMenu', () => {
spyOn(testUtils, 'getApplication').and.returnValue({ id: 'test2' });
spyOn(testUtils, 'getUiSettings').and.returnValue({ id: 'test2' });
spyOn(testUtils, 'getHideLocalCluster').and.returnValue(true);
spyOn(testUtils, 'getDataSourceSelection').and.returnValue(dataSourceSelection);

const component = shallowWithIntl(
<TopNavMenu
Expand All @@ -157,6 +162,7 @@ describe('TopNavMenu', () => {
fullWidth: true,
activeOption: [{ label: 'what', id: '1' }],
},
dataSourceSelection: new DataSourceSelection(),
}}
/>
);
Expand Down

0 comments on commit ee737c5

Please sign in to comment.