Copilot commented on code in PR #703: URL: https://github.com/apache/shenyu-dashboard/pull/703#discussion_r4229384568
########## src/routes/System/pagination.test.js: ########## @@ -0,0 +1,153 @@ +/* + * 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 ConnectedPlugin from "./Plugin"; +import ConnectedNamespacePlugin from "./NamespacePlugin"; +import ConnectedInstance from "./Instance"; +import ConnectedRole from "./Role"; +import ConnectedUser from "./User"; +import ConnectedScale from "./Scale"; +import ConnectedNamespace from "./Namespace"; + +jest.mock("./Plugin/AddModal", () => () => null); +jest.mock("./Role/AddModal", () => () => null); +jest.mock("./User/AddModal", () => () => null); +jest.mock("./User/DataPermModal", () => () => null); +jest.mock("./Scale/PolicyModal", () => () => null); +jest.mock("./Scale/RuleModal", () => () => null); +jest.mock("./Namespace/AddModal", () => () => null); +jest.mock( + "../../utils/AuthButton", + () => + ({ children }) => + children, +); +jest.mock("../../utils/IntlUtils", () => ({ + getCurrentLocale: jest.fn(), + getIntlContent: (key) => key, +})); +jest.mock("../../utils/AuthRoute", () => ({ refreshAuthMenus: jest.fn() })); +jest.mock("../../components/_utils/utils", () => ({ + defaultNamespaceId: "default", +})); + +const makeComponent = (Connected) => { + const dispatch = jest.fn(); + const component = new Connected.WrappedComponent({ + dispatch, + currentNamespaceId: "namespace-1", + namespace: { namespaceList: [] }, + }); + component.state = { + ...component.state, + currentPage: 3, + pageSize: 50, + selectedRowKeys: ["row-1"], + }; + component.setState = (update, callback) => { + component.state = { ...component.state, ...update }; + if (callback) callback(); + }; + return { component, dispatch }; +}; + +it.each([ + [ + ConnectedPlugin, + "searchOnNamechange", + "name", + { target: { value: "demo" } }, + ], + [ + ConnectedPlugin, + "searchOnRolechange", + "role", + { target: { value: "demo" } }, + ], + [ConnectedPlugin, "enabledOnchange", "enabled", false], + [ + ConnectedNamespacePlugin, + "searchOnchange", + "name", + { target: { value: "demo" } }, + ], + [ConnectedNamespacePlugin, "enabledOnchange", "enabled", false], + [ + ConnectedInstance, + "instanceIpOnchange", + "instanceIp", + { target: { value: "demo" } }, + ], + [ConnectedInstance, "instanceTypeOnchange", "instanceType", "bootstrap"], +])( + "resets page before querying (case %#)", + (Connected, handler, field, input) => { + const { component, dispatch } = makeComponent(Connected); + component[handler](input); + expect(component.state.currentPage).toBe(1); + expect(dispatch).toHaveBeenCalledTimes(1); + expect(dispatch.mock.calls[0][0].payload).toEqual( + expect.objectContaining({ + currentPage: 1, + pageSize: 50, + [field]: input && input.target ? input.target.value : input, + }), + ); + }, +); + +it.each([ + [ConnectedRole, "searchOnchange", "roleName"], + [ConnectedUser, "searchOnchange", "userName"], + [ConnectedScale, "searchMetricNameOnchange", "metricName"], +])( + "queries page one when Search is clicked (case %#)", + (Connected, handler, field) => { + const { component, dispatch } = makeComponent(Connected); + component[handler]({ target: { value: "demo" } }); + expect(dispatch).not.toHaveBeenCalled(); + component.searchClick(); + expect(component.state.currentPage).toBe(1); + expect(dispatch.mock.calls[0][0].payload).toEqual( + expect.objectContaining({ + currentPage: 1, + pageSize: 50, + [field]: "demo", + }), + ); + }, +); + +it.each([ConnectedPlugin, ConnectedNamespacePlugin, ConnectedNamespace])( + "preserves the selected page size and filters after deletion (case %#)", + (Connected) => { + const { component, dispatch } = makeComponent(Connected); + component.state = { + ...component.state, + name: "demo", + namespaceId: "filter-namespace", + }; + component.deleteClick(); + expect(dispatch.mock.calls[0][0].fetchValue).toEqual( + expect.objectContaining({ + currentPage: 3, + pageSize: 50, + name: "demo", Review Comment: This test sets `namespaceId` in state and the test name claims filters are preserved, but the assertion never checks that `namespaceId` is present in `fetchValue`. That leaves a gap where `namespaceId` could be dropped without this test catching it. Either assert `namespaceId: \"filter-namespace\"` is included in the dispatched `fetchValue`, or remove `namespaceId` from the test setup if it is not intended to be preserved here. ########## src/routes/Plugin/Common/index.js: ########## @@ -94,18 +94,20 @@ export default class Common extends Component { type: "common/resetData", }); - if (prevProps.plugins && prevProps.plugins.length > 0) { - this.getAllSelectors(selectorPage, selectorPageSize, prevProps.plugins); - } else { - dispatch({ - type: "global/fetchPlugins", - payload: { - callback: (pluginList) => { - this.getAllSelectors(selectorPage, selectorPageSize, pluginList); + this.setState({ selectorPage: 1 }, () => { + if (prevProps.plugins && prevProps.plugins.length > 0) { + this.getAllSelectors(1, selectorPageSize, prevProps.plugins); + } else { + dispatch({ + type: "global/fetchPlugins", + payload: { + callback: (pluginList) => { + this.getAllSelectors(1, selectorPageSize, pluginList); + }, }, - }, - }); - } + }); + } + }); Review Comment: This branch uses `prevProps.plugins` to decide whether to fetch plugins and to supply the plugin list to `getAllSelectors`. If `plugins` becomes available in the *current* props at the same time as the route id changes (e.g., store update + navigation), `prevProps.plugins` can be empty while `this.props.plugins` is already populated—triggering an unnecessary `global/fetchPlugins` and extra network/state churn. Prefer using `this.props.plugins` (or `plugins` destructured from current props) for both the length check and the list passed into `getAllSelectors`. ########## src/routes/Plugin/Common/selectorPagination.test.js: ########## @@ -66,3 +66,69 @@ it("resets rule pagination and uses rule page size when selecting a selector", ( }, }); }); + +const makeSearchComponent = (plugins = [{ name: "divide", pluginId: "5" }]) => { + const dispatch = jest.fn(); + const component = new Common({ + dispatch, + plugins, + match: { params: { id: "divide" } }, + currentNamespaceId: "namespace-1", + }); + component.state = { + ...component.state, + selectorPage: 3, + selectorPageSize: 50, + selectorName: "demo", + }; + component.setState = (update, callback) => { + component.state = { ...component.state, ...update }; + if (callback) callback(); + }; + return { component, dispatch }; +}; + +it("searches selectors from page one while preserving the filter and page size", () => { + const { component, dispatch } = makeSearchComponent(); + component.searchSelector(); + expect(component.state.selectorPage).toBe(1); + expect(dispatch).toHaveBeenCalledWith({ + type: "common/fetchSelector", + payload: { + currentPage: 1, + pageSize: 50, + pluginId: "5", + name: "demo", + namespaceId: "namespace-1", + }, + }); +}); + +it.each([true, false])( + "resets selector pagination when switching plugins (cached: %s)", + (cached) => { + const plugins = [{ name: "divide", pluginId: "5" }]; + const { component, dispatch } = makeSearchComponent(cached ? plugins : []); + component.componentDidUpdate({ + ...component.props, + match: { params: { id: "old-plugin" } }, + }); + if (!cached) { + const fetch = dispatch.mock.calls.find( + ([action]) => action.type === "global/fetchPlugins", + ); Review Comment: If the expected `global/fetchPlugins` dispatch does not occur, `fetch` will be `undefined` and this will throw a non-descriptive error (`Cannot read properties of undefined`). Adding an explicit expectation (e.g., assert `fetch` is defined) before invoking the callback will make failures easier to diagnose. -- 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]
