Copilot commented on code in PR #725:
URL: https://github.com/apache/shenyu-dashboard/pull/725#discussion_r4336140689
##########
src/models/instance.js:
##########
@@ -21,20 +21,36 @@ import {
findInstanceAnalysis,
} from "../services/api";
+// Whether a response for `namespaceId` arrived after the user had already
+// switched to another namespace, so it must not replace the newer data.
+function* isFromPreviousNamespace(namespaceId, select) {
+ if (namespaceId === undefined) {
+ return false;
+ }
+ const currentNamespaceId = yield select(
+ ({ global }) => global.currentNamespaceId,
+ );
+ return namespaceId !== currentNamespaceId;
+}
+
export default {
namespace: "instance",
state: {
instanceList: [],
total: 0,
pieData: [],
- lineList: [],
+ lineData: [],
+ analysisNamespaceId: undefined,
},
effects: {
- *fetch(params, { call, put }) {
+ *fetch(params, { call, put, select }) {
const { payload } = params;
const json = yield call(getInstancesByNamespace, payload);
+ if (yield* isFromPreviousNamespace(payload.namespaceId, select)) {
+ return;
+ }
Review Comment:
`fetch` always makes the network request even if the namespace is already
stale at effect start, and only drops the response afterwards. Since
`fetchAnalysis` now has a pre-call guard to skip stale requests, consider
adding the same early check in `fetch` (i.e., check staleness before
`call(...)`) to avoid unnecessary requests/load when rapid namespace switching
occurs.
##########
src/models/instance.js:
##########
@@ -21,20 +21,36 @@ import {
findInstanceAnalysis,
} from "../services/api";
+// Whether a response for `namespaceId` arrived after the user had already
+// switched to another namespace, so it must not replace the newer data.
+function* isFromPreviousNamespace(namespaceId, select) {
Review Comment:
This comment is now slightly inaccurate because `isFromPreviousNamespace` is
also used as a *pre-request* guard in `fetchAnalysis` (to skip clearing/loading
when the user has already switched away). Update the comment to reflect that it
detects whether the namespace is no longer current (usable both before starting
a request and before applying a response).
##########
src/routes/System/Instance/namespaceSwitch.test.js:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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 ConnectedInstance from "./index";
+
+jest.mock("echarts", () => ({ init: jest.fn() }));
+jest.mock(
+ "../../../utils/AuthButton",
+ () =>
+ ({ children }) =>
+ children,
+);
+jest.mock("../../../utils/IntlUtils", () => ({
+ getCurrentLocale: jest.fn(),
+ getIntlContent: (key) => key,
+}));
+
+const Instance = ConnectedInstance.WrappedComponent;
+
+it("refreshes the table and the analysis when the namespace changes", () => {
+ const dispatch = jest.fn();
+ const instance = { pieData: [], lineData: [] };
+ const prevProps = {
+ dispatch,
+ instance,
+ language: "en-US",
+ currentNamespaceId: "namespace-a",
+ };
+ const component = new Instance({
+ ...prevProps,
+ currentNamespaceId: "namespace-b",
+ });
+
+ component.componentDidUpdate(prevProps);
+
+ expect(dispatch.mock.calls.map(([action]) => action)).toEqual([
+ {
+ type: "instance/fetch",
+ payload: expect.objectContaining({ namespaceId: "namespace-b" }),
+ },
+ {
+ type: "instance/fetchAnalysis",
+ payload: { namespaceId: "namespace-b" },
+ },
+ ]);
+});
+
+describe("chart rendering after the analysis is cleared", () => {
+ const echarts = jest.requireActual("echarts");
+ // jsdom has no canvas, so let ECharts estimate text widths instead.
+ echarts.setPlatformAPI({ createCanvas: () => null });
+ let component;
Review Comment:
`echarts.setPlatformAPI(...)` mutates global ECharts platform state for the
entire Jest environment, which can leak across tests and cause order-dependent
failures. Consider moving this into a `beforeAll` and restoring in an
`afterAll` (e.g., by saving the previous platform API if possible), or
isolating this suite via `jest.isolateModules()`/`jest.resetModules()` so the
mutation can’t affect other test files.
--
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]