Fixed tests

This commit is contained in:
Adina Totorean 2025-01-08 16:25:56 +02:00
parent 2d5eb7ce24
commit 2a680d86db
4 changed files with 59 additions and 64 deletions

View file

@ -62,6 +62,10 @@ export async function presendFilters(
const filterAttribute = filterToSend?.attribute as string;
const filterValue = filterToSend?.value as string;
if (!filterValue) {
throw new NodeOperationError(this.getNode(), 'Please provide Value to use filtering.');
}
let body: IDataObject;
if (typeof requestOptions.body === 'string') {
try {

View file

@ -1,7 +1,8 @@
import { presendOptions } from '../GenericFunctions';
import { NodeOperationError } from 'n8n-workflow';
describe('presendOptions', () => {
import { presendAdditionalFields } from '../GenericFunctions';
describe('presendAdditionalFields', () => {
let mockContext: any;
beforeEach(() => {
@ -25,7 +26,7 @@ describe('presendOptions', () => {
headers: {},
};
const result = await presendOptions.call(mockContext, requestOptions);
const result = await presendAdditionalFields.call(mockContext, requestOptions);
expect(result).toEqual(requestOptions);
});
@ -40,10 +41,10 @@ describe('presendOptions', () => {
headers: {},
};
await expect(presendOptions.call(mockContext, requestOptions)).rejects.toThrow(
await expect(presendAdditionalFields.call(mockContext, requestOptions)).rejects.toThrow(
new NodeOperationError(
mockContext.getNode(),
'At least one of the options (Description, Precedence, Path, or RoleArn) must be provided to update the group.',
'At least one of the additional fields must be provided to update the group.',
),
);
});
@ -58,10 +59,10 @@ describe('presendOptions', () => {
headers: {},
};
await expect(presendOptions.call(mockContext, requestOptions)).rejects.toThrow(
await expect(presendAdditionalFields.call(mockContext, requestOptions)).rejects.toThrow(
new NodeOperationError(
mockContext.getNode(),
'At least one of the options (Description, Precedence, Path, or RoleArn) must be provided to update the group.',
'At least one of the additional fields must be provided to update the group.',
),
);
});

View file

@ -1,7 +1,8 @@
import { presendFilter } from '../GenericFunctions';
import { NodeOperationError } from 'n8n-workflow';
describe('presendFilter', () => {
import { presendFilters } from '../GenericFunctions';
describe('presendFilters', () => {
let mockContext: any;
beforeEach(() => {
@ -15,8 +16,8 @@ describe('presendFilter', () => {
test('should return request options with applied filter when all parameters are provided', async () => {
mockContext.getNodeParameter.mockImplementation((param: string) => {
if (param === 'additionalFields') {
return { filterAttribute: 'name', filterType: 'exactMatch', filterValue: 'John' };
if (param === 'filters') {
return { filter: { attribute: 'name', value: 'John' } };
}
return {};
});
@ -28,19 +29,19 @@ describe('presendFilter', () => {
headers: {},
};
const result = await presendFilter.call(mockContext, requestOptions);
const result = await presendFilters.call(mockContext, requestOptions);
expect(result.body).toBe(
JSON.stringify({
Filter: 'name = "John"',
Filter: '"name"^="John"',
}),
);
});
test('should throw an error if any filter parameter is missing', async () => {
mockContext.getNodeParameter.mockImplementation((param: string) => {
if (param === 'additionalFields') {
return { filterAttribute: 'name', filterType: 'exactMatch' };
if (param === 'filters') {
return { filter: { attribute: 'name' } }; // Missing value
}
return {};
});
@ -52,18 +53,15 @@ describe('presendFilter', () => {
headers: {},
};
await expect(presendFilter.call(mockContext, requestOptions)).rejects.toThrow(
new NodeOperationError(
mockContext.getNode(),
'Please provide Filter Attribute, Filter Type, and Filter Value to use filtering.',
),
await expect(presendFilters.call(mockContext, requestOptions)).rejects.toThrow(
new NodeOperationError(mockContext.getNode(), 'Please provide Value to use filtering.'),
);
});
test('should parse requestOptions.body if it is a string', async () => {
mockContext.getNodeParameter.mockImplementation((param: string) => {
if (param === 'additionalFields') {
return { filterAttribute: 'name', filterType: 'startsWith', filterValue: 'Jo' };
if (param === 'filters') {
return { filter: { attribute: 'name', value: 'Jo' } };
}
return {};
});
@ -75,24 +73,40 @@ describe('presendFilter', () => {
headers: {},
};
const result = await presendFilter.call(mockContext, requestOptions);
const result = await presendFilters.call(mockContext, requestOptions);
expect(result.body).toBe(
JSON.stringify({
key: 'value',
Filter: 'name ^= "Jo"',
Filter: '"name"^="Jo"',
}),
);
});
test('should throw an error if requestOptions.body is an invalid JSON string', async () => {
mockContext.getNodeParameter.mockImplementation((param: string) => {
if (param === 'filters') {
return { filter: { attribute: 'name', value: 'John' } };
}
return {};
});
const requestOptions = {
method: 'POST' as const,
url: '/example-endpoint',
body: '{invalidJson}',
headers: {},
};
await expect(presendFilters.call(mockContext, requestOptions)).rejects.toThrow(
new NodeOperationError(mockContext.getNode(), 'Failed to parse requestOptions body'),
);
});
test('should not parse requestOptions.body if it is already an object', async () => {
mockContext.getNodeParameter.mockImplementation((param: string) => {
if (param === 'additionalFields') {
return {
filterAttribute: 'email',
filterType: 'exactMatch',
filterValue: 'test@example.com',
};
if (param === 'filters') {
return { filter: { attribute: 'email', value: 'test@example.com' } };
}
return {};
});
@ -104,37 +118,12 @@ describe('presendFilter', () => {
headers: {},
};
const result = await presendFilter.call(mockContext, requestOptions);
const result = await presendFilters.call(mockContext, requestOptions);
expect(result.body).toBe(
JSON.stringify({
key: 'value',
Filter: 'email = "test@example.com"',
}),
);
});
test('should handle unsupported filterType values by defaulting to the provided filterType', async () => {
mockContext.getNodeParameter.mockImplementation((param: string) => {
if (param === 'additionalFields') {
return { filterAttribute: 'name', filterType: 'unknownType', filterValue: 'John' };
}
return {};
});
const requestOptions = {
method: 'POST' as const,
url: '/example-endpoint',
body: { key: 'value' },
headers: {},
};
const result = await presendFilter.call(mockContext, requestOptions);
expect(result.body).toBe(
JSON.stringify({
key: 'value',
Filter: 'name unknownType "John"',
Filter: '"email"^="test@example.com"',
}),
);
});

View file

@ -1,7 +1,8 @@
import { presendPath } from '../GenericFunctions';
import { NodeOperationError } from 'n8n-workflow';
describe('presendPath', () => {
import { presendVerifyPath } from '../GenericFunctions';
describe('presendVerifyPath', () => {
let mockContext: any;
let mockGetNodeParameter: jest.Mock;
@ -23,7 +24,7 @@ describe('presendPath', () => {
body: {},
};
const result = await presendPath.call(mockContext, requestOptions);
const result = await presendVerifyPath.call(mockContext, requestOptions);
expect(result).toEqual(requestOptions);
});
@ -38,7 +39,7 @@ describe('presendPath', () => {
body: {},
};
await expect(presendPath.call(mockContext, requestOptions)).rejects.toThrow(
await expect(presendVerifyPath.call(mockContext, requestOptions)).rejects.toThrow(
new NodeOperationError(mockContext.getNode(), 'Path must be between 1 and 512 characters.'),
);
});
@ -53,7 +54,7 @@ describe('presendPath', () => {
body: {},
};
await expect(presendPath.call(mockContext, requestOptions)).rejects.toThrow(
await expect(presendVerifyPath.call(mockContext, requestOptions)).rejects.toThrow(
new NodeOperationError(mockContext.getNode(), 'Path must be between 1 and 512 characters.'),
);
});
@ -69,7 +70,7 @@ describe('presendPath', () => {
body: {},
};
await expect(presendPath.call(mockContext, requestOptions)).rejects.toThrow(
await expect(presendVerifyPath.call(mockContext, requestOptions)).rejects.toThrow(
new NodeOperationError(
mockContext.getNode(),
'Path must begin and end with a forward slash and contain valid ASCII characters.',
@ -87,7 +88,7 @@ describe('presendPath', () => {
body: {},
};
await expect(presendPath.call(mockContext, requestOptions)).rejects.toThrow(
await expect(presendVerifyPath.call(mockContext, requestOptions)).rejects.toThrow(
new NodeOperationError(
mockContext.getNode(),
'Path must begin and end with a forward slash and contain valid ASCII characters.',