From 17d7e86621e8d05bbae543a1024cb07e51ae90ce Mon Sep 17 00:00:00 2001 From: Eduard Carrerars Date: Wed, 22 Jan 2025 14:02:59 +0100 Subject: [PATCH 1/2] fix(attributeParse): evaluateFieldComparison use index 0 of many2one values --- src/helpers/attributeParser.ts | 3 +++ src/spec/attributeParser.spec.ts | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/helpers/attributeParser.ts b/src/helpers/attributeParser.ts index 691171a..69a257a 100644 --- a/src/helpers/attributeParser.ts +++ b/src/helpers/attributeParser.ts @@ -76,6 +76,9 @@ const evaluateFieldComparison = ({ expectedValue, fields = {}, }: FieldComparisonParams & { fields: any }): FieldComparisonResult => { + if (fields?.fieldName?.type !== "many2one" && valueInObject) { + valueInObject = valueInObject[0]; + } const result: FieldComparisonResult = { modifiedValueInObject: valueInObject, modifiedExpectedValue: null, diff --git a/src/spec/attributeParser.spec.ts b/src/spec/attributeParser.spec.ts index b6dc075..b9833d0 100644 --- a/src/spec/attributeParser.spec.ts +++ b/src/spec/attributeParser.spec.ts @@ -479,6 +479,20 @@ describe("An Attribute Parser", () => { }); expect(evaluatedAttrs.invisible).toBeTruthy(); }); + it("should properly use the id of a many2one value", () => { + const tagAttributes = { + json_attrs: + '{"invisible":{"condition":"AND","rules":[{"field":"autoconsum_id","operator":"=","value":10}]}}', + }; + const values = { autoconsum_id: [10, "Autoconsum"] }; + const evaluatedAttrs = evaluateAttributes({ + tagAttributes, + values, + fields, + fallbackMode: false, + }); + expect(evaluatedAttrs.invisible).toBeTruthy(); + }); it("should properly parse a many2one attribute with undefined value", () => { const tagAttributes = { json_attrs: From f65429e6d385daa72761e6fe3ef21722bb7c6e40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Gu=CC=88ell=20Segarra?= Date: Wed, 22 Jan 2025 14:33:41 +0100 Subject: [PATCH 2/2] fix: change approach in order to pass all tests --- src/helpers/attributeParser.ts | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/helpers/attributeParser.ts b/src/helpers/attributeParser.ts index 69a257a..daf396f 100644 --- a/src/helpers/attributeParser.ts +++ b/src/helpers/attributeParser.ts @@ -76,9 +76,6 @@ const evaluateFieldComparison = ({ expectedValue, fields = {}, }: FieldComparisonParams & { fields: any }): FieldComparisonResult => { - if (fields?.fieldName?.type !== "many2one" && valueInObject) { - valueInObject = valueInObject[0]; - } const result: FieldComparisonResult = { modifiedValueInObject: valueInObject, modifiedExpectedValue: null, @@ -117,14 +114,18 @@ const evaluateFieldComparison = ({ ) { result.modifiedExpectedValue = undefined; } else { - result.modifiedValueInObject = - result.modifiedValueInObject === undefined - ? false - : result.modifiedValueInObject; - result.modifiedValueInObject = - result.modifiedValueInObject === null - ? false - : result.modifiedValueInObject; + if (result.modifiedValueInObject === undefined) { + result.modifiedValueInObject = false; + } else if ( + Array.isArray(result.modifiedValueInObject) && + result.modifiedValueInObject[0] !== undefined + ) { + result.modifiedValueInObject = result.modifiedValueInObject[0]; + } + + if (result.modifiedValueInObject === null) { + result.modifiedValueInObject = false; + } } if (