Skip to content

Commit

Permalink
[ML] Improve KibanaContext types.
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Aug 1, 2019
1 parent f446c08 commit 64b198a
Show file tree
Hide file tree
Showing 22 changed files with 258 additions and 120 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { IndexPattern } from 'ui/index_patterns';

export const indexPatternMock = ({
id: 'the-index-pattern-id',
title: 'the-index-pattern-title',
fields: [],
} as unknown) as IndexPattern;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { IndexPatterns } from 'ui/index_patterns';

export const indexPatternsMock = (new (class {
fieldFormats = [];
config = {};
savedObjectsClient = {};
refreshSavedObjectsCache = {};
clearCache = jest.fn();
get = jest.fn();
getDefault = jest.fn();
getFields = jest.fn();
getIds = jest.fn();
getTitles = jest.fn();
make = jest.fn();
})() as unknown) as IndexPatterns;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export const kibanaConfigMock = {
get: <T>(key: string): T => ({} as T),
has: (key: string) => false,
set: (key: string, value: any) => {},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { indexPatternMock } from './index_pattern';
import { indexPatternsMock } from './index_patterns';
import { kibanaConfigMock } from './kibana_config';
import { savedSearchMock } from './saved_search';

export const kibanaContextValueMock = {
combinedQuery: {},
currentIndexPattern: indexPatternMock,
currentSavedSearch: savedSearchMock,
indexPatterns: indexPatternsMock,
kbnBaseUrl: 'url',
kibanaConfig: kibanaConfigMock,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export const savedSearchMock = {
id: 'the-saved-search-id',
title: 'the-saved-search-title',
searchSource: {},
columns: [],
sort: [],
destroy: () => {},
};
28 changes: 18 additions & 10 deletions x-pack/legacy/plugins/ml/public/contexts/kibana/kibana_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,32 @@

import React from 'react';

import { IndexPattern } from 'ui/index_patterns';
import { KibanaConfig } from 'src/legacy/server/kbn_server';
import { SavedSearch } from 'src/legacy/core_plugins/kibana/public/discover/types';

import { IndexPattern, IndexPatterns } from 'ui/index_patterns';

// set() method is missing in original d.ts
export interface KibanaConfigTypeFix extends KibanaConfig {
set(key: string, value: any): void;
}

export interface KibanaContextValue {
combinedQuery?: any;
currentIndexPattern?: IndexPattern;
currentSavedSearch?: any;
indexPatterns?: any;
kbnBaseUrl?: string;
kibanaConfig?: any;
combinedQuery: any;
currentIndexPattern: IndexPattern;
currentSavedSearch: SavedSearch;
indexPatterns: IndexPatterns;
kbnBaseUrl: string;
kibanaConfig: KibanaConfigTypeFix;
}

export type SavedSearchQuery = object;

// This context provides dependencies which can be injected
// via angularjs only (like services, currentIndexPattern etc.).
// Because we cannot just import these dependencies, the default value
// for the context is just {} and the value's type has optional
// attributes for the angularjs based dependencies. Therefore, the
// for the context is just {} and of type `Partial<KibanaContextValue>`
// for the angularjs based dependencies. Therefore, the
// actual dependencies are set like we did previously with KibanaContext
// in the wrapping angularjs directive. In the custom hook we check if
// the dependencies are present with error reporting if they weren't
Expand All @@ -34,4 +42,4 @@ export type SavedSearchQuery = object;
// Multiple custom hooks can be created to access subsets of
// the overall context value if necessary too,
// see useCurrentIndexPattern() for example.
export const KibanaContext = React.createContext<KibanaContextValue>({});
export const KibanaContext = React.createContext<Partial<KibanaContextValue>>({});
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ export const useKibanaContext = () => {
throw new Error('required attribute is undefined');
}

return context as Required<KibanaContextValue>;
return context as KibanaContextValue;
};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { shallow } from 'enzyme';
import React from 'react';

import { KibanaContext } from '../../../../../contexts/kibana';
import { kibanaContextValueMock } from '../../../../../contexts/kibana/__mocks__/kibana_context_value';

import { IndexPattern } from 'ui/index_patterns';
import { getPivotQuery } from '../../../../common';

import { SourceIndexPreview } from './source_index_preview';
Expand All @@ -22,12 +22,6 @@ jest.mock('react', () => {

describe('Data Frame: <SourceIndexPreview />', () => {
test('Minimal initialization', () => {
const currentIndexPattern = ({
id: 'the-index-pattern-id',
title: 'the-index-pattern-title',
fields: [],
} as unknown) as IndexPattern;

const props = {
query: getPivotQuery('the-query'),
};
Expand All @@ -36,16 +30,7 @@ describe('Data Frame: <SourceIndexPreview />', () => {
// with the Provider being the outer most component.
const wrapper = shallow(
<div>
<KibanaContext.Provider
value={{
combinedQuery: {},
currentIndexPattern,
currentSavedSearch: {},
indexPatterns: {},
kbnBaseUrl: 'url',
kibanaConfig: {},
}}
>
<KibanaContext.Provider value={kibanaContextValueMock}>
<SourceIndexPreview {...props} />
</KibanaContext.Provider>
</div>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { shallow } from 'enzyme';
import React from 'react';

import { KibanaContext } from '../../../../../contexts/kibana';
import { kibanaContextValueMock } from '../../../../../contexts/kibana/__mocks__/kibana_context_value';

import { StepCreateForm } from './step_create_form';
import { IndexPattern } from 'ui/index_patterns';

jest.mock('../../../../../contexts/ui/use_ui_chrome_context');

Expand All @@ -30,26 +30,11 @@ describe('Data Frame: <StepCreateForm />', () => {
onChange() {},
};

const currentIndexPattern = ({
id: 'the-index-pattern-id',
title: 'the-index-pattern-title',
fields: [],
} as unknown) as IndexPattern;

// Using a wrapping <div> element because shallow() would fail
// with the Provider being the outer most component.
const wrapper = shallow(
<div>
<KibanaContext.Provider
value={{
combinedQuery: {},
currentIndexPattern,
currentSavedSearch: {},
indexPatterns: {},
kbnBaseUrl: 'url',
kibanaConfig: {},
}}
>
<KibanaContext.Provider value={kibanaContextValueMock}>
<StepCreateForm {...props} />
</KibanaContext.Provider>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,26 @@ export const StepCreateForm: SFC<Props> = React.memo(

const id = await newIndexPattern.create();

// id returns false if there's a duplicate index pattern.
if (id === false) {
toastNotifications.addDanger(
i18n.translate('xpack.ml.dataframe.stepCreateForm.duplicateIndexPatternErrorMessage', {
defaultMessage:
'An error occurred creating the Kibana index pattern {indexPatternName}: The index pattern already exists.',
values: { indexPatternName },
})
);
return;
}

// check if there's a default index pattern, if not,
// set the newly created one as the default index pattern.
if (!kibanaContext.kibanaConfig.get('defaultIndex')) {
await kibanaContext.kibanaConfig.set('defaultIndex', id);
}

toastNotifications.addSuccess(
i18n.translate('xpack.ml.dataframe.stepCreateForm.reateIndexPatternSuccessMessage', {
i18n.translate('xpack.ml.dataframe.stepCreateForm.createIndexPatternSuccessMessage', {
defaultMessage: 'Kibana index pattern {indexPatternName} created successfully.',
values: { indexPatternName },
})
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 64b198a

Please sign in to comment.