|
1 | 1 | import { FilteringLogic, IFilteringExpression } from './filtering-expression.interface'; |
2 | 2 | import { FilteringExpressionsTree, IFilteringExpressionsTree } from './filtering-expressions-tree'; |
3 | | -import { recreateExpression, recreateTree, recreateTreeFromFields } from './expressions-tree-util'; |
| 3 | +import { ExpressionsTreeUtil, recreateExpression, recreateTree, recreateTreeFromFields } from './expressions-tree-util'; |
4 | 4 | import { IgxBooleanFilteringOperand, IgxDateFilteringOperand, IgxDateTimeFilteringOperand, IgxNumberFilteringOperand, IgxStringFilteringOperand, IgxTimeFilteringOperand } from './filtering-condition'; |
5 | 5 | import type { EntityType, FieldType } from './grid-types'; |
6 | 6 |
|
@@ -479,3 +479,61 @@ describe('Unit testing FilteringUtil', () => { |
479 | 479 | }); |
480 | 480 |
|
481 | 481 | }); |
| 482 | + |
| 483 | +describe('ExpressionsTreeUtil.extractUniqueFieldNamesFromFilterTree', () => { |
| 484 | + it('Should return an empty array for undefined input', () => { |
| 485 | + const result = ExpressionsTreeUtil.extractUniqueFieldNamesFromFilterTree(undefined); |
| 486 | + expect(result).toEqual([]); |
| 487 | + }); |
| 488 | + |
| 489 | + it('Should return an empty array for a tree with no operands', () => { |
| 490 | + const tree = new FilteringExpressionsTree(FilteringLogic.And); |
| 491 | + const result = ExpressionsTreeUtil.extractUniqueFieldNamesFromFilterTree(tree); |
| 492 | + expect(result).toEqual([]); |
| 493 | + }); |
| 494 | + |
| 495 | + it('Should return unique field names from a flat filtering tree', () => { |
| 496 | + const tree = new FilteringExpressionsTree(FilteringLogic.And); |
| 497 | + tree.filteringOperands.push({ fieldName: 'ProductName', conditionName: 'contains', searchVal: 'A' } as IFilteringExpression); |
| 498 | + tree.filteringOperands.push({ fieldName: 'Downloads', conditionName: 'greaterThan', searchVal: 100 } as IFilteringExpression); |
| 499 | + tree.filteringOperands.push({ fieldName: 'ProductName', conditionName: 'startsWith', searchVal: 'B' } as IFilteringExpression); |
| 500 | + |
| 501 | + const result = ExpressionsTreeUtil.extractUniqueFieldNamesFromFilterTree(tree); |
| 502 | + expect(result.length).toBe(2); |
| 503 | + expect(result).toContain('ProductName'); |
| 504 | + expect(result).toContain('Downloads'); |
| 505 | + }); |
| 506 | + |
| 507 | + it('Should return unique field names from a nested filtering tree', () => { |
| 508 | + const outerTree = new FilteringExpressionsTree(FilteringLogic.Or); |
| 509 | + const innerTree = new FilteringExpressionsTree(FilteringLogic.And); |
| 510 | + |
| 511 | + outerTree.filteringOperands.push({ fieldName: 'ID', conditionName: 'equals', searchVal: 1 } as IFilteringExpression); |
| 512 | + innerTree.filteringOperands.push({ fieldName: 'ProductName', conditionName: 'contains', searchVal: 'test' } as IFilteringExpression); |
| 513 | + innerTree.filteringOperands.push({ fieldName: 'ID', conditionName: 'lessThan', searchVal: 50 } as IFilteringExpression); |
| 514 | + outerTree.filteringOperands.push(innerTree); |
| 515 | + |
| 516 | + const result = ExpressionsTreeUtil.extractUniqueFieldNamesFromFilterTree(outerTree); |
| 517 | + expect(result.length).toBe(2); |
| 518 | + expect(result).toContain('ID'); |
| 519 | + expect(result).toContain('ProductName'); |
| 520 | + }); |
| 521 | + |
| 522 | + it('Should return unique field names from a deeply nested filtering tree', () => { |
| 523 | + const root = new FilteringExpressionsTree(FilteringLogic.And); |
| 524 | + const level1 = new FilteringExpressionsTree(FilteringLogic.Or); |
| 525 | + const level2 = new FilteringExpressionsTree(FilteringLogic.And); |
| 526 | + |
| 527 | + level2.filteringOperands.push({ fieldName: 'Downloads', conditionName: 'greaterThan', searchVal: 10 } as IFilteringExpression); |
| 528 | + level1.filteringOperands.push({ fieldName: 'ProductName', conditionName: 'contains', searchVal: 'x' } as IFilteringExpression); |
| 529 | + level1.filteringOperands.push(level2); |
| 530 | + root.filteringOperands.push({ fieldName: 'ID', conditionName: 'equals', searchVal: 5 } as IFilteringExpression); |
| 531 | + root.filteringOperands.push(level1); |
| 532 | + |
| 533 | + const result = ExpressionsTreeUtil.extractUniqueFieldNamesFromFilterTree(root); |
| 534 | + expect(result.length).toBe(3); |
| 535 | + expect(result).toContain('ID'); |
| 536 | + expect(result).toContain('ProductName'); |
| 537 | + expect(result).toContain('Downloads'); |
| 538 | + }); |
| 539 | +}); |
0 commit comments