diff --git a/src/plugins/data_views/common/data_views/data_views.test.ts b/src/plugins/data_views/common/data_views/data_views.test.ts index 01e2b9b1d95f3..e5e9b2d83faf7 100644 --- a/src/plugins/data_views/common/data_views/data_views.test.ts +++ b/src/plugins/data_views/common/data_views/data_views.test.ts @@ -19,10 +19,11 @@ import { IDataViewsApiClient, } from '../types'; import { stubbedSavedObjectIndexPattern } from '../data_view.stub'; +import { DataViewMissingIndices } from '../lib'; const createFieldsFetcher = () => ({ - getFieldsForWildcard: jest.fn(async () => ({ fields: [], indices: [] })), + getFieldsForWildcard: jest.fn(async () => ({ fields: [], indices: ['test'] })), } as any as IDataViewsApiClient); const fieldFormats = fieldFormatsMock; @@ -71,6 +72,7 @@ describe('IndexPatterns', () => { const indexPatternObj = { id: 'id', version: 'a', attributes: { title: 'title' } }; beforeEach(() => { + jest.clearAllMocks(); savedObjectsClient = {} as SavedObjectsClientCommon; savedObjectsClient.find = jest.fn( () => Promise.resolve([indexPatternObj]) as Promise>> @@ -220,6 +222,35 @@ describe('IndexPatterns', () => { expect((await indexPatterns.get(id)).fields.length).toBe(1); }); + test('existing indices, so dataView.matchedIndices.length equals 1 ', async () => { + const id = '1'; + setDocsourcePayload(id, { + id: 'foo', + version: 'foo', + attributes: { + title: 'something', + }, + }); + const dataView = await indexPatterns.get(id); + expect(dataView.matchedIndices.length).toBe(1); + }); + + test('missing indices, so dataView.matchedIndices.length equals 0 ', async () => { + const id = '1'; + setDocsourcePayload(id, { + id: 'foo', + version: 'foo', + attributes: { + title: 'something', + }, + }); + apiClient.getFieldsForWildcard = jest.fn().mockImplementation(async () => { + throw new DataViewMissingIndices('Catch me if you can!'); + }); + const dataView = await indexPatterns.get(id); + expect(dataView.matchedIndices.length).toBe(0); + }); + test('savedObjectCache pre-fetches title, type, typeMeta', async () => { expect(await indexPatterns.getIds()).toEqual(['id']); expect(savedObjectsClient.find).toHaveBeenCalledWith({ diff --git a/src/plugins/data_views/common/data_views/data_views.ts b/src/plugins/data_views/common/data_views/data_views.ts index 13fbb9cec243e..afccfdcee79fc 100644 --- a/src/plugins/data_views/common/data_views/data_views.ts +++ b/src/plugins/data_views/common/data_views/data_views.ts @@ -569,8 +569,8 @@ export class DataViewsService { }; /** - * Refresh field list for a given index pattern. - * @param indexPattern + * Refresh field list for a given data view. + * @param dataView * @param displayErrors - If set false, API consumer is responsible for displaying and handling errors. */ refreshFields = async (dataView: DataView, displayErrors: boolean = true) => { @@ -582,22 +582,19 @@ export class DataViewsService { await this.refreshFieldsFn(dataView); } catch (err) { if (err instanceof DataViewMissingIndices) { - this.onNotification( - { title: err.message, color: 'danger', iconType: 'error' }, - `refreshFields:${dataView.getIndexPattern()}` + // not considered an error, check dataView.matchedIndices.length to be 0 + } else { + this.onError( + err, + { + title: i18n.translate('dataViews.fetchFieldErrorTitle', { + defaultMessage: 'Error fetching fields for data view {title} (ID: {id})', + values: { id: dataView.id, title: dataView.getIndexPattern() }, + }), + }, + dataView.getIndexPattern() ); } - - this.onError( - err, - { - title: i18n.translate('dataViews.fetchFieldErrorTitle', { - defaultMessage: 'Error fetching fields for data view {title} (ID: {id})', - values: { id: dataView.id, title: dataView.getIndexPattern() }, - }), - }, - dataView.getIndexPattern() - ); } }; @@ -635,14 +632,12 @@ export class DataViewsService { return { fields: this.fieldArrayToMap(updatedFieldList, fieldAttrs), indices }; } catch (err) { if (err instanceof DataViewMissingIndices) { - if (displayErrors) { - this.onNotification( - { title: err.message, color: 'danger', iconType: 'error' }, - `refreshFieldSpecMap:${title}` - ); - } + // not considered an error, check dataView.matchedIndices.length to be 0 return {}; } + if (!displayErrors) { + throw err; + } this.onError( err, @@ -798,19 +793,13 @@ export class DataViewsService { const fieldsAndIndices = await this.initFromSavedObjectLoadFields({ savedObjectId: savedObject.id, spec, + displayErrors, }); fields = fieldsAndIndices.fields; indices = fieldsAndIndices.indices; } catch (err) { if (err instanceof DataViewMissingIndices) { - this.onNotification( - { - title: err.message, - color: 'danger', - iconType: 'error', - }, - `initFromSavedObject:${spec.title}` - ); + // not considered an error, check dataView.matchedIndices.length to be 0 } else { this.onError( err,