From 5ac4aa5fc8cd91a2baaea789894d10d31e010aab Mon Sep 17 00:00:00 2001 From: Radek Podrazky Date: Thu, 31 Oct 2024 12:35:19 +0100 Subject: [PATCH] fix(Filters): make operator parsing case insensitive Closes UXD-1661 --- .../Filters/FilterRow/FilterRow.tsx | 6 ++- .../Filters/FilterRow/filterRow.test.tsx | 40 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/components/Filters/FilterRow/filterRow.test.tsx diff --git a/src/components/Filters/FilterRow/FilterRow.tsx b/src/components/Filters/FilterRow/FilterRow.tsx index 2591b3c77..a21f18ffc 100644 --- a/src/components/Filters/FilterRow/FilterRow.tsx +++ b/src/components/Filters/FilterRow/FilterRow.tsx @@ -93,8 +93,10 @@ const getConditionComponent = curry( )(fieldValue, fields), ); -const getOperatorOptions = (operatorValue, operatorOptions) => - find(propEq('value', operatorValue), operatorOptions); +export const getOperatorOptions = ( + operatorValue: string, + operatorOptions: { value: string; label: string }[], +) => find(propEq('value', operatorValue.toLowerCase()), operatorOptions); const getFieldOptions = map(normalizeOptions); diff --git a/src/components/Filters/FilterRow/filterRow.test.tsx b/src/components/Filters/FilterRow/filterRow.test.tsx new file mode 100644 index 000000000..278b243d2 --- /dev/null +++ b/src/components/Filters/FilterRow/filterRow.test.tsx @@ -0,0 +1,40 @@ +import { getOperatorOptions } from './FilterRow'; + +describe('getOperatorOptions', () => { + it('should return the correct operator option when a matching value exists', () => { + const operatorValue = 'or'; + const operatorOptions = [ + { value: 'or', label: 'or' }, + { value: 'and', label: 'and' }, + ]; + const result = getOperatorOptions(operatorValue, operatorOptions); + expect(result).toEqual({ value: 'or', label: 'or' }); + }); + + it('should return the correct operator option when a matching value exists (CASE INSENSITIVE)', () => { + const operatorValue = 'OR'; + const operatorOptions = [ + { value: 'or', label: 'or' }, + { value: 'and', label: 'and' }, + ]; + const result = getOperatorOptions(operatorValue, operatorOptions); + expect(result).toEqual({ value: 'or', label: 'or' }); + }); + + it('should return undefined when no matching operator value is found', () => { + const operatorValue = 'greaterThan'; + const operatorOptions = [ + { value: 'or', label: 'or' }, + { value: 'and', label: 'and' }, + ]; + const result = getOperatorOptions(operatorValue, operatorOptions); + expect(result).toBeUndefined(); + }); + + it('should return undefined when operatorOptions array is empty', () => { + const operatorValue = 'or'; + const operatorOptions = []; + const result = getOperatorOptions(operatorValue, operatorOptions); + expect(result).toBeUndefined(); + }); +});