Skip to content

Commit

Permalink
FIO-8598 fixed normalization of radio component values depending on s…
Browse files Browse the repository at this point in the history
…torage type
  • Loading branch information
HannaKurban committed Jun 28, 2024
1 parent 74ac883 commit 5d5209f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/process/normalize/__tests__/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,32 @@ it('Should normalize a radio component value with a number', () => {
expect(context.data).to.deep.equal({radio: 0});
});

it('Should normalize a radio component value with a string if storage type is set to string', () => {
const radioComp = {
type: 'radio',
key: 'radio',
input: true,
label: 'Radio',
dataType: 'string',
values: [
{
label: '1',
value: 1,
},
{
label: '0',
value: 0,
}
]
};
const data = {
radio: 0
};
const context = generateProcessorContext(radioComp, data);
normalizeProcessSync(context);
expect(context.data).to.deep.equal({radio: '0'});
});

it('Should normalize a number component value with a string value', () => {
const numberComp = {
type: 'number',
Expand Down
12 changes: 10 additions & 2 deletions src/process/normalize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,15 @@ const normalizeDayComponentValue = (component: DayComponent, form: any, value: a
return dateParts.join('/');
};

const normalizeRadioComponentValue = (value: any) => {
const normalizeRadioComponentValue = (value: any, dataType?: string) => {
switch(dataType) {
case 'number':
return +value;
case 'string':
return typeof value === 'object' ? JSON.stringify(value) : String(value);
case 'boolean':
return !(!value || value.toString() === 'false');
}
const isEquivalent = toString(value) === Number(value).toString();
if (!isNaN(parseFloat(value)) && isFinite(value) && isEquivalent) {
return +value;
Expand Down Expand Up @@ -318,7 +326,7 @@ export const normalizeProcessSync: ProcessorFnSync<NormalizeScope> = (context) =
scope.normalize[path].normalized = true;
}
} else if (isRadioComponent(component)) {
set(data, path, normalizeRadioComponentValue(value));
set(data, path, normalizeRadioComponentValue(value, component.dataType));
scope.normalize[path].normalized = true;
} else if (isSelectComponent(component)) {
set(data, path, normalizeSelectComponentValue(component, value));
Expand Down

0 comments on commit 5d5209f

Please sign in to comment.