From 7c1a9ee338da7561b84255304946e20d2138bb98 Mon Sep 17 00:00:00 2001 From: RayenRomdhane Date: Tue, 12 Dec 2023 11:22:25 +0100 Subject: [PATCH] fix link issue with array of objects --- src/models/Component.js | 6 ++- src/models/DefaultData.js | 3 ++ tests/unit/models/Component.spec.js | 53 ++++++++++++++++++++++++++- tests/unit/models/DefaultData.spec.js | 24 ++++++++++++ 4 files changed, 82 insertions(+), 4 deletions(-) diff --git a/src/models/Component.js b/src/models/Component.js index 4895e935..8f314f2d 100644 --- a/src/models/Component.js +++ b/src/models/Component.js @@ -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) { @@ -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); } diff --git a/src/models/DefaultData.js b/src/models/DefaultData.js index 405be49f..6a1e9336 100644 --- a/src/models/DefaultData.js +++ b/src/models/DefaultData.js @@ -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); } }); } diff --git a/tests/unit/models/Component.spec.js b/tests/unit/models/Component.spec.js index a2de49b1..f1ffb0d1 100644 --- a/tests/unit/models/Component.spec.js +++ b/tests/unit/models/Component.spec.js @@ -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(); @@ -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', () => { @@ -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'); @@ -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'); }); }); diff --git a/tests/unit/models/DefaultData.spec.js b/tests/unit/models/DefaultData.spec.js index e361a947..0a79ba94 100644 --- a/tests/unit/models/DefaultData.spec.js +++ b/tests/unit/models/DefaultData.spec.js @@ -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', + }), + ], + }), + ], + }), ], }), ]; @@ -241,6 +259,12 @@ describe('Test class: DefaultData', () => { targetRef: 'laptop3', type: 'Default', }), + new ComponentLinkDefinition({ + attributeRef: 'link4', + sourceRef: 'server', + targetRef: 'laptop4', + type: 'Default', + }), ]); }); });