|
| 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 | +}); |
0 commit comments