n8n/packages/nodes-base/test/nodes/TriggerHelpers.ts
Elias Meire 0ff0f1aa11
Some checks failed
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
Benchmark Docker Image CI / build (push) Has been cancelled
test(Schedule Trigger Node): Add tests and extract trigger test helper (no-changelog) (#10625)
2024-09-27 16:53:00 +01:00

95 lines
2.7 KiB
TypeScript

import { mock } from 'jest-mock-extended';
import merge from 'lodash/merge';
import { returnJsonArray, type InstanceSettings } from 'n8n-core';
import { ScheduledTaskManager } from 'n8n-core/dist/ScheduledTaskManager';
import type {
IDataObject,
INode,
INodeType,
ITriggerFunctions,
Workflow,
WorkflowExecuteMode,
} from 'n8n-workflow';
type MockDeepPartial<T> = Parameters<typeof mock<T>>[0];
type TestTriggerNodeOptions = {
node?: MockDeepPartial<INode>;
timezone?: string;
workflowStaticData?: IDataObject;
};
type TriggerNodeTypeClass = new () => INodeType & Required<Pick<INodeType, 'trigger'>>;
export const createTestTriggerNode = (Trigger: TriggerNodeTypeClass) => {
const trigger = new Trigger();
const emit: jest.MockedFunction<ITriggerFunctions['emit']> = jest.fn();
const setupTriggerFunctions = (
mode: WorkflowExecuteMode,
options: TestTriggerNodeOptions = {},
) => {
const timezone = options.timezone ?? 'Europe/Berlin';
const version = trigger.description.version;
const node = merge(
{
type: trigger.description.name,
name: trigger.description.defaults.name ?? `Test Node (${trigger.description.name})`,
typeVersion: typeof version === 'number' ? version : version.at(-1),
} satisfies Partial<INode>,
options.node,
) as INode;
const workflow = mock<Workflow>({ timezone: options.timezone ?? 'Europe/Berlin' });
const scheduledTaskManager = new ScheduledTaskManager(mock<InstanceSettings>());
const helpers = mock<ITriggerFunctions['helpers']>({
returnJsonArray,
registerCron: (cronExpression, onTick) =>
scheduledTaskManager.registerCron(workflow, cronExpression, onTick),
});
const triggerFunctions = mock<ITriggerFunctions>({
helpers,
emit,
getTimezone: () => timezone,
getNode: () => node,
getMode: () => mode,
getWorkflowStaticData: () => options.workflowStaticData ?? {},
getNodeParameter: (parameterName, fallback) => node.parameters[parameterName] ?? fallback,
});
return triggerFunctions;
};
return {
trigger: async (options: TestTriggerNodeOptions = {}) => {
const triggerFunctions = setupTriggerFunctions('trigger', options);
const response = await trigger.trigger.call(triggerFunctions);
expect(response?.manualTriggerFunction).toBeUndefined();
return {
close: jest.fn(response?.closeFunction),
emit,
};
},
triggerManual: async (options: TestTriggerNodeOptions = {}) => {
const triggerFunctions = setupTriggerFunctions('manual', options);
const response = await trigger.trigger.call(triggerFunctions);
expect(response?.manualTriggerFunction).toBeInstanceOf(Function);
await response?.manualTriggerFunction?.();
return {
close: jest.fn(response?.closeFunction),
emit,
};
},
};
};