fix(editor): Connecting nodes to triggers when adding them together (#9042)

This commit is contained in:
Milorad FIlipović 2024-04-05 15:46:02 +02:00 committed by GitHub
parent 32df17104c
commit f2143620ba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 107 additions and 4 deletions

View file

@ -660,16 +660,19 @@ describe('NDV', () => {
});
it('Stop listening for trigger event from NDV', () => {
cy.intercept('POST', '/rest/workflows/run').as('workflowRun');
workflowPage.actions.addInitialNodeToCanvas('Local File Trigger', {
keepNdvOpen: true,
action: 'On Changes To A Specific File',
isTrigger: true,
});
ndv.getters.triggerPanelExecuteButton().should('exist');
ndv.getters.triggerPanelExecuteButton().click();
ndv.getters.triggerPanelExecuteButton().realClick();
ndv.getters.triggerPanelExecuteButton().should('contain', 'Stop Listening');
ndv.getters.triggerPanelExecuteButton().click();
ndv.getters.triggerPanelExecuteButton().should('contain', 'Test step');
workflowPage.getters.successToast().should('exist');
ndv.getters.triggerPanelExecuteButton().realClick();
cy.wait('@workflowRun').then(() => {
ndv.getters.triggerPanelExecuteButton().should('contain', 'Test step');
workflowPage.getters.successToast().should('exist');
});
});
});

View file

@ -1,6 +1,7 @@
import { setActivePinia } from 'pinia';
import { createTestingPinia } from '@pinia/testing';
import { useNodeCreatorStore } from '@/stores/nodeCreator.store';
import { useNodeTypesStore } from '@/stores/nodeTypes.store';
import { useWorkflowsStore } from '@/stores/workflows.store';
import { useActions } from '../composables/useActions';
import {
@ -9,8 +10,10 @@ import {
NODE_CREATOR_OPEN_SOURCES,
NO_OP_NODE_TYPE,
SCHEDULE_TRIGGER_NODE_TYPE,
SLACK_NODE_TYPE,
SPLIT_IN_BATCHES_NODE_TYPE,
TRIGGER_NODE_CREATOR_VIEW,
WEBHOOK_NODE_TYPE,
} from '@/constants';
describe('useActions', () => {
@ -89,5 +92,85 @@ describe('useActions', () => {
],
});
});
test('should connect node to schedule trigger when adding them together', () => {
const workflowsStore = useWorkflowsStore();
const nodeCreatorStore = useNodeCreatorStore();
const nodeTypesStore = useNodeTypesStore();
vi.spyOn(workflowsStore, 'workflowTriggerNodes', 'get').mockReturnValue([
{ type: SCHEDULE_TRIGGER_NODE_TYPE } as never,
]);
vi.spyOn(nodeCreatorStore, 'openSource', 'get').mockReturnValue(
NODE_CREATOR_OPEN_SOURCES.ADD_NODE_BUTTON,
);
vi.spyOn(nodeCreatorStore, 'selectedView', 'get').mockReturnValue(TRIGGER_NODE_CREATOR_VIEW);
nodeTypesStore.nodeTypes = {
[SCHEDULE_TRIGGER_NODE_TYPE]: {
1: {
name: SCHEDULE_TRIGGER_NODE_TYPE,
displayName: 'Schedule Trigger',
group: ['trigger'],
version: 1,
defaults: {},
inputs: [],
outputs: [],
properties: [],
description: '',
},
},
};
const { getAddedNodesAndConnections } = useActions();
expect(
getAddedNodesAndConnections([
{ type: SCHEDULE_TRIGGER_NODE_TYPE, openDetail: true },
{ type: SLACK_NODE_TYPE },
]),
).toEqual({
connections: [{ from: { nodeIndex: 0 }, to: { nodeIndex: 1 } }],
nodes: [{ type: SCHEDULE_TRIGGER_NODE_TYPE, openDetail: true }, { type: SLACK_NODE_TYPE }],
});
});
test('should connect node to webhook trigger when adding them together', () => {
const workflowsStore = useWorkflowsStore();
const nodeCreatorStore = useNodeCreatorStore();
const nodeTypesStore = useNodeTypesStore();
vi.spyOn(workflowsStore, 'workflowTriggerNodes', 'get').mockReturnValue([
{ type: SCHEDULE_TRIGGER_NODE_TYPE } as never,
]);
vi.spyOn(nodeCreatorStore, 'openSource', 'get').mockReturnValue(
NODE_CREATOR_OPEN_SOURCES.ADD_NODE_BUTTON,
);
vi.spyOn(nodeCreatorStore, 'selectedView', 'get').mockReturnValue(TRIGGER_NODE_CREATOR_VIEW);
nodeTypesStore.nodeTypes = {
[WEBHOOK_NODE_TYPE]: {
1: {
name: WEBHOOK_NODE_TYPE,
displayName: 'Webhook',
group: ['trigger'],
version: 1,
defaults: {},
inputs: [],
outputs: [],
properties: [],
description: '',
},
},
};
const { getAddedNodesAndConnections } = useActions();
expect(
getAddedNodesAndConnections([
{ type: WEBHOOK_NODE_TYPE, openDetail: true },
{ type: SLACK_NODE_TYPE },
]),
).toEqual({
connections: [{ from: { nodeIndex: 0 }, to: { nodeIndex: 1 } }],
nodes: [{ type: WEBHOOK_NODE_TYPE, openDetail: true }, { type: SLACK_NODE_TYPE }],
});
});
});
});

View file

@ -163,6 +163,18 @@ export const useActions = () => {
};
}
/**
* Checks if added nodes contain trigger followed by another node
* In this case, we should connect the trigger with the following node
*/
function shouldConnectWithExistingTrigger(addedNodes: AddedNode[]): boolean {
if (addedNodes.length === 2) {
const isTriggerNode = useNodeTypesStore().isTriggerNode(addedNodes[0].type);
return isTriggerNode;
}
return false;
}
function shouldPrependManualTrigger(addedNodes: AddedNode[]): boolean {
const { selectedView, openSource } = useNodeCreatorStore();
const { workflowTriggerNodes } = useWorkflowsStore();
@ -228,6 +240,11 @@ export const useActions = () => {
from: { nodeIndex: 0 },
to: { nodeIndex: 1 },
});
} else if (shouldConnectWithExistingTrigger(addedNodes)) {
connections.push({
from: { nodeIndex: 0 },
to: { nodeIndex: 1 },
});
}
addedNodes.forEach((node, index) => {