-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
244 additions
and
47 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
...lugins/discover/public/application/components/__snapshots__/discover_topnav.test.tsx.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
71 changes: 71 additions & 0 deletions
71
src/plugins/discover/public/application/components/discover_topnav.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,71 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { shallowWithIntl } from '@kbn/test/jest'; | ||
import { inspectorPluginMock } from '../../../../inspector/public/mocks'; | ||
import { indexPatternMock } from '../../__mocks__/index_pattern'; | ||
import { getTopNavLinks } from './top_nav/get_top_nav_links'; | ||
import { DiscoverServices } from '../../build_services'; | ||
import { GetStateReturn } from '../angular/discover_state'; | ||
import { savedSearchMock } from '../../__mocks__/saved_search'; | ||
import { dataPluginMock } from '../../../../data/public/mocks'; | ||
import { createFilterManagerMock } from '../../../../data/public/query/filter_manager/filter_manager.mock'; | ||
import { uiSettingsMock as mockUiSettings } from '../../__mocks__/ui_settings'; | ||
import { IndexPatternAttributes } from '../../../../data/common/index_patterns'; | ||
import { SavedObject } from '../../../../../core/types'; | ||
import { navigationPluginMock } from '../../../../navigation/public/mocks'; | ||
import { DiscoverTopNav } from './discover_topnav'; | ||
|
||
function getProps() { | ||
const state = ({} as unknown) as GetStateReturn; | ||
const services = ({ | ||
navigation: navigationPluginMock.createStartContract(), | ||
capabilities: { | ||
discover: { | ||
save: true, | ||
}, | ||
}, | ||
uiSettings: mockUiSettings, | ||
} as unknown) as DiscoverServices; | ||
const indexPattern = indexPatternMock; | ||
return { | ||
indexPattern: indexPatternMock, | ||
opts: { | ||
config: mockUiSettings, | ||
data: dataPluginMock.createStartContract(), | ||
filterManager: createFilterManagerMock(), | ||
indexPatternList: (indexPattern as unknown) as Array<SavedObject<IndexPatternAttributes>>, | ||
sampleSize: 10, | ||
savedSearch: savedSearchMock, | ||
setHeaderActionMenu: jest.fn(), | ||
timefield: indexPattern.timeFieldName || '', | ||
setAppState: jest.fn(), | ||
services, | ||
stateContainer: {} as GetStateReturn, | ||
}, | ||
topNavMenu: getTopNavLinks({ | ||
getFieldCounts: jest.fn(), | ||
indexPattern, | ||
inspectorAdapters: inspectorPluginMock, | ||
navigateTo: jest.fn(), | ||
savedSearch: savedSearchMock, | ||
services, | ||
state, | ||
}), | ||
state, | ||
updateQuery: jest.fn(), | ||
}; | ||
} | ||
|
||
describe('Discover topnav component', () => { | ||
test('renders correctly', () => { | ||
const component = shallowWithIntl(<DiscoverTopNav {...getProps()} />); | ||
expect(component).toMatchSnapshot(); | ||
}); | ||
}); |
51 changes: 51 additions & 0 deletions
51
src/plugins/discover/public/application/components/discover_topnav.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,51 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
import React, { useMemo } from 'react'; | ||
import { DiscoverProps } from './types'; | ||
|
||
export const DiscoverTopNav = ({ | ||
topNavMenu, | ||
indexPattern, | ||
updateQuery, | ||
state, | ||
opts, | ||
}: Pick<DiscoverProps, 'topNavMenu' | 'indexPattern' | 'updateQuery' | 'state' | 'opts'>) => { | ||
const showDatePicker = useMemo(() => indexPattern.isTimeBased(), [indexPattern]); | ||
const { TopNavMenu } = opts.services.navigation.ui; | ||
|
||
const updateSavedQueryId = (newSavedQueryId: string | undefined) => { | ||
const { appStateContainer, setAppState } = opts.stateContainer; | ||
if (newSavedQueryId) { | ||
setAppState({ savedQuery: newSavedQueryId }); | ||
} else { | ||
// remove savedQueryId from state | ||
const newState = { | ||
...appStateContainer.getState(), | ||
}; | ||
delete newState.savedQuery; | ||
appStateContainer.set(newState); | ||
} | ||
}; | ||
return ( | ||
<TopNavMenu | ||
appName="discover" | ||
config={topNavMenu} | ||
indexPatterns={[indexPattern]} | ||
onQuerySubmit={updateQuery} | ||
onSavedQueryIdChange={updateSavedQueryId} | ||
query={state.query} | ||
setMenuMountPoint={opts.setHeaderActionMenu} | ||
savedQueryId={state.savedQuery} | ||
screenTitle={opts.savedSearch.title} | ||
showDatePicker={showDatePicker} | ||
showSaveQuery={!!opts.services.capabilities.discover.saveQuery} | ||
showSearchBar={true} | ||
useDefaultBehaviors={true} | ||
/> | ||
); | ||
}; |