From 5d5209fdbb229dab21c6c9bf202d41458fdb94ad Mon Sep 17 00:00:00 2001 From: Hanna Kurban Date: Fri, 28 Jun 2024 16:54:02 +0300 Subject: [PATCH 1/2] FIO-8598 fixed normalization of radio component values depending on storage type --- .../normalize/__tests__/normalize.test.ts | 26 +++++++++++++++++++ src/process/normalize/index.ts | 12 +++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/process/normalize/__tests__/normalize.test.ts b/src/process/normalize/__tests__/normalize.test.ts index 9608906d..414a0b46 100644 --- a/src/process/normalize/__tests__/normalize.test.ts +++ b/src/process/normalize/__tests__/normalize.test.ts @@ -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', diff --git a/src/process/normalize/index.ts b/src/process/normalize/index.ts index 45b513c3..ad2df332 100644 --- a/src/process/normalize/index.ts +++ b/src/process/normalize/index.ts @@ -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; @@ -318,7 +326,7 @@ export const normalizeProcessSync: ProcessorFnSync = (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)); From 8bc2666e38188522d06ff3266042f87751b581ac Mon Sep 17 00:00:00 2001 From: John Teague Date: Fri, 28 Jun 2024 13:43:35 -0500 Subject: [PATCH 2/2] - added addtional test cases for radio normalization - increased coverage thresholds --- package.json | 10 +- .../normalize/__tests__/normalize.test.ts | 394 ++++++++++++------ yarn.lock | 5 - 3 files changed, 264 insertions(+), 145 deletions(-) diff --git a/package.json b/package.json index c18f888a..149d889d 100644 --- a/package.json +++ b/package.json @@ -105,10 +105,10 @@ }, "nyc": { "check-coverage": true, - "statements": 62, - "branches": 52, - "functions": 57, - "lines": 60, + "statements": 64, + "branches": 55, + "functions": 60, + "lines": 63, "include": [ "src/**/*.ts", "src/**/*.js" @@ -121,4 +121,4 @@ ], "all": true } -} \ No newline at end of file +} diff --git a/src/process/normalize/__tests__/normalize.test.ts b/src/process/normalize/__tests__/normalize.test.ts index 414a0b46..9d815bde 100644 --- a/src/process/normalize/__tests__/normalize.test.ts +++ b/src/process/normalize/__tests__/normalize.test.ts @@ -5,160 +5,284 @@ import { normalizeProcessSync } from '../'; import { generateProcessorContext } from '../../__tests__/fixtures/util'; it('Should normalize a time component with a valid time value that doees not match dataFormat', async () => { - const timeComp: TimeComponent = { - type: 'time', - key: 'time', - label: 'Time', - input: true, - dataFormat: 'HH:mm:ss' - }; - const data = { time: '12:00' }; - const context = generateProcessorContext(timeComp, data); - normalizeProcessSync(context); - expect(context.data).to.deep.equal({time: '12:00:00'}); + const timeComp: TimeComponent = { + type: 'time', + key: 'time', + label: 'Time', + input: true, + dataFormat: 'HH:mm:ss', + }; + const data = { time: '12:00' }; + const context = generateProcessorContext(timeComp, data); + normalizeProcessSync(context); + expect(context.data).to.deep.equal({ time: '12:00:00' }); }); it('Should normalize a select boxes component with an incorrect data model', () => { - const selectBoxesComp: SelectBoxesComponent = { - type: 'selectboxes', - key: 'selectBoxes', - label: 'Select Boxes', - input: true, - values: [ - {label: 'One', value: 'one'}, - {label: 'Two', value: 'two'}, - {label: 'Three', value: 'three'} - ] - }; - const data = { - selectBoxes: '' - }; - const context = generateProcessorContext(selectBoxesComp, data); - normalizeProcessSync(context); - expect(context.data).to.deep.equal({selectBoxes: {}}); + const selectBoxesComp: SelectBoxesComponent = { + type: 'selectboxes', + key: 'selectBoxes', + label: 'Select Boxes', + input: true, + values: [ + { label: 'One', value: 'one' }, + { label: 'Two', value: 'two' }, + { label: 'Three', value: 'three' }, + ], + }; + const data = { + selectBoxes: '', + }; + const context = generateProcessorContext(selectBoxesComp, data); + normalizeProcessSync(context); + expect(context.data).to.deep.equal({ selectBoxes: {} }); }); it('Should normalize an email component value', () => { - const emailComp = { - type: 'email', - key: 'email', - input: true, - label: 'Email' - }; - const data = { - email: 'BrendanBond@Gmail.com' - }; - const context = generateProcessorContext(emailComp, data); - normalizeProcessSync(context); - expect(context.data).to.deep.equal({email: 'brendanbond@gmail.com'}); + const emailComp = { + type: 'email', + key: 'email', + input: true, + label: 'Email', + }; + const data = { + email: 'BrendanBond@Gmail.com', + }; + const context = generateProcessorContext(emailComp, data); + normalizeProcessSync(context); + expect(context.data).to.deep.equal({ email: 'brendanbond@gmail.com' }); }); it('Should normalize a radio component with a string value', () => { - const radioComp = { - type: 'radio', - key: 'radio', - input: true, - label: 'Radio', - values: [ - { - label: 'Yes', - value: 'true', - }, - { - label: 'No', - value: 'false', - } - ] - }; - const data = { - radio: 'true' - }; - const context = generateProcessorContext(radioComp, data); - normalizeProcessSync(context); - expect(context.data).to.deep.equal({radio: true}); + const radioComp = { + type: 'radio', + key: 'radio', + input: true, + label: 'Radio', + values: [ + { + label: 'Yes', + value: 'true', + }, + { + label: 'No', + value: 'false', + }, + ], + }; + const data = { + radio: 'true', + }; + const context = generateProcessorContext(radioComp, data); + normalizeProcessSync(context); + expect(context.data).to.deep.equal({ radio: true }); +}); +it('Should normalize a radio component with a string value of false', () => { + const radioComp = { + type: 'radio', + key: 'radio', + input: true, + label: 'Radio', + values: [ + { + label: 'Yes', + value: 'true', + }, + { + label: 'No', + value: 'false', + }, + ], + }; + const data = { + radio: 'false', + }; + const context = generateProcessorContext(radioComp, data); + normalizeProcessSync(context); + expect(context.data).to.deep.equal({ radio: false }); }); it('Should normalize a radio component value with a number', () => { - const radioComp = { - type: 'radio', - key: 'radio', - input: true, - label: 'Radio', - values: [ - { - label: 'Yes', - value: '1', - }, - { - label: 'No', - value: '0', - } - ] - }; - const data = { - radio: '0' - }; - const context = generateProcessorContext(radioComp, data); - normalizeProcessSync(context); - expect(context.data).to.deep.equal({radio: 0}); + const radioComp = { + type: 'radio', + key: 'radio', + input: true, + label: 'Radio', + values: [ + { + label: 'Yes', + value: '1', + }, + { + label: 'No', + 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 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'}); + 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 radio component value with a number if storage type is set to number', () => { + const radioComp = { + type: 'radio', + key: 'radio', + input: true, + label: 'Radio', + dataType: 'number', + values: [ + { + label: '1', + value: 1, + }, + { + label: '0', + value: 2, + }, + ], + }; + const data = { + radio: 1, + }; + const context = generateProcessorContext(radioComp, data); + normalizeProcessSync(context); + expect(context.data).to.deep.equal({ radio: 1 }); +}); +it('Should normalize a radio component value with a boolean if storage type is set to boolean', () => { + const radioComp = { + type: 'radio', + key: 'radio', + input: true, + label: 'Radio', + dataType: 'boolean', + values: [ + { + label: '1', + value: 'true', + }, + { + label: '0', + value: 'false', + }, + ], + }; + const data = { + radio: true, + }; + const context = generateProcessorContext(radioComp, data); + normalizeProcessSync(context); + expect(context.data).to.deep.equal({ radio: true }); +}); +it('Should normalize a radio component value with a false boolean if storage type is set to boolean and has value of "false"', () => { + const radioComp = { + type: 'radio', + key: 'radio', + input: true, + label: 'Radio', + dataType: 'boolean', + values: [ + { + label: '1', + value: 'true', + }, + { + label: '0', + value: 'false', + }, + ], + }; + const data = { + radio: 'false', + }; + const context = generateProcessorContext(radioComp, data); + normalizeProcessSync(context); + expect(context.data).to.deep.equal({ radio: false }); +}); + +it('Should normalize a radio component value with an object if storage type is set to string and value is an object', () => { + const radioComp = { + type: 'radio', + key: 'radio', + input: true, + label: 'Radio', + dataType: 'string', + values: [ + { + label: '1', + value: 'true', + }, + { + label: '0', + value: 'false', + }, + ], + }; + const data = { + radio: { test: 'test' }, + }; + const context = generateProcessorContext(radioComp, data); + normalizeProcessSync(context); + expect(context.data).to.deep.equal({ + radio: JSON.stringify({ test: 'test' }), + }); }); it('Should normalize a number component value with a string value', () => { - const numberComp = { - type: 'number', - key: 'number', - input: true, - label: 'Number' - }; - const data = { - number: '000123' - }; - const context = generateProcessorContext(numberComp, data); - normalizeProcessSync(context); - expect(context.data).to.deep.equal({number: 123}); + const numberComp = { + type: 'number', + key: 'number', + input: true, + label: 'Number', + }; + const data = { + number: '000123', + }; + const context = generateProcessorContext(numberComp, data); + normalizeProcessSync(context); + expect(context.data).to.deep.equal({ number: 123 }); }); it('Should normalize a number component value with a multiple values allowed', () => { - const numberComp = { - type: 'number', - key: 'number', - input: true, - label: 'Number', - multiple: true - }; - const data = { - number: [ - '000.0123', - '123' - ] - }; - const context = generateProcessorContext(numberComp, data); - normalizeProcessSync(context); - expect(context.data).to.deep.equal({number: [0.0123, 123]}); + const numberComp = { + type: 'number', + key: 'number', + input: true, + label: 'Number', + multiple: true, + }; + const data = { + number: ['000.0123', '123'], + }; + const context = generateProcessorContext(numberComp, data); + normalizeProcessSync(context); + expect(context.data).to.deep.equal({ number: [0.0123, 123] }); }); diff --git a/yarn.lock b/yarn.lock index a1d33bc8..153a559d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4925,11 +4925,6 @@ yargs@^15.0.2: y18n "^4.0.0" yargs-parser "^18.1.2" -yarn.lock@^0.0.1-security: - version "0.0.1-security" - resolved "https://registry.yarnpkg.com/yarn.lock/-/yarn.lock-0.0.1-security.tgz#8e7117924bfe916671b21f14212ba1bb49dfe0c7" - integrity sha512-ZRX6v5zGCJMI1T2aO+BQxJggy1vvorXEwonQhWXIC+brO7lkDB3zWelVNAti183ddH6FmJP8z4UDCJnJlioK4Q== - yn@3.1.1: version "3.1.1" resolved "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"