Skip to content

Commit

Permalink
feat(edit-content) add utility tests #28879
Browse files Browse the repository at this point in the history
  • Loading branch information
oidacra committed Jul 12, 2024
1 parent 24d5a8b commit 4b7ae27
Showing 1 changed file with 332 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { DotCategory } from '@dotcms/dotcms-models';
import { DotCategory, DotCMSContentlet, DotCMSContentTypeField } from '@dotcms/dotcms-models';

import {
addSelected,
categoryDeepCopy,
clearCategoriesAfterIndex,
clearParentPathAfterIndex,
removeItemByKey,
transformCategories,
transformSelectedCategories,
updateChecked
} from './category-field.utils';

import {
CATEGORY_FIELD_CONTENTLET_MOCK,
CATEGORY_FIELD_MOCK,
CATEGORY_FIELD_VARIABLE_NAME,
CATEGORY_LEVEL_1
} from '../mocks/category-field.mocks';
import { DotCategoryFieldKeyValueObj } from '../models/dot-category-field.models';
Expand Down Expand Up @@ -260,4 +264,331 @@ describe('CategoryFieldUtils', () => {
expect(result).toEqual(expected);
});
});

describe('removeItemByKey', () => {
let array: DotCategoryFieldKeyValueObj[];

beforeEach(() => {
array = [
{ key: '1', value: 'Category 1', inode: 'inode1', path: 'path1' },
{ key: '2', value: 'Category 2', inode: 'inode2', path: 'path2' },
{ key: '3', value: 'Category 3', inode: 'inode3', path: 'path3' }
];
});

it('should remove item with a single key', () => {
const key = '2';
const result = removeItemByKey(array, key);
expect(result).toEqual([
{ key: '1', value: 'Category 1', inode: 'inode1', path: 'path1' },
{ key: '3', value: 'Category 3', inode: 'inode3', path: 'path3' }
]);
});

it('should remove items with an array of keys', () => {
const keys = ['1', '3'];
const result = removeItemByKey(array, keys);
expect(result).toEqual([
{ key: '2', value: 'Category 2', inode: 'inode2', path: 'path2' }
]);
});

it('should return the same array if key is not found', () => {
const key = '4';
const result = removeItemByKey(array, key);
expect(result).toEqual(array);
});

it('should return the same array if keys array is empty', () => {
const keys: string[] = [];
const result = removeItemByKey(array, keys);
expect(result).toEqual(array);
});

it('should handle an empty array input', () => {
const emptyArray: DotCategoryFieldKeyValueObj[] = [];
const key = '1';
const result = removeItemByKey(emptyArray, key);
expect(result).toEqual([]);
});

it('should handle an empty array input with keys array', () => {
const emptyArray: DotCategoryFieldKeyValueObj[] = [];
const keys = ['1', '2'];
const result = removeItemByKey(emptyArray, keys);
expect(result).toEqual([]);
});
});
describe('addSelected', () => {
let array: DotCategoryFieldKeyValueObj[];

beforeEach(() => {
array = [
{ key: '1', value: 'Category 1', inode: 'inode1', path: 'path1' },
{ key: '2', value: 'Category 2', inode: 'inode2', path: 'path2' }
];
});

it('should add a single item to the array', () => {
const newItem = { key: '3', value: 'Category 3', inode: 'inode3', path: 'path3' };
const result = addSelected(array, newItem);
expect(result).toEqual([
{ key: '1', value: 'Category 1', inode: 'inode1', path: 'path1' },
{ key: '2', value: 'Category 2', inode: 'inode2', path: 'path2' },
{ key: '3', value: 'Category 3', inode: 'inode3', path: 'path3' }
]);
});

it('should add multiple items to the array', () => {
const newItems = [
{ key: '3', value: 'Category 3', inode: 'inode3', path: 'path3' },
{ key: '4', value: 'Category 4', inode: 'inode4', path: 'path4' }
];
const result = addSelected(array, newItems);
expect(result).toEqual([
{ key: '1', value: 'Category 1', inode: 'inode1', path: 'path1' },
{ key: '2', value: 'Category 2', inode: 'inode2', path: 'path2' },
{ key: '3', value: 'Category 3', inode: 'inode3', path: 'path3' },
{ key: '4', value: 'Category 4', inode: 'inode4', path: 'path4' }
]);
});

it('should not add duplicate items to the array', () => {
const newItem = { key: '2', value: 'Category 2', inode: 'inode2', path: 'path2' };
const result = addSelected(array, newItem);
expect(result).toEqual([
{ key: '1', value: 'Category 1', inode: 'inode1', path: 'path1' },
{ key: '2', value: 'Category 2', inode: 'inode2', path: 'path2' }
]);
});

it('should handle adding items to an empty array', () => {
const emptyArray: DotCategoryFieldKeyValueObj[] = [];
const newItem = { key: '1', value: 'Category 1', inode: 'inode1', path: 'path1' };
const result = addSelected(emptyArray, newItem);
expect(result).toEqual([
{ key: '1', value: 'Category 1', inode: 'inode1', path: 'path1' }
]);
});

it('should handle adding an empty array of items', () => {
const newItems: DotCategoryFieldKeyValueObj[] = [];
const result = addSelected(array, newItems);
expect(result).toEqual(array);
});

it('should add items correctly when array is empty', () => {
const emptyArray: DotCategoryFieldKeyValueObj[] = [];
const newItems = [
{ key: '1', value: 'Category 1', inode: 'inode1', path: 'path1' },
{ key: '2', value: 'Category 2', inode: 'inode2', path: 'path2' }
];
const result = addSelected(emptyArray, newItems);
expect(result).toEqual(newItems);
});
});

