diff --git a/openmetadata-ui/src/main/resources/ui/src/pages/PoliciesPage/PoliciesDetailPage/PoliciesDetailPage.test.tsx b/openmetadata-ui/src/main/resources/ui/src/pages/PoliciesPage/PoliciesDetailPage/PoliciesDetailPage.test.tsx new file mode 100644 index 00000000000..2a20f6958f9 --- /dev/null +++ b/openmetadata-ui/src/main/resources/ui/src/pages/PoliciesPage/PoliciesDetailPage/PoliciesDetailPage.test.tsx @@ -0,0 +1,165 @@ +/* + * Copyright 2021 Collate + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { render, screen } from '@testing-library/react'; +import React from 'react'; +import { POLICY_DATA } from '../policies.mock'; +import PoliciesDetailPage from './PoliciesDetailPage'; + +jest.mock('react-router-dom', () => ({ + useHistory: jest.fn(), + useParams: jest.fn().mockReturnValue({ fqn: 'policy' }), +})); + +jest.mock('../../../axiosAPIs/rolesAPIV1', () => ({ + getPolicyByName: jest + .fn() + .mockImplementation(() => Promise.resolve(POLICY_DATA)), + getRoleByName: jest.fn().mockImplementation(() => Promise.resolve()), + patchPolicy: jest.fn().mockImplementation(() => Promise.resolve()), + patchRole: jest.fn().mockImplementation(() => Promise.resolve()), +})); + +jest.mock('../../../axiosAPIs/teamsAPI', () => ({ + getTeamByName: jest.fn().mockImplementation(() => Promise.resolve()), + patchTeamDetail: jest.fn().mockImplementation(() => Promise.resolve()), +})); + +jest.mock('../../../components/common/description/Description', () => + jest + .fn() + .mockReturnValue(
Description
) +); + +jest.mock( + '../../../components/common/error-with-placeholder/ErrorPlaceHolder', + () => jest.fn().mockReturnValue(
ErrorPlaceholder
) +); + +jest.mock( + '../../../components/common/rich-text-editor/RichTextEditorPreviewer', + () => jest.fn().mockReturnValue(
Previewer
) +); + +jest.mock( + '../../../components/common/title-breadcrumb/title-breadcrumb.component', + () => + jest.fn().mockReturnValue(
BreadCrumb
) +); + +jest.mock('../../../components/Loader/Loader', () => + jest.fn().mockReturnValue(
Loader
) +); + +jest.mock('../../../components/PermissionProvider/PermissionProvider', () => ({ + usePermissionProvider: jest.fn().mockReturnValue({ + getEntityPermissionByFqn: jest.fn().mockReturnValue({ + Create: true, + Delete: true, + ViewAll: true, + EditAll: true, + EditDescription: true, + EditDisplayName: true, + EditCustomFields: true, + }), + }), +})); + +jest.mock('../../../constants/HelperTextUtil', () => ({ + NO_PERMISSION_FOR_ACTION: '', + NO_PERMISSION_TO_VIEW: '', +})); + +jest.mock('../../../utils/CommonUtils', () => ({ + getEntityName: jest.fn().mockReturnValue(''), +})); + +jest.mock('../../../utils/PermissionsUtils', () => ({ + DEFAULT_ENTITY_PERMISSION: { + Create: true, + Delete: true, + ViewAll: true, + EditAll: true, + EditDescription: true, + EditDisplayName: true, + EditCustomFields: true, + }, +})); + +jest.mock('../../../utils/RouterUtils', () => ({ + getAddPolicyRulePath: jest.fn(), + getEditPolicyRulePath: jest.fn(), + getRoleWithFqnPath: jest.fn(), + getSettingPath: jest.fn(), + getTeamsWithFqnPath: jest.fn(), +})); + +jest.mock('../../../utils/ToastUtils', () => ({ + showErrorToast: jest.fn().mockReturnValue(''), +})); + +describe('Test Policy details page', () => { + it('Should render the policy details page component', async () => { + render(); + + const container = await screen.findByTestId('policy-details-container'); + + const breadCrumb = await screen.findByTestId('breadcrumb'); + + const description = await screen.findByTestId('description-data'); + + const rulesTab = await screen.findByText('Rules'); + + const rolesTab = await screen.findByText('Roles'); + + const teamsTab = await screen.findByText('Teams'); + + expect(container).toBeInTheDocument(); + + expect(breadCrumb).toBeInTheDocument(); + + expect(description).toBeInTheDocument(); + + expect(rulesTab).toBeInTheDocument(); + + expect(rolesTab).toBeInTheDocument(); + + expect(teamsTab).toBeInTheDocument(); + }); + + it('Should render the rule card and its attributes', async () => { + render(); + + const ruleCard = await screen.findByTestId('rule-card'); + + const ruleName = await screen.findByTestId('rule-name'); + + const ruleDescription = await screen.findByTestId('description'); + + const ruleResources = await screen.findByTestId('resources'); + + const ruleOperations = await screen.findByTestId('operations'); + + const ruleEffect = await screen.findByTestId('effect'); + + const ruleCondition = await screen.findByTestId('condition'); + + expect(ruleCard).toBeInTheDocument(); + expect(ruleName).toBeInTheDocument(); + expect(ruleDescription).toBeInTheDocument(); + expect(ruleResources).toBeInTheDocument(); + expect(ruleOperations).toBeInTheDocument(); + expect(ruleEffect).toBeInTheDocument(); + expect(ruleCondition).toBeInTheDocument(); + }); +}); diff --git a/openmetadata-ui/src/main/resources/ui/src/pages/PoliciesPage/PoliciesDetailPage/PoliciesDetailPage.tsx b/openmetadata-ui/src/main/resources/ui/src/pages/PoliciesPage/PoliciesDetailPage/PoliciesDetailPage.tsx index f790a9b6d4d..be88178b109 100644 --- a/openmetadata-ui/src/main/resources/ui/src/pages/PoliciesPage/PoliciesDetailPage/PoliciesDetailPage.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/pages/PoliciesPage/PoliciesDetailPage/PoliciesDetailPage.tsx @@ -490,7 +490,9 @@ const PoliciesDetailPage = () => { direction="vertical" size={20}> {policy.rules.map((rule) => ( - + ({ + Link: jest.fn().mockImplementation(({ children, to, ...res }) => ( + + {children} + + )), +})); + +jest.mock('../../../components/common/DeleteWidget/DeleteWidgetModal', () => + jest.fn().mockReturnValue(
Delete Widget
) +); + +jest.mock( + '../../../components/common/rich-text-editor/RichTextEditorPreviewer', + () => jest.fn().mockReturnValue(
Previewer
) +); + +jest.mock('../../../components/PermissionProvider/PermissionProvider', () => ({ + usePermissionProvider: jest.fn().mockReturnValue({ + permissions: { + policy: { + Create: true, + Delete: true, + ViewAll: true, + EditAll: true, + EditDescription: true, + EditDisplayName: true, + EditCustomFields: true, + }, + role: { + Create: true, + Delete: true, + ViewAll: true, + EditAll: true, + EditDescription: true, + EditDisplayName: true, + EditCustomFields: true, + }, + }, + }), +})); + +jest.mock('../../../constants/HelperTextUtil', () => ({ + NO_PERMISSION_FOR_ACTION: '', + NO_PERMISSION_TO_VIEW: '', +})); + +jest.mock('../../../utils/CommonUtils', () => ({ + getEntityName: jest.fn().mockReturnValue(''), +})); + +jest.mock('../../../utils/PermissionsUtils', () => ({ + checkPermission: jest.fn().mockReturnValue(true), + LIST_CAP: 1, +})); + +jest.mock('../../../utils/RouterUtils', () => ({ + getPolicyWithFqnPath: jest.fn(), + getRoleWithFqnPath: jest.fn(), +})); + +const mockProps = { + policies: POLICY_LIST_WITH_PAGING.data as Policy[], + fetchPolicies: jest.fn(), +}; + +describe('Test Roles List Component', () => { + it('Should render the list component', async () => { + render(); + + const container = await screen.findByTestId('policies-list-table'); + + expect(container).toBeInTheDocument(); + }); + + it('Should render all table columns', async () => { + render(); + + const container = await screen.findByTestId('policies-list-table'); + + const nameCol = await screen.findByText('Name'); + const descriptionCol = await screen.findByText('Description'); + const rolesCol = await screen.findByText('Roles'); + const actionsCol = await screen.findByText('Actions'); + + expect(container).toBeInTheDocument(); + expect(nameCol).toBeInTheDocument(); + expect(descriptionCol).toBeInTheDocument(); + expect(rolesCol).toBeInTheDocument(); + expect(actionsCol).toBeInTheDocument(); + }); +}); diff --git a/openmetadata-ui/src/main/resources/ui/src/pages/PoliciesPage/PoliciesListPage/PoliciesList.tsx b/openmetadata-ui/src/main/resources/ui/src/pages/PoliciesPage/PoliciesListPage/PoliciesList.tsx index a0726284269..65b5dccf295 100644 --- a/openmetadata-ui/src/main/resources/ui/src/pages/PoliciesPage/PoliciesListPage/PoliciesList.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/pages/PoliciesPage/PoliciesListPage/PoliciesList.tsx @@ -173,6 +173,7 @@ const PoliciesList: FC = ({ policies, fetchPolicies }) => { ({ + useHistory: jest.fn().mockImplementation(() => ({ + push: mockPush, + })), +})); + +jest.mock('../../../axiosAPIs/rolesAPIV1', () => ({ + getPolicies: jest + .fn() + .mockImplementation(() => Promise.resolve(POLICY_LIST_WITH_PAGING)), +})); + +jest.mock('../../../components/common/next-previous/NextPrevious', () => + jest.fn().mockReturnValue(
NextPrevious
) +); + +jest.mock('../../../components/Loader/Loader', () => + jest.fn().mockReturnValue(
Loader
) +); + +jest.mock('./PoliciesList', () => + jest.fn().mockReturnValue(
PoliciesList
) +); + +jest.mock('../../../utils/PermissionsUtils', () => ({ + checkPermission: jest.fn().mockReturnValue(true), +})); + +jest.mock('../../../components/PermissionProvider/PermissionProvider', () => ({ + usePermissionProvider: jest.fn().mockReturnValue({ + permissions: { + policy: { + Create: true, + Delete: true, + ViewAll: true, + EditAll: true, + EditDescription: true, + EditDisplayName: true, + EditCustomFields: true, + }, + }, + }), +})); + +describe('Test Policies List Page', () => { + it('Should render the list component', async () => { + render(); + + const container = await screen.findByTestId('policies-list-container'); + const addPolicyButton = await screen.findByTestId('add-policy'); + + const policyList = await screen.findByTestId('policies-list'); + + expect(container).toBeInTheDocument(); + expect(addPolicyButton).toBeInTheDocument(); + expect(policyList).toBeInTheDocument(); + }); + + it('Add policy button should work', async () => { + render(); + + const container = await screen.findByTestId('policies-list-container'); + const addPolicyButton = await screen.findByTestId('add-policy'); + + expect(container).toBeInTheDocument(); + expect(addPolicyButton).toBeInTheDocument(); + + fireEvent.click(addPolicyButton); + + expect(mockPush).toHaveBeenCalledWith( + '/settings/access/policies/add-policy' + ); + }); +}); diff --git a/openmetadata-ui/src/main/resources/ui/src/pages/PoliciesPage/PoliciesListPage/PoliciesListPage.tsx b/openmetadata-ui/src/main/resources/ui/src/pages/PoliciesPage/PoliciesListPage/PoliciesListPage.tsx index 8aa4c4ae6bf..f53c29baef3 100644 --- a/openmetadata-ui/src/main/resources/ui/src/pages/PoliciesPage/PoliciesListPage/PoliciesListPage.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/pages/PoliciesPage/PoliciesListPage/PoliciesListPage.tsx @@ -110,7 +110,10 @@ const PoliciesListPage = () => { ) : isEmpty(policies) ? ( fetchErrorPlaceHolder() ) : ( - + ({ getPolicies: jest .fn() .mockImplementation(() => Promise.resolve(POLICY_LIST_WITH_PAGING)), + getRoles: jest + .fn() + .mockImplementation(() => Promise.resolve(ROLES_LIST_WITH_PAGING)), })); jest.mock( @@ -36,6 +39,10 @@ jest.mock('../../../utils/CommonUtils', () => ({ getEntityName: jest.fn().mockReturnValue('data'), })); +jest.mock('../../../utils/ToastUtils', () => ({ + showErrorToast: jest.fn(), +})); + const onSave = jest.fn(); const onCancel = jest.fn(); @@ -82,7 +89,9 @@ describe('Test Add attribute modal', () => { expect(sumbitButton).toBeInTheDocument(); - fireEvent.click(sumbitButton); + act(() => { + fireEvent.click(sumbitButton); + }); expect(onSave).toBeCalled(); }); @@ -94,7 +103,9 @@ describe('Test Add attribute modal', () => { expect(cancelButton).toBeInTheDocument(); - fireEvent.click(cancelButton); + act(() => { + fireEvent.click(cancelButton); + }); expect(onCancel).toBeCalled(); });