-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Discover] Catch potental missing meta property of filter (#129183)
* Catching a potential error caused by a broken filter object without meta property
- Loading branch information
Showing
3 changed files
with
69 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/plugins/discover/public/application/main/components/layout/utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
15 changes: 15 additions & 0 deletions
15
src/plugins/discover/public/application/main/components/layout/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |