Skip to content

Commit 6ec6b51

Browse files
fix(n8n Trigger Node): Merge with Workflow Trigger node (#11174)
1 parent d78ab29 commit 6ec6b51

3 files changed

Lines changed: 141 additions & 4 deletions

File tree

packages/nodes-base/nodes/N8nTrigger/N8nTrigger.node.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
} from 'n8n-workflow';
77
import { NodeConnectionType } from 'n8n-workflow';
88

9-
type eventType = 'Instance started' | undefined;
9+
type eventType = 'Instance started' | 'Workflow activated' | 'Workflow updated' | undefined;
1010

1111
export class N8nTrigger implements INodeType {
1212
description: INodeTypeDescription = {
@@ -30,26 +30,46 @@ export class N8nTrigger implements INodeType {
3030
type: 'multiOptions',
3131
required: true,
3232
default: [],
33-
description:
34-
'Specifies under which conditions an execution should happen: <b>Instance started</b>: Triggers when this n8n instance is started or re-started',
33+
description: `Specifies under which conditions an execution should happen:
34+
<ul>
35+
<li><b>Active Workflow Updated</b>: Triggers when this workflow is updated</li>
36+
<li><b>Instance Started</b>: Triggers when this n8n instance is started or re-started</li>
37+
<li><b>Workflow Activated</b>: Triggers when this workflow is activated</li>
38+
</ul>`,
3539
options: [
40+
{
41+
name: 'Active Workflow Updated',
42+
value: 'update',
43+
description: 'Triggers when this workflow is updated',
44+
},
3645
{
3746
name: 'Instance Started',
3847
value: 'init',
3948
description: 'Triggers when this n8n instance is started or re-started',
4049
},
50+
{
51+
name: 'Workflow Activated',
52+
value: 'activate',
53+
description: 'Triggers when this workflow is activated',
54+
},
4155
],
4256
},
4357
],
4458
};
4559

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

4963
const activationMode = this.getActivationMode();
5064

5165
if (events.includes(activationMode)) {
5266
let event: eventType;
67+
if (activationMode === 'activate') {
68+
event = 'Workflow activated';
69+
}
70+
if (activationMode === 'update') {
71+
event = 'Workflow updated';
72+
}
5373
if (activationMode === 'init') {
5474
event = 'Instance started';
5575
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/* eslint-disable n8n-nodes-base/node-filename-against-convention */
2+
import { N8nTrigger } from '../N8nTrigger.node';
3+
4+
describe('N8nTrigger', () => {
5+
let n8nTrigger: N8nTrigger;
6+
let mockTriggerFunctions: any;
7+
8+
beforeEach(() => {
9+
n8nTrigger = new N8nTrigger();
10+
11+
// Mock trigger functions
12+
mockTriggerFunctions = {
13+
emit: jest.fn(),
14+
getNodeParameter: jest.fn(),
15+
getActivationMode: jest.fn(),
16+
getWorkflow: jest.fn(() => ({ id: 'test-workflow-id' })),
17+
helpers: {
18+
returnJsonArray: jest.fn((data) => data),
19+
},
20+
};
21+
});
22+
23+
describe('trigger', () => {
24+
it('should emit event when activation mode matches selected events', async () => {
25+
mockTriggerFunctions.getNodeParameter.mockReturnValue(['activate']);
26+
mockTriggerFunctions.getActivationMode.mockReturnValue('activate');
27+
28+
await n8nTrigger.trigger.call(mockTriggerFunctions);
29+
30+
expect(mockTriggerFunctions.emit).toHaveBeenCalledWith([
31+
[
32+
{
33+
event: 'Workflow activated',
34+
timestamp: expect.any(String),
35+
workflow_id: 'test-workflow-id',
36+
},
37+
],
38+
]);
39+
});
40+
41+
it('should not emit event when activation mode does not match selected events', async () => {
42+
mockTriggerFunctions.getNodeParameter.mockReturnValue(['update']);
43+
mockTriggerFunctions.getActivationMode.mockReturnValue('activate');
44+
45+
await n8nTrigger.trigger.call(mockTriggerFunctions);
46+
47+
expect(mockTriggerFunctions.emit).not.toHaveBeenCalled();
48+
});
49+
50+
it('should return manual trigger function', async () => {
51+
const result = await n8nTrigger.trigger.call(mockTriggerFunctions);
52+
53+
expect(result).toHaveProperty('manualTriggerFunction');
54+
expect(typeof result.manualTriggerFunction).toBe('function');
55+
});
56+
57+
it('should emit correct event for instance started', async () => {
58+
mockTriggerFunctions.getNodeParameter.mockReturnValue(['init']);
59+
mockTriggerFunctions.getActivationMode.mockReturnValue('init');
60+
61+
await n8nTrigger.trigger.call(mockTriggerFunctions);
62+
63+
expect(mockTriggerFunctions.emit).toHaveBeenCalledWith([
64+
[
65+
{
66+
event: 'Instance started',
67+
timestamp: expect.any(String),
68+
workflow_id: 'test-workflow-id',
69+
},
70+
],
71+
]);
72+
});
73+
74+
it('should emit correct event for workflow updated', async () => {
75+
mockTriggerFunctions.getNodeParameter.mockReturnValue(['update']);
76+
mockTriggerFunctions.getActivationMode.mockReturnValue('update');
77+
78+
await n8nTrigger.trigger.call(mockTriggerFunctions);
79+
80+
expect(mockTriggerFunctions.emit).toHaveBeenCalledWith([
81+
[
82+
{
83+
event: 'Workflow updated',
84+
timestamp: expect.any(String),
85+
workflow_id: 'test-workflow-id',
86+
},
87+
],
88+
]);
89+
});
90+
});
91+
92+
describe('description', () => {
93+
it('should have correct properties', () => {
94+
expect(n8nTrigger.description).toMatchObject({
95+
displayName: 'n8n Trigger',
96+
name: 'n8nTrigger',
97+
group: ['trigger'],
98+
version: 1,
99+
});
100+
});
101+
102+
it('should have required properties configuration', () => {
103+
const eventsProperty = n8nTrigger.description.properties.find((p) => p.name === 'events');
104+
expect(eventsProperty).toBeDefined();
105+
expect(eventsProperty?.required).toBe(true);
106+
expect(eventsProperty?.type).toBe('multiOptions');
107+
});
108+
});
109+
});

packages/nodes-base/nodes/WorkflowTrigger/WorkflowTrigger.node.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type activationType = 'activate' | 'update';
1212
export class WorkflowTrigger implements INodeType {
1313
description: INodeTypeDescription = {
1414
displayName: 'Workflow Trigger',
15+
hidden: true,
1516
name: 'workflowTrigger',
1617
icon: 'fa:network-wired',
1718
iconColor: 'orange-red',
@@ -28,6 +29,13 @@ export class WorkflowTrigger implements INodeType {
2829
inputs: [],
2930
outputs: [NodeConnectionType.Main],
3031
properties: [
32+
{
33+
displayName:
34+
"This node is deprecated and would not be updated in the future. Please use 'n8n Trigger' node instead.",
35+
name: 'oldVersionNotice',
36+
type: 'notice',
37+
default: '',
38+
},
3139
{
3240
displayName: 'Events',
3341
name: 'events',

0 commit comments

Comments
 (0)