Skip to content

Commit

Permalink
[Discover] Fixed tooltips for text and keyword fields displaying 'Unk…
Browse files Browse the repository at this point in the history
…nown field' in expanded document (#133536)

* [Discover] Fixed tooltips for text and keyword fields displaying 'Unknown field'

* [Discover] Added tests for getFieldTypeName function

* [Discover] Fixing issue where i18n defaultMessage was a variable instead of a constant string

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
davismcphee and kibanamachine authored Jun 10, 2022
1 parent 5d32987 commit 3e3ee3e
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import type { DataViewField, DataView } from '@kbn/data-views-plugin/public';
import { getTypeForFieldIcon } from '../../../../utils/get_type_for_field_icon';
import { DiscoverFieldDetails } from './discover_field_details';
import { FieldDetails } from './types';
import { getFieldTypeName } from './lib/get_field_type_name';
import { getFieldTypeName } from '../../../../utils/get_field_type_name';
import { DiscoverFieldVisualize } from './discover_field_visualize';

function wrapOnDot(str?: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { i18n } from '@kbn/i18n';
import { FieldIcon, FieldIconProps } from '@kbn/react-field';
import { getFieldSubtypeMulti } from '@kbn/data-views-plugin/public';
import type { DataViewField } from '@kbn/data-views-plugin/public';
import { getFieldTypeName } from './field_type_name';
import { getFieldTypeName } from '../../utils/get_field_type_name';

interface Props {
fieldName: string;
Expand Down

This file was deleted.

34 changes: 34 additions & 0 deletions src/plugins/discover/public/utils/get_field_type_name.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 {
getFieldTypeName,
KNOWN_FIELD_TYPES,
UNKNOWN_FIELD_TYPE_MESSAGE,
} from './get_field_type_name';

describe('getFieldTypeName', () => {
describe('known field types should be recognized', () => {
it.each(Object.values(KNOWN_FIELD_TYPES))(
`'%s' should return a string that does not match '${UNKNOWN_FIELD_TYPE_MESSAGE}'`,
(field) => {
const fieldTypeName = getFieldTypeName(field);
expect(typeof fieldTypeName).toBe('string');
expect(fieldTypeName).not.toBe(UNKNOWN_FIELD_TYPE_MESSAGE);
}
);
});

it(`should return '${UNKNOWN_FIELD_TYPE_MESSAGE}' when passed undefined`, () => {
expect(getFieldTypeName(undefined)).toBe(UNKNOWN_FIELD_TYPE_MESSAGE);
});

it(`should return '${UNKNOWN_FIELD_TYPE_MESSAGE}' when passed an unknown field type`, () => {
expect(getFieldTypeName('unknown_field_type')).toBe(UNKNOWN_FIELD_TYPE_MESSAGE);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,70 +7,92 @@
*/

import { i18n } from '@kbn/i18n';
import { KBN_FIELD_TYPES, ES_FIELD_TYPES } from '@kbn/data-plugin/public';

export function getFieldTypeName(type: string) {
export const KNOWN_FIELD_TYPES = {
BOOLEAN: KBN_FIELD_TYPES.BOOLEAN,
CONFLICT: KBN_FIELD_TYPES.CONFLICT,
DATE: KBN_FIELD_TYPES.DATE,
GEO_POINT: KBN_FIELD_TYPES.GEO_POINT,
GEO_SHAPE: KBN_FIELD_TYPES.GEO_SHAPE,
IP: KBN_FIELD_TYPES.IP,
KEYWORD: ES_FIELD_TYPES.KEYWORD,
MURMUR3: KBN_FIELD_TYPES.MURMUR3,
NUMBER: KBN_FIELD_TYPES.NUMBER,
NESTED: KBN_FIELD_TYPES.NESTED,
SOURCE: 'source',
STRING: KBN_FIELD_TYPES.STRING,
TEXT: ES_FIELD_TYPES.TEXT,
VERSION: ES_FIELD_TYPES.VERSION,
};

export const UNKNOWN_FIELD_TYPE_MESSAGE = i18n.translate(
'discover.fieldNameIcons.unknownFieldAriaLabel',
{
defaultMessage: 'Unknown field',
}
);

export function getFieldTypeName(type?: string) {
switch (type) {
case 'boolean':
case KNOWN_FIELD_TYPES.BOOLEAN:
return i18n.translate('discover.fieldNameIcons.booleanAriaLabel', {
defaultMessage: 'Boolean field',
});
case 'conflict':
case KNOWN_FIELD_TYPES.CONFLICT:
return i18n.translate('discover.fieldNameIcons.conflictFieldAriaLabel', {
defaultMessage: 'Conflicting field',
});
case 'date':
case KNOWN_FIELD_TYPES.DATE:
return i18n.translate('discover.fieldNameIcons.dateFieldAriaLabel', {
defaultMessage: 'Date field',
});
case 'geo_point':
case KNOWN_FIELD_TYPES.GEO_POINT:
return i18n.translate('discover.fieldNameIcons.geoPointFieldAriaLabel', {
defaultMessage: 'Geo point field',
});
case 'geo_shape':
case KNOWN_FIELD_TYPES.GEO_SHAPE:
return i18n.translate('discover.fieldNameIcons.geoShapeFieldAriaLabel', {
defaultMessage: 'Geo shape field',
});
case 'ip':
case KNOWN_FIELD_TYPES.IP:
return i18n.translate('discover.fieldNameIcons.ipAddressFieldAriaLabel', {
defaultMessage: 'IP address field',
});
case 'murmur3':
case KNOWN_FIELD_TYPES.MURMUR3:
return i18n.translate('discover.fieldNameIcons.murmur3FieldAriaLabel', {
defaultMessage: 'Murmur3 field',
});
case 'number':
case KNOWN_FIELD_TYPES.NUMBER:
return i18n.translate('discover.fieldNameIcons.numberFieldAriaLabel', {
defaultMessage: 'Number field',
});
case 'source':
case KNOWN_FIELD_TYPES.SOURCE:
// Note that this type is currently not provided, type for _source is undefined
return i18n.translate('discover.fieldNameIcons.sourceFieldAriaLabel', {
defaultMessage: 'Source field',
});
case 'string':
case KNOWN_FIELD_TYPES.STRING:
return i18n.translate('discover.fieldNameIcons.stringFieldAriaLabel', {
defaultMessage: 'String field',
});
case 'text':
case KNOWN_FIELD_TYPES.TEXT:
return i18n.translate('discover.fieldNameIcons.textFieldAriaLabel', {
defaultMessage: 'Text field',
});
case 'keyword':
case KNOWN_FIELD_TYPES.KEYWORD:
return i18n.translate('discover.fieldNameIcons.keywordFieldAriaLabel', {
defaultMessage: 'Keyword field',
});

case 'nested':
case KNOWN_FIELD_TYPES.NESTED:
return i18n.translate('discover.fieldNameIcons.nestedFieldAriaLabel', {
defaultMessage: 'Nested field',
});
case 'version':
case KNOWN_FIELD_TYPES.VERSION:
return i18n.translate('discover.fieldNameIcons.versionFieldAriaLabel', {
defaultMessage: 'Version field',
});
default:
return i18n.translate('discover.fieldNameIcons.unknownFieldAriaLabel', {
defaultMessage: 'Unknown field',
});
return UNKNOWN_FIELD_TYPE_MESSAGE;
}
}

0 comments on commit 3e3ee3e

Please sign in to comment.