diff --git a/src/plugins/data_source_management/public/components/data_source_menu/create_data_source_menu.test.tsx b/src/plugins/data_source_management/public/components/data_source_menu/create_data_source_menu.test.tsx index df299687928b..a490431d90c4 100644 --- a/src/plugins/data_source_management/public/components/data_source_menu/create_data_source_menu.test.tsx +++ b/src/plugins/data_source_management/public/components/data_source_menu/create_data_source_menu.test.tsx @@ -14,7 +14,6 @@ import React from 'react'; import { act, render } from '@testing-library/react'; import { DataSourceComponentType, DataSourceSelectableConfig } from './types'; import { ReactWrapper } from 'enzyme'; -import { mockDataSourcePluginSetupWithShowLocalCluster } from '../../mocks'; import * as utils from '../utils'; describe('create data source menu', () => { @@ -44,10 +43,11 @@ describe('create data source menu', () => { notifications, }, }; - const TestComponent = createDataSourceMenu( - uiSettings, - mockDataSourcePluginSetupWithShowLocalCluster - ); + + spyOn(utils, 'getApplication').and.returnValue({ id: 'test2' }); + spyOn(utils, 'getUiSettings').and.returnValue(uiSettings); + spyOn(utils, 'getHideLocalCluster').and.returnValue({ enabled: true }); + const TestComponent = createDataSourceMenu(); const component = render(); expect(component).toMatchSnapshot(); @@ -71,10 +71,10 @@ describe('create data source menu', () => { notifications, }, }; - const TestComponent = createDataSourceMenu( - uiSettings, - mockDataSourcePluginSetupWithShowLocalCluster - ); + spyOn(utils, 'getApplication').and.returnValue({ id: 'test2' }); + spyOn(utils, 'getUiSettings').and.returnValue(uiSettings); + spyOn(utils, 'getHideLocalCluster').and.returnValue({ enabled: true }); + const TestComponent = createDataSourceMenu(); await act(async () => { component = render(); }); @@ -137,10 +137,12 @@ describe('when setMenuMountPoint is provided', () => { notifications, }, }; - const TestComponent = createDataSourceMenu( - uiSettings, - mockDataSourcePluginSetupWithShowLocalCluster - ); + + spyOn(utils, 'getApplication').and.returnValue({ id: 'test2' }); + spyOn(utils, 'getUiSettings').and.returnValue(uiSettings); + spyOn(utils, 'getHideLocalCluster').and.returnValue({ enabled: true }); + + const TestComponent = createDataSourceMenu(); const component = render(); act(() => { mountPoint(portalTarget); diff --git a/src/plugins/data_source_management/public/components/data_source_menu/create_data_source_menu.tsx b/src/plugins/data_source_management/public/components/data_source_menu/create_data_source_menu.tsx index 51fcb0db0857..a628a1bf2a15 100644 --- a/src/plugins/data_source_management/public/components/data_source_menu/create_data_source_menu.tsx +++ b/src/plugins/data_source_management/public/components/data_source_menu/create_data_source_menu.tsx @@ -5,20 +5,16 @@ import React from 'react'; import { EuiHeaderLinks } from '@elastic/eui'; -import { IUiSettingsClient } from 'src/core/public'; -import { DataSourcePluginSetup } from 'src/plugins/data_source/public'; import { DataSourceMenu } from './data_source_menu'; import { DataSourceMenuProps } from './types'; import { MountPointPortal } from '../../../../opensearch_dashboards_react/public'; -import { getApplication } from '../utils'; +import { getApplication, getHideLocalCluster, getUiSettings } from '../utils'; -export function createDataSourceMenu( - uiSettings: IUiSettingsClient, - dataSourcePluginSetup: DataSourcePluginSetup -) { +export function createDataSourceMenu() { const application = getApplication(); + const uiSettings = getUiSettings(); + const hideLocalCluster = getHideLocalCluster().enabled; return (props: DataSourceMenuProps) => { - const { hideLocalCluster } = dataSourcePluginSetup; if (props.setMenuMountPoint) { return ( diff --git a/src/plugins/data_source_management/public/components/data_source_menu/index.ts b/src/plugins/data_source_management/public/components/data_source_menu/index.ts index 216cf33c6ee3..92bf9c8cae37 100644 --- a/src/plugins/data_source_management/public/components/data_source_menu/index.ts +++ b/src/plugins/data_source_management/public/components/data_source_menu/index.ts @@ -4,6 +4,7 @@ */ export { DataSourceMenu } from './data_source_menu'; +export { createDataSourceMenu } from './create_data_source_menu'; export { DataSourceSelectableConfig, DataSourceAggregatedViewConfig, diff --git a/src/plugins/data_source_management/public/components/utils.ts b/src/plugins/data_source_management/public/components/utils.ts index a9f428f5cfa7..9fbed5fb405f 100644 --- a/src/plugins/data_source_management/public/components/utils.ts +++ b/src/plugins/data_source_management/public/components/utils.ts @@ -10,6 +10,7 @@ import { IUiSettingsClient, ToastsStart, ApplicationStart, + CoreStart, } from 'src/core/public'; import { deepFreeze } from '@osd/std'; import { @@ -303,3 +304,14 @@ export const dataSourceOptionGroupLabel = deepFreeze('Application'); +export const [getUiSettings, setUiSettings] = createGetterSetter( + 'UiSettings' +); + +export interface HideLocalCluster { + enabled: boolean; +} + +export const [getHideLocalCluster, setHideLocalCluster] = createGetterSetter( + 'HideLocalCluster' +); diff --git a/src/plugins/data_source_management/public/index.ts b/src/plugins/data_source_management/public/index.ts index da7fd8992459..0cc7370d2cdc 100644 --- a/src/plugins/data_source_management/public/index.ts +++ b/src/plugins/data_source_management/public/index.ts @@ -22,4 +22,5 @@ export { DataSourceViewConfig, DataSourceMenuProps, DataSourceMultiSelectableConfig, + createDataSourceMenu, } from './components/data_source_menu'; diff --git a/src/plugins/data_source_management/public/plugin.ts b/src/plugins/data_source_management/public/plugin.ts index 2461044a680b..63b171e5e493 100644 --- a/src/plugins/data_source_management/public/plugin.ts +++ b/src/plugins/data_source_management/public/plugin.ts @@ -21,7 +21,7 @@ import { noAuthCredentialAuthMethod, sigV4AuthMethod, usernamePasswordAuthMethod import { DataSourceSelectorProps } from './components/data_source_selector/data_source_selector'; import { createDataSourceMenu } from './components/data_source_menu/create_data_source_menu'; import { DataSourceMenuProps } from './components/data_source_menu'; -import { setApplication } from './components/utils'; +import { setApplication, setHideLocalCluster, setUiSettings } from './components/utils'; export interface DataSourceManagementSetupDependencies { management: ManagementSetup; @@ -101,11 +101,14 @@ export class DataSourceManagementPlugin registerAuthenticationMethod(sigV4AuthMethod); } + setHideLocalCluster({ enabled: dataSource.hideLocalCluster }); + setUiSettings(uiSettings); + return { registerAuthenticationMethod, ui: { DataSourceSelector: createDataSourceSelector(uiSettings, dataSource), - getDataSourceMenu: () => createDataSourceMenu(uiSettings, dataSource), + getDataSourceMenu: () => createDataSourceMenu(), }, }; } diff --git a/src/plugins/navigation/public/top_nav_menu/__snapshots__/top_nav_menu.test.tsx.snap b/src/plugins/navigation/public/top_nav_menu/__snapshots__/top_nav_menu.test.tsx.snap new file mode 100644 index 000000000000..7a7063db0601 --- /dev/null +++ b/src/plugins/navigation/public/top_nav_menu/__snapshots__/top_nav_menu.test.tsx.snap @@ -0,0 +1,77 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`TopNavMenu mounts the data source menu as well as top nav menu 1`] = ` + + + + + + + + +`; + +exports[`TopNavMenu mounts the data source menu if showDataSourceMenu is true 1`] = ` + + + + + +`; diff --git a/src/plugins/navigation/public/top_nav_menu/top_nav_menu.test.tsx b/src/plugins/navigation/public/top_nav_menu/top_nav_menu.test.tsx index 7b4269da9636..a63aaf4d60d2 100644 --- a/src/plugins/navigation/public/top_nav_menu/top_nav_menu.test.tsx +++ b/src/plugins/navigation/public/top_nav_menu/top_nav_menu.test.tsx @@ -35,6 +35,7 @@ import { MountPoint } from 'opensearch-dashboards/public'; 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'; const dataShim = { ui: { @@ -118,6 +119,9 @@ describe('TopNavMenu', () => { }); it('mounts the data source menu if showDataSourceMenu is true', async () => { + spyOn(testUtils, 'getApplication').and.returnValue({ id: 'test2' }); + spyOn(testUtils, 'getUiSettings').and.returnValue({ id: 'test2' }); + spyOn(testUtils, 'getHideLocalCluster').and.returnValue(true); const component = shallowWithIntl( { /> ); - expect(component.find('DataSourceMenu').length).toBe(1); + expect(component).toMatchSnapshot(); }); it('mounts the data source menu as well as top nav menu', async () => { + spyOn(testUtils, 'getApplication').and.returnValue({ id: 'test2' }); + spyOn(testUtils, 'getUiSettings').and.returnValue({ id: 'test2' }); + spyOn(testUtils, 'getHideLocalCluster').and.returnValue(true); + const component = shallowWithIntl( { /> ); - expect(component.find('DataSourceMenu').length).toBe(1); + expect(component).toMatchSnapshot(); expect(component.find(TOP_NAV_ITEM_SELECTOR).length).toBe(menuItems.length); }); diff --git a/src/plugins/navigation/public/top_nav_menu/top_nav_menu.tsx b/src/plugins/navigation/public/top_nav_menu/top_nav_menu.tsx index 6814d254510f..c7aaa2763695 100644 --- a/src/plugins/navigation/public/top_nav_menu/top_nav_menu.tsx +++ b/src/plugins/navigation/public/top_nav_menu/top_nav_menu.tsx @@ -41,7 +41,7 @@ import { } from '../../../data/public'; import { TopNavMenuData } from './top_nav_menu_data'; import { TopNavMenuItem } from './top_nav_menu_item'; -import { DataSourceMenu, DataSourceMenuProps } from '../../../data_source_management/public'; +import { DataSourceMenuProps, createDataSourceMenu } from '../../../data_source_management/public'; export type TopNavMenuProps = StatefulSearchBarProps & Omit & { @@ -115,11 +115,17 @@ export function TopNavMenu(props: TopNavMenuProps): ReactElement | null { return ( {renderItems()} - {showDataSourceMenu && } + {renderDataSourceMenu()} ); } + function renderDataSourceMenu(): ReactElement | null { + if (!showDataSourceMenu) return null; + const DataSourceMenu = createDataSourceMenu(); + return ; + } + function renderSearchBar(): ReactElement | null { // Validate presence of all required fields if (!showSearchBar || !props.data) return null;