Skip to content

Commit 314fa84

Browse files
committed
Move StubIndexPattern to data plugin and convert to TS. (#78518)
# Conflicts: # src/test_utils/public/stub_index_pattern.js
1 parent c39243c commit 314fa84

9 files changed

Lines changed: 153 additions & 101 deletions

File tree

src/fixtures/stubbed_logstash_index_pattern.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
* under the License.
1818
*/
1919

20-
import StubIndexPattern from 'test_utils/stub_index_pattern';
2120
import stubbedLogstashFields from 'fixtures/logstash_fields';
2221

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

2626
const uiSettingSetupMock = uiSettingsServiceMock.createSetupContract();
@@ -46,7 +46,7 @@ export default function stubbedLogstashIndexPatternService() {
4646
};
4747
});
4848

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

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import sinon from 'sinon';
21+
22+
import { CoreSetup } from 'src/core/public';
23+
import { FieldFormat as FieldFormatImpl } from '../../common/field_formats';
24+
import { IFieldType, FieldSpec } from '../../common/index_patterns';
25+
import { FieldFormatsStart } from '../field_formats';
26+
import { IndexPattern, indexPatterns, KBN_FIELD_TYPES, fieldList } from '../';
27+
import { getFieldFormatsRegistry } from '../test_utils';
28+
import { setFieldFormats } from '../services';
29+
30+
setFieldFormats(({
31+
getDefaultInstance: () =>
32+
({
33+
getConverterFor: () => (value: any) => value,
34+
convert: (value: any) => JSON.stringify(value),
35+
} as FieldFormatImpl),
36+
} as unknown) as FieldFormatsStart);
37+
38+
export function getStubIndexPattern(
39+
pattern: string,
40+
getConfig: (cfg: any) => any,
41+
timeField: string | null,
42+
fields: FieldSpec[] | IFieldType[],
43+
core: CoreSetup
44+
): IndexPattern {
45+
return (new StubIndexPattern(
46+
pattern,
47+
getConfig,
48+
timeField,
49+
fields,
50+
core
51+
) as unknown) as IndexPattern;
52+
}
53+
54+
export class StubIndexPattern {
55+
id: string;
56+
title: string;
57+
popularizeField: Function;
58+
timeFieldName: string | null;
59+
isTimeBased: () => boolean;
60+
getConfig: (cfg: any) => any;
61+
getNonScriptedFields: Function;
62+
getScriptedFields: Function;
63+
getFieldByName: Function;
64+
getSourceFiltering: Function;
65+
metaFields: string[];
66+
fieldFormatMap: Record<string, any>;
67+
getComputedFields: Function;
68+
flattenHit: Function;
69+
formatHit: Record<string, any>;
70+
fieldsFetcher: Record<string, any>;
71+
formatField: Function;
72+
getFormatterForField: () => { convert: Function };
73+
_reindexFields: Function;
74+
stubSetFieldFormat: Function;
75+
fields?: FieldSpec[];
76+
77+
constructor(
78+
pattern: string,
79+
getConfig: (cfg: any) => any,
80+
timeField: string | null,
81+
fields: FieldSpec[] | IFieldType[],
82+
core: CoreSetup
83+
) {
84+
const registeredFieldFormats = getFieldFormatsRegistry(core);
85+
86+
this.id = pattern;
87+
this.title = pattern;
88+
this.popularizeField = sinon.stub();
89+
this.timeFieldName = timeField;
90+
this.isTimeBased = () => Boolean(this.timeFieldName);
91+
this.getConfig = getConfig;
92+
this.getNonScriptedFields = sinon.spy(IndexPattern.prototype.getNonScriptedFields);
93+
this.getScriptedFields = sinon.spy(IndexPattern.prototype.getScriptedFields);
94+
this.getFieldByName = sinon.spy(IndexPattern.prototype.getFieldByName);
95+
this.getSourceFiltering = sinon.stub();
96+
this.metaFields = ['_id', '_type', '_source'];
97+
this.fieldFormatMap = {};
98+
99+
this.getComputedFields = IndexPattern.prototype.getComputedFields.bind(this);
100+
this.flattenHit = indexPatterns.flattenHitWrapper(
101+
(this as unknown) as IndexPattern,
102+
this.metaFields
103+
);
104+
this.formatHit = indexPatterns.formatHitProvider(
105+
(this as unknown) as IndexPattern,
106+
registeredFieldFormats.getDefaultInstance(KBN_FIELD_TYPES.STRING)
107+
);
108+
this.fieldsFetcher = { apiClient: { baseUrl: '' } };
109+
this.formatField = this.formatHit.formatField;
110+
this.getFormatterForField = () => ({
111+
convert: () => '',
112+
});
113+
114+
this._reindexFields = function () {
115+
this.fields = fieldList((this.fields || fields) as FieldSpec[], false);
116+
};
117+
118+
this.stubSetFieldFormat = function (
119+
fieldName: string,
120+
id: string,
121+
params: Record<string, any>
122+
) {
123+
const FieldFormat = registeredFieldFormats.getType(id);
124+
this.fieldFormatMap[fieldName] = new FieldFormat!(params);
125+
this._reindexFields();
126+
};
127+
128+
this._reindexFields();
129+
130+
return this;
131+
}
132+
}

