fantonangeli commented on code in PR #3391: URL: https://github.com/apache/incubator-kie-tools/pull/3391#discussion_r2687303231
########## packages/runtime-tools-swf-enveloped-components/tests/components/SwfCombinedEditor.test.tsx: ########## @@ -0,0 +1,144 @@ +/* + * 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 * as React from "react"; +import { render, waitFor } from "@testing-library/react"; +import "@testing-library/jest-dom"; +import SwfCombinedEditor from "@kie-tools/runtime-tools-swf-enveloped-components/dist/workflowDetails/envelope/components/SwfCombinedEditor/SwfCombinedEditor"; +import { WorkflowInstance, WorkflowInstanceState } from "@kie-tools/runtime-tools-swf-gateway-api/dist/types"; + +global.TextDecoder = require("util").TextDecoder; + +// Mock for envelope API +const mockColorNodesSend = jest.fn(); +const mockSubscribe = jest.fn((callback) => { + callback(); + return { unsubscribe: jest.fn() }; +}); + +const mockEnvelopeAndChannelApi = { + notifications: { + kogitoSwfCombinedEditor_colorNodes: { + send: mockColorNodesSend, + }, + kogitoSwfCombinedEditor_combinedEditorReady: { + subscribe: mockSubscribe, + }, + }, +}; + +// Mock useController to provide a mock editor with envelope API +jest.mock("@kie-tools/runtime-tools-swf-enveloped-components/dist/workflowDetails/hooks/useController", () => ({ + useController: jest.fn(() => [ + { + getEnvelopeServer: () => ({ + envelopeApi: mockEnvelopeAndChannelApi, + }), + }, + jest.fn(), + ]), +})); + +describe("SwfCombinedEditor - ForEach node coloring", () => { + beforeEach(() => { + jest.clearAllMocks(); + mockColorNodesSend.mockClear(); + }); + + it("should color ForEach nodes using nodeDefinitions metadata", async () => { + const workflowInstance: WorkflowInstance = { + id: "c55b6474-d83b-47c5-ac32-bf12c9adc9e1", + processId: "foreach", + state: WorkflowInstanceState.Completed, + endpoint: "http://localhost:4000/foreach", + nodes: [ + { + id: "1", + nodeId: "n1", + name: "ForEach", + definitionId: "3", + type: "ForEachNode", + enter: new Date("2024-10-18T12:25:00.192Z"), + }, + { + id: "2", + nodeId: "n2", + name: "End", + definitionId: "2", + type: "EndNode", + enter: new Date("2024-10-18T12:26:00.192Z"), + }, Review Comment: It seems I forgot the `nodes` here. Let's add a start node as well. ```suggestion { id: "1", nodeId: "1", name: "Start", definitionId: "1", type: "StartNode", enter: new Date("2024-10-18T12:25:00.192Z"), }, { id: "2", nodeId: "2", name: "ForEach", definitionId: "3", type: "ForEachNode", enter: new Date("2024-10-18T12:25:00.192Z"), }, { id: "3", nodeId: "3", name: "End", definitionId: "2", type: "EndNode", enter: new Date("2024-10-18T12:26:00.192Z"), }, ``` ########## packages/runtime-tools-swf-enveloped-components/src/workflowDetails/envelope/components/SwfCombinedEditor/SwfCombinedEditor.tsx: ########## @@ -152,6 +152,11 @@ const SwfCombinedEditor: React.FC<ISwfCombinedEditorProps & OUIAProps> = ({ nodes.forEach((node) => { nodeNames.push(node.name); + const nodeDefinition = nodeDefinitions?.find((def) => def.id === node.definitionId); Review Comment: The `.find()` on `nodeDefinitions` is executed for every node in the forEach loop. If the array is large, this can have performance issues (nodes x nodesDefinitions). It's better to create a Map for the `nodeDefinitions` or an object once, with the definition id as key, for easy access. You can store it using `useMemo` hook to memoize it. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
