Skip to content

Commit

Permalink
Use dataSource to check if data source enabled
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Wang <[email protected]>
  • Loading branch information
wanglam committed Apr 18, 2024
1 parent a486b62 commit 057a0f5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 60 deletions.
42 changes: 2 additions & 40 deletions public/components/__tests__/data_source_top_nav_menu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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, DataSourceContextProvider } from '../../contexts';
import { DataSourceContext } from '../../contexts';

function setup(options: Partial<DataSourceTopNavMenuProps> = {}) {
const user = userEvent.setup({});
Expand Down Expand Up @@ -38,15 +38,10 @@ function setup(options: Partial<DataSourceTopNavMenuProps> = {}) {
);

const DataSourceConsumer = () => {
const { dataSourceEnabled, selectedDataSourceOption } = useContext(DataSourceContext);
const { selectedDataSourceOption } = useContext(DataSourceContext);

return (
<div>
<input
value={JSON.stringify(dataSourceEnabled)}
aria-label="dataSourceEnabled"
onChange={() => {}}
/>
<input
value={
selectedDataSourceOption === undefined
Expand All @@ -65,13 +60,6 @@ function setup(options: Partial<DataSourceTopNavMenuProps> = {}) {
<DataSourceTopNavMenu
notifications={coreStart.notifications}
savedObjects={coreStart.savedObjects}
dataSource={{
dataSourceEnabled: true,
noAuthenticationTypeEnabled: true,
awsSigV4AuthEnabled: true,
hideLocalCluster: false,
usernamePasswordAuthEnabled: true,
}}
dataSourceManagement={{
registerAuthenticationMethod: jest.fn(),
ui: {
Expand All @@ -89,42 +77,16 @@ function setup(options: Partial<DataSourceTopNavMenuProps> = {}) {
}

describe('<DataSourceTopNavMenu />', () => {
it('should not render data source menu when data source and data source management not defined', () => {
setup({
dataSource: undefined,
dataSourceManagement: undefined,
});
expect(screen.queryByText('Data Source Menu')).not.toBeInTheDocument();
});
it('should not render data source menu when data source not defined', () => {
setup({
dataSource: undefined,
});
expect(screen.queryByText('Data Source Menu')).not.toBeInTheDocument();
});
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 not render data source menu when data source not enabled', () => {
setup({
dataSource: {
dataSourceEnabled: false,
noAuthenticationTypeEnabled: false,
awsSigV4AuthEnabled: false,
hideLocalCluster: false,
usernamePasswordAuthEnabled: false,
},
});
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('dataSourceEnabled')).toHaveValue('true');
expect(screen.getByLabelText('selectedDataSourceOption')).toHaveValue('null');
});

Expand Down
3 changes: 1 addition & 2 deletions public/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const MlCommonsPluginApp = ({
savedObjects,
setActionMenu,
}: MlCommonsPluginAppDeps) => {
const dataSourceEnabled = !!dataSource?.dataSourceEnabled;
const dataSourceEnabled = !!dataSource;
return (
<I18nProvider>
<DataSourceContextProvider
Expand Down Expand Up @@ -86,7 +86,6 @@ export const MlCommonsPluginApp = ({
{dataSourceEnabled && (
<DataSourceTopNavMenu
notifications={notifications}
dataSource={dataSource}
dataSourceManagement={dataSourceManagement}
setActionMenu={setActionMenu}
savedObjects={savedObjects}
Expand Down
23 changes: 5 additions & 18 deletions public/components/data_source_top_nav_menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,32 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { useMemo, useContext, useEffect, useCallback } from 'react';
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 type { DataSourcePluginSetup } from '../../../../src/plugins/data_source/public';
import { DataSourceContext } from '../contexts/data_source_context';

export interface DataSourceTopNavMenuProps {
notifications: CoreStart['notifications'];
savedObjects: SavedObjectsStart;
dataSource?: DataSourcePluginSetup;
dataSourceManagement?: DataSourceManagementPluginSetup;
setActionMenu: (menuMount: MountPoint | undefined) => void;
}

export const DataSourceTopNavMenu = ({
dataSource,
savedObjects,
notifications,
setActionMenu,
dataSourceManagement,
}: DataSourceTopNavMenuProps) => {
const dataSourceEnabled = !!dataSource?.dataSourceEnabled;
const DataSourceMenu = useMemo(
() => (dataSourceEnabled ? dataSourceManagement?.ui.getDataSourceMenu() : null),
[dataSourceEnabled, dataSourceManagement]
);
const {
selectedDataSourceOption,
setSelectedDataSourceOption,
setDataSourceEnabled,
} = useContext(DataSourceContext);
const DataSourceMenu = useMemo(() => dataSourceManagement?.ui.getDataSourceMenu(), [
dataSourceManagement,
]);
const { selectedDataSourceOption, setSelectedDataSourceOption } = useContext(DataSourceContext);
const activeOption = useMemo(() => (selectedDataSourceOption ? [selectedDataSourceOption] : []), [
selectedDataSourceOption,
]);
Expand All @@ -51,10 +42,6 @@ export const DataSourceTopNavMenu = ({
[setSelectedDataSourceOption]
);

useEffect(() => {
setDataSourceEnabled(dataSourceEnabled);
}, [dataSourceEnabled, setDataSourceEnabled]);

if (!DataSourceMenu) {
return null;
}
Expand Down

0 comments on commit 057a0f5

Please sign in to comment.