From f032cbccaf3736c95cdb2783f3ddcfb9da7b2585 Mon Sep 17 00:00:00 2001
From: Abhishek Porwal <80886271+Abhishek332@users.noreply.github.com>
Date: Fri, 9 Feb 2024 17:24:16 +0530
Subject: [PATCH] added unit test for AppInstall component (#15117)
---
.../src/pages/AppInstall/AppInstall.test.tsx | 270 ++++++++++++++++++
1 file changed, 270 insertions(+)
create mode 100644 openmetadata-ui/src/main/resources/ui/src/pages/AppInstall/AppInstall.test.tsx
diff --git a/openmetadata-ui/src/main/resources/ui/src/pages/AppInstall/AppInstall.test.tsx b/openmetadata-ui/src/main/resources/ui/src/pages/AppInstall/AppInstall.test.tsx
new file mode 100644
index 00000000000..c06b2056ff7
--- /dev/null
+++ b/openmetadata-ui/src/main/resources/ui/src/pages/AppInstall/AppInstall.test.tsx
@@ -0,0 +1,270 @@
+/*
+ * 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 { AppMarketPlaceDefinition } from '../../generated/entity/applications/marketplace/appMarketPlaceDefinition';
+import AppInstall from './AppInstall.component';
+
+const mockPush = jest.fn();
+const mockShowErrorToast = jest.fn();
+const mockShowSuccessToast = jest.fn();
+const mockFormatFormDataForSubmit = jest.fn();
+const mockInstallApplication = jest.fn();
+const mockGetMarketPlaceApplicationByFqn = jest
+ .fn()
+ .mockResolvedValue({} as AppMarketPlaceDefinition);
+const ERROR = 'ERROR';
+const MARKETPLACE_DATA = {
+ allowConfiguration: true,
+};
+
+jest.mock('react-router-dom', () => ({
+ useHistory: jest.fn().mockImplementation(() => ({
+ push: mockPush,
+ })),
+}));
+
+jest.mock(
+ '../../components/AddDataQualityTest/components/TestSuiteScheduler',
+ () =>
+ jest.fn().mockImplementation(({ onSubmit, onCancel }) => (
+ <>
+ TestSuiteScheduler
+
+
+ >
+ ))
+);
+
+jest.mock(
+ '../../components/Applications/AppDetails/ApplicationSchemaClassBase',
+ () => ({
+ importSchema: jest.fn().mockResolvedValue({}),
+ })
+);
+
+jest.mock(
+ '../../components/Applications/AppInstallVerifyCard/AppInstallVerifyCard.component',
+ () =>
+ jest.fn().mockImplementation(({ onSave, onCancel }) => (
+ <>
+ AppInstallVerifyCard
+
+
+ >
+ ))
+);
+
+jest.mock('../../components/common/ErrorWithPlaceholder/ErrorPlaceHolder', () =>
+ jest.fn().mockImplementation(() =>
ErrorPlaceHolder
)
+);
+
+jest.mock('../../components/common/FormBuilder/FormBuilder', () =>
+ jest.fn().mockImplementation(({ onSubmit, onCancel }) => (
+
+ FormBuilder
+
+
+
+ ))
+);
+
+jest.mock('../../components/IngestionStepper/IngestionStepper.component', () =>
+ jest.fn().mockImplementation(() => IngestionStepper
)
+);
+
+jest.mock('../../components/Loader/Loader', () =>
+ jest.fn().mockImplementation(() => Loader
)
+);
+
+jest.mock('../../components/PageLayoutV1/PageLayoutV1', () =>
+ jest.fn().mockImplementation(({ pageTitle, children }) => (
+
+ PageLayoutV1
+
{pageTitle}
+ {children}
+ {}
+
+ ))
+);
+
+jest.mock('../../hooks/useFqn', () => ({
+ useFqn: jest.fn().mockReturnValue({ fqn: 'test-fqn' }),
+}));
+
+jest.mock('../../rest/applicationAPI', () => ({
+ installApplication: jest.fn(() => mockInstallApplication()),
+}));
+
+jest.mock('../../rest/applicationMarketPlaceAPI', () => ({
+ getMarketPlaceApplicationByFqn: jest.fn(() =>
+ mockGetMarketPlaceApplicationByFqn()
+ ),
+}));
+
+jest.mock('../../utils/CommonUtils', () => ({
+ getIngestionFrequency: jest.fn(),
+ getEntityMissingError: jest.fn(),
+}));
+
+jest.mock('../../utils/JSONSchemaFormUtils', () => ({
+ formatFormDataForSubmit: jest.fn(() => mockFormatFormDataForSubmit()),
+}));
+
+jest.mock('../../utils/RouterUtils', () => ({
+ getMarketPlaceAppDetailsPath: jest.fn(),
+ getSettingPath: jest.fn(),
+}));
+
+jest.mock('../../utils/ToastUtils', () => ({
+ showErrorToast: jest.fn((...args) => mockShowErrorToast(...args)),
+ showSuccessToast: jest.fn((...args) => mockShowSuccessToast(...args)),
+}));
+
+describe('AppInstall component', () => {
+ it('should render necessary elements', async () => {
+ await act(async () => {
+ render();
+ });
+
+ expect(mockGetMarketPlaceApplicationByFqn).toHaveBeenCalled();
+
+ expect(screen.getByText('PageLayoutV1')).toBeInTheDocument();
+ expect(screen.getByText('IngestionStepper')).toBeInTheDocument();
+ expect(screen.getByText('AppInstallVerifyCard')).toBeInTheDocument();
+ });
+
+ it('actions check without allowConfiguration', async () => {
+ await act(async () => {
+ render();
+ });
+
+ // change ActiveServiceStep to 3
+ act(() => {
+ userEvent.click(
+ screen.getByRole('button', { name: 'Save AppInstallVerifyCard' })
+ );
+ });
+
+ expect(screen.getByText('TestSuiteScheduler')).toBeInTheDocument();
+ expect(screen.queryByText('AppInstallVerifyCard')).not.toBeInTheDocument();
+
+ // TestSuiteScheduler
+ await act(async () => {
+ userEvent.click(
+ screen.getByRole('button', { name: 'Submit TestSuiteScheduler' })
+ );
+ });
+
+ expect(mockInstallApplication).toHaveBeenCalled();
+ expect(mockShowSuccessToast).toHaveBeenCalledWith(
+ 'message.app-installed-successfully'
+ );
+
+ // change ActiveServiceStep to 1
+ act(() => {
+ userEvent.click(
+ screen.getByRole('button', { name: 'Cancel TestSuiteScheduler' })
+ );
+ });
+
+ expect(screen.getByText('AppInstallVerifyCard')).toBeInTheDocument();
+
+ userEvent.click(
+ screen.getByRole('button', { name: 'Cancel AppInstallVerifyCard' })
+ );
+
+ // will call for Submit TestSuiteScheduler and Cancel AppInstallVerifyCard
+ expect(mockPush).toHaveBeenCalledTimes(2);
+ });
+
+ it('actions check with allowConfiguration', async () => {
+ mockGetMarketPlaceApplicationByFqn.mockResolvedValueOnce(MARKETPLACE_DATA);
+
+ await act(async () => {
+ render();
+ });
+
+ // change ActiveServiceStep to 2
+ act(() => {
+ userEvent.click(
+ screen.getByRole('button', { name: 'Save AppInstallVerifyCard' })
+ );
+ });
+
+ expect(screen.getByText('FormBuilder')).toBeInTheDocument();
+ expect(screen.queryByText('AppInstallVerifyCard')).not.toBeInTheDocument();
+
+ // change ActiveServiceStep to 3
+ act(() => {
+ userEvent.click(
+ screen.getByRole('button', { name: 'Submit FormBuilder' })
+ );
+ });
+
+ expect(screen.getByText('TestSuiteScheduler')).toBeInTheDocument();
+
+ // change ActiveServiceStep to 2
+ act(() => {
+ userEvent.click(
+ screen.getByRole('button', { name: 'Cancel TestSuiteScheduler' })
+ );
+ });
+
+ // change ActiveServiceStep to 1
+ act(() => {
+ userEvent.click(
+ screen.getByRole('button', { name: 'Cancel FormBuilder' })
+ );
+ });
+
+ expect(screen.getByText('AppInstallVerifyCard')).toBeInTheDocument();
+ });
+
+ it('errors check in fetching application data', async () => {
+ mockGetMarketPlaceApplicationByFqn.mockRejectedValueOnce(ERROR);
+
+ await act(async () => {
+ render();
+ });
+
+ expect(mockShowErrorToast).toHaveBeenCalledWith(ERROR);
+ expect(screen.getByText('ErrorPlaceHolder')).toBeInTheDocument();
+ });
+
+ it('error check in installing application', async () => {
+ mockInstallApplication.mockRejectedValueOnce(ERROR);
+
+ await act(async () => {
+ render();
+ });
+
+ // change ActiveServiceStep to 3
+ act(() => {
+ userEvent.click(
+ screen.getByRole('button', { name: 'Save AppInstallVerifyCard' })
+ );
+ });
+
+ expect(screen.getByText('TestSuiteScheduler')).toBeInTheDocument();
+
+ await act(async () => {
+ userEvent.click(
+ screen.getByRole('button', { name: 'Submit TestSuiteScheduler' })
+ );
+ });
+
+ expect(mockShowErrorToast).toHaveBeenCalledWith(ERROR);
+ });
+});