diff --git a/openmetadata-ui/src/main/resources/ui/src/pages/CustomPropertiesPageV1/CustomPropertiesPageV1.test.tsx b/openmetadata-ui/src/main/resources/ui/src/pages/CustomPropertiesPageV1/CustomPropertiesPageV1.test.tsx new file mode 100644 index 00000000000..3141eda2b32 --- /dev/null +++ b/openmetadata-ui/src/main/resources/ui/src/pages/CustomPropertiesPageV1/CustomPropertiesPageV1.test.tsx @@ -0,0 +1,179 @@ +/* + * Copyright 2024 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 { act, render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import React from 'react'; +import { EntityTabs } from '../../enums/entity.enum'; +import { Type } from '../../generated/entity/type'; +import CustomEntityDetailV1 from './CustomPropertiesPageV1'; + +const mockPush = jest.fn(); + +jest.mock('react-router-dom', () => ({ + useHistory: jest.fn().mockImplementation(() => ({ + push: mockPush, + })), + useParams: jest.fn().mockReturnValue({ + tab: EntityTabs.CUSTOM_PROPERTIES, + }), +})); + +jest.mock('../../components/common/ErrorWithPlaceholder/ErrorPlaceHolder', () => + jest.fn().mockImplementation(() =>
ErrorPlaceHolder
) +); + +jest.mock( + '../../components/common/TitleBreadcrumb/TitleBreadcrumb.component', + () => jest.fn(() =>
TitleBreadcrumb
) +); + +jest.mock('../../components/CustomEntityDetail/CustomPropertyTable', () => ({ + CustomPropertyTable: jest.fn(({ updateEntityType }) => ( + + )), +})); + +jest.mock('../../components/PageHeader/PageHeader.component', () => + jest.fn().mockReturnValue(
PageHeader
) +); + +jest.mock('../../components/PageLayoutV1/PageLayoutV1', () => + jest.fn(({ children }) =>
{children}
) +); + +const mockGetEntityPermission = jest.fn().mockResolvedValue({ + EditAll: true, +}); + +jest.mock('../../components/PermissionProvider/PermissionProvider', () => ({ + usePermissionProvider: jest.fn().mockImplementation(() => ({ + getEntityPermission: jest.fn(() => mockGetEntityPermission()), + })), +})); + +jest.mock('../../components/SchemaEditor/SchemaEditor', () => { + return jest.fn().mockImplementation(() =>
SchemaEditor
); +}); + +jest.mock('../../components/TabsLabel/TabsLabel.component', () => + jest.fn().mockImplementation(({ name }) =>
{name}
) +); + +const mockUpdateType = jest.fn().mockResolvedValue({}); +const mockGetTypeByFQN = jest.fn().mockResolvedValue({ id: 'id' }); + +jest.mock('../../rest/metadataTypeAPI', () => ({ + getTypeByFQN: jest.fn(() => mockGetTypeByFQN()), + updateType: jest.fn(() => mockUpdateType()), +})); + +jest.mock('../../utils/GlobalSettingsUtils', () => ({ + getSettingPageEntityBreadCrumb: jest.fn(), +})); + +const mockShowErrorToast = jest.fn(); + +jest.mock('../../utils/ToastUtils', () => ({ + showErrorToast: jest.fn((...args) => mockShowErrorToast(...args)), +})); + +describe('CustomPropertiesPageV1 component', () => { + it('actions check during render', async () => { + await act(async () => { + render(); + }); + + expect(mockGetTypeByFQN).toHaveBeenCalled(); + expect(mockGetEntityPermission).toHaveBeenCalled(); + }); + + it('tab change should work properly', async () => { + await act(async () => { + render(); + }); + + act(() => { + userEvent.click( + screen.getByRole('tab', { + name: 'label.schema', + }) + ); + }); + + expect(screen.getByText('SchemaEditor')).toBeInTheDocument(); + }); + + it('update entity type should call updateType api', async () => { + await act(async () => { + render(); + }); + + await act(async () => { + userEvent.click( + screen.getByRole('button', { + name: 'Update Entity Type', + }) + ); + }); + + expect(mockUpdateType).toHaveBeenCalled(); + }); + + it('add entity should call mockPush', async () => { + await act(async () => { + render(); + }); + + userEvent.click( + screen.getByRole('button', { + name: 'label.add-entity', + }) + ); + + expect(mockPush).toHaveBeenCalled(); + }); + + it('failed in fetch entityType should not fetch permission', async () => { + const ERROR = 'Error in fetching type'; + mockGetTypeByFQN.mockRejectedValueOnce(ERROR); + + await act(async () => { + render(); + }); + + expect(mockShowErrorToast).toHaveBeenCalledWith(ERROR); + expect(mockGetEntityPermission).not.toHaveBeenCalled(); + }); + + it('errors check', async () => { + mockGetEntityPermission.mockRejectedValueOnce('Error'); + mockUpdateType.mockRejectedValueOnce('Error'); + + await act(async () => { + render(); + }); + + // update entity type + await act(async () => { + userEvent.click( + screen.getByRole('button', { + name: 'Update Entity Type', + }) + ); + }); + + expect(mockShowErrorToast).toHaveBeenCalledTimes(2); + }); +});