fix(n8n Trigger Node): Merge with Workflow Trigger node (#11174)
Some checks are pending
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions

This commit is contained in:
Michael Kret 2024-10-10 08:00:55 +03:00 committed by GitHub
parent d78ab2983a
commit 6ec6b5197a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 141 additions and 4 deletions

View file

@ -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 = {
@ -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';
}

View file

@ -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');
});
});
});

View file

@ -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',
@ -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',