Skip to content

Commit

Permalink
[Discover] Catch potental missing meta property of filter (#129183)
Browse files Browse the repository at this point in the history
* Catching a potential error caused by a broken filter object without meta property
  • Loading branch information
kertal authored Apr 11, 2022
1 parent 688f0ab commit 73fb70b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { useDiscoverServices } from '../../../../utils/use_discover_services';
import { DiscoverNoResults } from '../no_results';
import { LoadingSpinner } from '../loading_spinner/loading_spinner';
import { generateFilters } from '../../../../../../data/public';
import { DataViewField } from '../../../../../../data_views/public';
import { DataView, DataViewField, DataViewType } from '../../../../../../data_views/public';
import { DiscoverSidebarResponsive } from '../sidebar';
import { DiscoverLayoutProps } from './types';
import { SEARCH_FIELDS_FROM_SOURCE, SHOW_FIELD_STATISTICS } from '../../../../../common';
Expand All @@ -48,7 +48,7 @@ import {
import { FieldStatisticsTable } from '../field_stats_table';
import { VIEW_MODE } from '../../../../components/view_mode_toggle';
import { DOCUMENTS_VIEW_CLICK, FIELD_STATISTICS_VIEW_CLICK } from '../field_stats_table/constants';
import { DataViewType, DataView } from '../../../../../../data_views/public';
import { hasActiveFilter } from './utils';

/**
* Local storage key for sidebar persistence state
Expand Down Expand Up @@ -293,9 +293,7 @@ export function DiscoverLayout({
data={data}
error={dataState.error}
hasQuery={!!state.query?.query}
hasFilters={
state.filters && state.filters.filter((f) => !f.meta.disabled).length > 0
}
hasFilters={hasActiveFilter(state.filters)}
onDisableFilters={onDisableFilters}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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 { Filter } from '@kbn/es-query';
import { hasActiveFilter } from './utils';

const testFilter: Filter = {
meta: {
alias: null,
disabled: false,
negate: false,
},
query: { query: 'hi' },
};
const testFilterDisabled: Filter = {
meta: {
alias: null,
disabled: true,
negate: false,
},
query: { query: 'hi' },
};

const testFilterBroken = {} as Filter;

describe('hasActiveFilter', () => {
test('only active filters', () => {
const filters = [testFilter];
const result = hasActiveFilter(filters);
expect(result).toBe(true);
});
test('only disabled filters', () => {
const filters = [testFilterDisabled];
const result = hasActiveFilter(filters);
expect(result).toBe(false);
});
test('disabled and active filters', () => {
const filters = [testFilter, testFilterDisabled];
const result = hasActiveFilter(filters);
expect(result).toBe(true);
});
test('broken filter - edge case', () => {
const filters = [testFilterBroken];
const result = hasActiveFilter(filters);
expect(result).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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 { Filter } from '@kbn/es-query';

/**
* Returns if true there's at least 1 active filter
*/
export function hasActiveFilter(filters: Filter[] | undefined) {
return filters && filters.filter((f) => !f.meta?.disabled).length > 0;
}

0 comments on commit 73fb70b

Please sign in to comment.