Skip to content

Commit

Permalink
Move IndexPatternsSelector ⇒ NP (#51620)
Browse files Browse the repository at this point in the history
* Move IndexPatternsSelector to NP
Replace import from ui/index_patterns with new platform imports

* karma mock

* added mock

* Fix jest tests
  • Loading branch information
Liza Katz authored Nov 26, 2019
1 parent 07bc690 commit 75d261d
Show file tree
Hide file tree
Showing 23 changed files with 141 additions and 39 deletions.
1 change: 0 additions & 1 deletion src/legacy/core_plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export {
CONTAINS_SPACES,
getFromSavedObject,
getRoutes,
IndexPatternSelect,
validateIndexPattern,
ILLEGAL_CHARACTERS,
INDEX_PATTERN_ILLEGAL_CHARACTERS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const createSetupContractMock = () => {
flattenHitWrapper: jest.fn().mockImplementation(flattenHitWrapper),
formatHitProvider: jest.fn(),
indexPatterns: jest.fn() as any,
IndexPatternSelect: jest.fn(),
__LEGACY: {
// For BWC we must temporarily export the class implementation of Field,
// which is only used externally by the Index Pattern UI.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
} from 'src/core/public';
import { FieldFormatsStart } from '../../../../../plugins/data/public';
import { Field, FieldList, FieldListInterface, FieldType } from './fields';
import { createIndexPatternSelect } from './components';
import { setNotifications, setFieldFormats } from './services';

import {
Expand Down Expand Up @@ -79,7 +78,6 @@ export class IndexPatternsService {
return {
...this.setupApi,
indexPatterns: new IndexPatterns(uiSettings, savedObjectsClient, http),
IndexPatternSelect: createIndexPatternSelect(savedObjectsClient),
};
}

Expand All @@ -91,7 +89,6 @@ export class IndexPatternsService {
// static code

/** @public */
export { IndexPatternSelect } from './components';
export {
CONTAINS_SPACES,
getFromSavedObject,
Expand Down Expand Up @@ -120,4 +117,4 @@ export type IndexPatternsStart = ReturnType<IndexPatternsService['start']>;
export { IndexPattern, IndexPatterns, StaticIndexPattern, Field, FieldType, FieldListInterface };

/** @public */
export { getIndexPatternTitle, findIndexPatternByTitle } from './utils';
export { findIndexPatternByTitle } from './utils';
13 changes: 0 additions & 13 deletions src/legacy/core_plugins/data/public/index_patterns/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,6 @@ export async function findIndexPatternByTitle(
);
}

export async function getIndexPatternTitle(
client: SavedObjectsClientContract,
indexPatternId: string
): Promise<SimpleSavedObject<any>> {
const savedObject = (await client.get('index-pattern', indexPatternId)) as SimpleSavedObject<any>;

if (savedObject.error) {
throw new Error(`Unable to get index-pattern title: ${savedObject.error.message}`);
}

return savedObject.attributes.title;
}

function indexPatternContainsSpaces(indexPattern: string): boolean {
return indexPattern.includes(' ');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,27 @@
* under the License.
*/

jest.mock('ui/new_platform');
jest.mock('ui/index_patterns');
jest.mock('../../../../../core_plugins/data/public/legacy', () => ({
indexPatterns: {
indexPatterns: {
get: jest.fn(),
}
}
}));

jest.mock('ui/new_platform', () => ({
npStart: {
plugins: {
data: {
ui: {
IndexPatternSelect: () => {
return <div/>;
}
}
}
},
},
}));

import React from 'react';
import { shallowWithIntl, mountWithIntl } from 'test_utils/enzyme_helpers';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
import PropTypes from 'prop-types';
import React from 'react';
import { injectI18n } from '@kbn/i18n/react';
import { IndexPatternSelect } from 'ui/index_patterns';

import {
EuiFormRow,
} from '@elastic/eui';

import { npStart } from 'ui/new_platform';
const { IndexPatternSelect } = npStart.plugins.data.ui;

function IndexPatternSelectFormRowUi(props) {
const {
controlIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,24 @@
* under the License.
*/

jest.mock('ui/new_platform');
jest.mock('ui/index_patterns');
jest.mock('ui/new_platform', () => ({
npStart: {
plugins: {
data: {
ui: {
IndexPatternSelect: () => {
return <div/>;
}
}
}
},
},
}));

import React from 'react';
import sinon from 'sinon';
import { shallow } from 'enzyme';

import { mountWithIntl, shallowWithIntl } from 'test_utils/enzyme_helpers';
import { findTestSubject } from '@elastic/eui/lib/test';
import { getIndexPatternMock } from './__tests__/get_index_pattern_mock';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,30 @@
* under the License.
*/

jest.mock('ui/new_platform');
jest.mock('ui/index_patterns');

import React from 'react';
import sinon from 'sinon';
import { shallow } from 'enzyme';
import { mountWithIntl } from 'test_utils/enzyme_helpers';

jest.mock('ui/new_platform', () => ({
npStart: {
plugins: {
data: {
ui: {
IndexPatternSelect: () => {
return <div/>;
}
}
}
},
},
}));

import { findTestSubject } from '@elastic/eui/lib/test';
import { getIndexPatternMock } from './__tests__/get_index_pattern_mock';

import {
RangeControlEditor,
} from './range_control_editor';
import { RangeControlEditor } from './range_control_editor';

const controlParams = {
id: '1',
Expand Down
1 change: 0 additions & 1 deletion src/legacy/ui/public/index_patterns/__mocks__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export {
CONTAINS_SPACES,
getFromSavedObject,
getRoutes,
IndexPatternSelect,
validateIndexPattern,
ILLEGAL_CHARACTERS,
INDEX_PATTERN_ILLEGAL_CHARACTERS,
Expand Down
1 change: 0 additions & 1 deletion src/legacy/ui/public/index_patterns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const {
FieldList, // only used in Discover and StubIndexPattern
flattenHitWrapper,
formatHitProvider,
IndexPatternSelect, // only used in x-pack/plugin/maps and input control vis
} = data.indexPatterns;

// static code
Expand Down
7 changes: 7 additions & 0 deletions src/legacy/ui/public/new_platform/new_platform.karma_mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const mockObservable = () => {
};
};

const mockComponent = () => {
return null;
};

export const mockUiSettings = {
get: (item) => {
return mockUiSettings[item];
Expand Down Expand Up @@ -139,6 +143,9 @@ export const npStart = {
getProvider: sinon.fake(),
},
getSuggestions: sinon.fake(),
ui: {
IndexPatternSelect: mockComponent,
},
query: {
filterManager: {
getFetches$: sinon.fake(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { SavedObjectsClientContract, SimpleSavedObject } from '../../../../../core/public';

export async function getIndexPatternTitle(
client: SavedObjectsClientContract,
indexPatternId: string
): Promise<SimpleSavedObject<any>> {
const savedObject = (await client.get('index-pattern', indexPatternId)) as SimpleSavedObject<any>;

if (savedObject.error) {
throw new Error(`Unable to get index-pattern title: ${savedObject.error.message}`);
}

return savedObject.attributes.title;
}
20 changes: 20 additions & 0 deletions src/plugins/data/public/index_patterns/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export { getIndexPatternTitle } from './get_index_pattern_title';
3 changes: 3 additions & 0 deletions src/plugins/data/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ const createStartContract = (): Start => {
search: { search: jest.fn() },
fieldFormats: fieldFormatsMock as FieldFormatsStart,
query: queryStartMock,
ui: {
IndexPatternSelect: jest.fn(),
},
};
return startContract;
};
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { getSuggestionsProvider } from './suggestions_provider';
import { SearchService } from './search/search_service';
import { FieldFormatsService } from './field_formats_provider';
import { QueryService } from './query';
import { createIndexPatternSelect } from './ui/index_pattern_select';

export class DataPublicPlugin implements Plugin<DataPublicPluginSetup, DataPublicPluginStart> {
private readonly autocomplete = new AutocompleteProviderRegister();
Expand Down Expand Up @@ -59,6 +60,9 @@ export class DataPublicPlugin implements Plugin<DataPublicPluginSetup, DataPubli
search: this.searchService.start(core),
fieldFormats: this.fieldFormatsService.start(),
query: this.queryService.start(),
ui: {
IndexPatternSelect: createIndexPatternSelect(core.savedObjects.client),
},
};
}

Expand Down
4 changes: 4 additions & 0 deletions src/plugins/data/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { FieldFormatsSetup, FieldFormatsStart } from './field_formats_provider';
import { ISearchSetup, ISearchStart } from './search';
import { IGetSuggestions } from './suggestions_provider/types';
import { QuerySetup, QueryStart } from './query';
import { IndexPatternSelectProps } from './ui/index_pattern_select';

export interface DataPublicPluginSetup {
autocomplete: AutocompletePublicPluginSetup;
Expand All @@ -38,6 +39,9 @@ export interface DataPublicPluginStart {
search: ISearchStart;
fieldFormats: FieldFormatsStart;
query: QueryStart;
ui: {
IndexPatternSelect: React.ComponentType<IndexPatternSelectProps>;
};
}

export * from './autocomplete_provider/types';
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/public/ui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
* under the License.
*/

export { IndexPatternSelect } from './index_pattern_select';
export { FilterBar } from './filter_bar';
export { applyFiltersPopover } from './apply_filters';
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import _ from 'lodash';
import React, { Component } from 'react';

import { EuiComboBox } from '@elastic/eui';
import { SavedObjectsClientContract, SimpleSavedObject } from '../../../../../../core/public';
import { getIndexPatternTitle } from '../utils';
import { SavedObjectsClientContract, SimpleSavedObject } from '../../../../../core/public';
import { getIndexPatternTitle } from '../../index_patterns/lib';

interface IndexPatternSelectProps {
export interface IndexPatternSelectProps {
onChange: (opt: any) => void;
indexPatternId: string;
placeholder: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
EuiFormHelpText,
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { IndexPatternSelect } from 'ui/index_patterns';
import { SingleFieldSelect } from '../../../../components/single_field_select';
import { FormattedMessage } from '@kbn/i18n/react';
import { getTermsFields } from '../../../../index_pattern_util';
Expand All @@ -25,6 +24,9 @@ import {
indexPatternService,
} from '../../../../kibana_services';

import { npStart } from 'ui/new_platform';
const { IndexPatternSelect } = npStart.plugins.data.ui;

export class JoinExpression extends Component {

state = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import _ from 'lodash';
import React, { Fragment, Component } from 'react';
import PropTypes from 'prop-types';

import { IndexPatternSelect } from 'ui/index_patterns';
import { SingleFieldSelect } from '../../../components/single_field_select';
import { RENDER_AS } from './render_as';
import { indexPatternService } from '../../../kibana_services';
Expand All @@ -22,6 +21,9 @@ import {
} from '@elastic/eui';
import { ES_GEO_FIELD_TYPE } from '../../../../common/constants';

import { npStart } from 'ui/new_platform';
const { IndexPatternSelect } = npStart.plugins.data.ui;

function filterGeoField({ type }) {
return [ES_GEO_FIELD_TYPE.GEO_POINT].includes(type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import _ from 'lodash';
import React, { Fragment, Component } from 'react';
import PropTypes from 'prop-types';

import { IndexPatternSelect } from 'ui/index_patterns';
import { SingleFieldSelect } from '../../../components/single_field_select';
import { indexPatternService } from '../../../kibana_services';
import { i18n } from '@kbn/i18n';
Expand All @@ -20,6 +19,8 @@ import {
} from '@elastic/eui';
import { ES_GEO_FIELD_TYPE } from '../../../../common/constants';

import { npStart } from 'ui/new_platform';
const { IndexPatternSelect } = npStart.plugins.data.ui;
const GEO_FIELD_TYPES = [ES_GEO_FIELD_TYPE.GEO_POINT];

function filterGeoField({ type }) {
Expand Down
Loading

0 comments on commit 75d261d

Please sign in to comment.