rearrange test files

This commit is contained in:
Ria 2025-01-23 10:25:03 +01:00
parent 6189c72bdb
commit 9fdfdacab3
3 changed files with 136 additions and 93 deletions

View file

@ -1,97 +1,5 @@
import {
NodeOperationError,
type IExecuteFunctions,
type IDataObject,
NodeExecutionOutput,
} from 'n8n-workflow';
import { testWorkflows, getWorkflowFilenames } from '@test/nodes/Helpers';
import { Summarize } from '../Summarize.node';
import { checkIfFieldExists, type Aggregations } from '../utils';
const workflows = getWorkflowFilenames(__dirname);
describe('Test Summarize Node', () => {
testWorkflows(workflows);
let summarizeNode: Summarize;
let mockExecuteFunctions: IExecuteFunctions;
beforeEach(() => {
summarizeNode = new Summarize();
mockExecuteFunctions = {
getNode: jest.fn().mockReturnValue({ name: 'test-node' }),
getNodeParameter: jest.fn(),
getInputData: jest.fn(),
} as unknown as IExecuteFunctions;
});
it('should throw error if node version is < 1.1 and fields not found', async () => {
mockExecuteFunctions.getNode = jest.fn().mockReturnValue({ typeVersion: 1 });
const items = [{ a: 1 }, { b: 2 }, { c: 3 }];
const aggregations: Aggregations = [
{ aggregation: 'sum', field: 'b' },
{ aggregation: 'count', field: 'd' },
];
mockExecuteFunctions.getInputData = jest.fn().mockReturnValue(items);
mockExecuteFunctions.getNodeParameter = jest
.fn()
.mockReturnValueOnce({}) // options
.mockReturnValueOnce('') // fieldsToSplitBy
.mockReturnValueOnce(aggregations); // fieldsToSummarize
await expect(async () => {
await summarizeNode.execute.bind(mockExecuteFunctions)();
}).rejects.toThrow(NodeOperationError);
});
});
describe('Test Summarize Node, checkIfFieldExists', () => {
let mockExecuteFunctions: IExecuteFunctions;
beforeEach(() => {
mockExecuteFunctions = {
getNode: jest.fn().mockReturnValue({ name: 'test-node' }),
} as unknown as IExecuteFunctions;
});
const items = [{ a: 1 }, { b: 2 }, { c: 3 }];
it('should not throw error if all fields exist', () => {
const aggregations: Aggregations = [
{ aggregation: 'sum', field: 'a' },
{ aggregation: 'count', field: 'c' },
];
const getValue = (item: IDataObject, field: string) => item[field];
expect(() => {
checkIfFieldExists.call(mockExecuteFunctions, items, aggregations, getValue);
}).not.toThrow();
});
it('should throw NodeOperationError if any field does not exist', () => {
const aggregations: Aggregations = [
{ aggregation: 'sum', field: 'b' },
{ aggregation: 'count', field: 'd' },
];
const getValue = (item: IDataObject, field: string) => item[field];
expect(() => {
checkIfFieldExists.call(mockExecuteFunctions, items, aggregations, getValue);
}).toThrow(NodeOperationError);
});
it("should throw NodeOperationError with error message containing the field name that doesn't exist", () => {
const aggregations: Aggregations = [{ aggregation: 'count', field: 'D' }];
const getValue = (item: IDataObject, field: string) => item[field];
expect(() => {
checkIfFieldExists.call(mockExecuteFunctions, items, aggregations, getValue);
}).toThrow("The field 'D' does not exist in any items");
});
it('should not throw error if field is empty string', () => {
const aggregations: Aggregations = [{ aggregation: 'count', field: '' }];
const getValue = (item: IDataObject, field: string) => item[field];
expect(() => {
checkIfFieldExists.call(mockExecuteFunctions, items, aggregations, getValue);
}).not.toThrow();
});
});
describe('Test Summarize Node', () => testWorkflows(workflows));

View file

