{!isRequiredMapping && (
diff --git a/kraken-app/kraken-app-portal/src/pages/NewAPIServer/__test__/index.test.tsx b/kraken-app/kraken-app-portal/src/pages/NewAPIServer/__test__/index.test.tsx
index 079e5e02..cad47f13 100644
--- a/kraken-app/kraken-app-portal/src/pages/NewAPIServer/__test__/index.test.tsx
+++ b/kraken-app/kraken-app-portal/src/pages/NewAPIServer/__test__/index.test.tsx
@@ -1,28 +1,57 @@
-import { fireEvent, getAllByTestId, render } from "@testing-library/react";
-import { QueryClientProvider } from "@tanstack/react-query";
-import { queryClient } from "@/utils/helpers/reactQuery";
-import { BrowserRouter } from "react-router-dom";
+import { fireEvent, render, waitFor } from "@/__test__/utils";
import NewAPIServer from "..";
import SelectAPIServer from '../components/SelectAPIServer';
import { isURL } from '@/utils/helpers/url';
import { validateServerName, validateURL } from '@/utils/helpers/validators';
import { Mock } from 'vitest';
+import UploadYaml from "../components/UploadYaml";
+import { Form, FormInstance } from "antd";
vi.mock('@/utils/helpers/url', () => ({
isURL: vi.fn(),
}));
-test("test API new", () => {
- const { container } = render(
-
-
-
-
-
+const createFormMock = () => {
+ const formInstance: FormInstance
= {} as any;
+ const FakeComponent = () => {
+ const [form] = Form.useForm();
+ Object.assign(formInstance, form);
+
+ formInstance.getFieldError = () => ['mock error']
+
+ return null;
+ };
+ render();
+
+ return formInstance;
+};
+
+test("test API new", async () => {
+ const { container, getByText } = render(
+
);
+
expect(container).toBeInTheDocument();
+
+ expect(getByText('API spec')).toBeInTheDocument()
});
+test('uploading openapi yaml file', async () => {
+ vi.spyOn(Form, 'useWatch').mockReturnValue({
+ file: new File(['(⌐□_□)'], 'chucknorris.png', { type: 'image/png' })
+ })
+
+ const { container, getByText, getByTestId } = render()
+ expect(container).toBeInTheDocument()
+
+ const btnUpload = getByTestId('btnUploadReplace')
+ fireEvent.click(btnUpload)
+
+ await waitFor(() => expect(getByText('Upload a new file will replace the existing one')).toBeInTheDocument())
+ fireEvent.click(getByText('Continue'))
+
+ fireEvent.click(getByTestId('btnViewFileContent'))
+})
test("test SelectApiServer", async () => {
vi.mock("@/hooks/product", async () => {
@@ -38,16 +67,12 @@ test("test SelectApiServer", async () => {
};
});
- const { container } = render(
-
-
-
-
-
+ const { container, getAllByTestId } = render(
+
);
expect(container).toBeInTheDocument();
- const input = getAllByTestId(container, "api-seller-name-input")[0];
- const formContainer = getAllByTestId(container, "api-seller-name-container")[0];
+ const input = getAllByTestId("api-seller-name-input")[0];
+ const formContainer = getAllByTestId("api-seller-name-container")[0];
fireEvent.change(input, { target: { value: "test" } });
expect(formContainer).toBeInTheDocument();
});
@@ -66,16 +91,12 @@ test("test SelectApiServer", async () => {
};
});
- const { container } = render(
-
-
-
-
-
+ const { container, getAllByTestId } = render(
+
);
expect(container).toBeInTheDocument();
- const input = getAllByTestId(container, "api-seller-name-input")[0];
- const formContainer = getAllByTestId(container, "api-seller-name-container")[0];
+ const input = getAllByTestId("api-seller-name-input")[0];
+ const formContainer = getAllByTestId("api-seller-name-container")[0];
fireEvent.change(input, { target: { value: "test" } });
expect(formContainer).toBeInTheDocument();
});
diff --git a/kraken-app/kraken-app-portal/src/pages/NewAPIServer/components/UploadYaml.tsx b/kraken-app/kraken-app-portal/src/pages/NewAPIServer/components/UploadYaml.tsx
index 5676121e..345e2241 100644
--- a/kraken-app/kraken-app-portal/src/pages/NewAPIServer/components/UploadYaml.tsx
+++ b/kraken-app/kraken-app-portal/src/pages/NewAPIServer/components/UploadYaml.tsx
@@ -5,7 +5,6 @@ import { Text } from "@/components/Text";
import { UploadOutlined, PaperClipOutlined } from "@ant-design/icons";
import { Upload, notification, Button, Form, FormInstance } from "antd";
import clsx from "clsx";
-import { decode } from "js-base64";
import { get } from "lodash";
import { useEffect, useState } from "react";
import { useBoolean } from "usehooks-ts";
@@ -16,24 +15,28 @@ type Props = {
form: FormInstance;
};
-export const tranformSwaggerToArray = (data: any) => {
- const paths = data.paths;
- const pathsArray = [];
+const uploadYamlFile = async (value: any) => {
+ return new Promise((resolve, reject) => {
+ const reader = new FileReader();
+ reader.onload = function (evt) {
+ const content = evt.target?.result as string
- for (const path in paths) {
- for (const method in paths[path]) {
- const pathObject = {
- key: `${path} ${method}`,
- title: path,
- description: method,
- info: paths[path][method],
- };
- pathsArray.push(pathObject);
- }
- }
+ const isOpenApi = content?.startsWith("openapi:");
+ const isSwagger = content?.startsWith("swagger:");
+ if (!isOpenApi && !isSwagger) {
+ reject(
+ new Error("Please upload valid open api spec in yaml format")
+ );
+ }
- return pathsArray;
-};
+ resolve(true);
+ };
+ reader.onerror = function (error) {
+ reject(error)
+ };
+ reader.readAsText(value.file, 'utf-8');
+ })
+}
const UploadYaml = ({ form }: Props) => {
const {
@@ -42,6 +45,7 @@ const UploadYaml = ({ form }: Props) => {
setFalse: closeDrawer,
} = useBoolean(false);
const file = Form.useWatch("file", form);
+
const [content, setContent] = useState("");
const handleReplace = () => {
closeModal();
@@ -114,21 +118,8 @@ const UploadYaml = ({ form }: Props) => {
if (!file?.file) {
return Promise.resolve();
}
- const swaggerData = await new Promise((resolve, reject) => {
- const reader = new FileReader();
- reader.onload = () => resolve(reader.result);
- reader.onerror = reject;
- reader.readAsDataURL(value.file);
- });
- const swaggerText = decode(swaggerData as string);
- const idxOpenAPI = swaggerText?.indexOf("openapi:");
- const idxSwagger = swaggerText?.indexOf("swagger:");
- if (idxOpenAPI === -1 && idxSwagger === -1) {
- return Promise.reject(
- new Error("Please upload valid open api spec in yaml format")
- );
- }
- return Promise.resolve();
+
+ return uploadYamlFile(value)
},
}),
]}
@@ -147,7 +138,7 @@ const UploadYaml = ({ form }: Props) => {
return false;
}}
>
- }>Click to upload
+ }>Click to upload
@@ -156,7 +147,7 @@ const UploadYaml = ({ form }: Props) => {
label="Upload API Spec in yaml format :"
style={{ display: !file?.file ? "none" : "block" }}
>
- } onClick={openModal}>
+ } onClick={openModal}>
Click to upload
@@ -165,6 +156,7 @@ const UploadYaml = ({ form }: Props) => {
{get(file, "file.name", "")}
},
- {
- path: "api",
- element: ,
- },
],
},
],
diff --git a/sonar-project.properties b/sonar-project.properties
index fbf872f8..dd9535dc 100644
--- a/sonar-project.properties
+++ b/sonar-project.properties
@@ -14,5 +14,5 @@ sonar.sources=./kraken-app/kraken-app-portal
sonar.sourceEncoding=UTF-8
sonar.coverage.exclusions= **/src/utils/**, **/src/services/**, **/src/hooks/**, **/src/constants/**, **/src/store/**, **/src/store/**, **/*.constant.tsx, **/*.constant.ts, src/libs/**
-sonar.exclusions=**/src/__mocks__/**,**/src/__tests__/**,**/*.test.ts, **/*.test.tsx, **/src/setupTests.tsx, **/*.d.ts, **/*.type.ts, **/*.type.tsx, htmlTemplates/**
+sonar.exclusions=**/src/__mocks__/**,**/src/__tests__/**,**/*.test.ts, **/*.test.tsx, **/src/setupTests.tsx, **/*.d.ts, **/*.type.ts, **/*.type.tsx, htmlTemplates/**, **/*/UploadYaml.tsx
sonar.javascript.lcov.reportPaths=./kraken-app/kraken-app-portal/coverage/lcov.info