Skip to content

Commit

Permalink
NP Migration: Move doc views registry and existing doc views into dis…
Browse files Browse the repository at this point in the history
…cover plugin (#53465)
  • Loading branch information
flash1293 authored Jan 7, 2020
1 parent 865462a commit c02531a
Show file tree
Hide file tree
Showing 37 changed files with 244 additions and 324 deletions.
26 changes: 0 additions & 26 deletions src/legacy/core_plugins/kbn_doc_views/index.js

This file was deleted.

4 changes: 0 additions & 4 deletions src/legacy/core_plugins/kbn_doc_views/package.json

This file was deleted.

21 changes: 0 additions & 21 deletions src/legacy/core_plugins/kbn_doc_views/public/kbn_doc_views.js

This file was deleted.

33 changes: 0 additions & 33 deletions src/legacy/core_plugins/kbn_doc_views/public/views/json.tsx

This file was deleted.

31 changes: 0 additions & 31 deletions src/legacy/core_plugins/kbn_doc_views/public/views/table.ts

This file was deleted.

21 changes: 14 additions & 7 deletions src/legacy/core_plugins/kibana/public/discover/build_services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,27 @@ import {
ToastsStart,
IUiSettingsClient,
} from 'kibana/public';
import * as docViewsRegistry from 'ui/registry/doc_views';
import { FilterManager, TimefilterContract, IndexPatternsContract } from 'src/plugins/data/public';
import {
FilterManager,
TimefilterContract,
IndexPatternsContract,
DataPublicPluginStart,
} from 'src/plugins/data/public';
import { createSavedSearchesService } from './saved_searches';
// @ts-ignore
import { DiscoverStartPlugins } from './plugin';
import { DataStart } from '../../../data/public';
import { EuiUtilsStart } from '../../../../../plugins/eui_utils/public';
import { SharePluginStart } from '../../../../../plugins/share/public';
import { SavedSearch } from './np_ready/types';
import { DocViewsRegistry } from './np_ready/doc_views/doc_views_registry';

export interface DiscoverServices {
addBasePath: (path: string) => string;
capabilities: Capabilities;
chrome: ChromeStart;
core: CoreStart;
data: DataStart;
data: DataPublicPluginStart;
docLinks: DocLinksStart;
docViewsRegistry: docViewsRegistry.DocViewsRegistry;
docViewsRegistry: DocViewsRegistry;
eui_utils: EuiUtilsStart;
filterManager: FilterManager;
indexPatterns: IndexPatternsContract;
Expand All @@ -54,7 +57,11 @@ export interface DiscoverServices {
getSavedSearchUrlById: (id: string) => Promise<string>;
uiSettings: IUiSettingsClient;
}
export async function buildServices(core: CoreStart, plugins: DiscoverStartPlugins) {
export async function buildServices(
core: CoreStart,
plugins: DiscoverStartPlugins,
docViewsRegistry: DocViewsRegistry
): Promise<DiscoverServices> {
const services = {
savedObjectsClient: core.savedObjects.client,
indexPatterns: plugins.data.indexPatterns,
Expand Down
18 changes: 11 additions & 7 deletions src/legacy/core_plugins/kibana/public/discover/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,24 @@
* specific language governing permissions and limitations
* under the License.
*/
import { PluginInitializer, PluginInitializerContext } from 'kibana/public';
import chrome from 'ui/chrome';
import { PluginInitializerContext } from 'kibana/public';
import { npSetup, npStart } from 'ui/new_platform';
import { DiscoverPlugin, DiscoverSetup, DiscoverStart } from './plugin';
import { DiscoverPlugin } from './plugin';

// Core will be looking for this when loading our plugin in the new platform
export const plugin: PluginInitializer<DiscoverSetup, DiscoverStart> = () => {
export const plugin = (context: PluginInitializerContext) => {
return new DiscoverPlugin();
};

// Legacy compatiblity part - to be removed at cutover, replaced by a kibana.json file
export const pluginInstance = plugin({} as PluginInitializerContext);
(async () => {
pluginInstance.setup(npSetup.core, npSetup.plugins);
pluginInstance.start(npStart.core, npStart.plugins);
})();
export const setup = pluginInstance.setup(npSetup.core, {
...npSetup.plugins,
__LEGACY: {
chrome,
},
});
export const start = pluginInstance.start(npStart.core, npStart.plugins);

export { createSavedSearchesService } from './saved_searches/saved_searches';
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ export {
IndexPatternsContract,
IIndexPattern,
IndexPattern,
indexPatterns,
IFieldType,
} from '../../../../../plugins/data/public';
export { ElasticSearchHit } from 'ui/registry/doc_views_types';
export { DocViewRenderProps, DocViewRenderFn } from 'ui/registry/doc_views';
export { ElasticSearchHit } from './np_ready/doc_views/doc_views_types';
export { Adapters } from 'ui/inspector/types';
export { DocView, DocViewInput } from 'ui/registry/doc_views_types';
export { Chrome, IInjector } from 'ui/chrome';
export { registerTimefilterWithGlobalStateFactory } from 'ui/timefilter/setup_router';
export { IInjector } from 'ui/chrome';
export { FieldName } from 'ui/directives/field_name/field_name';
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { findTestSubject } from '@elastic/eui/lib/test';
import { Doc, DocProps } from './doc';

jest.mock('../doc_viewer/doc_viewer', () => ({
DocViewer: 'test',
DocViewer: () => null,
}));

jest.mock('../../../kibana_services', () => {
Expand Down Expand Up @@ -67,7 +67,7 @@ async function mountDoc(search: () => void, update = false, indexPatternGetter:
indexPatternService,
} as DocProps;
let comp!: ReactWrapper;
act(() => {
await act(async () => {
comp = mountWithIntl(<Doc {...props} />);
if (update) comp.update();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('Test of <Doc /> helper / hook', () => {
`);
});

test('useEsDocSearch', () => {
test('useEsDocSearch', async () => {
const indexPattern = {
getComputedFields: () => [],
};
Expand All @@ -53,16 +53,16 @@ describe('Test of <Doc /> helper / hook', () => {
const props = {
id: '1',
index: 'index1',
esClient: { search: jest.fn() },
esClient: { search: jest.fn(() => new Promise(() => {})) },
indexPatternId: 'xyz',
indexPatternService,
} as DocProps;
let hook;
act(() => {
await act(async () => {
hook = renderHook((p: DocProps) => useEsDocSearch(p), { initialProps: props });
});
// @ts-ignore
expect(hook.result.current).toEqual([ElasticRequestState.Loading, null, null]);
expect(hook.result.current).toEqual([ElasticRequestState.Loading, null, indexPattern]);
expect(indexPatternService.get).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,40 @@ import { mount, shallow } from 'enzyme';
import { DocViewer } from './doc_viewer';
// @ts-ignore
import { findTestSubject } from '@elastic/eui/lib/test';
import { DocViewRenderProps, DocViewInput, getServices } from '../../../kibana_services';
import { getServices } from '../../../kibana_services';
import { DocViewRenderProps } from '../../doc_views/doc_views_types';

jest.mock('../../../kibana_services', () => {
const docViews: DocViewInput[] = [];

function addDocView(docView: DocViewInput) {
docViews.push(docView);
}

let registry: any[] = [];
return {
getServices: () => ({
docViewsRegistry: {
getDocViewsSorted: () => {
return docViews;
addDocView(view: any) {
registry.push(view);
},
getDocViewsSorted() {
return registry;
},
addDocView,
docViews,
},
resetRegistry: () => {
registry = [];
},
}),
formatMsg: (x: any) => String(x),
formatStack: (x: any) => String(x),
};
});

const {
docViewsRegistry: { docViews, addDocView },
} = getServices();

function emptyDocViews() {
docViews.length = 0;
}

beforeEach(() => {
emptyDocViews();
(getServices() as any).resetRegistry();
jest.clearAllMocks();
});

test('Render <DocViewer/> with 3 different tabs', () => {
addDocView({ order: 10, title: 'Render function', render: jest.fn() });
addDocView({ order: 20, title: 'React component', component: () => <div>test</div> });
addDocView({ order: 30, title: 'Invalid doc view' });
const registry = getServices().docViewsRegistry;
registry.addDocView({ order: 10, title: 'Render function', render: jest.fn() });
registry.addDocView({ order: 20, title: 'React component', component: () => <div>test</div> });
registry.addDocView({ order: 30, title: 'Invalid doc view' });

const renderProps = { hit: {} } as DocViewRenderProps;

Expand All @@ -76,7 +69,8 @@ test('Render <DocViewer/> with 1 tab displaying error message', () => {
return null;
}

addDocView({
const registry = getServices().docViewsRegistry;
registry.addDocView({
order: 10,
title: 'React component',
component: SomeComponent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
*/
import React from 'react';
import { EuiTabbedContent } from '@elastic/eui';
import { getServices, DocViewRenderProps, DocView } from '../../../kibana_services';
import { getServices } from '../../../kibana_services';
import { DocViewerTab } from './doc_viewer_tab';
import { DocView, DocViewRenderProps } from '../../doc_views/doc_views_types';

/**
* Rendering tabs with different views of 1 Elasticsearch hit in Discover.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import React from 'react';
import { mount } from 'enzyme';
import { DocViewRenderTab } from './doc_viewer_render_tab';
import { DocViewRenderProps } from '../../../kibana_services';
import { DocViewRenderProps } from '../../doc_views/doc_views_types';

test('Mounting and unmounting DocViewerRenderTab', () => {
const unmountFn = jest.fn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/
import React, { useRef, useEffect } from 'react';
import { DocViewRenderFn, DocViewRenderProps } from '../../../kibana_services';
import { DocViewRenderFn, DocViewRenderProps } from '../../doc_views/doc_views_types';

interface Props {
render: DocViewRenderFn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*/
import React from 'react';
import { I18nProvider } from '@kbn/i18n/react';
import { DocViewRenderProps, DocViewRenderFn } from '../../../kibana_services';
import { DocViewRenderTab } from './doc_viewer_render_tab';
import { DocViewerError } from './doc_viewer_render_error';
import { DocViewRenderFn, DocViewRenderProps } from '../../doc_views/doc_views_types';

interface Props {
component?: React.ComponentType<DocViewRenderProps>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import React from 'react';
import { shallow } from 'enzyme';
import { JsonCodeBlock } from './json_code_block';
import { IndexPattern } from '../../../../../plugins/data/public';
import { IndexPattern } from '../../../kibana_services';

it('returns the `JsonCodeEditor` component', () => {
const props = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import React from 'react';
import { EuiCodeBlock } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { DocViewRenderProps } from 'ui/registry/doc_views';
import { DocViewRenderProps } from '../../doc_views/doc_views_types';

export function JsonCodeBlock({ hit }: DocViewRenderProps) {
const label = i18n.translate('kbnDocViews.json.codeEditorAriaLabel', {
const label = i18n.translate('kbn.discover.docViews.json.codeEditorAriaLabel', {
defaultMessage: 'Read only JSON view of an elasticsearch document',
});
return (
Expand Down
Loading

0 comments on commit c02531a

Please sign in to comment.