Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
machadoum committed Aug 17, 2023
1 parent e1b8690 commit 98bef6f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
17 changes: 17 additions & 0 deletions x-pack/plugins/security_solution/public/rules/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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.
*/

module.exports = {
preset: '@kbn/test',
rootDir: '../../../../..',
roots: ['<rootDir>/x-pack/plugins/security_solution/public/rules'],
coverageDirectory:
'<rootDir>/target/kibana-coverage/jest/x-pack/plugins/security_solution/public/rules',
coverageReporters: ['text', 'html'],
collectCoverageFrom: ['<rootDir>/x-pack/plugins/security_solution/public/rules/**/*.{ts,tsx}'],
moduleNameMapper: require('../../server/__mocks__/module_name_map'),
};
31 changes: 31 additions & 0 deletions x-pack/plugins/security_solution/public/rules/landing.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 { render } from '@testing-library/react';
import React from 'react';
import { TestProviders } from '../common/mock';
import { RulesLandingHeader } from './landing';

const mockUseUserData = jest.fn().mockReturnValue([{ canUserCRUD: true }]);

jest.mock('../detections/components/user_info', () => ({
useUserData: () => mockUseUserData(),
}));

describe('Rules landing page header', () => {
it('renders', () => {
const { queryByTestId } = render(<RulesLandingHeader />, { wrapper: TestProviders });
expect(queryByTestId(`ruleLandingHeader`)).toBeInTheDocument();
});

it('should disable "create rule" when user has no CRUD access', () => {
mockUseUserData.mockReturnValue([{ canUserCRUD: false }]);

const { queryByTestId } = render(<RulesLandingHeader />, { wrapper: TestProviders });

expect(queryByTestId(`createRuleBtn`)).toHaveAttribute('disabled');
});
});
5 changes: 3 additions & 2 deletions x-pack/plugins/security_solution/public/rules/landing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ const CREATE_RULE_BUTTON = i18n.translate('xpack.securitySolution.rules.landing.
defaultMessage: 'Create rule',
});

const RulesLandingHeader: React.FC = () => {
export const RulesLandingHeader: React.FC = () => {
const [{ canUserCRUD }] = useUserData();
useReadonlyHeader(READ_ONLY_BADGE_TOOLTIP);

return (
<EuiFlexGroup gutterSize="none" direction="row">
<EuiFlexGroup gutterSize="none" direction="row" data-test-subj="ruleLandingHeader">
<EuiFlexItem>
<Title title={RULES_PAGE_TITLE} />
</EuiFlexItem>
Expand All @@ -45,6 +45,7 @@ const RulesLandingHeader: React.FC = () => {
deepLinkId={SecurityPageName.rulesCreate}
iconType="plusInCircle"
isDisabled={!hasUserCRUDPermission(canUserCRUD)}
data-test-subj="createRuleBtn"
>
{CREATE_RULE_BUTTON}
</SecuritySolutionLinkButton>
Expand Down

0 comments on commit 98bef6f

Please sign in to comment.