Add unit tests for policies components (#7467)

* Add unit tests for policies components

* Add unit test for policy listing page
This commit is contained in:
Sachin Chaurasiya 2022-09-19 10:19:51 +05:30 committed by GitHub
parent 449526e590
commit 404dba18b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 393 additions and 6 deletions

View File

@ -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(<div data-testid="description-data">Description</div>)
);
jest.mock(
'../../../components/common/error-with-placeholder/ErrorPlaceHolder',
() => jest.fn().mockReturnValue(<div>ErrorPlaceholder</div>)
);
jest.mock(
'../../../components/common/rich-text-editor/RichTextEditorPreviewer',
() => jest.fn().mockReturnValue(<div data-testid="previewer">Previewer</div>)
);
jest.mock(
'../../../components/common/title-breadcrumb/title-breadcrumb.component',
() =>
jest.fn().mockReturnValue(<div data-testid="breadcrumb">BreadCrumb</div>)
);
jest.mock('../../../components/Loader/Loader', () =>
jest.fn().mockReturnValue(<div>Loader</div>)
);
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(<PoliciesDetailPage />);
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(<PoliciesDetailPage />);
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();
});
});

View File

@ -490,7 +490,9 @@ const PoliciesDetailPage = () => {
direction="vertical"
size={20}>
{policy.rules.map((rule) => (
<Card key={rule.name || 'rule'}>
<Card
data-testid="rule-card"
key={rule.name || 'rule'}>
<Space
align="baseline"
className="tw-w-full tw-justify-between tw-pb-5"

View File

@ -0,0 +1,111 @@
/*
* 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 } from '../../../generated/entity/policies/policy';
import { POLICY_LIST_WITH_PAGING } from '../../RolesPage/Roles.mock';
import PoliciesList from './PoliciesList';
jest.mock('react-router-dom', () => ({
Link: jest.fn().mockImplementation(({ children, to, ...res }) => (
<a href={to} {...res}>
{children}
</a>
)),
}));
jest.mock('../../../components/common/DeleteWidget/DeleteWidgetModal', () =>
jest.fn().mockReturnValue(<div>Delete Widget</div>)
);
jest.mock(
'../../../components/common/rich-text-editor/RichTextEditorPreviewer',
() => jest.fn().mockReturnValue(<div data-testid="previewer">Previewer</div>)
);
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(<PoliciesList {...mockProps} />);
const container = await screen.findByTestId('policies-list-table');
expect(container).toBeInTheDocument();
});
it('Should render all table columns', async () => {
render(<PoliciesList {...mockProps} />);
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();
});
});

View File

@ -173,6 +173,7 @@ const PoliciesList: FC<PolicyListProps> = ({ policies, fetchPolicies }) => {
<Table
className="policies-list-table"
columns={columns}
data-testid="policies-list-table"
dataSource={policies}
pagination={false}
size="small"

View File

@ -0,0 +1,94 @@
/*
* 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 { fireEvent, render, screen } from '@testing-library/react';
import React from 'react';
import { POLICY_LIST_WITH_PAGING } from '../../RolesPage/Roles.mock';
import PoliciesListPage from './PoliciesListPage';
const mockPush = jest.fn();
jest.mock('react-router-dom', () => ({
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(<div>NextPrevious</div>)
);
jest.mock('../../../components/Loader/Loader', () =>
jest.fn().mockReturnValue(<div>Loader</div>)
);
jest.mock('./PoliciesList', () =>
jest.fn().mockReturnValue(<div data-testid="policies-list">PoliciesList</div>)
);
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(<PoliciesListPage />);
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(<PoliciesListPage />);
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'
);
});
});

View File

@ -110,7 +110,10 @@ const PoliciesListPage = () => {
) : isEmpty(policies) ? (
fetchErrorPlaceHolder()
) : (
<Row className="policies-list-container" gutter={[16, 16]}>
<Row
className="policies-list-container"
data-testid="policies-list-container"
gutter={[16, 16]}>
<Col span={24}>
<Space align="center" className="tw-w-full tw-justify-end" size={16}>
<Tooltip

View File

@ -11,16 +11,19 @@
* limitations under the License.
*/
import { fireEvent, render, screen } from '@testing-library/react';
import { act, fireEvent, render, screen } from '@testing-library/react';
import React from 'react';
import { EntityType } from '../../../enums/entity.enum';
import { POLICY_LIST_WITH_PAGING } from '../Roles.mock';
import { POLICY_LIST_WITH_PAGING, ROLES_LIST_WITH_PAGING } from '../Roles.mock';
import AddAttributeModal from './AddAttributeModal';
jest.mock('../../../axiosAPIs/rolesAPIV1', () => ({
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();
});