src/plugins/data/public/test_utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@
1818
*/
1919

2020
export { getFieldFormatsRegistry } from './field_formats/field_formats_registry.stub';
21+
export { getStubIndexPattern, StubIndexPattern } from './index_patterns/index_pattern.stub';

src/plugins/discover/public/application/components/sidebar/discover_field.test.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@
2020
import React from 'react';
2121
import { findTestSubject } from '@elastic/eui/lib/test';
2222
// @ts-ignore
23-
import StubIndexPattern from 'test_utils/stub_index_pattern';
24-
// @ts-ignore
2523
import stubbedLogstashFields from 'fixtures/logstash_fields';
2624
import { mountWithIntl } from 'test_utils/enzyme_helpers';
2725
import { DiscoverField } from './discover_field';
2826
import { coreMock } from '../../../../../../core/public/mocks';
2927
import { IndexPatternField } from '../../../../../data/public';
28+
import { getStubIndexPattern } from '../../../../../data/public/test_utils';
3029

3130
jest.mock('../../../kibana_services', () => ({
3231
getServices: () => ({
@@ -53,12 +52,12 @@ jest.mock('../../../kibana_services', () => ({
5352
}));
5453

5554
function getComponent(selected = false, showDetails = false, useShortDots = false) {
56-
const indexPattern = new StubIndexPattern(
55+
const indexPattern = getStubIndexPattern(
5756
'logstash-*',
5857
(cfg: any) => cfg,
5958
'time',
6059
stubbedLogstashFields(),
61-
coreMock.createStart()
60+
coreMock.createSetup()
6261
);
6362

6463
const field = new IndexPatternField(

src/plugins/discover/public/application/components/sidebar/discover_sidebar.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import _ from 'lodash';
2121
import { ReactWrapper } from 'enzyme';
2222
import { findTestSubject } from '@elastic/eui/lib/test';
2323
// @ts-ignore
24-
import StubIndexPattern from 'test_utils/stub_index_pattern';
25-
// @ts-ignore
2624
import realHits from 'fixtures/real_hits.js';
2725
// @ts-ignore
2826
import stubbedLogstashFields from 'fixtures/logstash_fields';
@@ -31,6 +29,7 @@ import React from 'react';
3129
import { DiscoverSidebar, DiscoverSidebarProps } from './discover_sidebar';
3230
import { coreMock } from '../../../../../../core/public/mocks';
3331
import { IndexPatternAttributes } from '../../../../../data/common';
32+
import { getStubIndexPattern } from '../../../../../data/public/test_utils';
3433
import { SavedObject } from '../../../../../../core/types';
3534

3635
jest.mock('../../../kibana_services', () => ({
@@ -65,14 +64,15 @@ jest.mock('./lib/get_index_pattern_field_list', () => ({
6564
}));
6665

6766
function getCompProps() {
68-
const indexPattern = new StubIndexPattern(
67+
const indexPattern = getStubIndexPattern(
6968
'logstash-*',
7069
(cfg: any) => cfg,
7170
'time',
7271
stubbedLogstashFields(),
73-
coreMock.createStart()
72+
coreMock.createSetup()
7473
);
7574

75+
// @ts-expect-error _.each() is passing additional args to flattenHit
7676
const hits = _.each(_.cloneDeep(realHits), indexPattern.flattenHit) as Array<
7777
Record<string, unknown>
7878
>;

src/plugins/discover/public/application/components/sidebar/lib/field_calculator.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,23 @@ import _ from 'lodash';
2121
// @ts-ignore
2222
import realHits from 'fixtures/real_hits.js';
2323
// @ts-ignore
24-
import StubIndexPattern from 'test_utils/stub_index_pattern';
25-
// @ts-ignore
2624
import stubbedLogstashFields from 'fixtures/logstash_fields';
2725
import { coreMock } from '../../../../../../../core/public/mocks';
2826
import { IndexPattern } from '../../../../../../data/public';
27+
import { getStubIndexPattern } from '../../../../../../data/public/test_utils';
2928
// @ts-ignore
3029
import { fieldCalculator } from './field_calculator';
3130

3231
let indexPattern: IndexPattern;
3332

3433
describe('fieldCalculator', function () {
3534
beforeEach(function () {
36-
indexPattern = new StubIndexPattern(
35+
indexPattern = getStubIndexPattern(
3736
'logstash-*',
3837
(cfg: any) => cfg,
3938
'time',
4039
stubbedLogstashFields(),
41-
coreMock.createStart()
40+
coreMock.createSetup()
4241
);
4342
});
4443
it('should have a _countMissing that counts nulls & undefineds in an array', function () {

src/plugins/saved_objects/public/saved_object/saved_object.test.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ import {
2626
SavedObjectSaveOpts,
2727
} from '../types';
2828

29-
// @ts-ignore
30-
import StubIndexPattern from 'test_utils/stub_index_pattern';
3129
import { coreMock } from '../../../../core/public/mocks';
3230
import { dataPluginMock, createSearchSourceMock } from '../../../../plugins/data/public/mocks';
31+
import { getStubIndexPattern, StubIndexPattern } from '../../../../plugins/data/public/test_utils';
3332
import { SavedObjectAttributes, SimpleSavedObject } from 'kibana/public';
3433
import { IIndexPattern } from '../../../data/common/index_patterns';
3534

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

297-
const indexPattern = new StubIndexPattern(
296+
const indexPattern = getStubIndexPattern(
298297
'my-index',
299298
getConfig,
300299
null,
301300
[],
302301
coreMock.createSetup()
303302
);
304-
indexPattern.title = indexPattern.id;
303+
indexPattern.title = indexPattern.id!;
305304
savedObject.searchSource!.setField('index', indexPattern);
306305
return savedObject.save(saveOptionsMock).then(() => {
307306
const args = (savedObjectsClientStub.create as jest.Mock).mock.calls[0];
@@ -335,7 +334,7 @@ describe('Saved Object', () => {
335334
type: 'dashboard',
336335
} as SimpleSavedObject<SavedObjectAttributes>);
337336

338-
const indexPattern = new StubIndexPattern(
337+
const indexPattern = getStubIndexPattern(
339338
'non-existant-index',
340339
getConfig,
341340
null,
@@ -662,14 +661,14 @@ describe('Saved Object', () => {
662661

663662
const savedObject = new SavedObjectClass(config);
664663
savedObject.hydrateIndexPattern = jest.fn().mockImplementation(() => {
665-
const indexPattern = new StubIndexPattern(
664+
const indexPattern = getStubIndexPattern(
666665
indexPatternId,
667666
getConfig,
668667
null,
669668
[],
670669
coreMock.createSetup()
671670
);
672-
indexPattern.title = indexPattern.id;
671+
indexPattern.title = indexPattern.id!;
673672
savedObject.searchSource!.setField('index', indexPattern);
674673
return Bluebird.resolve(indexPattern);
675674
});

src/plugins/vis_type_table/public/table_vis_controller.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import 'angular-mocks';
2222
import 'angular-sanitize';
2323
import $ from 'jquery';
2424

25-
// @ts-ignore
26-
import StubIndexPattern from 'test_utils/stub_index_pattern';
2725
import { getAngularModule } from './get_inner_angular';
2826
import { initTableVisLegacyModule } from './table_vis_legacy_module';
2927
import { getTableVisTypeDefinition } from './table_vis_type';
@@ -32,6 +30,7 @@ import { stubFields } from '../../data/public/stubs';
3230
import { tableVisResponseHandler } from './table_vis_response_handler';
3331
import { coreMock } from '../../../core/public/mocks';
3432
import { IAggConfig, search } from '../../data/public';
33+
import { getStubIndexPattern } from '../../data/public/test_utils';
3534
// TODO: remove linting disable
3635
import { searchServiceMock } from '../../data/public/search/mocks';
3736

@@ -105,7 +104,7 @@ describe('Table Vis - Controller', () => {
105104
);
106105

107106
beforeEach(() => {
108-
stubIndexPattern = new StubIndexPattern(
107+
stubIndexPattern = getStubIndexPattern(
109108
'logstash-*',
110109
(cfg: any) => cfg,
111110
'time',

0 commit comments

Comments
 (0)