Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sulemanof committed Jan 30, 2020
1 parent 436b3e7 commit 81ce24a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,9 @@
* under the License.
*/

import { IndexPattern, Field } from 'src/plugins/data/public';
import { IndexPattern } from 'src/plugins/data/public';
import { VisState } from 'src/legacy/core_plugins/visualizations/public';
import {
AggConfig,
AggType,
AggGroupNames,
BUCKET_TYPES,
IndexedArray,
EditorConfig,
} from '../legacy_imports';
import { AggConfig, AggType, AggGroupNames, BUCKET_TYPES, EditorConfig } from '../legacy_imports';
import {
getAggParamsToRender,
getAggTypeOptions,
Expand Down Expand Up @@ -111,8 +104,10 @@ describe('DefaultEditorAggParams helpers', () => {
name: 'field',
type: 'field',
filterFieldTypes,
getAvailableFields: jest.fn((fields: IndexedArray<Field>) =>
fields.filter(({ type }) => filterFieldTypes.includes(type))
getAvailableFields: jest.fn((aggConfig: AggConfig) =>
aggConfig
.getIndexPattern()
.fields.filter(({ type }) => filterFieldTypes.includes(type))
),
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export { AggParamOption } from 'ui/agg_types/agg_params';
export { CidrMask } from 'ui/agg_types/buckets/lib/cidr_mask';

export { PersistedState } from 'ui/persisted_state';
export { IndexedArray } from 'ui/indexed_array';
export { getDocLink } from 'ui/documentation_links';
export { documentationLinks } from 'ui/documentation_links/documentation_links';
export { move } from 'ui/utils/collection';
Expand Down
43 changes: 43 additions & 0 deletions src/legacy/ui/public/agg_types/param_types/field.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
* under the License.
*/

import { get } from 'lodash';
import { BaseParamType } from './base';
import { FieldParamType } from './field';
import { ES_FIELD_TYPES, KBN_FIELD_TYPES } from '../../../../../plugins/data/public';
import { AggConfig } from '../agg_config';
import { IMetricAggConfig } from '../metrics/metric_agg_type';
import { Schema } from '../schemas';

jest.mock('ui/new_platform');

Expand Down Expand Up @@ -98,6 +101,46 @@ describe('Field', () => {
type: 'field',
});

indexPattern.fields[1].aggregatable = true;

const fields = aggParam.getAvailableFields(agg);

expect(fields.length).toBe(2);
});

it('should return only numeric fields if filterFieldTypes was specified as a function', () => {
const aggParam = new FieldParamType({
name: 'field',
type: 'field',
filterFieldTypes: (aggConfig: IMetricAggConfig) =>
get(aggConfig.schema, 'aggSettings.top_hits.allowStrings', false)
? '*'
: KBN_FIELD_TYPES.NUMBER,
});
const fields = aggParam.getAvailableFields(agg);

expect(fields.length).toBe(1);
expect(fields[0].type).toBe(KBN_FIELD_TYPES.NUMBER);
});

it('should return all fields if filterFieldTypes was specified as a function and aggSettings allow string type fields', () => {
const aggParam = new FieldParamType({
name: 'field',
type: 'field',
filterFieldTypes: (aggConfig: IMetricAggConfig) =>
get(aggConfig.schema, 'aggSettings.top_hits.allowStrings', false)
? '*'
: KBN_FIELD_TYPES.NUMBER,
});

agg.schema = {
aggSettings: {
top_hits: {
allowStrings: true,
},
},
} as Schema;

const fields = aggParam.getAvailableFields(agg);

expect(fields.length).toBe(2);
Expand Down
7 changes: 1 addition & 6 deletions src/legacy/ui/public/agg_types/param_types/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import { SavedObjectNotFound } from '../../../../../plugins/kibana_utils/public'
import { BaseParamType } from './base';
import { toastNotifications } from '../../notify';
import { propFilter } from '../filter';
import { Field, IFieldList, KBN_FIELD_TYPES } from '../../../../../plugins/data/public';
import { isNestedField } from '../../../../../plugins/data/public';
import { Field, KBN_FIELD_TYPES, isNestedField } from '../../../../../plugins/data/public';
import { IMetricAggConfig } from '../metrics/metric_agg_type';

const filterByType = propFilter('type');
Expand Down Expand Up @@ -124,10 +123,6 @@ export class FieldParamType extends BaseParamType {
return false;
}

if (!filterFieldTypes) {
return true;
}

if (isFunction(filterFieldTypes)) {
const filter = filterFieldTypes(aggConfig as IMetricAggConfig);

Expand Down

0 comments on commit 81ce24a

Please sign in to comment.