From 98bef6f4890131c7b79ed527e35511ee4a56ebec Mon Sep 17 00:00:00 2001 From: Pablo Neves Machado Date: Thu, 17 Aug 2023 16:43:33 +0200 Subject: [PATCH] Add unit test --- .../public/rules/jest.config.js | 17 ++++++++++ .../public/rules/landing.test.tsx | 31 +++++++++++++++++++ .../public/rules/landing.tsx | 5 +-- 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 x-pack/plugins/security_solution/public/rules/jest.config.js create mode 100644 x-pack/plugins/security_solution/public/rules/landing.test.tsx diff --git a/x-pack/plugins/security_solution/public/rules/jest.config.js b/x-pack/plugins/security_solution/public/rules/jest.config.js new file mode 100644 index 0000000000000..b976316c48555 --- /dev/null +++ b/x-pack/plugins/security_solution/public/rules/jest.config.js @@ -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: ['/x-pack/plugins/security_solution/public/rules'], + coverageDirectory: + '/target/kibana-coverage/jest/x-pack/plugins/security_solution/public/rules', + coverageReporters: ['text', 'html'], + collectCoverageFrom: ['/x-pack/plugins/security_solution/public/rules/**/*.{ts,tsx}'], + moduleNameMapper: require('../../server/__mocks__/module_name_map'), +}; diff --git a/x-pack/plugins/security_solution/public/rules/landing.test.tsx b/x-pack/plugins/security_solution/public/rules/landing.test.tsx new file mode 100644 index 0000000000000..37100e82ac9bb --- /dev/null +++ b/x-pack/plugins/security_solution/public/rules/landing.test.tsx @@ -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(, { wrapper: TestProviders }); + expect(queryByTestId(`ruleLandingHeader`)).toBeInTheDocument(); + }); + + it('should disable "create rule" when user has no CRUD access', () => { + mockUseUserData.mockReturnValue([{ canUserCRUD: false }]); + + const { queryByTestId } = render(, { wrapper: TestProviders }); + + expect(queryByTestId(`createRuleBtn`)).toHaveAttribute('disabled'); + }); +}); diff --git a/x-pack/plugins/security_solution/public/rules/landing.tsx b/x-pack/plugins/security_solution/public/rules/landing.tsx index 61d5b625abeca..09d19e7c31f28 100644 --- a/x-pack/plugins/security_solution/public/rules/landing.tsx +++ b/x-pack/plugins/security_solution/public/rules/landing.tsx @@ -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 ( - + </EuiFlexItem> @@ -45,6 +45,7 @@ const RulesLandingHeader: React.FC = () => { deepLinkId={SecurityPageName.rulesCreate} iconType="plusInCircle" isDisabled={!hasUserCRUDPermission(canUserCRUD)} + data-test-subj="createRuleBtn" > {CREATE_RULE_BUTTON} </SecuritySolutionLinkButton>