Skip to content

Commit

Permalink
[Security Solution][Platform] - Bug fix when loading a saved query in…
Browse files Browse the repository at this point in the history
… detections (#114347) (#114668)

Bug in checking === for null when possibility of undefined, added tests.

Co-authored-by: Yara Tercero <[email protected]>
  • Loading branch information
kibanamachine and yctercero authored Oct 16, 2021
1 parent a85e1cd commit 3cb89f1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*/

import { navTabs } from '../../../app/home/home_navigations';
import { getTitle } from './helpers';
import { getTitle, isQueryStateEmpty } from './helpers';
import { CONSTANTS } from './constants';

describe('Helpers Url_State', () => {
describe('getTitle', () => {
Expand All @@ -31,4 +32,58 @@ describe('Helpers Url_State', () => {
expect(result).toEqual('');
});
});

describe('isQueryStateEmpty', () => {
test('returns true if queryState is undefined', () => {
const result = isQueryStateEmpty(undefined, CONSTANTS.savedQuery);
expect(result).toBeTruthy();
});

test('returns true if queryState is null', () => {
const result = isQueryStateEmpty(null, CONSTANTS.savedQuery);
expect(result).toBeTruthy();
});

test('returns true if url key is "query" and queryState is empty string', () => {
const result = isQueryStateEmpty({}, CONSTANTS.appQuery);
expect(result).toBeTruthy();
});

test('returns false if url key is "query" and queryState is not empty', () => {
const result = isQueryStateEmpty(
{ query: { query: '*:*' }, language: 'kuery' },
CONSTANTS.appQuery
);
expect(result).toBeFalsy();
});

test('returns true if url key is "filters" and queryState is empty', () => {
const result = isQueryStateEmpty([], CONSTANTS.filters);
expect(result).toBeTruthy();
});

test('returns false if url key is "filters" and queryState is not empty', () => {
const result = isQueryStateEmpty(
[{ query: { query: '*:*' }, meta: { key: '123' } }],
CONSTANTS.filters
);
expect(result).toBeFalsy();
});

// TODO: Is this a bug, or intended?
test('returns false if url key is "timeline" and queryState is empty', () => {
const result = isQueryStateEmpty({}, CONSTANTS.timeline);
expect(result).toBeFalsy();
});

test('returns true if url key is "timeline" and queryState id is empty string', () => {
const result = isQueryStateEmpty({ id: '', isOpen: true }, CONSTANTS.timeline);
expect(result).toBeTruthy();
});

test('returns false if url key is "timeline" and queryState is not empty', () => {
const result = isQueryStateEmpty({ id: '123', isOpen: true }, CONSTANTS.timeline);
expect(result).toBeFalsy();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,11 @@ export const updateTimerangeUrl = (
return timeRange;
};

export const isQueryStateEmpty = (queryState: ValueUrlState | null, urlKey: KeyUrlState) =>
queryState === null ||
export const isQueryStateEmpty = (
queryState: ValueUrlState | undefined | null,
urlKey: KeyUrlState
): boolean =>
queryState == null ||
(urlKey === CONSTANTS.appQuery && isEmpty((queryState as Query).query)) ||
(urlKey === CONSTANTS.filters && isEmpty(queryState)) ||
(urlKey === CONSTANTS.timeline && (queryState as TimelineUrl).id === '');
Expand Down

0 comments on commit 3cb89f1

Please sign in to comment.