chore: adding extra unit tests

This commit is contained in:
Bassel Kanso 2024-06-13 16:42:38 +03:00
parent 085ed80578
commit cc05b7b4cb
3 changed files with 63 additions and 15 deletions

View File

@ -10,7 +10,7 @@ export type NestedComponent = {
export const retrieveNestedComponents = (appComponents: Components): NestedComponent[] => {
const nestedComponents = Object.keys(appComponents).reduce((acc: NestedComponent[], current) => {
const componentAttributes = appComponents?.[current]?.schema?.attributes ?? [];
const currentComponentNestedCompos = getComponentsFromComponent(
const currentComponentNestedCompos = getComponentsNestedWithinComponent(
componentAttributes,
current as Internal.UID.Component
);
@ -20,7 +20,7 @@ export const retrieveNestedComponents = (appComponents: Components): NestedCompo
return mergeComponents(nestedComponents);
};
const getComponentsFromComponent = (
const getComponentsNestedWithinComponent = (
componentAttributes: AttributeType[],
parentCompoUid: Internal.UID.Component
) => {
@ -37,7 +37,7 @@ const getComponentsFromComponent = (
}, []);
};
// merge duplicate components
// Merge duplicate components
const mergeComponents = (originalComponents: NestedComponent[]): NestedComponent[] => {
const componentMap = new Map();
// Populate the map with component and its parents

View File

@ -3,10 +3,10 @@ import type { NestedComponent } from '../components/DataManagerProvider/utils/re
import type { Internal } from '@strapi/types';
const findComponent = <T extends { component: Internal.UID.Component }>(
componentName: Internal.UID.Schema,
componentUid: Internal.UID.Schema,
components: Array<T>
) => {
return components.find((c) => c.component === componentName);
return components.find((c) => c.component === componentUid);
};
/**

View File

@ -1,8 +1,9 @@
import { getChildrenMaxDepth } from '../getMaxDepth';
import { getChildrenMaxDepth, getComponentDepth } from '../getMaxDepth';
import type { ComponentWithChildren } from '../../components/DataManagerProvider/utils/retrieveComponentsThatHaveComponents';
import type { NestedComponent } from '../../components/DataManagerProvider/utils/retrieveNestedComponents';
const components: Array<ComponentWithChildren> = [
const componentsWithChildComponents: Array<ComponentWithChildren> = [
{
component: 'basic.parent-compo',
childComponents: [
@ -80,16 +81,63 @@ const components: Array<ComponentWithChildren> = [
},
];
describe('getMaxDepth', () => {
test('A component with no child component should have 0 max depth', () => {
const componentsMaxDepth = getChildrenMaxDepth('basic.nested-compo6', components);
const nestedComponents: Array<NestedComponent> = [
{
component: 'default.dish',
uidsOfAllParents: ['default.openingtimes', 'default.closingperiod'],
},
{
component: 'basic.nested-compo1',
uidsOfAllParents: ['basic.parent-compo'],
},
{
component: 'basic.nested-compo6',
uidsOfAllParents: ['basic.nested-compo5', 'basic.another-parent-compo'],
},
{
component: 'basic.nested-compo5',
uidsOfAllParents: ['basic.nested-compo4'],
},
{
component: 'basic.nested-compo4',
uidsOfAllParents: ['basic.nested-compo3'],
},
{
component: 'basic.nested-compo3',
uidsOfAllParents: ['basic.nested-compo2'],
},
{
component: 'basic.nested-compo2',
uidsOfAllParents: ['basic.nested-compo1'],
},
];
expect(componentsMaxDepth).toEqual(0);
describe('Component Depth Calculations', () => {
describe('getMaxDepth', () => {
it('A component with no child component should have 0 max depth', () => {
const componentsMaxDepth = getChildrenMaxDepth(
'basic.nested-compo6',
componentsWithChildComponents
);
expect(componentsMaxDepth).toEqual(0);
});
it('should accurately give the max depth of components children', () => {
const componentsMaxDepth = getChildrenMaxDepth(
'default.openingtimes',
componentsWithChildComponents
);
expect(componentsMaxDepth).toEqual(4);
});
});
test('should accurately give the max depth of components children', () => {
const componentsMaxDepth = getChildrenMaxDepth('default.openingtimes', components);
expect(componentsMaxDepth).toEqual(4);
describe('getComponentDepth', () => {
it('A component depth should reflect its position in the component tree', () => {
expect(getComponentDepth('basic.nested-compo1', nestedComponents)).toEqual(1);
expect(getComponentDepth('basic.nested-compo4', nestedComponents)).toEqual(4);
expect(getComponentDepth('basic.nested-compo6', nestedComponents)).toEqual(6);
});
});
});