From b6a42d68d68c6879fce5ce89a5554be3bb3a30fa Mon Sep 17 00:00:00 2001 From: alex prozorov Date: Tue, 21 Jan 2025 22:44:05 +0200 Subject: [PATCH] update kbn grouping tests and create unit tests for useLatestFindingsGrouping hook --- .../src/components/grouping.mock.tsx | 3 + .../src/components/grouping.test.tsx | 27 ++++ .../use_latest_findings_grouping.test.tsx | 117 ++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 x-pack/solutions/security/plugins/cloud_security_posture/public/pages/configurations/latest_findings/use_latest_findings_grouping.test.tsx diff --git a/src/platform/packages/shared/kbn-grouping/src/components/grouping.mock.tsx b/src/platform/packages/shared/kbn-grouping/src/components/grouping.mock.tsx index 787a31ec5fd8a..ca706746498e0 100644 --- a/src/platform/packages/shared/kbn-grouping/src/components/grouping.mock.tsx +++ b/src/platform/packages/shared/kbn-grouping/src/components/grouping.mock.tsx @@ -130,6 +130,9 @@ export const mockGroupingProps = { unitsCountWithoutNull: { value: 14, }, + nullGroupItems: { + doc_count: 1, + }, }, groupingId: 'test-grouping-id', isLoading: false, diff --git a/src/platform/packages/shared/kbn-grouping/src/components/grouping.test.tsx b/src/platform/packages/shared/kbn-grouping/src/components/grouping.test.tsx index c76be444fdd27..5270c9f340655 100644 --- a/src/platform/packages/shared/kbn-grouping/src/components/grouping.test.tsx +++ b/src/platform/packages/shared/kbn-grouping/src/components/grouping.test.tsx @@ -223,5 +223,32 @@ describe('Grouping', () => { expect(customGroupsUnit).toHaveBeenCalledWith(3, testProps.selectedGroup, true); expect(screen.getByTestId('group-count').textContent).toBe('3 custom units'); }); + + it('calls custom groupsUnit callback with hasNullGroup = false', () => { + const customGroupsUnit = jest.fn( + (n, parentSelectedGroup, hasNullGroup) => `${n} custom units` + ); + + const customProps = { + ...testProps, + groupsUnit: customGroupsUnit, + data: { + ...testProps.data, + nullGroupItems: { + ...testProps.data.nullGroupItems, + doc_count: 0, + }, + }, + }; + + render( + + + + ); + + expect(customGroupsUnit).toHaveBeenCalledWith(3, testProps.selectedGroup, false); + expect(screen.getByTestId('group-count').textContent).toBe('3 custom units'); + }); }); }); diff --git a/x-pack/solutions/security/plugins/cloud_security_posture/public/pages/configurations/latest_findings/use_latest_findings_grouping.test.tsx b/x-pack/solutions/security/plugins/cloud_security_posture/public/pages/configurations/latest_findings/use_latest_findings_grouping.test.tsx new file mode 100644 index 0000000000000..66210347aff1f --- /dev/null +++ b/x-pack/solutions/security/plugins/cloud_security_posture/public/pages/configurations/latest_findings/use_latest_findings_grouping.test.tsx @@ -0,0 +1,117 @@ +/* + * 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 React from 'react'; +import { renderHook } from '@testing-library/react'; +import { useLatestFindingsGrouping } from './use_latest_findings_grouping'; +import { useCloudSecurityGrouping } from '../../../components/cloud_security_grouping'; +import { useDataViewContext } from '../../../common/contexts/data_view_context'; +import { useGetCspBenchmarkRulesStatesApi } from '@kbn/cloud-security-posture/src/hooks/use_get_benchmark_rules_state_api'; +import { getGroupingQuery } from '@kbn/grouping'; +import { useGroupedFindings } from './use_grouped_findings'; + +jest.mock('../../../components/cloud_security_grouping'); +jest.mock('../../../common/contexts/data_view_context'); +jest.mock('@kbn/cloud-security-posture/src/hooks/use_get_benchmark_rules_state_api'); +jest.mock('@kbn/grouping', () => ({ + getGroupingQuery: jest.fn().mockImplementation((params) => { + return { + query: { bool: {} }, + }; + }), + parseGroupingQuery: jest.fn().mockReturnValue({}), +})); +jest.mock('./use_grouped_findings'); + +describe('useLatestFindingsGrouping', () => { + const mockGroupPanelRenderer =
Mock Group Panel Renderer
; + const mockGetGroupStats = jest.fn(); + + beforeEach(() => { + (useCloudSecurityGrouping as jest.Mock).mockReturnValue({ + grouping: { selectedGroups: ['cloud.account.id'] }, + }); + (useDataViewContext as jest.Mock).mockReturnValue({ dataView: {} }); + (useGetCspBenchmarkRulesStatesApi as jest.Mock).mockReturnValue({ data: {} }); + (useGroupedFindings as jest.Mock).mockReturnValue({ + data: {}, + isFetching: false, + }); + }); + + it('calls getGroupingQuery with correct rootAggregations', () => { + renderHook(() => + useLatestFindingsGrouping({ + groupPanelRenderer: mockGroupPanelRenderer, + getGroupStats: mockGetGroupStats, + groupingLevel: 0, + groupFilters: [], + selectedGroup: 'cloud.account.id', + }) + ); + + expect(getGroupingQuery).toHaveBeenCalledWith( + expect.objectContaining({ + rootAggregations: [ + { + failedFindings: { + filter: { + term: { + 'result.evaluation': { value: 'failed' }, + }, + }, + }, + passedFindings: { + filter: { + term: { + 'result.evaluation': { value: 'passed' }, + }, + }, + }, + nullGroupItems: { + missing: { field: 'cloud.account.id' }, + }, + }, + ], + }) + ); + }); + + it('calls getGroupingQuery without nullGroupItems when selectedGroup is "none"', () => { + renderHook(() => + useLatestFindingsGrouping({ + groupPanelRenderer: mockGroupPanelRenderer, + getGroupStats: mockGetGroupStats, + groupingLevel: 0, + groupFilters: [], + selectedGroup: 'none', + }) + ); + + expect(getGroupingQuery).toHaveBeenCalledWith( + expect.objectContaining({ + rootAggregations: [ + { + failedFindings: { + filter: { + term: { + 'result.evaluation': { value: 'failed' }, + }, + }, + }, + passedFindings: { + filter: { + term: { + 'result.evaluation': { value: 'passed' }, + }, + }, + }, + }, + ], + }) + ); + }); +});