Skip to content

Commit ee737c5

Browse files
committed
test: add tests for service
Signed-off-by: tygao <[email protected]>
1 parent 33c970c commit ee737c5

File tree

6 files changed

+91
-5
lines changed

6 files changed

+91
-5
lines changed

examples/multiple_data_source_examples/public/components/data_source_view_example.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { CoreStart, MountPoint } from 'opensearch-dashboards/public';
1818
import {
1919
DataSourceManagementPluginSetup,
2020
DataSourceViewConfig,
21+
DataSourceSelection,
2122
} from 'src/plugins/data_source_management/public';
2223
import { ComponentProp } from './types';
2324
import { COLUMNS } from './constants';
@@ -88,6 +89,7 @@ export const DataSourceViewExample = ({
8889
setSelectedDataSources(ds);
8990
},
9091
}}
92+
dataSourceSelection={new DataSourceSelection()}
9193
/>
9294
);
9395
}, [setActionMenu, notifications, savedObjects]);

src/plugins/data_source_management/public/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ export {
2424
DataSourceMultiSelectableConfig,
2525
createDataSourceMenu,
2626
} from './components/data_source_menu';
27+
export { DataSourceSelection } from './service/data_source_selection_service';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
import { DataSourceSelection } from './data_source_selection_service';
6+
import { generateComponentId } from '../components/utils';
7+
8+
describe('DataSourceSelection service', () => {
9+
it('basic set, get and remove', async () => {
10+
const dataSourceSelection = new DataSourceSelection();
11+
const id = generateComponentId();
12+
const dataSource = { id: 'id', label: 'label' };
13+
expect(dataSourceSelection.getSelectionValue().get(id)).toBe(undefined);
14+
dataSourceSelection.selectDataSource(id, [dataSource]);
15+
expect(dataSourceSelection.getSelectionValue().get(id)).toStrictEqual([dataSource]);
16+
dataSourceSelection.remove(id);
17+
expect(dataSourceSelection.getSelectionValue().get(id)).toBe(undefined);
18+
});
19+
20+
it('multiple set and get', async () => {
21+
const dataSourceSelection = new DataSourceSelection();
22+
const id1 = generateComponentId();
23+
const id2 = generateComponentId();
24+
25+
const dataSource = { id: 'id', label: 'label' };
26+
expect(dataSourceSelection.getSelectionValue().get(id1)).toBe(undefined);
27+
expect(dataSourceSelection.getSelectionValue().get(id2)).toBe(undefined);
28+
dataSourceSelection.selectDataSource(id1, [dataSource]);
29+
dataSourceSelection.selectDataSource(id2, [dataSource]);
30+
expect(dataSourceSelection.getSelectionValue().get(id1)).toStrictEqual([dataSource]);
31+
expect(dataSourceSelection.getSelectionValue().get(id2)).toStrictEqual([dataSource]);
32+
dataSourceSelection.remove(id1);
33+
expect(dataSourceSelection.getSelectionValue().get(id1)).toBe(undefined);
34+
expect(dataSourceSelection.getSelectionValue().get(id2)).toStrictEqual([dataSource]);
35+
});
36+
37+
it('support subscribing selected observable', (done) => {
38+
const dataSourceSelection = new DataSourceSelection();
39+
const selectedDataSource$ = dataSourceSelection.getSelection$();
40+
const id = generateComponentId();
41+
const dataSource = { id: 'id', label: 'label' };
42+
dataSourceSelection.selectDataSource(id, [dataSource]);
43+
selectedDataSource$.subscribe((newValue) => {
44+
expect(newValue.get(id)).toStrictEqual([dataSource]);
45+
done();
46+
});
47+
});
48+
});

src/plugins/data_source_management/public/service/data_source_selection_service.ts

-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import uuid from 'uuid';
76
import { BehaviorSubject } from 'rxjs';
87
import { DataSourceOption } from '../components/data_source_menu/types';
98

@@ -30,8 +29,4 @@ export class DataSourceSelection {
3029
public getSelection$ = () => {
3130
return this.selectedDataSource$;
3231
};
33-
34-
public generateComponentId = () => {
35-
return uuid.v4();
36-
};
3732
}

src/plugins/navigation/public/top_nav_menu/__snapshots__/top_nav_menu.test.tsx.snap

+34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/plugins/navigation/public/top_nav_menu/top_nav_menu.test.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { TopNavMenu } from './top_nav_menu';
3636
import { TopNavMenuData } from './top_nav_menu_data';
3737
import { shallowWithIntl, mountWithIntl } from 'test_utils/enzyme_helpers';
3838
import * as testUtils from '../../../data_source_management/public/components/utils';
39+
import { DataSourceSelection } from '../../../data_source_management/public/service/data_source_selection_service';
3940

4041
const dataShim = {
4142
ui: {
@@ -63,6 +64,7 @@ describe('TopNavMenu', () => {
6364
run: jest.fn(),
6465
},
6566
];
67+
const dataSourceSelection = new DataSourceSelection();
6668

6769
it('Should render nothing when no config is provided', () => {
6870
const component = shallowWithIntl(<TopNavMenu appName={'test'} />);
@@ -122,6 +124,7 @@ describe('TopNavMenu', () => {
122124
spyOn(testUtils, 'getApplication').and.returnValue({ id: 'test2' });
123125
spyOn(testUtils, 'getUiSettings').and.returnValue({ id: 'test2' });
124126
spyOn(testUtils, 'getHideLocalCluster').and.returnValue(true);
127+
spyOn(testUtils, 'getDataSourceSelection').and.returnValue(dataSourceSelection);
125128
const component = shallowWithIntl(
126129
<TopNavMenu
127130
appName={'test'}
@@ -133,6 +136,7 @@ describe('TopNavMenu', () => {
133136
fullWidth: true,
134137
activeOption: [{ label: 'what', id: '1' }],
135138
},
139+
dataSourceSelection,
136140
}}
137141
/>
138142
);
@@ -144,6 +148,7 @@ describe('TopNavMenu', () => {
144148
spyOn(testUtils, 'getApplication').and.returnValue({ id: 'test2' });
145149
spyOn(testUtils, 'getUiSettings').and.returnValue({ id: 'test2' });
146150
spyOn(testUtils, 'getHideLocalCluster').and.returnValue(true);
151+
spyOn(testUtils, 'getDataSourceSelection').and.returnValue(dataSourceSelection);
147152

148153
const component = shallowWithIntl(
149154
<TopNavMenu
@@ -157,6 +162,7 @@ describe('TopNavMenu', () => {
157162
fullWidth: true,
158163
activeOption: [{ label: 'what', id: '1' }],
159164
},
165+
dataSourceSelection: new DataSourceSelection(),
160166
}}
161167
/>
162168
);

0 commit comments

Comments
 (0)