Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Maja Grubic committed Feb 7, 2021
1 parent f7c845e commit 21b011e
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<string, any>, fieldName: string) => {
return fieldName === '_source' ? hit._source : indexPattern.flattenHit(hit)[fieldName];
};

export const indexPatternWithSourceFilterMock = indexPattern;
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit 21b011e

Please sign in to comment.