-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update kbn grouping tests and create unit tests for useLatestFindings…
…Grouping hook
- Loading branch information
1 parent
ff5eb9e
commit fb2fec0
Showing
3 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
...posture/public/pages/configurations/latest_findings/use_latest_findings_grouping.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* 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 = ( | ||
selectedGroup: string, | ||
fieldBucket: any, | ||
nullGroupMessage?: string, | ||
isLoading?: boolean | ||
) => <div>Mock Group Panel Renderer</div>; | ||
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' }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
], | ||
}) | ||
); | ||
}); | ||
}); |