Skip to content

Commit

Permalink
update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
angorayc committed Jan 9, 2024
1 parent 98b200c commit efb7d8c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,31 +168,33 @@ const LensEmbeddableComponent: React.FC<LensEmbeddableComponentProps> = ({
[dispatch, inputsModelId]
);

const onFilterCallback = useCallback(() => {
const callback: EmbeddableComponentProps['onFilter'] = async (e) => {
if (!isClickTriggerEvent(e) || preferredSeriesType !== 'area' || disableOnClickFilter) {
e.preventDefault();
const onFilterCallback = useCallback(
(event) => {
if (disableOnClickFilter) {
event.preventDefault();
return;
}
// Update timerange when clicking on a dot in an area chart
const [{ query }] = await createFiltersFromValueClickAction({
data: e.data,
negate: e.negate,
});
const rangeFilter: RangeFilterParams = query?.range['@timestamp'];
if (rangeFilter?.gte && rangeFilter?.lt) {
updateDateRange({
range: [rangeFilter.gte, rangeFilter.lt],
const callback: EmbeddableComponentProps['onFilter'] = async (e) => {
if (!isClickTriggerEvent(e) || preferredSeriesType !== 'area') {
e.preventDefault();
return;
}
// Update timerange when clicking on a dot in an area chart
const [{ query }] = await createFiltersFromValueClickAction({
data: e.data,
negate: e.negate,
});
}
};
return callback;
}, [
createFiltersFromValueClickAction,
updateDateRange,
preferredSeriesType,
disableOnClickFilter,
]);
const rangeFilter: RangeFilterParams = query?.range['@timestamp'];
if (rangeFilter?.gte && rangeFilter?.lt) {
updateDateRange({
range: [rangeFilter.gte, rangeFilter.lt],
});
}
};
return callback;
},
[createFiltersFromValueClickAction, updateDateRange, preferredSeriesType, disableOnClickFilter]
);

const adHocDataViews = useMemo(
() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import moment from 'moment';

import type { DataViewBase } from '@kbn/es-query';
import { fields } from '@kbn/data-plugin/common/mocks';
import { render } from '@testing-library/react';

import { useGlobalTime } from '../../../../common/containers/use_global_time';
import {
Expand All @@ -27,7 +28,6 @@ import { useTimelineEvents } from '../../../../common/components/events_viewer/u
import { TableId } from '@kbn/securitysolution-data-table';
import { createStore } from '../../../../common/store';
import { mockEventViewerResponse } from '../../../../common/components/events_viewer/mock';
import { mount } from 'enzyme';
import type { UseFieldBrowserOptionsProps } from '../../../../timelines/components/fields_browser';
import type { TransformColumnsProps } from '../../../../common/components/control_columns';
import { INSPECT_ACTION } from '../../../../common/components/visualization_actions/use_actions';
Expand Down Expand Up @@ -118,7 +118,7 @@ describe('PreviewHistogram', () => {
responses: [{ hits: { total: 1 } }],
});

const wrapper = mount(
const { getByTestId } = render(
<TestProviders store={store}>
<PreviewHistogram
addNoiseWarning={jest.fn()}
Expand All @@ -131,7 +131,7 @@ describe('PreviewHistogram', () => {
</TestProviders>
);

expect(wrapper.find('[data-test-subj="visualization-embeddable"]').exists()).toBeTruthy();
expect(getByTestId('visualization-embeddable')).toBeInTheDocument();
});

test('should render inspect action', () => {
Expand All @@ -141,7 +141,7 @@ describe('PreviewHistogram', () => {
responses: [{ hits: { total: 1 } }],
});

mount(
render(
<TestProviders store={store}>
<PreviewHistogram
addNoiseWarning={jest.fn()}
Expand All @@ -157,14 +157,37 @@ describe('PreviewHistogram', () => {
expect(mockVisualizationEmbeddable.mock.calls[0][0].withActions).toEqual(INSPECT_ACTION);
});

test('should disable filter when clicking on the chart', () => {
(useVisualizationResponse as jest.Mock).mockReturnValue({
loading: false,
requests: [],
responses: [{ hits: { total: 1 } }],
});

render(
<TestProviders store={store}>
<PreviewHistogram
addNoiseWarning={jest.fn()}
timeframeOptions={getLastMonthTimeframe()}
previewId={'test-preview-id'}
spaceId={'default'}
ruleType={'query'}
indexPattern={getMockIndexPattern()}
/>
</TestProviders>
);

expect(mockVisualizationEmbeddable.mock.calls[0][0].disableOnClickFilter).toBeTruthy();
});

test('should show chart legend when if it is not EQL rule', () => {
(useVisualizationResponse as jest.Mock).mockReturnValue({
loading: false,
requests: [],
responses: [{ hits: { total: 1 } }],
});

mount(
render(
<TestProviders store={store}>
<PreviewHistogram
addNoiseWarning={jest.fn()}
Expand Down Expand Up @@ -199,7 +222,7 @@ describe('PreviewHistogram', () => {
responses: [{ hits: { total: 0 } }],
});

mount(
render(
<TestProviders store={store}>
<PreviewHistogram
addNoiseWarning={jest.fn()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ const PreviewHistogramComponent = ({
<EuiFlexItem grow={1}>
<VisualizationEmbeddable
applyGlobalQueriesAndFilters={false}
disableOnClickFilter={true}
enableLegendActions={false}
extraOptions={extraVisualizationOptions}
getLensAttributes={getRulePreviewLensAttributes}
Expand Down

0 comments on commit efb7d8c

Please sign in to comment.