From 21b011e8b15937e8dccd0a0122f78f1c71b55eb0 Mon Sep 17 00:00:00 2001 From: Maja Grubic Date: Sun, 7 Feb 2021 20:21:35 +0000 Subject: [PATCH] Add unit test --- .../index_pattern_with_source_filter.ts | 83 +++++++++++++++++++ .../helpers/get_field_list.test.ts | 47 +++++++++++ 2 files changed, 130 insertions(+) create mode 100644 src/plugins/discover/public/__mocks__/index_pattern_with_source_filter.ts create mode 100644 src/plugins/discover/public/application/helpers/get_field_list.test.ts diff --git a/src/plugins/discover/public/__mocks__/index_pattern_with_source_filter.ts b/src/plugins/discover/public/__mocks__/index_pattern_with_source_filter.ts new file mode 100644 index 0000000000000..d87c140e852f9 --- /dev/null +++ b/src/plugins/discover/public/__mocks__/index_pattern_with_source_filter.ts @@ -0,0 +1,83 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { IIndexPatternFieldList } from '../../../data/common/index_patterns/fields'; +import { IndexPattern } from '../../../data/common'; +import { indexPatterns } from '../../../data/public'; + +const fields = [ + { + name: '_source', + type: '_source', + scripted: false, + filterable: false, + aggregatable: false, + }, + { + name: '_index', + type: 'string', + scripted: false, + filterable: true, + aggregatable: false, + }, + { + name: 'message', + type: 'string', + scripted: false, + filterable: false, + aggregatable: false, + }, + { + name: 'extension', + type: 'string', + scripted: false, + filterable: true, + aggregatable: true, + }, + { + name: 'bytes', + type: 'number', + scripted: false, + filterable: true, + aggregatable: true, + }, + { + name: 'scripted', + type: 'number', + scripted: true, + filterable: false, + }, +] as IIndexPatternFieldList; + +fields.getByName = (name: string) => { + return fields.find((field) => field.name === name); +}; + +const indexPattern = ({ + id: 'the-index-pattern-id', + title: 'the-index-pattern-title', + metaFields: ['_index', '_score'], + formatField: jest.fn(), + flattenHit: undefined, + formatHit: jest.fn((hit) => hit._source), + fields, + sourceFilters: [{ value: 'bytes' }], + getComputedFields: () => ({ docvalueFields: [], scriptFields: {}, storedFields: ['*'] }), + getSourceFiltering: () => ({}), + getFieldByName: () => ({}), + timeFieldName: '', + docvalueFields: [], +} as unknown) as IndexPattern; + +indexPattern.flattenHit = indexPatterns.flattenHitWrapper(indexPattern, indexPattern.metaFields); +indexPattern.isTimeBased = () => !!indexPattern.timeFieldName; +indexPattern.formatField = (hit: Record, fieldName: string) => { + return fieldName === '_source' ? hit._source : indexPattern.flattenHit(hit)[fieldName]; +}; + +export const indexPatternWithSourceFilterMock = indexPattern; diff --git a/src/plugins/discover/public/application/helpers/get_field_list.test.ts b/src/plugins/discover/public/application/helpers/get_field_list.test.ts new file mode 100644 index 0000000000000..cbf5579a8ab0f --- /dev/null +++ b/src/plugins/discover/public/application/helpers/get_field_list.test.ts @@ -0,0 +1,47 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { indexPatternMock } from '../../__mocks__/index_pattern'; +import { getFieldListFromIndexPattern } from './get_field_list'; +import { indexPatternWithSourceFilterMock } from '../../__mocks__/index_pattern_with_source_filter'; + +describe('get field list test', () => { + describe('no source filters', () => { + test('returns all fields', async () => { + expect(getFieldListFromIndexPattern(indexPatternMock, false)).toEqual([{ field: '*' }]); + }); + + test('returns all fields with unmapped fields', async () => { + expect(getFieldListFromIndexPattern(indexPatternMock, true)).toEqual([ + { field: '*', include_unmapped: 'true' }, + ]); + }); + }); + + describe('with source filters', () => { + test('returns all fields', async () => { + const fields = [ + { field: '_index' }, + { field: 'message' }, + { field: 'extension' }, + { field: 'scripted' }, + ]; + expect(getFieldListFromIndexPattern(indexPatternWithSourceFilterMock, false)).toEqual(fields); + }); + + test('returns all fields with unmapped fields', async () => { + const fields = [ + { field: '_index', include_unmapped: 'true' }, + { field: 'message', include_unmapped: 'true' }, + { field: 'extension', include_unmapped: 'true' }, + { field: 'scripted', include_unmapped: 'true' }, + ]; + expect(getFieldListFromIndexPattern(indexPatternWithSourceFilterMock, true)).toEqual(fields); + }); + }); +});