Skip to content

Commit

Permalink
fix link issue with array of objects
Browse files Browse the repository at this point in the history
  • Loading branch information
RayenRomdhane committed Dec 12, 2023
1 parent fac649a commit 7c1a9ee
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/models/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ class Component extends FileInformation {
if (attributes[index].name === name) {
return attributes[index];
}
if (attributes[index].type === 'Object') {
if (attributes[index].type === 'Object' || (attributes[index].definition?.type === 'Array'
&& attributes[index].definition?.itemType === 'Object')) {
const attribute = this.__getAttributeByName(attributes[index].value, name);

if (attribute) {
Expand All @@ -299,7 +300,8 @@ class Component extends FileInformation {
*/
__setAttributesByField(result, attributes, field, ...values) {
attributes.forEach((attribute) => {
if (attribute?.type === 'Object') {
if (attribute?.type === 'Object' || (attribute?.definition?.type === 'Array'
&& attribute?.definition?.itemType === 'Object')) {
this.__setAttributesByField(result, attribute.value, field, ...values);
}

Expand Down
3 changes: 3 additions & 0 deletions src/models/DefaultData.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,9 @@ class DefaultData {
this.definitions.links.push(linkDefinition);
} else if (attributeDefinition.type === 'Object') {
this.__setLinkDefinitions(type, attributeDefinition.definedAttributes);
} else if (attributeDefinition.type === 'Array'
&& attributeDefinition.itemType === 'Object') {
this.__setLinkDefinitions(type, attributeDefinition.itemDefinition[0].definedAttributes);
}
});
}
Expand Down
53 changes: 51 additions & 2 deletions tests/unit/models/Component.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,25 @@ describe('Test class: Component', () => {
value: [subAttribute],
});

const aooItemSubAttribute = new ComponentAttribute({
name: 'aoo_sub',
value: 'aoo sub test',
});

const arrayOfObjectItem = new ComponentAttribute({
type: 'Object',
value: [aooItemSubAttribute],
});

const arrayOfObjectRootAttribute = new ComponentAttribute({
name: 'aoo_root',
type: 'Array',
definition: new ComponentAttributeDefinition({ type: 'Array', itemType: 'Object' }),
value: [arrayOfObjectItem],
});

component.attributes.push(rootAttribute);
component.attributes.push(arrayOfObjectRootAttribute);

it('Should return null on unknown attribute', () => {
expect(component.getAttributeByName('unknown')).toBeNull();
Expand All @@ -874,6 +892,10 @@ describe('Test class: Component', () => {
it('Should return sub attribute on asking sub attribute', () => {
expect(component.getAttributeByName('sub')).toEqual(subAttribute);
});

it('Should return sub attribute of item of array of object on asking sub attribute', () => {
expect(component.getAttributeByName('aoo_sub')).toEqual(aooItemSubAttribute);
});
});

describe('Test method: getAttributesByType', () => {
Expand Down Expand Up @@ -904,9 +926,27 @@ describe('Test class: Component', () => {
definition: new ComponentAttributeDefinition({ name: 'root', type: 'Object' }),
});

const aooItemSubAttribute = new ComponentAttribute({
name: 'aoo_sub',
type: 'String',
value: 42,
definition: new ComponentAttributeDefinition({ name: 'aoo_sub', type: 'String' }),
});
const arrayOfObjectItem = new ComponentAttribute({
type: 'Object',
value: [aooItemSubAttribute],
definition: new ComponentAttributeDefinition({ type: 'Object' }),
});
const arrayOfObjectRootAttribute = new ComponentAttribute({
name: 'aoo_root',
type: 'Array',
definition: new ComponentAttributeDefinition({ type: 'Array', itemType: 'Object' }),
value: [arrayOfObjectItem],
});

component.attributes.push(attribute1);
component.attributes.push(rootAttribute);

component.attributes.push(arrayOfObjectRootAttribute);
it('Should return an empty array on unknown attribute types', () => {
const result = component.getAttributesByType('unknownType');

Expand All @@ -922,9 +962,18 @@ describe('Test class: Component', () => {
it('Should search in sub-attributes and return attributes Object type', () => {
const result = component.getAttributesByType('Object');

expect(result).toHaveLength(2);
expect(result).toHaveLength(3);
expect(result[0].name).toBe('sub');
expect(result[1].name).toBe('root');
expect(result[2].name).toBe(null);
});

it('Should search in sub-attributes and return attributes String type', () => {
const result = component.getAttributesByType('String');

expect(result).toHaveLength(2);
expect(result[0].name).toBe('attribute1');
expect(result[1].name).toBe('aoo_sub');
});
});

Expand Down
24 changes: 24 additions & 0 deletions tests/unit/models/DefaultData.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,24 @@ describe('Test class: DefaultData', () => {
}),
],
}),
new ComponentAttributeDefinition({
name: 'arrayOfObject',
type: 'Array',
itemType: 'Object',
itemDefinition: [
new ComponentAttributeDefinition({
type: 'Object',
definedAttributes: [
new ComponentAttributeDefinition({
name: 'link4',
type: 'Link',
linkRef: 'laptop4',
linkType: 'Default',
}),
],
}),
],
}),
],
}),
];
Expand All @@ -241,6 +259,12 @@ describe('Test class: DefaultData', () => {
targetRef: 'laptop3',
type: 'Default',
}),
new ComponentLinkDefinition({
attributeRef: 'link4',
sourceRef: 'server',
targetRef: 'laptop4',
type: 'Default',
}),
]);
});
});
Expand Down

0 comments on commit 7c1a9ee

Please sign in to comment.