-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* add multi data source support * add unit tests for server side * Use dataSource to check if data source enabled * Add comments for DO_NOT_FETCH --------- Signed-off-by: Lin Wang <[email protected]>
- Loading branch information
Showing
50 changed files
with
2,077 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
public/components/__tests__/data_source_top_nav_menu.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React, { useContext } from 'react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { render, screen, waitFor } from '../../../test/test_utils'; | ||
import { DataSourceTopNavMenu, DataSourceTopNavMenuProps } from '../data_source_top_nav_menu'; | ||
import { coreMock } from '../../../../../src/core/public/mocks'; | ||
import { DataSourceContext } from '../../contexts'; | ||
|
||
function setup(options: Partial<DataSourceTopNavMenuProps> = {}) { | ||
const user = userEvent.setup({}); | ||
const coreStart = coreMock.createStart(); | ||
const DataSourceMenu = ({ componentConfig: { onSelectedDataSources } }) => ( | ||
<div> | ||
<div>Data Source Menu</div> | ||
<div> | ||
<button | ||
onClick={() => { | ||
onSelectedDataSources([]); | ||
}} | ||
aria-label="invalidDataSource" | ||
> | ||
Invalid data source | ||
</button> | ||
<button | ||
onClick={() => { | ||
onSelectedDataSources([{ id: 'ds1', label: 'Data Source 1' }]); | ||
}} | ||
aria-label="validDataSource" | ||
> | ||
Valid data source | ||
</button> | ||
</div> | ||
</div> | ||
); | ||
|
||
const DataSourceConsumer = () => { | ||
const { selectedDataSourceOption } = useContext(DataSourceContext); | ||
|
||
return ( | ||
<div> | ||
<input | ||
value={ | ||
selectedDataSourceOption === undefined | ||
? 'undefined' | ||
: JSON.stringify(selectedDataSourceOption) | ||
} | ||
aria-label="selectedDataSourceOption" | ||
onChange={() => {}} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
const renderResult = render( | ||
<> | ||
<DataSourceTopNavMenu | ||
notifications={coreStart.notifications} | ||
savedObjects={coreStart.savedObjects} | ||
dataSourceManagement={{ | ||
registerAuthenticationMethod: jest.fn(), | ||
ui: { | ||
DataSourceSelector: () => null, | ||
getDataSourceMenu: () => DataSourceMenu, | ||
}, | ||
}} | ||
setActionMenu={jest.fn()} | ||
{...options} | ||
/> | ||
<DataSourceConsumer /> | ||
</> | ||
); | ||
return { user, renderResult }; | ||
} | ||
|
||
describe('<DataSourceTopNavMenu />', () => { | ||
it('should not render data source menu when data source management not defined', () => { | ||
setup({ | ||
dataSourceManagement: undefined, | ||
}); | ||
expect(screen.queryByText('Data Source Menu')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('should render data source menu and data source context', () => { | ||
setup(); | ||
expect(screen.getByText('Data Source Menu')).toBeInTheDocument(); | ||
expect(screen.getByLabelText('selectedDataSourceOption')).toHaveValue('null'); | ||
}); | ||
|
||
it('should set selected data source option to undefined', async () => { | ||
const { user } = setup(); | ||
expect(screen.getByText('Data Source Menu')).toBeInTheDocument(); | ||
await user.click(screen.getByLabelText('invalidDataSource')); | ||
await waitFor(() => { | ||
expect(screen.getByLabelText('selectedDataSourceOption')).toHaveValue('undefined'); | ||
}); | ||
}); | ||
|
||
it('should set selected data source option to valid data source', async () => { | ||
const { user } = setup(); | ||
expect(screen.getByText('Data Source Menu')).toBeInTheDocument(); | ||
await user.click(screen.getByLabelText('validDataSource')); | ||
await waitFor(() => { | ||
expect(screen.getByLabelText('selectedDataSourceOption')).toHaveValue( | ||
JSON.stringify({ id: 'ds1', label: 'Data Source 1' }) | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React, { useMemo, useContext, useCallback } from 'react'; | ||
|
||
import type { CoreStart, MountPoint, SavedObjectsStart } from '../../../../src/core/public'; | ||
import type { | ||
DataSourceManagementPluginSetup, | ||
DataSourceSelectableConfig, | ||
} from '../../../../src/plugins/data_source_management/public'; | ||
import { DataSourceContext } from '../contexts/data_source_context'; | ||
|
||
export interface DataSourceTopNavMenuProps { | ||
notifications: CoreStart['notifications']; | ||
savedObjects: SavedObjectsStart; | ||
dataSourceManagement?: DataSourceManagementPluginSetup; | ||
setActionMenu: (menuMount: MountPoint | undefined) => void; | ||
} | ||
|
||
export const DataSourceTopNavMenu = ({ | ||
savedObjects, | ||
notifications, | ||
setActionMenu, | ||
dataSourceManagement, | ||
}: DataSourceTopNavMenuProps) => { | ||
const DataSourceMenu = useMemo(() => dataSourceManagement?.ui.getDataSourceMenu(), [ | ||
dataSourceManagement, | ||
]); | ||
const { selectedDataSourceOption, setSelectedDataSourceOption } = useContext(DataSourceContext); | ||
const activeOption = useMemo(() => (selectedDataSourceOption ? [selectedDataSourceOption] : []), [ | ||
selectedDataSourceOption, | ||
]); | ||
|
||
const handleDataSourcesSelected = useCallback< | ||
DataSourceSelectableConfig['onSelectedDataSources'] | ||
>( | ||
(dataSourceOptions) => { | ||
setSelectedDataSourceOption(dataSourceOptions[0]); | ||
}, | ||
[setSelectedDataSourceOption] | ||
); | ||
|
||
if (!DataSourceMenu) { | ||
return null; | ||
} | ||
return ( | ||
<DataSourceMenu | ||
componentType="DataSourceSelectable" | ||
componentConfig={{ | ||
notifications, | ||
savedObjects: savedObjects.client, | ||
onSelectedDataSources: handleDataSourcesSelected, | ||
activeOption, | ||
}} | ||
setMenuMountPoint={setActionMenu} | ||
/> | ||
); | ||
}; |
File renamed without changes.
Oops, something went wrong.