diff --git a/src/legacy/core_plugins/data/public/query/query_bar/components/__snapshots__/query_bar_input.test.tsx.snap b/src/legacy/core_plugins/data/public/query/query_bar/components/__snapshots__/query_bar_input.test.tsx.snap index b13916a3e5b292..ea93d2c096cb55 100644 --- a/src/legacy/core_plugins/data/public/query/query_bar/components/__snapshots__/query_bar_input.test.tsx.snap +++ b/src/legacy/core_plugins/data/public/query/query_bar/components/__snapshots__/query_bar_input.test.tsx.snap @@ -133,6 +133,17 @@ exports[`QueryBarInput Should disable autoFocus on EuiFieldText when disableAuto "query": "response:200", } } + savedObjectsClient={ + Object { + "bulkCreate": [MockFunction], + "bulkGet": [MockFunction], + "create": [MockFunction], + "delete": [MockFunction], + "find": [MockFunction], + "get": [MockFunction], + "update": [MockFunction], + } + } screenTitle="Another Screen" store={ Object { @@ -511,6 +522,17 @@ exports[`QueryBarInput Should pass the query language to the language switcher 1 "query": "response:200", } } + savedObjectsClient={ + Object { + "bulkCreate": [MockFunction], + "bulkGet": [MockFunction], + "create": [MockFunction], + "delete": [MockFunction], + "find": [MockFunction], + "get": [MockFunction], + "update": [MockFunction], + } + } screenTitle="Another Screen" store={ Object { @@ -889,6 +911,17 @@ exports[`QueryBarInput Should render the given query 1`] = ` "query": "response:200", } } + savedObjectsClient={ + Object { + "bulkCreate": [MockFunction], + "bulkGet": [MockFunction], + "create": [MockFunction], + "delete": [MockFunction], + "find": [MockFunction], + "get": [MockFunction], + "update": [MockFunction], + } + } screenTitle="Another Screen" store={ Object { diff --git a/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar.test.tsx b/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar.test.tsx index 9de311f4a15a5d..18733737c6c547 100644 --- a/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar.test.tsx +++ b/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar.test.tsx @@ -26,9 +26,9 @@ import { QueryBar } from './query_bar'; import { IndexPattern } from '../../../index'; import { coreMock } from '../../../../../../../core/public/mocks'; -const setupMock = coreMock.createSetup(); +const startMock = coreMock.createStart(); -setupMock.uiSettings.get.mockImplementation((key: string) => { +startMock.uiSettings.get.mockImplementation((key: string) => { switch (key) { case 'timepicker:quickRanges': return [ @@ -98,7 +98,8 @@ describe('QueryBar', () => { it('Should render the given query', () => { const component = shallowWithIntl( { it('Should create a unique PersistedLog based on the appName and query language', () => { shallowWithIntl( { it('Should render only timepicker when no options provided', () => { const component = shallowWithIntl( { it('Should not show timepicker when asked', () => { const component = shallowWithIntl( { it('Should render timepicker with options', () => { const component = shallowWithIntl( { it('Should render only query input bar', () => { const component = shallowWithIntl( { it('Should NOT render query input bar if disabled', () => { const component = shallowWithIntl( { it('Should NOT render query input bar if missing options', () => { const component = shallowWithIntl( { onSubmit={this.onInputSubmit} persistedLog={this.persistedLog} uiSettings={this.props.uiSettings} + savedObjectsClient={this.props.savedObjectsClient} /> ); diff --git a/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_input.test.tsx b/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_input.test.tsx index d04c6032980ff0..fa9bad78625c0b 100644 --- a/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_input.test.tsx +++ b/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_input.test.tsx @@ -29,7 +29,7 @@ import { mountWithIntl } from 'test_utils/enzyme_helpers'; import { QueryLanguageSwitcher } from './language_switcher'; import { QueryBarInput, QueryBarInputUI } from './query_bar_input'; import { coreMock } from '../../../../../../../core/public/mocks'; -const setupMock = coreMock.createSetup(); +const startMock = coreMock.createStart(); import { IndexPattern } from '../../../index'; const noop = () => { @@ -93,7 +93,8 @@ describe('QueryBarInput', () => { indexPatterns={[mockIndexPattern]} store={createMockStorage()} intl={null as any} - uiSettings={setupMock.uiSettings} + uiSettings={startMock.uiSettings} + savedObjectsClient={startMock.savedObjects.client} /> ); @@ -110,7 +111,8 @@ describe('QueryBarInput', () => { indexPatterns={[mockIndexPattern]} store={createMockStorage()} intl={null as any} - uiSettings={setupMock.uiSettings} + uiSettings={startMock.uiSettings} + savedObjectsClient={startMock.savedObjects.client} /> ); @@ -128,7 +130,8 @@ describe('QueryBarInput', () => { store={createMockStorage()} disableAutoFocus={true} intl={null as any} - uiSettings={setupMock.uiSettings} + uiSettings={startMock.uiSettings} + savedObjectsClient={startMock.savedObjects.client} /> ); @@ -148,7 +151,8 @@ describe('QueryBarInput', () => { store={createMockStorage()} disableAutoFocus={true} intl={null as any} - uiSettings={setupMock.uiSettings} + uiSettings={startMock.uiSettings} + savedObjectsClient={startMock.savedObjects.client} /> ); @@ -169,7 +173,8 @@ describe('QueryBarInput', () => { store={mockStorage} disableAutoFocus={true} intl={null as any} - uiSettings={setupMock.uiSettings} + uiSettings={startMock.uiSettings} + savedObjectsClient={startMock.savedObjects.client} /> ); @@ -194,7 +199,8 @@ describe('QueryBarInput', () => { store={createMockStorage()} disableAutoFocus={true} intl={null as any} - uiSettings={setupMock.uiSettings} + uiSettings={startMock.uiSettings} + savedObjectsClient={startMock.savedObjects.client} /> ); @@ -218,7 +224,8 @@ describe('QueryBarInput', () => { store={createMockStorage()} disableAutoFocus={true} intl={null as any} - uiSettings={setupMock.uiSettings} + uiSettings={startMock.uiSettings} + savedObjectsClient={startMock.savedObjects.client} /> ); @@ -247,9 +254,14 @@ describe('QueryBarInput', () => { store={createMockStorage()} disableAutoFocus={true} intl={null as any} - uiSettings={setupMock.uiSettings} + uiSettings={startMock.uiSettings} + savedObjectsClient={startMock.savedObjects.client} /> ); - expect(mockFetchIndexPatterns).toHaveBeenCalledWith(['logstash-*'], setupMock.uiSettings); + expect(mockFetchIndexPatterns).toHaveBeenCalledWith( + startMock.savedObjects.client, + ['logstash-*'], + startMock.uiSettings + ); }); }); diff --git a/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_input.tsx b/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_input.tsx index a8538bf7cb8acc..d9f66467b09bd8 100644 --- a/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_input.tsx +++ b/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_input.tsx @@ -32,7 +32,7 @@ import { debounce, compact, isEqual, omit } from 'lodash'; import { PersistedLog } from 'ui/persisted_log'; import { kfetch } from 'ui/kfetch'; import { Storage } from 'ui/storage'; -import { UiSettingsClientContract } from 'src/core/public'; +import { UiSettingsClientContract, SavedObjectsClientContract } from 'src/core/public'; import { IndexPattern, StaticIndexPattern } from '../../../index_patterns'; import { Query } from '../index'; import { fromUser, matchPairs, toUser } from '../lib'; @@ -44,6 +44,7 @@ import { fetchIndexPatterns } from '../lib/fetch_index_patterns'; interface Props { uiSettings: UiSettingsClientContract; indexPatterns: Array; + savedObjectsClient: SavedObjectsClientContract; store: Storage; intl: InjectedIntl; query: Query; @@ -111,6 +112,7 @@ export class QueryBarInputUI extends Component { ) as IndexPattern[]; const objectPatternsFromStrings = (await fetchIndexPatterns( + this.props.savedObjectsClient, stringPatterns, this.props.uiSettings )) as IndexPattern[]; @@ -451,6 +453,7 @@ export class QueryBarInputUI extends Component { 'onChange', 'onSubmit', 'uiSettings', + 'savedObjectsClient', ]); return ( diff --git a/src/legacy/core_plugins/data/public/query/query_bar/lib/fetch_index_patterns.ts b/src/legacy/core_plugins/data/public/query/query_bar/lib/fetch_index_patterns.ts index cc7057d06c30ce..4cf17dc9be37ef 100644 --- a/src/legacy/core_plugins/data/public/query/query_bar/lib/fetch_index_patterns.ts +++ b/src/legacy/core_plugins/data/public/query/query_bar/lib/fetch_index_patterns.ts @@ -16,14 +16,12 @@ * specific language governing permissions and limitations * under the License. */ - -import chrome from 'ui/chrome'; import { isEmpty } from 'lodash'; - -import { UiSettingsClientContract } from 'src/core/public'; +import { UiSettingsClientContract, SavedObjectsClientContract } from 'src/core/public'; import { getFromSavedObject } from '../../../index_patterns'; export async function fetchIndexPatterns( + savedObjectsClient: SavedObjectsClientContract, indexPatternStrings: string[], uiSettings: UiSettingsClientContract ) { @@ -32,7 +30,7 @@ export async function fetchIndexPatterns( } const searchString = indexPatternStrings.map(string => `"${string}"`).join(' | '); - const indexPatternsFromSavedObjects = await chrome.getSavedObjectsClient().find({ + const indexPatternsFromSavedObjects = await savedObjectsClient.find({ type: 'index-pattern', fields: ['title', 'fields'], search: searchString, @@ -48,12 +46,7 @@ export async function fetchIndexPatterns( const allMatches = exactMatches.length === indexPatternStrings.length ? exactMatches - : [...exactMatches, await fetchDefaultIndexPattern(defaultIndex)]; + : [...exactMatches, await savedObjectsClient.get('index-pattern', defaultIndex)]; return allMatches.map(getFromSavedObject); } - -const fetchDefaultIndexPattern = async (defaultIndex: string) => { - const savedObjectsClient = chrome.getSavedObjectsClient(); - return await savedObjectsClient.get('index-pattern', defaultIndex); -}; diff --git a/src/legacy/core_plugins/data/public/search/search_bar/components/search_bar.test.tsx b/src/legacy/core_plugins/data/public/search/search_bar/components/search_bar.test.tsx index 7aa4767e21dba7..211837a2ba9773 100644 --- a/src/legacy/core_plugins/data/public/search/search_bar/components/search_bar.test.tsx +++ b/src/legacy/core_plugins/data/public/search/search_bar/components/search_bar.test.tsx @@ -23,7 +23,7 @@ import { SearchBar } from './search_bar'; import { IndexPattern } from 'ui/index_patterns'; import { coreMock } from '../../../../../../../../src/core/public/mocks'; -const setupMock = coreMock.createSetup(); +const startMock = coreMock.createStart(); jest.mock('../../../../../data/public', () => { return { @@ -73,14 +73,6 @@ const mockIndexPattern = { ], } as IndexPattern; -const mockSavedQueryService = { - saveQuery: jest.fn(), - getAllSavedQueries: jest.fn(), - findSavedQueries: jest.fn(), - getSavedQuery: jest.fn(), - deleteSavedQuery: jest.fn(), -}; - const kqlQuery = { query: 'response:200', language: 'kuery', @@ -98,8 +90,8 @@ describe('SearchBar', () => { it('Should render query bar when no options provided (in reality - timepicker)', () => { const component = mountWithIntl( { it('Should render empty when timepicker is off and no options provided', () => { const component = mountWithIntl( { it('Should render filter bar, when required fields are provided', () => { const component = mountWithIntl( { it('Should NOT render filter bar, if disabled', () => { const component = mountWithIntl( { it('Should render query bar, when required fields are provided', () => { const component = mountWithIntl( { it('Should NOT render query bar, if disabled', () => { const component = mountWithIntl( { it('Should render query bar and filter bar', () => { const component = mountWithIntl( { query: this.props.query ? { ...this.props.query } : undefined, dateRangeFrom: get(this.props, 'dateRangeFrom', 'now-15m'), dateRangeTo: get(this.props, 'dateRangeTo', 'now'), + savedQueryService: createSavedQueryService(this.props.savedObjectsClient), }; public isDirty = () => { @@ -245,11 +248,11 @@ class SearchBarUI extends Component { try { let response; if (this.props.savedQuery && !saveAsNew) { - response = await this.props.savedQueryService.saveQuery(savedQueryAttributes, { + response = await this.state.savedQueryService.saveQuery(savedQueryAttributes, { overwrite: true, }); } else { - response = await this.props.savedQueryService.saveQuery(savedQueryAttributes); + response = await this.state.savedQueryService.saveQuery(savedQueryAttributes); } toastNotifications.addSuccess(`Your query "${response.attributes.title}" was saved`); @@ -365,7 +368,7 @@ class SearchBarUI extends Component { onSave={this.onInitiateSave} onSaveAsNew={this.onInitiateSaveNew} onLoad={this.onLoadSavedQuery} - savedQueryService={this.props.savedQueryService} + savedQueryService={this.state.savedQueryService} onClearSavedQuery={this.props.onClearSavedQuery} > ); @@ -375,6 +378,7 @@ class SearchBarUI extends Component { queryBar = ( { {this.state.showSaveQueryModal ? ( this.setState({ showSaveQueryModal: false })} showFilterOption={this.props.showFilterBar} @@ -446,7 +450,7 @@ class SearchBarUI extends Component { ) : null} {this.state.showSaveNewQueryModal ? ( this.onSave(savedQueryMeta, true)} onClose={() => this.setState({ showSaveNewQueryModal: false })} showFilterOption={this.props.showFilterBar} diff --git a/src/legacy/core_plugins/kibana_react/public/top_nav_menu/top_nav_menu.test.tsx b/src/legacy/core_plugins/kibana_react/public/top_nav_menu/top_nav_menu.test.tsx index 050e55aed43c30..91c8cf55f05e7b 100644 --- a/src/legacy/core_plugins/kibana_react/public/top_nav_menu/top_nav_menu.test.tsx +++ b/src/legacy/core_plugins/kibana_react/public/top_nav_menu/top_nav_menu.test.tsx @@ -23,7 +23,6 @@ import { TopNavMenuData } from './top_nav_menu_data'; import { shallowWithIntl } from 'test_utils/enzyme_helpers'; import { coreMock } from '../../../../../core/public/mocks'; -const setupMock = coreMock.createSetup(); const startMock = coreMock.createStart(); jest.mock('../../../../core_plugins/data/public', () => { @@ -58,7 +57,7 @@ describe('TopNavMenu', () => { const component = shallowWithIntl( ); @@ -70,7 +69,7 @@ describe('TopNavMenu', () => { const component = shallowWithIntl( @@ -83,7 +82,7 @@ describe('TopNavMenu', () => { const component = shallowWithIntl( @@ -96,7 +95,7 @@ describe('TopNavMenu', () => { const component = shallowWithIntl( diff --git a/src/legacy/core_plugins/kibana_react/public/top_nav_menu/top_nav_menu.tsx b/src/legacy/core_plugins/kibana_react/public/top_nav_menu/top_nav_menu.tsx index 5c11e99265f10b..0ca18d5ebacc30 100644 --- a/src/legacy/core_plugins/kibana_react/public/top_nav_menu/top_nav_menu.tsx +++ b/src/legacy/core_plugins/kibana_react/public/top_nav_menu/top_nav_menu.tsx @@ -25,7 +25,6 @@ import { UiSettingsClientContract, SavedObjectsClientContract } from 'src/core/p import { TopNavMenuData } from './top_nav_menu_data'; import { TopNavMenuItem } from './top_nav_menu_item'; import { SearchBar, SearchBarProps } from '../../../../core_plugins/data/public'; -import { createSavedQueryService } from '../../../data/public/search/search_bar/lib/saved_query_service'; type Props = Partial & { name: string; @@ -58,16 +57,13 @@ export function TopNavMenu(props: Props) { function renderSearchBar() { // Validate presense of all required fields - if (!props.showSearchBar) return; - - const savedQueryService = createSavedQueryService(props.savedObjectsClient); - + if (!props.showSearchBar || !props.savedObjectsClient) return; return ( {showCustomLabel ? (