Skip to content

Commit

Permalink
fix(editor): Connecting nodes to triggers when adding them together (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MiloradFilipovic authored and netroy committed Apr 11, 2024
1 parent 2489009 commit ae26b8f
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 4 deletions.
11 changes: 7 additions & 4 deletions cypress/e2e/5-ndv.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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 }],
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit ae26b8f

Please sign in to comment.