Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(n8n Trigger Node): Merge with Workflow Trigger node #11174

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions packages/nodes-base/nodes/N8nTrigger/N8nTrigger.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
} from 'n8n-workflow';
import { NodeConnectionType } from 'n8n-workflow';

type eventType = 'Instance started' | undefined;
type eventType = 'Instance started' | 'Workflow activated' | 'Workflow updated' | undefined;

export class N8nTrigger implements INodeType {
description: INodeTypeDescription = {
Expand All @@ -30,26 +30,46 @@ export class N8nTrigger implements INodeType {
type: 'multiOptions',
required: true,
default: [],
description:
'Specifies under which conditions an execution should happen: <b>Instance started</b>: Triggers when this n8n instance is started or re-started',
description: `Specifies under which conditions an execution should happen:
<ul>
<li><b>Active Workflow Updated</b>: Triggers when this workflow is updated</li>
<li><b>Instance Started</b>: Triggers when this n8n instance is started or re-started</li>
<li><b>Workflow Activated</b>: Triggers when this workflow is activated</li>
</ul>`,
options: [
{
name: 'Active Workflow Updated',
value: 'update',
description: 'Triggers when this workflow is updated',
},
{
name: 'Instance Started',
value: 'init',
description: 'Triggers when this n8n instance is started or re-started',
},
{
name: 'Workflow Activated',
value: 'activate',
description: 'Triggers when this workflow is activated',
},
],
},
],
};

async trigger(this: ITriggerFunctions): Promise<ITriggerResponse> {
const events = this.getNodeParameter('events', []) as string[];
const events = (this.getNodeParameter('events') as string[]) || [];

const activationMode = this.getActivationMode();

if (events.includes(activationMode)) {
let event: eventType;
if (activationMode === 'activate') {
event = 'Workflow activated';
}
if (activationMode === 'update') {
event = 'Workflow updated';
}
if (activationMode === 'init') {
event = 'Instance started';
}
Expand Down
109 changes: 109 additions & 0 deletions packages/nodes-base/nodes/N8nTrigger/test/trigger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/* eslint-disable n8n-nodes-base/node-filename-against-convention */
import { N8nTrigger } from '../N8nTrigger.node';

describe('N8nTrigger', () => {
let n8nTrigger: N8nTrigger;
let mockTriggerFunctions: any;

beforeEach(() => {
n8nTrigger = new N8nTrigger();

// Mock trigger functions
mockTriggerFunctions = {
emit: jest.fn(),
getNodeParameter: jest.fn(),
getActivationMode: jest.fn(),
getWorkflow: jest.fn(() => ({ id: 'test-workflow-id' })),
helpers: {
returnJsonArray: jest.fn((data) => data),
},
};
});

describe('trigger', () => {
it('should emit event when activation mode matches selected events', async () => {
mockTriggerFunctions.getNodeParameter.mockReturnValue(['activate']);
mockTriggerFunctions.getActivationMode.mockReturnValue('activate');

await n8nTrigger.trigger.call(mockTriggerFunctions);

expect(mockTriggerFunctions.emit).toHaveBeenCalledWith([
[
{
event: 'Workflow activated',
timestamp: expect.any(String),
workflow_id: 'test-workflow-id',
},
],
]);
});

it('should not emit event when activation mode does not match selected events', async () => {
mockTriggerFunctions.getNodeParameter.mockReturnValue(['update']);
mockTriggerFunctions.getActivationMode.mockReturnValue('activate');

await n8nTrigger.trigger.call(mockTriggerFunctions);

expect(mockTriggerFunctions.emit).not.toHaveBeenCalled();
});

it('should return manual trigger function', async () => {
const result = await n8nTrigger.trigger.call(mockTriggerFunctions);

expect(result).toHaveProperty('manualTriggerFunction');
expect(typeof result.manualTriggerFunction).toBe('function');
});

it('should emit correct event for instance started', async () => {
mockTriggerFunctions.getNodeParameter.mockReturnValue(['init']);
mockTriggerFunctions.getActivationMode.mockReturnValue('init');

await n8nTrigger.trigger.call(mockTriggerFunctions);

expect(mockTriggerFunctions.emit).toHaveBeenCalledWith([
[
{
event: 'Instance started',
timestamp: expect.any(String),
workflow_id: 'test-workflow-id',
},
],
]);
});

it('should emit correct event for workflow updated', async () => {
mockTriggerFunctions.getNodeParameter.mockReturnValue(['update']);
mockTriggerFunctions.getActivationMode.mockReturnValue('update');

await n8nTrigger.trigger.call(mockTriggerFunctions);

expect(mockTriggerFunctions.emit).toHaveBeenCalledWith([
[
{
event: 'Workflow updated',
timestamp: expect.any(String),
workflow_id: 'test-workflow-id',
},
],
]);
});
});

describe('description', () => {
it('should have correct properties', () => {
expect(n8nTrigger.description).toMatchObject({
displayName: 'n8n Trigger',
name: 'n8nTrigger',
group: ['trigger'],
version: 1,
});
});

it('should have required properties configuration', () => {
const eventsProperty = n8nTrigger.description.properties.find((p) => p.name === 'events');
expect(eventsProperty).toBeDefined();
expect(eventsProperty?.required).toBe(true);
expect(eventsProperty?.type).toBe('multiOptions');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type activationType = 'activate' | 'update';
export class WorkflowTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Workflow Trigger',
hidden: true,
name: 'workflowTrigger',
icon: 'fa:network-wired',
iconColor: 'orange-red',
Expand All @@ -28,6 +29,13 @@ export class WorkflowTrigger implements INodeType {
inputs: [],
outputs: [NodeConnectionType.Main],
properties: [
{
displayName:
"This node is deprecated and would not be updated in the future. Please use 'n8n Trigger' node instead.",
name: 'oldVersionNotice',
type: 'notice',
default: '',
},
{
displayName: 'Events',
name: 'events',
Expand Down
Loading