[Security Solution][Exceptions] - Fix empty selection showing for exception item field selection (#155221)

## Summary

Addresses https://github.com/elastic/kibana/issues/145540

- blank space no longer shows as selection option for exception item
field dropdown
This commit is contained in:
Yara Tercero 2023-04-20 11:24:26 -07:00 committed by GitHub
parent ada91f9a5e
commit afced9d47f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 3 deletions

View file

@ -84,7 +84,56 @@ describe('useField', () => {
expect(comboOptions).toEqual([{ label: 'bytes' }, { label: 'ssl' }, { label: '@timestamp' }]);
expect(selectedComboOptions).toEqual([]);
});
it('should not return an empty field as a combo option', () => {
it('should not return a selected field when empty string as a combo option', () => {
const newIndexPattern = {
...indexPattern,
fields: [
{
name: 'bytes',
type: 'number',
esTypes: ['long'],
count: 10,
scripted: false,
searchable: true,
aggregatable: true,
readFromDocValues: true,
},
{
name: 'ssl',
type: 'boolean',
esTypes: ['boolean'],
count: 20,
scripted: false,
searchable: true,
aggregatable: true,
readFromDocValues: true,
},
{
name: '@timestamp',
type: 'date',
esTypes: ['date'],
count: 30,
scripted: false,
searchable: true,
aggregatable: true,
readFromDocValues: true,
},
] as unknown as DataViewFieldBase[],
title: 'title1',
};
const { result } = renderHook(() =>
useField({
indexPattern: newIndexPattern,
onChange: onChangeMock,
selectedField: { name: '', type: 'keyword' },
})
);
const { comboOptions, selectedComboOptions } = result.current;
expect(comboOptions).toEqual([{ label: 'bytes' }, { label: 'ssl' }, { label: '@timestamp' }]);
expect(selectedComboOptions).toEqual([]);
});
it('should not return a selected field when string with spaces is written as a combo option', () => {
const newIndexPattern = {
...indexPattern,
fields: [

View file

@ -26,14 +26,13 @@ import {
GetFieldComboBoxPropsReturn,
} from './types';
import { disabledTypesWithTooltipText } from './disabled_types_with_tooltip_text';
import { paramContainsSpace } from '../param_contains_space';
const getExistingFields = (indexPattern: DataViewBase | undefined): DataViewFieldBase[] => {
return indexPattern != null ? indexPattern.fields : [];
};
const getSelectedFields = (selectedField: DataViewField | undefined): DataViewFieldBase[] => {
return selectedField && !paramContainsSpace(selectedField.name) ? [selectedField] : [];
return selectedField && selectedField.name.trim() !== '' ? [selectedField] : [];
};
const getAvailableFields = (