Copilot commented on code in PR #720:
URL: https://github.com/apache/shenyu-dashboard/pull/720#discussion_r4257901176


##########
src/routes/Document/ApiDoc.js:
##########
@@ -42,24 +42,18 @@ function ApiDoc() {
   const searchApiRef = React.createRef();
 
   const initData = async () => {
-    const { code, data = {} } = await getDocMenus();
+    const { code, data = [] } = await fetchShenYuDictByType({
+      type: "apidocEnv",
+    });
     if (code === 200) {
-      const { menuProjects = [] } = data;
-      const createKey = (treeData, keys) => {
-        treeData.forEach((item, index) => {
-          const { children, id, name } = item;
-          const key = [...keys, index].join("-");
-          item.key = key;
-          item.id = id;
-          item.name = name;
-          if (children?.length) {
-            createKey(children, [...keys, index]);
-          }
-        });
-      };
-      createKey(menuProjects, []);
-      data.menuProjects = menuProjects;
-      setApiData(data);
+      const envProps = data
+        .filter(({ enabled }) => enabled)
+        .map(({ dictName, dictValue, desc }) => ({
+          envLabel: dictName,
+          addressUrl: dictValue,
+          envDesc: desc,
+        }));
+      setApiData({ envProps });

Review Comment:
   `data = []` only applies when `data` is `undefined`; if the backend returns 
`null` (or any non-array) for `data`, `data.filter` will throw at runtime. 
Consider normalizing first (e.g., `const list = Array.isArray(data) ? data : 
[]`) and filtering/mapping from `list`, and optionally setting `envProps: []` 
for non-200 responses so downstream consumers don’t have to handle `undefined`.



##########
src/routes/Document/ApiDoc.test.js:
##########
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 React from "react";
+import { fireEvent, render, screen, waitFor } from "@testing-library/react";
+import {
+  fetchShenYuDictByType,
+  getApiDetail,
+  getApiMockRequest,
+} from "../../services/api";
+import ApiDoc from "./ApiDoc";
+
+jest.mock("../../services/api", () => ({
+  fetchShenYuDictByType: jest.fn(),
+  getApiDetail: jest.fn(),
+  getApiMockRequest: jest.fn(),
+}));
+

Review Comment:
   The mocked service functions keep call history across tests unless your Jest 
setup globally clears mocks. To make these tests isolated and 
order-independent, add a `beforeEach` that clears/reset mocks (e.g., 
`jest.clearAllMocks()` or `jest.resetAllMocks()` depending on your preference).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to