From 73358ee58c211e55608e09e1b45e1eeed8ec8df7 Mon Sep 17 00:00:00 2001 From: Viktor Tsvetkov <142901247+vtsvetkov-splunk@users.noreply.github.com> Date: Wed, 31 Jan 2024 14:38:12 +0100 Subject: [PATCH] feat(checkboxGroup): correctly parsing values with spaces (#1034) --- .../CheckboxGroup/checkboxGroup.utils.test.ts | 11 +++++++++++ .../components/CheckboxGroup/checkboxGroup.utils.ts | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/ui/src/components/CheckboxGroup/checkboxGroup.utils.test.ts b/ui/src/components/CheckboxGroup/checkboxGroup.utils.test.ts index 7ab1de3a4..0641b96ff 100644 --- a/ui/src/components/CheckboxGroup/checkboxGroup.utils.test.ts +++ b/ui/src/components/CheckboxGroup/checkboxGroup.utils.test.ts @@ -23,6 +23,17 @@ describe('parseValue', () => { expect(resultMap.get('collect_file')?.inputValue).toBe(1); expect(resultMap.get('collect_task')?.inputValue).toBe(1); }); + + it('should correctly parse a collection string with spaces into a Map', () => { + const collection = 'ec2_volumes/3600, ec2_instances/1800, ec2_reserved_instances/900'; + const resultMap = parseValue(collection); + + expect(resultMap.size).toBe(3); + expect(resultMap.get('ec2_volumes')?.inputValue).toBe(3600); + expect(resultMap.get('ec2_instances')?.inputValue).toBe(1800); + expect(resultMap.get('ec2_reserved_instances')?.inputValue).toBe(900); + }); + it('should return an empty Map for undefined collection', () => { const resultMap = parseValue(); expect(resultMap.size).toBe(0); diff --git a/ui/src/components/CheckboxGroup/checkboxGroup.utils.ts b/ui/src/components/CheckboxGroup/checkboxGroup.utils.ts index 698475b32..cb102c3f0 100644 --- a/ui/src/components/CheckboxGroup/checkboxGroup.utils.ts +++ b/ui/src/components/CheckboxGroup/checkboxGroup.utils.ts @@ -22,7 +22,7 @@ export function parseValue(collection?: string): ValueByField { const splitValues = collection.split(','); splitValues.forEach((rawValue) => { - const [field, inputValue] = rawValue.split('/'); + const [field, inputValue] = rawValue.trim().split('/'); const parsedInputValue = inputValue === '' ? undefined : Number(inputValue); if (!field || Number.isNaN(parsedInputValue)) { throw new Error(`Value is not parsable: ${collection}`);