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 fdf9e8dd..7159c720 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4253,6 +4253,23 @@ yargs@16.2.0, yargs@^16.2.0: y18n "^5.0.5" yargs-parser "^20.2.2" +yargs@^15.0.2: + version "15.4.1" + resolved "https://registry.yarnpkg.com/yargs/-/yargs-15.4.1.tgz#0d87a16de01aee9d8bec2bfbf74f67851730f4f8" + integrity sha512-aePbxDmcYW++PaqBsJ+HYUFwCdv4LVvdnhBy78E57PIor8/OVvhMrADFFEDh8DHDFRv/O9i3lPhsENjO7QX0+A== + dependencies: + cliui "^6.0.0" + decamelize "^1.2.0" + find-up "^4.1.0" + get-caller-file "^2.0.1" + require-directory "^2.1.1" + require-main-filename "^2.0.0" + set-blocking "^2.0.0" + string-width "^4.2.0" + which-module "^2.0.0" + y18n "^4.0.0" + yargs-parser "^18.1.2" + yn@3.1.1: version "3.1.1" resolved "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"