describe('transformCategories', () => {
const keyParentPath = ['1']; // make true clicked

it('should transform a single category', () => {
const category: DotCategory = {
key: '1',
inode: 'inode1',
categoryName: 'Category 1',
childrenCount: 2,
active: true,
categoryVelocityVarName: '',
description: null,
iDate: 0,
identifier: null,
keywords: null,
modDate: 0,
owner: '',
sortOrder: 0,
type: '',
parentList: [
{ key: 'root', categoryName: 'Root Parent', inode: 'rootInode' },
{
key: 'parent1',
categoryName: 'Parent 1',
inode: 'parentInode1'
}
]
};

const result = transformCategories(category, keyParentPath);

expect(result).toEqual({
key: '1',
inode: 'inode1',
value: 'Category 1',
hasChildren: true,
clicked: true, // from keyParentPath
path: 'Parent 1'
});
});

it('should transform an array of categories', () => {
const categories: DotCategory[] = [
{
key: '1',
inode: 'inode1',
categoryName: 'Category 1',
childrenCount: 2,
active: true,
categoryVelocityVarName: '',
description: null,
iDate: 0,
identifier: null,
keywords: null,
modDate: 0,
owner: '',
sortOrder: 0,
type: '',
parentList: [
{ key: 'root', categoryName: 'Root Parent', inode: 'rootInode' },
{
key: 'parent1',
categoryName: 'Parent 1',
inode: 'parentInode1'
}
]
},
{
key: '2',
inode: 'inode2',
categoryName: 'Category 2',
childrenCount: 0,
active: true,
categoryVelocityVarName: '',
description: null,
iDate: 0,
identifier: null,
keywords: null,
modDate: 0,
owner: '',
sortOrder: 0,
type: '',
parentList: [
{ key: 'root', categoryName: 'Root Parent', inode: 'rootInode' },
{
key: 'parent1',
categoryName: 'Parent 1',
inode: 'parentInode1'
}
]
}
];

const result = transformCategories(categories, keyParentPath);

expect(result).toEqual([
{
key: '1',
inode: 'inode1',
value: 'Category 1',
hasChildren: true,
clicked: true,
path: 'Parent 1'
},
{
key: '2',
inode: 'inode2',
value: 'Category 2',
hasChildren: false,
clicked: false,
path: 'Parent 1'
}
]);
});

it('should handle category with no parentList', () => {
const category: DotCategory = {
key: '1',
inode: 'inode1',
categoryName: 'Category 1',
childrenCount: 0,
active: true,
categoryVelocityVarName: '',
description: null,
iDate: 0,
identifier: null,
keywords: null,
modDate: 0,
owner: '',
sortOrder: 0,
type: ''
};

const result = transformCategories(category, keyParentPath);

expect(result).toEqual({
key: '1',
inode: 'inode1',
value: 'Category 1',
hasChildren: false,
clicked: false,
path: ''
});
});

it('should handle empty array of categories', () => {
const categories: DotCategory[] = [];

const result = transformCategories(categories, keyParentPath);

expect(result).toEqual([]);
});
});
describe('transformSelectedCategories', () => {
it('should return an empty array if contentlet is not provided', () => {
const result = transformSelectedCategories(CATEGORY_FIELD_MOCK, null as never);
expect(result).toEqual([]);
});

it('should return an empty array if variable is not provided', () => {
const variableField: DotCMSContentTypeField = { ...CATEGORY_FIELD_MOCK, variable: '' };
const result = transformSelectedCategories(
variableField,
CATEGORY_FIELD_CONTENTLET_MOCK
);
expect(result).toEqual([]);
});

it('should return an empty array if selected categories are not present in contentlet', () => {
const variableField: DotCMSContentTypeField = {
...CATEGORY_FIELD_MOCK,
variable: 'nonexistentField'
};
const result = transformSelectedCategories(
variableField,
CATEGORY_FIELD_CONTENTLET_MOCK
);
expect(result).toEqual([]);
});

it('should transform selected categories correctly', () => {
const result = transformSelectedCategories(
CATEGORY_FIELD_MOCK,
CATEGORY_FIELD_CONTENTLET_MOCK
);
expect(result).toEqual([
{ key: '1f208488057007cedda0e0b5d52ee3b3', value: 'Electrical' },
{ key: 'cb83dc32c0a198fd0ca427b3b587f4ce', value: 'Doors & Windows' }
]);
});

it('should handle empty selected categories in contentlet', () => {
const contentletWithEmptyCategories: DotCMSContentlet = {
...CATEGORY_FIELD_CONTENTLET_MOCK,
[CATEGORY_FIELD_VARIABLE_NAME]: []
};
const result = transformSelectedCategories(
CATEGORY_FIELD_MOCK,
contentletWithEmptyCategories
);
expect(result).toEqual([]);
});
});
});

0 comments on commit 4b7ae27

Please sign in to comment.