Skip to content

Commit

Permalink
[Discover] Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jughosta committed Nov 2, 2022
1 parent 3c215c5 commit 616d155
Showing 1 changed file with 74 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
* Side Public License, v 1.
*/

import { groupFields } from './group_fields';
import { getDefaultFieldFilter } from './field_filter';
import { getSelectedFields } from './group_fields';
import { DataViewField } from '@kbn/data-views-plugin/public';

const fields = [
Expand Down Expand Up @@ -44,58 +43,26 @@ const fields = [
];

describe('group_fields', function () {
it('should group fields in selected, popular, unpopular group', function () {
const fieldFilterState = getDefaultFieldFilter();

const actual = groupFields(fields as DataViewField[], ['currency'], 5, fieldFilterState, false);
it('should pick fields into selected group', function () {
const actual = getSelectedFields(fields as DataViewField[], ['currency']);
expect(actual).toMatchInlineSnapshot(`
Object {
"popular": Array [
Object {
"aggregatable": true,
"count": 1,
"esTypes": Array [
"text",
],
"name": "category",
"readFromDocValues": true,
"scripted": false,
"searchable": true,
"type": "string",
},
],
"selected": Array [
Object {
"aggregatable": true,
"count": 0,
"esTypes": Array [
"keyword",
],
"name": "currency",
"readFromDocValues": true,
"scripted": false,
"searchable": true,
"type": "string",
},
],
"unpopular": Array [
Object {
"aggregatable": true,
"count": 0,
"esTypes": Array [
"date",
],
"name": "customer_birth_date",
"readFromDocValues": true,
"scripted": false,
"searchable": true,
"type": "date",
},
],
}
Array [
Object {
"aggregatable": true,
"count": 0,
"esTypes": Array [
"keyword",
],
"name": "currency",
"readFromDocValues": true,
"scripted": false,
"searchable": true,
"type": "string",
},
]
`);
});
it('should group fields in selected, popular, unpopular group if they contain multifields', function () {
it('should pick fields into selected group if they contain multifields', function () {
const category = {
name: 'category',
type: 'string',
Expand Down Expand Up @@ -155,103 +122,75 @@ describe('group_fields', function () {
};
const fieldsToGroup = [category, currency, currencyKeyword] as DataViewField[];

const fieldFilterState = getDefaultFieldFilter();

const actual = groupFields(fieldsToGroup, ['currency'], 5, fieldFilterState, true);
const actual = getSelectedFields(fieldsToGroup, ['currency', 'currency.keyword']);

expect(actual.popular).toEqual([category]);
expect(actual.selected).toEqual([currency]);
expect(actual.unpopular).toEqual([]);
expect(actual).toEqual([currency, currencyKeyword]);
});

it('should sort selected fields by columns order ', function () {
const fieldFilterState = getDefaultFieldFilter();

const actual1 = groupFields(
fields as DataViewField[],
['customer_birth_date', 'currency', 'unknown'],
5,
fieldFilterState,
false
);
expect(actual1.selected.map((field) => field.name)).toEqual([
const actual1 = getSelectedFields(fields as DataViewField[], [
'customer_birth_date',
'currency',
'unknown',
]);
expect(actual1.map((field) => field.name)).toEqual([
'customer_birth_date',
'currency',
'unknown',
]);

const actual2 = groupFields(
fields as DataViewField[],
['currency', 'customer_birth_date', 'unknown'],
5,
fieldFilterState,
false
);
expect(actual2.selected.map((field) => field.name)).toEqual([
const actual2 = getSelectedFields(fields as DataViewField[], [
'currency',
'customer_birth_date',
'unknown',
]);
expect(actual2.map((field) => field.name)).toEqual([
'currency',
'customer_birth_date',
'unknown',
]);
});

it('should filter fields by a given name', function () {
const fieldFilterState = { ...getDefaultFieldFilter(), ...{ name: 'curr' } };

const actual1 = groupFields(
fields as DataViewField[],
['customer_birth_date', 'currency', 'unknown'],
5,
fieldFilterState,
false
);
expect(actual1.selected.map((field) => field.name)).toEqual(['currency']);
});

it('excludes unmapped fields if showUnmappedFields set to false', function () {
const fieldFilterState = getDefaultFieldFilter();
const fieldsWithUnmappedField = [...fields];
fieldsWithUnmappedField.push({
name: 'unknown_field',
type: 'unknown',
esTypes: ['unknown'],
count: 1,
scripted: false,
searchable: true,
aggregatable: true,
readFromDocValues: true,
});

const actual = groupFields(
fieldsWithUnmappedField as DataViewField[],
['customer_birth_date', 'currency'],
5,
fieldFilterState,
true
);
expect(actual.unpopular).toEqual([]);
});

it('includes unmapped fields when reading from source', function () {
const fieldFilterState = getDefaultFieldFilter();
const fieldsWithUnmappedField = [...fields];
fieldsWithUnmappedField.push({
name: 'unknown_field',
type: 'unknown',
esTypes: ['unknown'],
count: 0,
scripted: false,
searchable: false,
aggregatable: false,
readFromDocValues: false,
});

const actual = groupFields(
fieldsWithUnmappedField as DataViewField[],
['customer_birth_date', 'currency'],
5,
fieldFilterState,
false
);
expect(actual.unpopular.map((field) => field.name)).toEqual(['unknown_field']);
});
// it('excludes unmapped fields if useNewFieldsApi set to true', function () {
// const fieldsWithUnmappedField = [...fields];
// fieldsWithUnmappedField.push({
// name: 'unknown_field',
// type: 'unknown',
// esTypes: ['unknown'],
// count: 1,
// scripted: false,
// searchable: true,
// aggregatable: true,
// readFromDocValues: true,
// });
//
// expect(
// (fieldsWithUnmappedField as DataViewField[]).filter((field) => shouldShowField(field, true))
// .length
// ).toBe(fieldsWithUnmappedField.length - 1);
// });

// it('includes unmapped fields when reading from source', function () {
// const fieldFilterState = getDefaultFieldFilter();
// const fieldsWithUnmappedField = [...fields];
// fieldsWithUnmappedField.push({
// name: 'unknown_field',
// type: 'unknown',
// esTypes: ['unknown'],
// count: 0,
// scripted: false,
// searchable: false,
// aggregatable: false,
// readFromDocValues: false,
// });
//
// const actual = groupFields(
// fieldsWithUnmappedField as DataViewField[],
// ['customer_birth_date', 'currency'],
// 5,
// fieldFilterState,
// false
// );
// expect(actual.unpopular.map((field) => field.name)).toEqual(['unknown_field']);
// });
});

0 comments on commit 616d155

Please sign in to comment.