|
| 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 | +} |
0 commit comments