Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
angorayc committed May 11, 2022
1 parent b9fc79a commit 42ab30e
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* 2.0.
*/

import { showInitialLoadingSpinner } from './helpers';
import { formatAlertsData, showInitialLoadingSpinner } from './helpers';
import { result, textResult, stackedByBooleanField, stackedByTextField } from './mock_data';

describe('helpers', () => {
describe('showInitialLoadingSpinner', () => {
Expand Down Expand Up @@ -34,3 +35,15 @@ describe('helpers', () => {
});
});
});

describe('formatAlertsData', () => {
test('stack by a boolean field', () => {
const res = formatAlertsData(stackedByBooleanField);
expect(res).toEqual(result);
});

test('stack by a text field', () => {
const res = formatAlertsData(stackedByTextField);
expect(res).toEqual(textResult);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
export const stackedByBooleanField = {
took: 1,
timed_out: false,
_shards: { total: 1, successful: 1, skipped: 0, failed: 0 },
hits: {
total: {
value: 3,
relation: 'eq',
},
hits: [],
},
timeout: false,
aggregations: {
alertsByGrouping: {
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [
{
key: 1,
key_as_string: 'true',
doc_count: 2683,
alerts: {
buckets: [
{ key_as_string: '2022-05-10T15:34:48.075Z', key: 1652196888075, doc_count: 0 },
{ key_as_string: '2022-05-10T16:19:48.074Z', key: 1652199588074, doc_count: 0 },
{ key_as_string: '2022-05-10T17:04:48.073Z', key: 1652202288073, doc_count: 0 },
],
},
},
],
},
},
};

export const result = [
{ x: 1652196888075, y: 0, g: 'true' },
{ x: 1652199588074, y: 0, g: 'true' },
{ x: 1652202288073, y: 0, g: 'true' },
];

export const stackedByTextField = {
took: 1,
timeout: false,
_shards: { total: 1, successful: 1, skipped: 0, failed: 0 },
hits: {
total: {
value: 3,
relation: 'eq',
},
hits: [],
},
aggregations: {
alertsByGrouping: {
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [
{
key: 'MacBook-Pro.local',
doc_count: 2706,
alerts: {
buckets: [
{ key_as_string: '2022-05-10T15:34:48.075Z', key: 1652196888075, doc_count: 0 },
{ key_as_string: '2022-05-10T16:19:48.074Z', key: 1652199588074, doc_count: 0 },
{ key_as_string: '2022-05-10T17:04:48.073Z', key: 1652202288073, doc_count: 0 },
],
},
},
],
},
},
};

export const textResult = [
{ x: 1652196888075, y: 0, g: 'MacBook-Pro.local' },
{ x: 1652199588074, y: 0, g: 'MacBook-Pro.local' },
{ x: 1652202288073, y: 0, g: 'MacBook-Pro.local' },
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { MatrixHistogramType } from '../../../../../common/search_strategy';
import { getGenericData } from './helpers';
import { stackedByBooleanField, stackedByTextField, result, textResult } from './mock_data';

describe('getGenericData', () => {
test('stack by a boolean field', () => {
const res = getGenericData<MatrixHistogramType.events>(stackedByBooleanField, 'events.buckets');
expect(res).toEqual(result);
});

test('stack by a text field', () => {
const res = getGenericData<MatrixHistogramType.events>(stackedByTextField, 'events.buckets');
expect(res).toEqual(textResult);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export const stackedByBooleanField = [
{
key: 1,
key_as_string: 'true',
doc_count: 7125,
events: {
buckets: [
{ key_as_string: '2022-05-10T15:34:48.075Z', key: 1652196888075, doc_count: 0 },
{ key_as_string: '2022-05-10T16:19:48.074Z', key: 1652199588074, doc_count: 774 },
{ key_as_string: '2022-05-10T17:04:48.073Z', key: 1652202288073, doc_count: 415 },
],
},
},
];
export const result = [
{ x: 1652196888075, y: 0, g: 'true' },
{ x: 1652199588074, y: 774, g: 'true' },
{ x: 1652202288073, y: 415, g: 'true' },
];

export const stackedByTextField = [
{
key: 'MacBook-Pro.local',
doc_count: 7103,
events: {
buckets: [
{ key_as_string: '2022-05-10T15:34:48.075Z', key: 1652196888075, doc_count: 0 },
{ key_as_string: '2022-05-10T16:19:48.074Z', key: 1652199588074, doc_count: 774 },
{ key_as_string: '2022-05-10T17:04:48.073Z', key: 1652202288073, doc_count: 415 },
],
},
},
];

export const textResult = [
{ x: 1652196888075, y: 0, g: 'MacBook-Pro.local' },
{ x: 1652199588074, y: 774, g: 'MacBook-Pro.local' },
{ x: 1652202288073, y: 415, g: 'MacBook-Pro.local' },
];

0 comments on commit 42ab30e

Please sign in to comment.