forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Cloud Security] Add Fields selector to the CloudSecurityDataTable (e…
…lastic#167844) Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: @Omolola-Akinleye (cherry picked from commit 0c71076)
- Loading branch information
Showing
9 changed files
with
483 additions
and
101 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
...loud_security_posture/public/components/cloud_security_data_table/additional_controls.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,74 @@ | ||
/* | ||
* 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, { useState } from 'react'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { EuiButtonEmpty, EuiFlexItem } from '@elastic/eui'; | ||
import { type DataView } from '@kbn/data-views-plugin/common'; | ||
import numeral from '@elastic/numeral'; | ||
import { FieldsSelectorModal } from './fields_selector'; | ||
import { FindingsGroupBySelector } from '../../pages/configurations/layout/findings_group_by_selector'; | ||
import { useStyles } from './use_styles'; | ||
|
||
const formatNumber = (value: number) => { | ||
return value < 1000 ? value : numeral(value).format('0.0a'); | ||
}; | ||
|
||
export const AdditionalControls = ({ | ||
total, | ||
title, | ||
dataView, | ||
columns, | ||
onAddColumn, | ||
onRemoveColumn, | ||
}: { | ||
total: number; | ||
title: string; | ||
dataView: DataView; | ||
columns: string[]; | ||
onAddColumn: (column: string) => void; | ||
onRemoveColumn: (column: string) => void; | ||
}) => { | ||
const styles = useStyles(); | ||
|
||
const [isFieldSelectorModalVisible, setIsFieldSelectorModalVisible] = useState(false); | ||
|
||
const closeModal = () => setIsFieldSelectorModalVisible(false); | ||
const showModal = () => setIsFieldSelectorModalVisible(true); | ||
|
||
return ( | ||
<> | ||
{isFieldSelectorModalVisible && ( | ||
<FieldsSelectorModal | ||
columns={columns} | ||
dataView={dataView} | ||
closeModal={closeModal} | ||
onAddColumn={onAddColumn} | ||
onRemoveColumn={onRemoveColumn} | ||
/> | ||
)} | ||
<EuiFlexItem grow={0}> | ||
<span className="cspDataTableTotal">{`${formatNumber(total)} ${title}`}</span> | ||
</EuiFlexItem> | ||
<EuiFlexItem grow={0}> | ||
<EuiButtonEmpty | ||
className="cspDataTableFields" | ||
iconType="tableOfContents" | ||
onClick={showModal} | ||
size="xs" | ||
color="text" | ||
> | ||
{i18n.translate('xpack.csp.dataTable.fields', { | ||
defaultMessage: 'Fields', | ||
})} | ||
</EuiButtonEmpty> | ||
</EuiFlexItem> | ||
<EuiFlexItem grow={false} className={styles.groupBySelector}> | ||
<FindingsGroupBySelector type="default" /> | ||
</EuiFlexItem> | ||
</> | ||
); | ||
}; |
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
69 changes: 69 additions & 0 deletions
69
...oud_security_posture/public/components/cloud_security_data_table/fields_selector.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,69 @@ | ||
/* | ||
* 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 { render, fireEvent } from '@testing-library/react'; | ||
import { FieldsSelectorTable, FieldsSelectorCommonProps } from './fields_selector'; | ||
import { TestProvider } from '../../test/test_provider'; | ||
|
||
const mockDataView = { | ||
fields: { | ||
getAll: () => [ | ||
{ id: 'field1', name: 'field1', customLabel: 'Label 1', visualizable: true }, | ||
{ id: 'field2', name: 'field2', customLabel: 'Label 2', visualizable: true }, | ||
], | ||
}, | ||
} as any; | ||
|
||
const renderFieldsTable = (props: Partial<FieldsSelectorCommonProps> = {}) => { | ||
const defaultProps: FieldsSelectorCommonProps = { | ||
dataView: mockDataView, | ||
columns: [], | ||
onAddColumn: jest.fn(), | ||
onRemoveColumn: jest.fn(), | ||
}; | ||
|
||
return render( | ||
<TestProvider> | ||
<FieldsSelectorTable title="Fields" {...defaultProps} {...props} /> | ||
</TestProvider> | ||
); | ||
}; | ||
|
||
describe('FieldsSelectorTable', () => { | ||
it('renders the table with data correctly', () => { | ||
const { getByText } = renderFieldsTable(); | ||
|
||
expect(getByText('Label 1')).toBeInTheDocument(); | ||
expect(getByText('Label 2')).toBeInTheDocument(); | ||
}); | ||
|
||
it('calls onAddColumn when a checkbox is checked', () => { | ||
const onAddColumn = jest.fn(); | ||
const { getAllByRole } = renderFieldsTable({ | ||
onAddColumn, | ||
}); | ||
|
||
const checkbox = getAllByRole('checkbox')[0]; | ||
fireEvent.click(checkbox); | ||
|
||
expect(onAddColumn).toHaveBeenCalledWith('field1'); | ||
}); | ||
|
||
it('calls onRemoveColumn when a checkbox is unchecked', () => { | ||
const onRemoveColumn = jest.fn(); | ||
const { getAllByRole } = renderFieldsTable({ | ||
columns: ['field1', 'field2'], | ||
onRemoveColumn, | ||
}); | ||
|
||
const checkbox = getAllByRole('checkbox')[1]; | ||
fireEvent.click(checkbox); | ||
|
||
expect(onRemoveColumn).toHaveBeenCalledWith('field2'); | ||
}); | ||
}); |
Oops, something went wrong.