Skip to content

Commit

Permalink
[BUG] Check for duplicate data source id before fetching the data sou…
Browse files Browse the repository at this point in the history
…rces in index pattern selector (#6498) (#6508)

* remove duplicate entries



* add test



---------


(cherry picked from commit d2d410b)

Signed-off-by: Lu Yu <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent bd35609 commit 5b048ba
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { shallow } from 'enzyme';
import { SavedObjectsClientContract } from '../../../../../core/public';
import React from 'react';
import IndexPatternSelect from './index_pattern_select';

describe('IndexPatternSelect', () => {
let client: SavedObjectsClientContract;
const bulkGetMock = jest.fn();

const nextTick = () => new Promise((res) => process.nextTick(res));

beforeEach(() => {
client = {
find: jest.fn().mockResolvedValue({
savedObjects: [
{
references: [{ id: 'testDataSourceId', type: 'data-source' }],
attributes: { title: 'testTitle1' },
},
{
references: [{ id: 'testDataSourceId', type: 'data-source' }],
attributes: { title: 'testTitle2' },
},
],
}),
bulkGet: bulkGetMock,
get: jest.fn().mockResolvedValue({
references: [{ id: 'someId', type: 'data-source' }],
attributes: { title: 'testTitle' },
}),
} as any;
});

it('should render index pattern select', async () => {
const onChangeMock = jest.fn();
const compInstance = shallow(
<IndexPatternSelect
placeholder={'test index pattern'}
indexPatternId={'testId'}
onChange={onChangeMock}
data-test-subj={'testId'}
savedObjectsClient={client}
/>
).instance();

bulkGetMock.mockResolvedValue({ savedObjects: [{ attributes: { title: 'test1' } }] });
compInstance.debouncedFetch('');
await new Promise((resolve) => setTimeout(resolve, 300));
await nextTick();
expect(bulkGetMock).toBeCalledWith([{ id: 'testDataSourceId', type: 'data-source' }]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,15 @@ export default class IndexPatternSelect extends Component<IndexPatternSelectProp
// order than they were sent out. Only load results for the most recent search.
if (searchValue === this.state.searchValue) {
const dataSourcesToFetch: Array<{ type: string; id: string }> = [];
const dataSourceIdSet = new Set();
savedObjects.map((indexPatternSavedObject: SimpleSavedObject<any>) => {
const dataSourceReference = getDataSourceReference(indexPatternSavedObject.references);
if (dataSourceReference && !this.state.dataSourceIdToTitle.has(dataSourceReference.id)) {
if (
dataSourceReference &&
!this.state.dataSourceIdToTitle.has(dataSourceReference.id) &&
!dataSourceIdSet.has(dataSourceReference.id)
) {
dataSourceIdSet.add(dataSourceReference.id);
dataSourcesToFetch.push({ type: 'data-source', id: dataSourceReference.id });
}
});
Expand Down

0 comments on commit 5b048ba

Please sign in to comment.