Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
angorayc committed Dec 22, 2022
1 parent da58b26 commit 153f139
Show file tree
Hide file tree
Showing 11 changed files with 1,209 additions and 20 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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 { renderHook } from '@testing-library/react-hooks';
import { wrapper } from '../../../mocks';

import { useLensAttributes } from '../../../use_lens_attributes';

import { getAlertsHistogramLensAttributes } from './alerts_histogram';

jest.mock('../../../../../containers/sourcerer', () => ({
useSourcererDataView: jest.fn().mockReturnValue({
dataViewId: 'security-solution-my-test',
indicesExist: true,
selectedPatterns: ['signal-index'],
}),
}));

jest.mock('../../../../../utils/route/use_route_spy', () => ({
useRouteSpy: jest.fn().mockReturnValue([
{
detailName: 'mockRule',
pageName: 'rules',
tabName: 'alerts',
},
]),
}));

describe('getAlertsHistogramLensAttributes', () => {
it('should render without extra options', () => {
const { result } = renderHook(
() =>
useLensAttributes({
getLensAttributes: getAlertsHistogramLensAttributes,
stackByField: 'event.category',
}),
{ wrapper }
);

expect(result?.current).toMatchSnapshot();
});

it('should render with extra options - showBuildingBlockAlerts', () => {
const { result } = renderHook(
() =>
useLensAttributes({
extraOptions: { showBuildingBlockAlerts: true },
getLensAttributes: getAlertsHistogramLensAttributes,
stackByField: 'event.category',
}),
{ wrapper }
);

expect(result?.current).toMatchSnapshot();
});

it('should render with extra options - showOnlyThreatIndicatorAlerts', () => {
const { result } = renderHook(
() =>
useLensAttributes({
extraOptions: { showOnlyThreatIndicatorAlerts: true },
getLensAttributes: getAlertsHistogramLensAttributes,
stackByField: 'event.category',
}),
{ wrapper }
);

expect(result?.current).toMatchSnapshot();
});

it('should render with extra options - status', () => {
const { result } = renderHook(
() =>
useLensAttributes({
extraOptions: { status: 'open' },
getLensAttributes: getAlertsHistogramLensAttributes,
stackByField: 'event.category',
}),
{ wrapper }
);

expect(result?.current).toMatchSnapshot();
});

it('should render with extra options - breakdownField', () => {
const { result } = renderHook(
() =>
useLensAttributes({
extraOptions: { breakdownField: 'agent.type' },
getLensAttributes: getAlertsHistogramLensAttributes,
stackByField: 'event.category',
}),
{ wrapper }
);

expect(result?.current).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { buildAlertsOptionsFilters } from './utils';

export const getAlertsHistogramLensAttributes: GetLensAttributes = (
stackByField = 'kibana.alert.rule.name',
alertsOptions = {
extraOptions = {
showOnlyThreatIndicatorAlerts: false,
showBuildingBlockAlerts: false,
}
Expand Down Expand Up @@ -58,7 +58,7 @@ export const getAlertsHistogramLensAttributes: GetLensAttributes = (
query: '',
language: 'kuery',
},
filters: buildAlertsOptionsFilters(alertsOptions),
filters: buildAlertsOptionsFilters(extraOptions),
datasourceStates: {
formBased: {
layers: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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 { renderHook } from '@testing-library/react-hooks';
import { wrapper } from '../../../mocks';

import { useLensAttributes } from '../../../use_lens_attributes';

import { getAlertsTableLensAttributes } from './alerts_table';

jest.mock('../../../../../containers/sourcerer', () => ({
useSourcererDataView: jest.fn().mockReturnValue({
dataViewId: 'security-solution-my-test',
indicesExist: true,
selectedPatterns: ['signal-index'],
}),
}));

jest.mock('../../../../../utils/route/use_route_spy', () => ({
useRouteSpy: jest.fn().mockReturnValue([
{
pageName: 'alerts',
},
]),
}));

describe('getAlertsTableLensAttributes', () => {
it('should render without extra options', () => {
const { result } = renderHook(
() =>
useLensAttributes({
getLensAttributes: getAlertsTableLensAttributes,
stackByField: 'event.category',
}),
{ wrapper }
);

expect(result?.current).toMatchSnapshot();
});

it('should render with extra options - showBuildingBlockAlerts', () => {
const { result } = renderHook(
() =>
useLensAttributes({
extraOptions: { showBuildingBlockAlerts: true },
getLensAttributes: getAlertsTableLensAttributes,
stackByField: 'event.category',
}),
{ wrapper }
);

expect(result?.current).toMatchSnapshot();
});

it('should render with extra options - showOnlyThreatIndicatorAlerts', () => {
const { result } = renderHook(
() =>
useLensAttributes({
extraOptions: { showOnlyThreatIndicatorAlerts: true },
getLensAttributes: getAlertsTableLensAttributes,
stackByField: 'event.category',
}),
{ wrapper }
);

expect(result?.current).toMatchSnapshot();
});

it('should render with extra options - status', () => {
const { result } = renderHook(
() =>
useLensAttributes({
extraOptions: { status: 'open' },
getLensAttributes: getAlertsTableLensAttributes,
stackByField: 'event.category',
}),
{ wrapper }
);

expect(result?.current).toMatchSnapshot();
});

it('should render with extra options - breakdownField', () => {
const { result } = renderHook(
() =>
useLensAttributes({
extraOptions: { breakdownField: 'agent.type' },
getLensAttributes: getAlertsTableLensAttributes,
stackByField: 'event.category',
}),
{ wrapper }
);

expect(result?.current).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { buildAlertsOptionsFilters } from './utils';

export const getAlertsTableLensAttributes: GetLensAttributes = (
stackByField = 'kibana.alert.rule.name',
alertsOptions = {
extraOptions = {
showOnlyThreatIndicatorAlerts: false,
showBuildingBlockAlerts: false,
}
Expand Down Expand Up @@ -45,7 +45,7 @@ export const getAlertsTableLensAttributes: GetLensAttributes = (
query: '',
language: 'kuery',
},
filters: buildAlertsOptionsFilters(alertsOptions),
filters: buildAlertsOptionsFilters(extraOptions),
datasourceStates: {
formBased: {
layers: {
Expand Down Expand Up @@ -77,22 +77,22 @@ export const getAlertsTableLensAttributes: GetLensAttributes = (
},
},
'f04a71a3-399f-4d32-9efc-8a005e989991': {
label: `Count of ${alertsOptions.breakdownField}`,
label: `Count of ${extraOptions.breakdownField}`,
dataType: 'number',
operationType: 'count',
isBucketed: false,
scale: 'ratio',
sourceField: alertsOptions.breakdownField,
sourceField: extraOptions.breakdownField,
params: {
emptyAsNull: true,
},
},
'75ce269b-ee9c-4c7d-a14e-9226ba0fe059': {
label: `Top values of ${alertsOptions.breakdownField}`,
label: `Top values of ${extraOptions.breakdownField}`,
dataType: 'string',
operationType: 'terms',
scale: 'ordinal',
sourceField: alertsOptions.breakdownField,
sourceField: extraOptions.breakdownField,
isBucketed: true,
params: {
size: 1000,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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 { renderHook } from '@testing-library/react-hooks';
import { wrapper } from '../../../mocks';

import { useLensAttributes } from '../../../use_lens_attributes';

import { getAlertsTreemapLensAttributes } from './alerts_treemap';

jest.mock('../../../../../containers/sourcerer', () => ({
useSourcererDataView: jest.fn().mockReturnValue({
dataViewId: 'security-solution-my-test',
indicesExist: true,
selectedPatterns: ['signal-index'],
}),
}));

jest.mock('../../../../../utils/route/use_route_spy', () => ({
useRouteSpy: jest.fn().mockReturnValue([
{
pageName: 'alerts',
},
]),
}));

describe('getAlertsTreemapLensAttributes', () => {
it('should render without extra options', () => {
const { result } = renderHook(
() =>
useLensAttributes({
getLensAttributes: getAlertsTreemapLensAttributes,
stackByField: 'event.category',
}),
{ wrapper }
);

expect(result?.current).toMatchSnapshot();
});

it('should render with extra options - showBuildingBlockAlerts', () => {
const { result } = renderHook(
() =>
useLensAttributes({
extraOptions: { showBuildingBlockAlerts: true },
getLensAttributes: getAlertsTreemapLensAttributes,
stackByField: 'event.category',
}),
{ wrapper }
);

expect(result?.current).toMatchSnapshot();
});

it('should render with extra options - showOnlyThreatIndicatorAlerts', () => {
const { result } = renderHook(
() =>
useLensAttributes({
extraOptions: { showOnlyThreatIndicatorAlerts: true },
getLensAttributes: getAlertsTreemapLensAttributes,
stackByField: 'event.category',
}),
{ wrapper }
);

expect(result?.current).toMatchSnapshot();
});

it('should render with extra options - status', () => {
const { result } = renderHook(
() =>
useLensAttributes({
extraOptions: { status: 'open' },
getLensAttributes: getAlertsTreemapLensAttributes,
stackByField: 'event.category',
}),
{ wrapper }
);

expect(result?.current).toMatchSnapshot();
});

it('should render with extra options - breakdownField', () => {
const { result } = renderHook(
() =>
useLensAttributes({
extraOptions: { breakdownField: 'agent.type' },
getLensAttributes: getAlertsTreemapLensAttributes,
stackByField: 'event.category',
}),
{ wrapper }
);

expect(result?.current).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { buildAlertsOptionsFilters } from './utils';

export const getAlertsTreemapLensAttributes: GetLensAttributes = (
stackByField = 'kibana.alert.rule.name',
alertsOptions = {
extraOptions = {
showOnlyThreatIndicatorAlerts: false,
showBuildingBlockAlerts: false,
}
Expand Down Expand Up @@ -42,7 +42,7 @@ export const getAlertsTreemapLensAttributes: GetLensAttributes = (
query: '',
language: 'kuery',
},
filters: buildAlertsOptionsFilters(alertsOptions),
filters: buildAlertsOptionsFilters(extraOptions),
datasourceStates: {
formBased: {
layers: {
Expand Down Expand Up @@ -74,11 +74,11 @@ export const getAlertsTreemapLensAttributes: GetLensAttributes = (
},
},
'75ce269b-ee9c-4c7d-a14e-9226ba0fe059': {
label: `Top values of ${alertsOptions.breakdownField}`,
label: `Top values of ${extraOptions.breakdownField}`,
dataType: 'string',
operationType: 'terms',
scale: 'ordinal',
sourceField: alertsOptions.breakdownField,
sourceField: extraOptions.breakdownField,
isBucketed: true,
params: {
size: 1000,
Expand Down
Loading

0 comments on commit 153f139

Please sign in to comment.