Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] Move StubIndexPattern to data plugin and convert to TS. (#78518) #78605

Merged
merged 1 commit into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/fixtures/stubbed_logstash_index_pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
* under the License.
*/

import StubIndexPattern from 'test_utils/stub_index_pattern';
import stubbedLogstashFields from 'fixtures/logstash_fields';

import { getKbnFieldType } from '../plugins/data/common';
import { getStubIndexPattern } from '../plugins/data/public/test_utils';
import { uiSettingsServiceMock } from '../core/public/ui_settings/ui_settings_service.mock';

const uiSettingSetupMock = uiSettingsServiceMock.createSetupContract();
Expand All @@ -46,7 +46,7 @@ export default function stubbedLogstashIndexPatternService() {
};
});

const indexPattern = new StubIndexPattern('logstash-*', (cfg) => cfg, 'time', fields, {
const indexPattern = getStubIndexPattern('logstash-*', (cfg) => cfg, 'time', fields, {
uiSettings: uiSettingSetupMock,
});

Expand Down
132 changes: 132 additions & 0 deletions src/plugins/data/public/index_patterns/index_pattern.stub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* 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 sinon from 'sinon';

import { CoreSetup } from 'src/core/public';
import { FieldFormat as FieldFormatImpl } from '../../common/field_formats';
import { IFieldType, FieldSpec } from '../../common/index_patterns';
import { FieldFormatsStart } from '../field_formats';
import { IndexPattern, indexPatterns, KBN_FIELD_TYPES, fieldList } from '../';
import { getFieldFormatsRegistry } from '../test_utils';
import { setFieldFormats } from '../services';

setFieldFormats(({
getDefaultInstance: () =>
({
getConverterFor: () => (value: any) => value,
convert: (value: any) => JSON.stringify(value),
} as FieldFormatImpl),
} as unknown) as FieldFormatsStart);

export function getStubIndexPattern(
pattern: string,
getConfig: (cfg: any) => any,
timeField: string | null,
fields: FieldSpec[] | IFieldType[],
core: CoreSetup
): IndexPattern {
return (new StubIndexPattern(
pattern,
getConfig,
timeField,
fields,
core
) as unknown) as IndexPattern;
}

export class StubIndexPattern {
id: string;
title: string;
popularizeField: Function;
timeFieldName: string | null;
isTimeBased: () => boolean;
getConfig: (cfg: any) => any;
getNonScriptedFields: Function;
getScriptedFields: Function;
getFieldByName: Function;
getSourceFiltering: Function;
metaFields: string[];
fieldFormatMap: Record<string, any>;
getComputedFields: Function;
flattenHit: Function;
formatHit: Record<string, any>;
fieldsFetcher: Record<string, any>;
formatField: Function;
getFormatterForField: () => { convert: Function };
_reindexFields: Function;
stubSetFieldFormat: Function;
fields?: FieldSpec[];

constructor(
pattern: string,
getConfig: (cfg: any) => any,
timeField: string | null,
fields: FieldSpec[] | IFieldType[],
core: CoreSetup
) {
const registeredFieldFormats = getFieldFormatsRegistry(core);

this.id = pattern;
this.title = pattern;
this.popularizeField = sinon.stub();
this.timeFieldName = timeField;
this.isTimeBased = () => Boolean(this.timeFieldName);
this.getConfig = getConfig;
this.getNonScriptedFields = sinon.spy(IndexPattern.prototype.getNonScriptedFields);
this.getScriptedFields = sinon.spy(IndexPattern.prototype.getScriptedFields);
this.getFieldByName = sinon.spy(IndexPattern.prototype.getFieldByName);
this.getSourceFiltering = sinon.stub();
this.metaFields = ['_id', '_type', '_source'];
this.fieldFormatMap = {};

this.getComputedFields = IndexPattern.prototype.getComputedFields.bind(this);
this.flattenHit = indexPatterns.flattenHitWrapper(
(this as unknown) as IndexPattern,
this.metaFields
);
this.formatHit = indexPatterns.formatHitProvider(
(this as unknown) as IndexPattern,
registeredFieldFormats.getDefaultInstance(KBN_FIELD_TYPES.STRING)
);
this.fieldsFetcher = { apiClient: { baseUrl: '' } };
this.formatField = this.formatHit.formatField;
this.getFormatterForField = () => ({
convert: () => '',
});

this._reindexFields = function () {
this.fields = fieldList((this.fields || fields) as FieldSpec[], false);
};

this.stubSetFieldFormat = function (
fieldName: string,
id: string,
params: Record<string, any>
) {
const FieldFormat = registeredFieldFormats.getType(id);
this.fieldFormatMap[fieldName] = new FieldFormat!(params);
this._reindexFields();
};

this._reindexFields();

return this;
}
}
1 change: 1 addition & 0 deletions src/plugins/data/public/test_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
*/

export { getFieldFormatsRegistry } from './field_formats/field_formats_registry.stub';
export { getStubIndexPattern, StubIndexPattern } from './index_patterns/index_pattern.stub';
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
import React from 'react';
import { findTestSubject } from '@elastic/eui/lib/test';
// @ts-ignore
import StubIndexPattern from 'test_utils/stub_index_pattern';
// @ts-ignore
import stubbedLogstashFields from 'fixtures/logstash_fields';
import { mountWithIntl } from 'test_utils/enzyme_helpers';
import { DiscoverField } from './discover_field';
import { coreMock } from '../../../../../../core/public/mocks';
import { IndexPatternField } from '../../../../../data/public';
import { getStubIndexPattern } from '../../../../../data/public/test_utils';

jest.mock('../../../kibana_services', () => ({
getServices: () => ({
Expand All @@ -53,12 +52,12 @@ jest.mock('../../../kibana_services', () => ({
}));

function getComponent(selected = false, showDetails = false, useShortDots = false) {
const indexPattern = new StubIndexPattern(
const indexPattern = getStubIndexPattern(
'logstash-*',
(cfg: any) => cfg,
'time',
stubbedLogstashFields(),
coreMock.createStart()
coreMock.createSetup()
);

const field = new IndexPatternField(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import _ from 'lodash';
import { ReactWrapper } from 'enzyme';
import { findTestSubject } from '@elastic/eui/lib/test';
// @ts-ignore
import StubIndexPattern from 'test_utils/stub_index_pattern';
// @ts-ignore
import realHits from 'fixtures/real_hits.js';
// @ts-ignore
import stubbedLogstashFields from 'fixtures/logstash_fields';
Expand All @@ -31,6 +29,7 @@ import React from 'react';
import { DiscoverSidebar, DiscoverSidebarProps } from './discover_sidebar';
import { coreMock } from '../../../../../../core/public/mocks';
import { IndexPatternAttributes } from '../../../../../data/common';
import { getStubIndexPattern } from '../../../../../data/public/test_utils';
import { SavedObject } from '../../../../../../core/types';

jest.mock('../../../kibana_services', () => ({
Expand Down Expand Up @@ -65,14 +64,15 @@ jest.mock('./lib/get_index_pattern_field_list', () => ({
}));

function getCompProps() {
const indexPattern = new StubIndexPattern(
const indexPattern = getStubIndexPattern(
'logstash-*',
(cfg: any) => cfg,
'time',
stubbedLogstashFields(),
coreMock.createStart()
coreMock.createSetup()
);

// @ts-expect-error _.each() is passing additional args to flattenHit
const hits = _.each(_.cloneDeep(realHits), indexPattern.flattenHit) as Array<
Record<string, unknown>
>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,23 @@ import _ from 'lodash';
// @ts-ignore
import realHits from 'fixtures/real_hits.js';
// @ts-ignore
import StubIndexPattern from 'test_utils/stub_index_pattern';
// @ts-ignore
import stubbedLogstashFields from 'fixtures/logstash_fields';
import { coreMock } from '../../../../../../../core/public/mocks';
import { IndexPattern } from '../../../../../../data/public';
import { getStubIndexPattern } from '../../../../../../data/public/test_utils';
// @ts-ignore
import { fieldCalculator } from './field_calculator';

let indexPattern: IndexPattern;

describe('fieldCalculator', function () {
beforeEach(function () {
indexPattern = new StubIndexPattern(
indexPattern = getStubIndexPattern(
'logstash-*',
(cfg: any) => cfg,
'time',
stubbedLogstashFields(),
coreMock.createStart()
coreMock.createSetup()
);
});
it('should have a _countMissing that counts nulls & undefineds in an array', function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ import {
SavedObjectSaveOpts,
} from '../types';

// @ts-ignore
import StubIndexPattern from 'test_utils/stub_index_pattern';
import { coreMock } from '../../../../core/public/mocks';
import { dataPluginMock, createSearchSourceMock } from '../../../../plugins/data/public/mocks';
import { getStubIndexPattern, StubIndexPattern } from '../../../../plugins/data/public/test_utils';
import { SavedObjectAttributes, SimpleSavedObject } from 'kibana/public';
import { IIndexPattern } from '../../../data/common/index_patterns';

Expand Down Expand Up @@ -294,14 +293,14 @@ describe('Saved Object', () => {
type: 'dashboard',
} as SimpleSavedObject<SavedObjectAttributes>);

const indexPattern = new StubIndexPattern(
const indexPattern = getStubIndexPattern(
'my-index',
getConfig,
null,
[],
coreMock.createSetup()
);
indexPattern.title = indexPattern.id;
indexPattern.title = indexPattern.id!;
savedObject.searchSource!.setField('index', indexPattern);
return savedObject.save(saveOptionsMock).then(() => {
const args = (savedObjectsClientStub.create as jest.Mock).mock.calls[0];
Expand Down Expand Up @@ -335,7 +334,7 @@ describe('Saved Object', () => {
type: 'dashboard',
} as SimpleSavedObject<SavedObjectAttributes>);

const indexPattern = new StubIndexPattern(
const indexPattern = getStubIndexPattern(
'non-existant-index',
getConfig,
null,
Expand Down Expand Up @@ -662,14 +661,14 @@ describe('Saved Object', () => {

const savedObject = new SavedObjectClass(config);
savedObject.hydrateIndexPattern = jest.fn().mockImplementation(() => {
const indexPattern = new StubIndexPattern(
const indexPattern = getStubIndexPattern(
indexPatternId,
getConfig,
null,
[],
coreMock.createSetup()
);
indexPattern.title = indexPattern.id;
indexPattern.title = indexPattern.id!;
savedObject.searchSource!.setField('index', indexPattern);
return Bluebird.resolve(indexPattern);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import 'angular-mocks';
import 'angular-sanitize';
import $ from 'jquery';

// @ts-ignore
import StubIndexPattern from 'test_utils/stub_index_pattern';
import { getAngularModule } from './get_inner_angular';
import { initTableVisLegacyModule } from './table_vis_legacy_module';
import { getTableVisTypeDefinition } from './table_vis_type';
Expand All @@ -32,6 +30,7 @@ import { stubFields } from '../../data/public/stubs';
import { tableVisResponseHandler } from './table_vis_response_handler';
import { coreMock } from '../../../core/public/mocks';
import { IAggConfig, search } from '../../data/public';
import { getStubIndexPattern } from '../../data/public/test_utils';
// TODO: remove linting disable
import { searchServiceMock } from '../../data/public/search/mocks';

Expand Down Expand Up @@ -105,7 +104,7 @@ describe('Table Vis - Controller', () => {
);

beforeEach(() => {
stubIndexPattern = new StubIndexPattern(
stubIndexPattern = getStubIndexPattern(
'logstash-*',
(cfg: any) => cfg,
'time',
Expand Down
Loading