PromucFlow_constructor/app/client/src/components/propertyControls/TableComputeValue.test.tsx
Jacques Ikot f87f17e610
fix: parsing of nested parentheses in TableComputeValue expressions (#40326)
## Problem
The table compute value parser was unable to handle expressions with
nested parentheses, causing incorrect extraction of computation
expressions when functions with multiple parameters or nested function
calls were used. This caused issues when users tried to use more complex
expressions like `JSObject1.somefunction(currentRow["id"] || 0)`.

## Solution
Implemented a proper nested parentheses tracking algorithm that counts
opening and closing parentheses to find the correct end of the
computation expression, rather than simply looking for the first closing
parenthesis sequence.

## Why This Approach
This solution is robust because it:
1. Properly handles any level of nested parentheses in function calls
2. Maintains backward compatibility with existing simple expressions
3. Provides a more accurate way to extract the computation expression

## Testing
- Added a new test case with nested parentheses:
`JSObject1.somefunction(currentRow["id"] || 0)`
- Ensured existing test cases still pass
- Manually verified with complex expressions containing multiple nested
parentheses

This fix enables users to write more complex table computations
involving function calls with multiple parameters and conditions.


Fixes #40265 

## Automation

/ok-to-test tags="@tag.Table, @tag.Widget, @tag.Binding, @tag.Sanity,
@tag.PropertyPane"

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/14610729968>
> Commit: abbbaebfe5cf723109bee517e5f6f0cebf96a74a
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=14610729968&attempt=1"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.Table, @tag.Widget, @tag.Binding, @tag.Sanity,
@tag.PropertyPane`
> Spec:
> <hr>Wed, 23 Apr 2025 07:45:26 UTC
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [ ] No


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **Bug Fixes**
- Improved handling of computed values in table controls to correctly
process nested parentheses and avoid errors from malformed expressions.

- **Tests**
- Added test cases for complex computed expressions with nested
parentheses, logical operators, and malformed inputs to ensure
robustness.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Vivekanand Ilango <vivek@Vivekanands-MacBook-Pro.local>
Co-authored-by: Vivekanand Ilango <vivek.ilango@appsmith.com>
2025-04-25 03:10:03 -07:00

104 lines
3.2 KiB
TypeScript

import ComputeTablePropertyControlV2 from "./TableComputeValue";
describe("ComputeTablePropertyControlV2.getInputComputedValue", () => {
const tableName = "Table1";
const inputVariations = [
"currentRow.price",
`JSObject1.somefunction(currentRow["id"] || 0)`,
`
[
{
"value": "male",
"label": "male"
},
{
"value": "female",
"label": "female"
}
]
`,
`["123", "-456", "0.123", "-0.456"]`,
`["true", "false"]`,
`["null", "undefined"]`,
`{
"name": "John Doe",
"age": 30,
"isActive": true,
"address": {
"street": "123 Main St",
"city": "Boston"
},
"hobbies": ["reading", "gaming"]
}`,
"() => { return true; }",
"(x) => x * 2",
"currentRow.price * 2",
"currentRow.isValid && true",
"!currentRow.isDeleted",
];
it("1. should return the correct computed value", () => {
inputVariations.forEach((input) => {
const computedValue =
ComputeTablePropertyControlV2.getTableComputeBinding(tableName, input);
expect(
ComputeTablePropertyControlV2.getInputComputedValue(computedValue),
).toBe(`{{${input}}}`);
});
});
it("2. should handle addition values", () => {
const input = "currentRow.quantity + 5";
const computedValue = ComputeTablePropertyControlV2.getTableComputeBinding(
tableName,
input,
);
expect(
ComputeTablePropertyControlV2.getInputComputedValue(computedValue),
).toBe(`{{currentRow.quantity}}{{5}}`);
});
it("3. should handle nested parentheses correctly", () => {
const input =
"JSObject1.complexFunction(currentRow.id, JSObject2.helperFunction(currentRow.name))";
const computedValue = ComputeTablePropertyControlV2.getTableComputeBinding(
tableName,
input,
);
expect(
ComputeTablePropertyControlV2.getInputComputedValue(computedValue),
).toBe(`{{${input}}}`);
});
it("4. should handle malformed expressions with unbalanced parentheses", () => {
// Create a malformed expression by manually crafting a bad computedValue string
// Removing the proper closing parenthesis structure
const malformedComputedValue =
"{{(() => { const tableData = Table1.processedTableData || []; return tableData.length > 0 ? tableData.map((currentRow, currentIndex) => (currentRow.function(unbalanced)) : fallback })";
// The function should gracefully handle this and return the original string
// rather than throwing an error or returning a partial/incorrect result
expect(
ComputeTablePropertyControlV2.getInputComputedValue(
malformedComputedValue,
),
).toBe(malformedComputedValue);
});
it("5. should correctly parse complex but valid expressions with multiple nested parentheses", () => {
const complexInput =
"JSObject1.process(currentRow.value1, (currentRow.value2 || getDefault()), JSObject2.format(currentRow.value3))";
const computedValue = ComputeTablePropertyControlV2.getTableComputeBinding(
tableName,
complexInput,
);
expect(
ComputeTablePropertyControlV2.getInputComputedValue(computedValue),
).toBe(`{{${complexInput}}}`);
});
});