Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 committed Nov 20, 2024
1 parent 50043c7 commit 977957f
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class SingleMetricLensAttributes extends LensAttributes {
? {
id: 'percent',
params: {
decimals: 1,
decimals: 3,
},
}
: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function getSyntheticsKPIConfig({ dataView }: ConfigProps): SeriesConfig
label: 'Monitor Errors',
id: 'monitor_errors',
columnType: OPERATION_COLUMN,
field: 'monitor.check_group',
field: 'state.id',
columnFilters: [
{
language: 'kuery',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import { ConfigProps, SeriesConfig } from '../../types';
import { FieldLabels, FORMULA_COLUMN, RECORDS_FIELD } from '../constants';
import { buildExistsFilter } from '../utils';

export const FINAL_SUMMARY_KQL =
'summary: * and (summary.final_attempt: true or not summary.final_attempt: *)';
export const FINAL_SUMMARY_KQL = 'summary.final_attempt: true';
export function getSyntheticsSingleMetricConfig({ dataView }: ConfigProps): SeriesConfig {
return {
defaultSeriesType: 'line',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* 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 * as spaceHook from '../../../../../hooks/use_kibana_space';
import * as paramHook from '../../../hooks/use_url_params';
import * as redux from 'react-redux';
import { useMonitorFilters } from './use_monitor_filters';
import { WrappedHelper } from '../../../utils/testing';

describe('useMonitorFilters', () => {
beforeEach(() => {
jest.clearAllMocks();
});

const spaceSpy = jest.spyOn(spaceHook, 'useKibanaSpace');
const paramSpy = jest.spyOn(paramHook, 'useGetUrlParams');
const selSPy = jest.spyOn(redux, 'useSelector');

it('should return an empty array when no parameters are provided', () => {
const { result } = renderHook(() => useMonitorFilters({}), { wrapper: WrappedHelper });

expect(result.current).toEqual([]);
});

it('should return filters for allIds and schedules', () => {
spaceSpy.mockReturnValue({} as any);
paramSpy.mockReturnValue({ schedules: 'daily' } as any);
selSPy.mockReturnValue({ status: { allIds: ['id1', 'id2'] } });

const { result } = renderHook(() => useMonitorFilters({}), { wrapper: WrappedHelper });

expect(result.current).toEqual([{ field: 'monitor.id', values: ['id1', 'id2'] }]);
});

it('should return filters for allIds and empty schedules', () => {
spaceSpy.mockReturnValue({} as any);
paramSpy.mockReturnValue({ schedules: [] } as any);
selSPy.mockReturnValue({ status: { allIds: ['id1', 'id2'] } });

const { result } = renderHook(() => useMonitorFilters({}), { wrapper: WrappedHelper });

expect(result.current).toEqual([]);
});

it('should return filters for project IDs', () => {
spaceSpy.mockReturnValue({ space: null } as any);
paramSpy.mockReturnValue({ projects: ['project1', 'project2'] } as any);
selSPy.mockReturnValue({ status: { allIds: [] } });

const { result } = renderHook(() => useMonitorFilters({}), { wrapper: WrappedHelper });

expect(result.current).toEqual([
{ field: 'monitor.project.id', values: ['project1', 'project2'] },
]);
});

it('should return filters for tags and locations', () => {
spaceSpy.mockReturnValue({ space: null } as any);
paramSpy.mockReturnValue({
tags: ['tag1', 'tag2'],
locations: ['location1', 'location2'],
} as any);
selSPy.mockReturnValue({ status: { allIds: [] } });

const { result } = renderHook(() => useMonitorFilters({}), { wrapper: WrappedHelper });

expect(result.current).toEqual([
{ field: 'tags', values: ['tag1', 'tag2'] },
{ field: 'observer.geo.name', values: ['location1', 'location2'] },
]);
});

it('should include space filters for alerts', () => {
spaceSpy.mockReturnValue({ space: { id: 'space1' } } as any);
paramSpy.mockReturnValue({} as any);
selSPy.mockReturnValue({ status: { allIds: [] } });

const { result } = renderHook(() => useMonitorFilters({ forAlerts: true }), {
wrapper: WrappedHelper,
});

expect(result.current).toEqual([{ field: 'kibana.space_ids', values: ['space1'] }]);
});

it('should include space filters for non-alerts', () => {
spaceSpy.mockReturnValue({ space: { id: 'space2' } } as any);
paramSpy.mockReturnValue({} as any);
selSPy.mockReturnValue({ status: { allIds: [] } });

const { result } = renderHook(() => useMonitorFilters({}), { wrapper: WrappedHelper });

expect(result.current).toEqual([{ field: 'meta.space_id', values: ['space2'] }]);
});

it('should handle a combination of parameters', () => {
spaceSpy.mockReturnValue({ space: { id: 'space3' } } as any);
paramSpy.mockReturnValue({
schedules: 'daily',
projects: ['projectA'],
tags: ['tagB'],
locations: ['locationC'],
monitorTypes: 'http',
} as any);
selSPy.mockReturnValue({ status: { allIds: ['id3', 'id4'] } });

const { result } = renderHook(() => useMonitorFilters({ forAlerts: false }), {
wrapper: WrappedHelper,
});

expect(result.current).toEqual([
{ field: 'monitor.id', values: ['id3', 'id4'] },
{ field: 'monitor.project.id', values: ['projectA'] },
{ field: 'monitor.type', values: ['http'] },
{ field: 'tags', values: ['tagB'] },
{ field: 'observer.geo.name', values: ['locationC'] },
{ field: 'meta.space_id', values: ['space3'] },
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import { UrlFilter } from '@kbn/exploratory-view-plugin/public';
import { useSelector } from 'react-redux';
import { useGetUrlParams } from '../../../hooks';
import { isEmpty } from 'lodash';
import { useGetUrlParams } from '../../../hooks/use_url_params';
import { useKibanaSpace } from '../../../../../hooks/use_kibana_space';
import { selectOverviewStatus } from '../../../state/overview_status';

Expand All @@ -19,7 +20,7 @@ export const useMonitorFilters = ({ forAlerts }: { forAlerts?: boolean }): UrlFi

return [
// since schedule isn't available in heartbeat data, in that case we rely on monitor.id
...(allIds?.length && schedules ? [{ field: 'monitor.id', values: allIds }] : []),
...(allIds?.length && !isEmpty(schedules) ? [{ field: 'monitor.id', values: allIds }] : []),
...(projects?.length ? [{ field: 'monitor.project.id', values: getValues(projects) }] : []),
...(monitorTypes?.length ? [{ field: 'monitor.type', values: getValues(monitorTypes) }] : []),
...(tags?.length ? [{ field: 'tags', values: getValues(tags) }] : []),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,14 @@
* 2.0.
*/

import {
EuiFlexGroup,
EuiFlexItem,
EuiSkeletonText,
EuiPanel,
EuiSpacer,
EuiTitle,
} from '@elastic/eui';
import { EuiFlexGroup, EuiFlexItem, EuiPanel, EuiSpacer, EuiTitle } from '@elastic/eui';
import React from 'react';
import { useSelector } from 'react-redux';
import { i18n } from '@kbn/i18n';
import { selectOverviewStatus } from '../../../../../state/overview_status';
import { OverviewErrorsSparklines } from './overview_errors_sparklines';
import { useRefreshedRange } from '../../../../../hooks';
import { OverviewErrorsCount } from './overview_errors_count';

export function OverviewErrors() {
const { status } = useSelector(selectOverviewStatus);

const loading = !status?.allIds || status?.allIds.length === 0;

const { from, to } = useRefreshedRange(6, 'hours');

return (
Expand All @@ -34,18 +21,14 @@ export function OverviewErrors() {
<h3>{headingText}</h3>
</EuiTitle>
<EuiSpacer size="s" />
{loading ? (
<EuiSkeletonText lines={3} />
) : (
<EuiFlexGroup gutterSize="xl">
<EuiFlexItem grow={false}>
<OverviewErrorsCount from={from} to={to} />
</EuiFlexItem>
<EuiFlexItem grow={true}>
<OverviewErrorsSparklines from={from} to={to} />
</EuiFlexItem>
</EuiFlexGroup>
)}
<EuiFlexGroup gutterSize="xl">
<EuiFlexItem grow={false}>
<OverviewErrorsCount from={from} to={to} />
</EuiFlexItem>
<EuiFlexItem grow={true}>
<OverviewErrorsSparklines from={from} to={to} />
</EuiFlexItem>
</EuiFlexGroup>
</EuiPanel>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { Space } from '@kbn/spaces-plugin/common';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import { useFetcher } from '@kbn/observability-shared-plugin/public';
import { DEFAULT_SPACE_ID } from '@kbn/spaces-plugin/common';
import { ClientPluginsStart } from '../plugin';
import type { ClientPluginsStart } from '../plugin';

export const useKibanaSpace = () => {
const { services } = useKibana<ClientPluginsStart>();
Expand Down

0 comments on commit 977957f

Please sign in to comment.