Skip to content

Commit

Permalink
Fix tests, eslint, and conflicts with elastic#58679!
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeelmers committed Mar 12, 2020
1 parent 848c151 commit f56bc99
Show file tree
Hide file tree
Showing 103 changed files with 342 additions and 290 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import React from 'react';
import { render } from 'enzyme';
import { FieldName } from './field_name';

jest.mock('ui/new_platform');

// Note that it currently provides just 2 basic tests, there should be more, but
// the components involved will soon change
test('FieldName renders a string field by providing fieldType and fieldName', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { findTestSubject } from '@elastic/eui/lib/test';
import { SortOrder } from './helpers';
import { IndexPattern, IFieldType } from '../../../../../kibana_services';

jest.mock('ui/new_platform');

function getMockIndexPattern() {
return ({
id: 'test',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ import React, { useState, useMemo, useCallback } from 'react';
import { EuiFormRow, EuiIconTip, EuiCodeEditor, EuiScreenReaderOnly } from '@elastic/eui';
import { i18n } from '@kbn/i18n';

<<<<<<< HEAD
=======
import { isValidJson } from '../../utils';
>>>>>>> Fix TS!
import { AggParamEditorProps } from '../agg_param_props';

function RawJsonParamEditor({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,21 @@
*/

/* `ui/agg_types` dependencies */
export { BUCKET_TYPES, METRIC_TYPES } from '../../../../plugins/data/public';
export {
AggGroupNames,
aggGroupNamesMap,
AggParam,
AggParamType,
AggType,
aggTypes,
BUCKET_TYPES,
createAggConfigs,
FieldParamType,
IAggConfig,
IAggConfigs,
IAggGroupNames,
IAggType,
IFieldParamType,
METRIC_TYPES,
termsAggFilter,
} from 'ui/agg_types';
export { aggTypeFilters, propFilter } from 'ui/agg_types';
Expand Down
29 changes: 1 addition & 28 deletions src/legacy/core_plugins/vis_default_editor/public/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { groupAndSortBy, isValidJson } from './utils';
import { groupAndSortBy } from './utils';
import { AggGroupNames } from './legacy_imports';

jest.mock('ui/new_platform');
Expand Down Expand Up @@ -195,30 +195,3 @@ describe('Default Editor groupAggregationsBy', () => {
expect(groupAndSortBy(aggs, 'subtype', 'title')).toEqual(groupedAggs);
});
});

describe('DefaultEditor isValidJson', () => {
const input = {
valid: '{ "test": "json input" }',
invalid: 'strings are not json',
};

it('should return true when empty string', () => {
expect(isValidJson('')).toBeTruthy();
});

it('should return true when undefine', () => {
expect(isValidJson(undefined as any)).toBeTruthy();
});

it('should return false when invalid string', () => {
expect(isValidJson(input.invalid)).toBeFalsy();
});

it('should return true when valid string', () => {
expect(isValidJson(input.valid)).toBeTruthy();
});

it('should return false if a number', () => {
expect(isValidJson('0')).toBeFalsy();
});
});
29 changes: 0 additions & 29 deletions src/legacy/core_plugins/vis_default_editor/public/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,32 +78,3 @@ export function groupAndSortBy<
function sortByLabel<T>(a: GroupOrOption<T>, b: GroupOrOption<T>) {
return (a.label || '').toLowerCase().localeCompare((b.label || '').toLowerCase());
}

/**
* Check a string if it's a valid JSON.
*
* @param {string} value a string that should be validated
* @returns {boolean} true if value is a valid JSON or if value is an empty string, or a string with whitespaces, otherwise false
*/
export function isValidJson(value: string): boolean {
if (!value || value.length === 0) {
return true;
}

const trimmedValue = value.trim();

if (trimmedValue.length === 0) {
return true;
}

if (trimmedValue[0] === '{' || trimmedValue[0] === '[') {
try {
JSON.parse(trimmedValue);
return true;
} catch (e) {
return false;
}
} else {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ import { functionWrapper } from '../../../../plugins/expressions/common/expressi

jest.mock('ui/new_platform');

jest.mock('../../vis_default_editor/public/legacy_imports', () => ({
propFilter: jest.fn(),
AggGroupNames: {
Buckets: 'buckets',
Metrics: 'metrics',
},
aggTypeFilters: {
addFilter: jest.fn(),
},
BUCKET_TYPES: {
DATE_HISTOGRAM: 'date_histogram',
},
METRIC_TYPES: {
TOP_HITS: 'top_hits',
},
}));

describe('interpreter/functions#metric', () => {
const fn = functionWrapper(createMetricVisFn());
const context = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ import { createMetricVisTypeDefinition } from './metric_vis_type';

jest.mock('ui/new_platform');

jest.mock('../../vis_default_editor/public/legacy_imports', () => ({
propFilter: jest.fn(),
AggGroupNames: {
Buckets: 'buckets',
Metrics: 'metrics',
},
aggTypeFilters: {
addFilter: jest.fn(),
},
BUCKET_TYPES: {
DATE_HISTOGRAM: 'date_histogram',
},
METRIC_TYPES: {
TOP_HITS: 'top_hits',
},
}));

describe('metric_vis - createMetricVisTypeDefinition', () => {
let vis: Vis;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
import moment from 'moment';
import { i18n } from '@kbn/i18n';
import { get } from 'lodash';
import { parseEsInterval } from '../../../../../../plugins/data/public';
import { search } from '../../../../../../plugins/data/public';
const { parseEsInterval } = search.aggs;
import { GTE_INTERVAL_RE } from '../../../../../../plugins/vis_type_timeseries/common/interval_regexp';

export const AUTO_INTERVAL = 'auto';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

import { GTE_INTERVAL_RE } from '../../../../../plugins/vis_type_timeseries/common/interval_regexp';
import { i18n } from '@kbn/i18n';
import { parseInterval } from '../../../../../plugins/data/public';
import { search } from '../../../../../plugins/data/public';
const { parseInterval } = search.aggs;

export function validateInterval(bounds, panel, maxBuckets) {
const { interval } = panel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/

// eslint-disable-next-line
import { npSetup, npStart } from 'ui/new_platform';
import { PluginInitializerContext } from '../../../../../../core/public';
import { plugin } from '.';
Expand Down
1 change: 0 additions & 1 deletion src/legacy/ui/public/agg_types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export const {
isStringType,
isType,
isValidInterval,
OptionedParamType,
parentPipelineType,
propFilter,
siblingPipelineType,
Expand Down
84 changes: 80 additions & 4 deletions src/legacy/ui/public/new_platform/new_platform.karma_mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@
import sinon from 'sinon';
import { getFieldFormatsRegistry } from '../../../../test_utils/public/stub_field_formats';
import { METRIC_TYPE } from '@kbn/analytics';
import {
setIndexPatterns,
setQueryService,
setUiSettings,
setInjectedMetadata,
setFieldFormats,
setSearchService,
setOverlays,
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
} from '../../../../plugins/data/public/services';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { setAggs } from '../../../../../src/legacy/core_plugins/visualizations/public/np_ready/public/services';
import {
AggTypesRegistry,
getAggTypes,
AggConfigs,
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
} from '../../../../../src/plugins/data/public/search/aggs';
import { ComponentRegistry } from '../../../../../src/plugins/advanced_settings/public/';

const mockObservable = () => {
Expand Down Expand Up @@ -61,6 +79,18 @@ const mockCore = {
},
};

const mockAggTypesRegistry = () => {
const registry = new AggTypesRegistry();
const registrySetup = registry.setup();
const aggTypes = getAggTypes({ uiSettings: mockCore.uiSettings });
aggTypes.buckets.forEach(type => registrySetup.registerBucket(type));
aggTypes.metrics.forEach(type => registrySetup.registerMetric(type));

return registry;
};

const aggTypesRegistry = mockAggTypesRegistry();

let refreshInterval = undefined;
let isTimeRangeSelectorEnabled = true;
let isAutoRefreshSelectorEnabled = true;
Expand Down Expand Up @@ -169,10 +199,16 @@ export const npSetup = {
getSavedQueryCount: sinon.fake(),
},
},
__LEGACY: {
esClient: {
search: sinon.fake(),
msearch: sinon.fake(),
search: {
aggs: {
calculateAutoTimeExpression: sinon.fake(),
types: aggTypesRegistry.setup(),
},
__LEGACY: {
esClient: {
search: sinon.fake(),
msearch: sinon.fake(),
},
},
},
fieldFormats: getFieldFormatsRegistry(mockCore),
Expand Down Expand Up @@ -233,6 +269,9 @@ export const npSetup = {
}),
},
},
visTypeVega: {
config: sinon.fake(),
},
},
};

Expand Down Expand Up @@ -284,6 +323,9 @@ export const npStart = {
},
},
data: {
actions: {
createFiltersFromEvent: Promise.resolve(['yes']),
},
autocomplete: {
getProvider: sinon.fake(),
},
Expand Down Expand Up @@ -355,7 +397,27 @@ export const npStart = {
},
},
search: {
aggs: {
calculateAutoTimeExpression: sinon.fake(),
createAggConfigs: sinon.fake(),
createAggConfigs: (indexPattern, configStates = []) => {
return new AggConfigs(indexPattern, configStates, {
typesRegistry: aggTypesRegistry.start(),
});
},
types: aggTypesRegistry.start(),
},
__LEGACY: {
AggConfig: sinon.fake(),
AggType: sinon.fake(),
aggTypeFieldFilters: {
addFilter: sinon.fake(),
filter: sinon.fake(),
},
FieldParamType: sinon.fake(),
MetricAggType: sinon.fake(),
parentPipelineAggHelper: sinon.fake(),
siblingPipelineAggHelper: sinon.fake(),
esClient: {
search: sinon.fake(),
msearch: sinon.fake(),
Expand Down Expand Up @@ -404,8 +466,22 @@ export function __setup__(coreSetup) {
// no-op application register calls (this is overwritten to
// bootstrap an LP plugin outside of tests)
npSetup.core.application.register = () => {};

// Services that need to be set in the legacy platform since the legacy data plugin
// which previously provided them has been removed.
setInjectedMetadata(npSetup.core.injectedMetadata);
}

export function __start__(coreStart) {
npStart.core = coreStart;

// Services that need to be set in the legacy platform since the legacy data plugin
// which previously provided them has been removed.
setUiSettings(npStart.core.uiSettings);
setQueryService(npStart.plugins.data.query);
setIndexPatterns(npStart.plugins.data.indexPatterns);
setFieldFormats(npStart.plugins.data.fieldFormats);
setSearchService(npStart.plugins.data.search);
setAggs(npStart.plugins.data.search.aggs);
setOverlays(npStart.core.overlays);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,8 @@ export class TypeAutocompleteComponent extends ListComponent {
return false;
}

<<<<<<< HEAD:src/plugins/console/public/lib/autocomplete/components/type_autocomplete_component.js
return !_.find(tokens, nonValidIndexType);
}
=======
import { isValidJson } from './is_valid_json';
>>>>>>> Clean up utils & exports!:src/plugins/data/public/search/aggs/utils/is_valid_json.test.ts

getDefaultTermMeta() {
return 'type';
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/data/common/index_patterns/fields/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
* under the License.
*/

import { getFilterableKbnTypeNames, IFieldType } from '../..';
import { getFilterableKbnTypeNames } from '../../kbn_field_types';
import { IFieldType } from './types';

const filterableTypes = getFilterableKbnTypeNames();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import { setIndexPatterns } from '../../../public/services';
import { mockDataServices } from '../../../public/search/aggs/test_helpers';
import { createFiltersFromEvent, EventData } from './create_filters_from_event';

jest.mock('ui/new_platform');

const mockField = {
name: 'bytes',
indexPattern: {
Expand Down
Loading

0 comments on commit f56bc99

Please sign in to comment.