fix(editor): Allow connecting node to itself on new canvas (no-changelog) (#11143)

This commit is contained in:
Alex Grozav 2024-10-08 11:15:36 +03:00 committed by GitHub
parent 8566b3a999
commit 2161ff1217
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 98 additions and 73 deletions

View file

@ -1093,14 +1093,6 @@ describe('useCanvasOperations', () => {
}); });
describe('isConnectionAllowed', () => { describe('isConnectionAllowed', () => {
it('should return false if source and target nodes are the same', () => {
const node = mockNode({ id: '1', type: 'testType', name: 'Test Node' });
const { isConnectionAllowed } = useCanvasOperations({ router });
expect(
isConnectionAllowed(node, node, NodeConnectionType.Main, NodeConnectionType.Main),
).toBe(false);
});
it('should return false if target node type does not have inputs', () => { it('should return false if target node type does not have inputs', () => {
const workflowsStore = mockedStore(useWorkflowsStore); const workflowsStore = mockedStore(useWorkflowsStore);
const nodeTypesStore = mockedStore(useNodeTypesStore); const nodeTypesStore = mockedStore(useNodeTypesStore);
@ -1342,63 +1334,63 @@ describe('useCanvasOperations', () => {
).toBe(false); ).toBe(false);
}); });
// it('should return true if all conditions including filter are met', () => { it('should return true if all conditions including filter are met', () => {
// const workflowsStore = mockedStore(useWorkflowsStore); const workflowsStore = mockedStore(useWorkflowsStore);
// const nodeTypesStore = mockedStore(useNodeTypesStore); const nodeTypesStore = mockedStore(useNodeTypesStore);
//
// const sourceNode = mockNode({ const sourceNode = mockNode({
// id: '1', id: '1',
// type: 'sourceType', type: 'sourceType',
// name: 'Source Node', name: 'Source Node',
// typeVersion: 1, typeVersion: 1,
// }); });
// const sourceNodeTypeDescription = mockNodeTypeDescription({ const sourceNodeTypeDescription = mockNodeTypeDescription({
// name: sourceNode.type, name: sourceNode.type,
// outputs: [NodeConnectionType.Main], outputs: [NodeConnectionType.Main],
// }); });
//
// const targetNode = mockNode({ const targetNode = mockNode({
// id: '2', id: '2',
// type: 'targetType', type: 'targetType',
// name: 'Target Node', name: 'Target Node',
// typeVersion: 1, typeVersion: 1,
// }); });
// const targetNodeTypeDescription = mockNodeTypeDescription({ const targetNodeTypeDescription = mockNodeTypeDescription({
// name: targetNode.type, name: targetNode.type,
// inputs: [ inputs: [
// { {
// type: NodeConnectionType.Main, type: NodeConnectionType.Main,
// filter: { filter: {
// nodes: [sourceNode.type], nodes: [sourceNode.type],
// }, },
// }, },
// ], ],
// }); });
//
// const workflowObject = createTestWorkflowObject(workflowsStore.workflow); const workflowObject = createTestWorkflowObject(workflowsStore.workflow);
// workflowsStore.getCurrentWorkflow.mockReturnValue(workflowObject); workflowsStore.getCurrentWorkflow.mockReturnValue(workflowObject);
//
// const { isConnectionAllowed, editableWorkflowObject } = useCanvasOperations({ router }); const { isConnectionAllowed, editableWorkflowObject } = useCanvasOperations({ router });
//
// editableWorkflowObject.value.nodes[sourceNode.name] = sourceNode; editableWorkflowObject.value.nodes[sourceNode.name] = sourceNode;
// editableWorkflowObject.value.nodes[targetNode.name] = targetNode; editableWorkflowObject.value.nodes[targetNode.name] = targetNode;
// nodeTypesStore.getNodeType = vi.fn( nodeTypesStore.getNodeType = vi.fn(
// (nodeTypeName: string) => (nodeTypeName: string) =>
// ({ ({
// [sourceNode.type]: sourceNodeTypeDescription, [sourceNode.type]: sourceNodeTypeDescription,
// [targetNode.type]: targetNodeTypeDescription, [targetNode.type]: targetNodeTypeDescription,
// })[nodeTypeName], })[nodeTypeName],
// ); );
//
// expect( expect(
// isConnectionAllowed( isConnectionAllowed(
// sourceNode, sourceNode,
// targetNode, targetNode,
// NodeConnectionType.Main, NodeConnectionType.Main,
// NodeConnectionType.Main, NodeConnectionType.Main,
// ), ),
// ).toBe(true); ).toBe(true);
// }); });
it('should return true if all conditions are met and no filter is set', () => { it('should return true if all conditions are met and no filter is set', () => {
const workflowsStore = mockedStore(useWorkflowsStore); const workflowsStore = mockedStore(useWorkflowsStore);
@ -1454,6 +1446,44 @@ describe('useCanvasOperations', () => {
), ),
).toBe(true); ).toBe(true);
}); });
it('should return true if node connecting to itself', () => {
const workflowsStore = mockedStore(useWorkflowsStore);
const nodeTypesStore = mockedStore(useNodeTypesStore);
const sourceNode = mockNode({
id: '1',
type: 'sourceType',
name: 'Source Node',
typeVersion: 1,
});
const sourceNodeTypeDescription = mockNodeTypeDescription({
name: sourceNode.type,
outputs: [NodeConnectionType.Main],
});
const workflowObject = createTestWorkflowObject(workflowsStore.workflow);
workflowsStore.getCurrentWorkflow.mockReturnValue(workflowObject);
const { isConnectionAllowed, editableWorkflowObject } = useCanvasOperations({ router });
editableWorkflowObject.value.nodes[sourceNode.name] = sourceNode;
nodeTypesStore.getNodeType = vi.fn(
(nodeTypeName: string) =>
({
[sourceNode.type]: sourceNodeTypeDescription,
})[nodeTypeName],
);
expect(
isConnectionAllowed(
sourceNode,
sourceNode,
NodeConnectionType.Main,
NodeConnectionType.Main,
),
).toBe(true);
});
}); });
describe('deleteConnection', () => { describe('deleteConnection', () => {

View file

@ -176,7 +176,7 @@ describe('useNodeConnections', () => {
connections: defaultConnections, connections: defaultConnections,
}); });
it('returns false if source and target nodes are the same', () => { it('returns true if source and target nodes are the same', () => {
const connection = { const connection = {
source: 'node1', source: 'node1',
target: 'node1', target: 'node1',
@ -191,7 +191,7 @@ describe('useNodeConnections', () => {
index: 0, index: 0,
}), }),
}; };
expect(isValidConnection(connection)).toBe(false); expect(isValidConnection(connection)).toBe(true);
}); });
it('returns false if source and target handles are of the same mode', () => { it('returns false if source and target handles are of the same mode', () => {

View file

@ -1299,10 +1299,6 @@ export function useCanvasOperations({ router }: { router: ReturnType<typeof useR
): boolean { ): boolean {
const blocklist = [STICKY_NODE_TYPE]; const blocklist = [STICKY_NODE_TYPE];
if (sourceNode.id === targetNode.id) {
return false;
}
if (sourceConnectionType !== targetConnectionType) { if (sourceConnectionType !== targetConnectionType) {
return false; return false;
} }

View file

@ -63,11 +63,10 @@ export function useNodeConnections({
connection.targetHandle, connection.targetHandle,
); );
const isSameNode = connection.source === connection.target;
const isSameMode = sourceMode === targetMode; const isSameMode = sourceMode === targetMode;
const isSameType = sourceType === targetType; const isSameType = sourceType === targetType;
return !isSameNode && !isSameMode && isSameType; return !isSameMode && isSameType;
} }
return { return {