mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
81 lines
2.2 KiB
TypeScript
81 lines
2.2 KiB
TypeScript
import { prepareItems } from './GenericFunctions';
|
|
|
|
describe('MongoDB Node: Generic Functions', () => {
|
|
describe('prepareItems', () => {
|
|
it('should select fields', () => {
|
|
const items = [{ json: { name: 'John', age: 30 } }, { json: { name: 'Jane', age: 25 } }];
|
|
const fields = ['name'];
|
|
|
|
const result = prepareItems({ items, fields });
|
|
|
|
expect(result).toEqual([{ name: 'John' }, { name: 'Jane' }]);
|
|
});
|
|
|
|
it('should add updateKey to selected fields', () => {
|
|
const items = [{ json: { name: 'John', age: 30 } }, { json: { name: 'Jane', age: 25 } }];
|
|
const fields = ['age'];
|
|
const updateKey = 'name';
|
|
|
|
const result = prepareItems({ items, fields, updateKey });
|
|
|
|
expect(result).toEqual([
|
|
{ name: 'John', age: 30 },
|
|
{ name: 'Jane', age: 25 },
|
|
]);
|
|
});
|
|
|
|
it('should handle dot notation', () => {
|
|
const items = [{ json: { user: { name: 'John' } } }, { json: { user: { name: 'Jane' } } }];
|
|
const fields = ['user.name'];
|
|
const useDotNotation = true;
|
|
|
|
const result = prepareItems({ items, fields, updateKey: '', useDotNotation });
|
|
|
|
expect(result).toEqual([{ user: { name: 'John' } }, { user: { name: 'Jane' } }]);
|
|
});
|
|
|
|
it('should parse dates', () => {
|
|
const items = [
|
|
{ json: { date: '2023-10-01T00:00:00Z' } },
|
|
{ json: { date: '2023-10-02T00:00:00Z' } },
|
|
];
|
|
const fields = ['date'];
|
|
const dateFields = ['date'];
|
|
const useDotNotation = false;
|
|
const isUpdate = false;
|
|
const result = prepareItems({
|
|
items,
|
|
fields,
|
|
updateKey: '',
|
|
useDotNotation,
|
|
dateFields,
|
|
isUpdate,
|
|
});
|
|
expect(result).toEqual([
|
|
{ date: new Date('2023-10-01T00:00:00Z') },
|
|
{ date: new Date('2023-10-02T00:00:00Z') },
|
|
]);
|
|
});
|
|
|
|
it('should handle updates', () => {
|
|
// Should keep dot notation in result to not overwrite the original values
|
|
const items = [
|
|
{ json: { id: 1, user: { name: 'John', age: 30 } } },
|
|
{ json: { id: 2, user: { name: 'Jane', age: 25 } } },
|
|
];
|
|
const fields = ['user.name'];
|
|
const useDotNotation = true;
|
|
const isUpdate = true;
|
|
const result = prepareItems({
|
|
items,
|
|
fields,
|
|
updateKey: '',
|
|
useDotNotation,
|
|
dateFields: [],
|
|
isUpdate,
|
|
});
|
|
expect(result).toEqual([{ 'user.name': 'John' }, { 'user.name': 'Jane' }]);
|
|
});
|
|
});
|
|
});
|