Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cloud Posture] add tests for findings flyout toggling #150551

Merged
merged 5 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { RuleTab } from './rule_tab';
import type { BenchmarkId } from '../../../../common/types';
import { CISBenchmarkIcon } from '../../../components/cis_benchmark_icon';
import { BenchmarkName } from '../../../../common/types';
import { FINDINGS_FLYOUT } from '../test_subjects';

const tabs = [
{
Expand Down Expand Up @@ -112,7 +113,7 @@ export const FindingsRuleFlyout = ({ onClose, findings }: FindingFlyoutProps) =>
const [tab, setTab] = useState<FindingsTab>(tabs[0]);

return (
<EuiFlyout onClose={onClose}>
<EuiFlyout onClose={onClose} data-test-subj={FINDINGS_FLYOUT}>
<EuiFlyoutHeader>
<EuiFlexGroup alignItems="center">
<EuiFlexItem grow={false}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,40 @@ const chance = new Chance();
type TableProps = PropsOf<typeof FindingsTable>;

describe('<FindingsTable />', () => {
it('renders the zero state when status success and data has a length of zero ', async () => {
const renderWrapper = (opts?: Partial<TableProps>) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added this to simplify all tests in file

const props: TableProps = {
loading: false,
items: [],
items: opts?.items || [],
sorting: { sort: { field: '@timestamp', direction: 'desc' } },
pagination: { pageSize: 10, pageIndex: 1, totalItemCount: 0 },
pagination: { pageSize: 10, pageIndex: 1, totalItemCount: opts?.items?.length || 0 },
setTableOptions: jest.fn(),
onAddFilter: jest.fn(),
...opts,
};

render(
<TestProvider>
<FindingsTable {...props} />
</TestProvider>
);
return props;
};

it('opens/closes the flyout when clicked on expand/close buttons ', () => {
renderWrapper({ items: [getFindingsFixture()] });

expect(screen.queryByTestId(TEST_SUBJECTS.FINDINGS_FLYOUT)).not.toBeInTheDocument();
expect(screen.queryByTestId(TEST_SUBJECTS.FINDINGS_TABLE_EXPAND_COLUMN)).toBeInTheDocument();

userEvent.click(screen.getByTestId(TEST_SUBJECTS.FINDINGS_TABLE_EXPAND_COLUMN));
expect(screen.getByTestId(TEST_SUBJECTS.FINDINGS_FLYOUT)).toBeInTheDocument();

userEvent.click(screen.getByTestId('euiFlyoutCloseButton'));
expect(screen.queryByTestId(TEST_SUBJECTS.FINDINGS_FLYOUT)).not.toBeInTheDocument();
});

it('renders the zero state when status success and data has a length of zero ', async () => {
renderWrapper({ items: [] });

expect(
screen.getByTestId(TEST_SUBJECTS.LATEST_FINDINGS_TABLE_NO_FINDINGS_EMPTY_STATE)
Expand All @@ -42,22 +61,12 @@ describe('<FindingsTable />', () => {

it('renders the table with provided items', () => {
const names = chance.unique(chance.sentence, 10);
const data = names.map(getFindingsFixture);
const data = names.map((name) => {
const fixture = getFindingsFixture();
return { ...fixture, rule: { ...fixture.rule, name } };
});

const props: TableProps = {
loading: false,
items: data,
sorting: { sort: { field: '@timestamp', direction: 'desc' } },
pagination: { pageSize: 10, pageIndex: 1, totalItemCount: 0 },
setTableOptions: jest.fn(),
onAddFilter: jest.fn(),
};

render(
<TestProvider>
<FindingsTable {...props} />
</TestProvider>
);
renderWrapper({ items: data });

data.forEach((item) => {
expect(screen.getByText(item.rule.name)).toBeInTheDocument();
Expand All @@ -66,23 +75,12 @@ describe('<FindingsTable />', () => {

it('adds filter with a cell button click', () => {
const names = chance.unique(chance.sentence, 10);
const data = names.map(getFindingsFixture);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getFindingsFixture doesn't take any arguments so we're not using the unique names anymore. this used fail on CI because sometimes our tests would find identical strings. changed this to include unique rule names


const filterProps = { onAddFilter: jest.fn() };
const props: TableProps = {
loading: false,
items: data,
sorting: { sort: { field: '@timestamp', direction: 'desc' } },
pagination: { pageSize: 10, pageIndex: 1, totalItemCount: 0 },
setTableOptions: jest.fn(),
...filterProps,
};
const data = names.map((name) => {
const fixture = getFindingsFixture();
return { ...fixture, rule: { ...fixture.rule, name } };
});

render(
<TestProvider>
<FindingsTable {...props} />
</TestProvider>
);
const props = renderWrapper({ items: data });

const row = data[0];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { CspEvaluationBadge } from '../../../components/csp_evaluation_badge';
import {
FINDINGS_TABLE_CELL_ADD_FILTER,
FINDINGS_TABLE_CELL_ADD_NEGATED_FILTER,
FINDINGS_TABLE_EXPAND_COLUMN,
} from '../test_subjects';

export type OnAddFilter = <T extends string>(key: T, value: Serializable, negate: boolean) => void;
Expand All @@ -51,6 +52,7 @@ export const getExpandColumn = <T extends unknown>({
width: '40px',
actions: [
{
'data-test-subj': FINDINGS_TABLE_EXPAND_COLUMN,
name: i18n.translate('xpack.csp.expandColumnNameLabel', { defaultMessage: 'Expand' }),
description: i18n.translate('xpack.csp.expandColumnDescriptionLabel', {
defaultMessage: 'Expand',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* 2.0.
*/

export const FINDINGS_FLYOUT = 'findings_flyout';
export const FINDINGS_TABLE_EXPAND_COLUMN = 'findings_table_expand_column';
export const FINDINGS_TABLE = 'findings_table';
export const FINDINGS_BY_RESOURCE_TABLE_NO_FINDINGS_EMPTY_STATE =
'findings-by-resource-table-no-findings-empty-state';
Expand Down