diff --git a/packages/nodes-base/nodes/If/test/v2/IfV2.node.test.ts b/packages/nodes-base/nodes/If/test/v2/IfV2.node.test.ts index d0531ff3c2..e21f877091 100644 --- a/packages/nodes-base/nodes/If/test/v2/IfV2.node.test.ts +++ b/packages/nodes-base/nodes/If/test/v2/IfV2.node.test.ts @@ -1,5 +1,86 @@ +import { mock } from 'jest-mock-extended'; +import { get } from 'lodash'; +import { + NodeOperationError, + type IExecuteFunctions, + type INodeTypeDescription, + type IDataObject, + type IGetNodeParameterOptions, +} from 'n8n-workflow'; + import { testWorkflows, getWorkflowFilenames } from '@test/nodes/Helpers'; -const workflows = getWorkflowFilenames(__dirname); +import * as IfV2 from '../../V2/IfV2.node'; -describe('Test IF v2 Node', () => testWorkflows(workflows)); +jest.mock('lodash/set', () => jest.fn()); + +describe('Test IF v2 Node Tests', () => { + afterEach(() => jest.resetAllMocks()); + + describe('Test IF v2 Node Workflow Tests', () => testWorkflows(getWorkflowFilenames(__dirname))); + + describe('Test IF V2 Node Unit Tests', () => { + const node = new IfV2.IfV2(mock()); + + const input = [{ json: {} }]; + + const createMockExecuteFunction = ( + nodeParameters: IDataObject, + continueOnFail: boolean = false, + ) => { + const fakeExecuteFunction = { + getNodeParameter( + parameterName: string, + itemIndex: number, + fallbackValue?: IDataObject | undefined, + options?: IGetNodeParameterOptions | undefined, + ) { + const parameter = options?.extractValue ? `${parameterName}.value` : parameterName; + + const parameterValue = get(nodeParameters, parameter, fallbackValue); + + if ((parameterValue as IDataObject)?.nodeOperationError) { + throw new NodeOperationError(mock(), 'Get Options Error', { itemIndex }); + } + + return parameterValue; + }, + getNode() { + return node; + }, + continueOnFail: () => continueOnFail, + getInputData: () => input, + } as unknown as IExecuteFunctions; + return fakeExecuteFunction; + }; + + it('should return items if continue on fail is true', async () => { + const fakeExecuteFunction = createMockExecuteFunction( + { options: { nodeOperationError: true } }, + true, + ); + + const output = await node.execute.call(fakeExecuteFunction); + expect(output).toEqual([[], input]); + }); + + it('should throw an error if continue on fail is false and if there is an error', async () => { + const fakeExecuteFunction = createMockExecuteFunction( + { options: { nodeOperationError: true } }, + false, + ); + + await expect(node.execute.call(fakeExecuteFunction)).rejects.toThrow(NodeOperationError); + }); + + it('should assign a paired item if paired item is undefined', async () => { + const fakeExecuteFunction = createMockExecuteFunction( + { options: {}, conditions: true }, + false, + ); + + const output = await node.execute.call(fakeExecuteFunction); + expect(output).toEqual([[], [{ json: {}, pairedItem: { item: 0 } }]]); + }); + }); +});