diff --git a/src/process/normalize/__tests__/normalize.test.ts b/src/process/normalize/__tests__/normalize.test.ts index 9608906d..9d815bde 100644 --- a/src/process/normalize/__tests__/normalize.test.ts +++ b/src/process/normalize/__tests__/normalize.test.ts @@ -5,134 +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' }); +}); +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/src/process/normalize/index.ts b/src/process/normalize/index.ts index 64587bad..e4b816f6 100644 --- a/src/process/normalize/index.ts +++ b/src/process/normalize/index.ts @@ -104,7 +104,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; @@ -317,7 +325,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));