@ -0,0 +1,53 @@
import { NodeOperationError, type IExecuteFunctions, type IDataObject } from 'n8n-workflow';
import { checkIfFieldExists, type Aggregations } from '../../utils';
describe('Test Summarize Node, checkIfFieldExists', () => {
let mockExecuteFunctions: IExecuteFunctions;
beforeEach(() => {
mockExecuteFunctions = {
getNode: jest.fn().mockReturnValue({ name: 'test-node' }),
} as unknown as IExecuteFunctions;
});
const items = [{ a: 1 }, { b: 2 }, { c: 3 }];
it('should not throw error if all fields exist', () => {
const aggregations: Aggregations = [
{ aggregation: 'sum', field: 'a' },
{ aggregation: 'count', field: 'c' },
];
const getValue = (item: IDataObject, field: string) => item[field];
expect(() => {
checkIfFieldExists.call(mockExecuteFunctions, items, aggregations, getValue);
}).not.toThrow();
});
it('should throw NodeOperationError if any field does not exist', () => {
const aggregations: Aggregations = [
{ aggregation: 'sum', field: 'b' },
{ aggregation: 'count', field: 'd' },
];
const getValue = (item: IDataObject, field: string) => item[field];
expect(() => {
checkIfFieldExists.call(mockExecuteFunctions, items, aggregations, getValue);
}).toThrow(NodeOperationError);
});
it("should throw NodeOperationError with error message containing the field name that doesn't exist", () => {
const aggregations: Aggregations = [{ aggregation: 'count', field: 'D' }];
const getValue = (item: IDataObject, field: string) => item[field];
expect(() => {
checkIfFieldExists.call(mockExecuteFunctions, items, aggregations, getValue);
}).toThrow("The field 'D' does not exist in any items");
});
it('should not throw error if field is empty string', () => {
const aggregations: Aggregations = [{ aggregation: 'count', field: '' }];
const getValue = (item: IDataObject, field: string) => item[field];
expect(() => {
checkIfFieldExists.call(mockExecuteFunctions, items, aggregations, getValue);
}).not.toThrow();
});
});

View file

@ -0,0 +1,82 @@
import type { MockProxy } from 'jest-mock-extended';
import { mock } from 'jest-mock-extended';
import type { IExecuteFunctions } from 'n8n-workflow';
import { NodeExecutionOutput, NodeOperationError } from 'n8n-workflow';
import { Summarize } from '../../Summarize.node';
import type { Aggregations } from '../../utils';
let summarizeNode: Summarize;
let mockExecuteFunctions: MockProxy<IExecuteFunctions>;
describe('Test Summarize Node, execute', () => {
beforeEach(() => {
summarizeNode = new Summarize();
mockExecuteFunctions = mock<IExecuteFunctions>({
getNode: jest.fn().mockReturnValue({ name: 'test-node' }),
getNodeParameter: jest.fn(),
getInputData: jest.fn(),
helpers: {
constructExecutionMetaData: jest.fn().mockReturnValue([]),
},
});
});
afterEach(() => {
jest.clearAllMocks();
});
it('should handle field not found with hints if version > 1', async () => {
mockExecuteFunctions.getInputData.mockReturnValue([{ json: { someField: 1 } }]);
mockExecuteFunctions.getNode.mockReturnValue({
id: '1',
name: 'test-node',
type: 'test-type',
position: [0, 0],
parameters: {},
typeVersion: 1.1,
});
mockExecuteFunctions.getNodeParameter
.mockReturnValueOnce({}) // options
.mockReturnValueOnce('') // fieldsToSplitBy
.mockReturnValueOnce([{ field: 'nonexistentField', aggregation: 'sum' }]); // fieldsToSummarize
const result = await summarizeNode.execute.call(mockExecuteFunctions);
expect(result).toBeDefined();
expect(NodeExecutionOutput).toHaveBeenCalledWith(
expect.any(Array),
expect.arrayContaining([
expect.objectContaining({
message: expect.stringContaining('nonexistentField'),
location: 'outputPane',
}),
]),
);
});
it('should throw error if node version is < 1.1 and fields not found', async () => {
const items = [{ json: { a: 1, b: 2, c: 3 } }];
const aggregations: Aggregations = [
{ aggregation: 'sum', field: 'b' },
{ aggregation: 'count', field: 'd' },
];
mockExecuteFunctions.getNode.mockReturnValue({
id: '1',
name: 'test-node',
type: 'test-type',
position: [0, 0],
parameters: {},
typeVersion: 1,
});
mockExecuteFunctions.getInputData.mockReturnValue(items);
mockExecuteFunctions.getNodeParameter
.mockReturnValueOnce({}) // options
.mockReturnValueOnce('') // fieldsToSplitBy
.mockReturnValueOnce(aggregations); // fieldsToSummarize
await expect(async () => {
await summarizeNode.execute.bind(mockExecuteFunctions)();
}).rejects.toThrow(NodeOperationError);
});
});