Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIO-8107: correct small error in normalize processor #66

Merged
merged 4 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 94 additions & 10 deletions src/process/normalize/__tests__/normalize.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,104 @@
import { expect } from 'chai';

import { TimeComponent } from 'types';
import { TimeComponent, SelectBoxesComponent } from 'types';
import { normalizeProcessSync } from '../';
import { generateProcessorContext } from '../../__tests__/fixtures/util';

const timeField: TimeComponent = {
type: 'time',
key: 'time',
label: 'Time',
input: true,
dataFormat: 'HH:mm:ss'
};

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(timeField, data);
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: {}});
});

it('Should normalize an email component value', () => {
const emailComp = {
type: 'email',
key: 'email',
input: true,
label: 'Email'
};
const data = {
email: '[email protected]'
};
const context = generateProcessorContext(emailComp, data);
normalizeProcessSync(context);
expect(context.data).to.deep.equal({email: '[email protected]'});
});

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});
});

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});
});
28 changes: 26 additions & 2 deletions src/process/normalize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ import {
TimeComponent
} from "types";

type NormalizeScope = DefaultValueScope;
type NormalizeScope = DefaultValueScope & {
normalize?: {
[path: string]: any;
}
}

dayjs.extend(customParseFormat)

Expand Down Expand Up @@ -188,6 +192,9 @@ const normalizeSelectComponentValue = (component: SelectComponent, value: any) =
};

const normalizeSelectBoxesComponentValue = (value: any) => {
if (!value) {
value = {};
}
if (typeof value !== 'object') {
if (typeof value === 'string') {
return {
Expand Down Expand Up @@ -227,7 +234,7 @@ const normalizeMaskValue = (
if (component.inputMasks && component.inputMasks.length > 0) {
if (!value || typeof value !== 'object') {
return {
val: value,
value: value,
maskName: component.inputMasks[0].label
}
}
Expand Down Expand Up @@ -272,33 +279,50 @@ export const normalizeProcess: ProcessorFn<NormalizeScope> = async (context) =>

export const normalizeProcessSync: ProcessorFnSync<NormalizeScope> = (context) => {
const { component, form, scope, path, data, value } = context;
if (!scope.normalize) {
scope.normalize = {};
}
let { defaultValues } = scope;
scope.normalize[path] = {
type: component.type,
normalized: false
};
// First check for component-type-specific transformations
if (isAddressComponent(component)) {
set(data, path, normalizeAddressComponentValue(component, value));
scope.normalize[path].normalized = true;
} else if (isDayComponent(component)) {
set(data, path, normalizeDayComponentValue(component, form, value));
scope.normalize[path].normalized = true;
} else if (isEmailComponent(component)) {
if (value && typeof value === 'string') {
set(data, path, value.toLowerCase());
scope.normalize[path].normalized = true;
}
} else if (isRadioComponent(component)) {
set(data, path, normalizeRadioComponentValue(value));
scope.normalize[path].normalized = true;
} else if (isSelectComponent(component)) {
set(data, path, normalizeSelectComponentValue(component, value));
scope.normalize[path].normalized = true;
} else if (isSelectBoxesComponent(component)) {
set(data, path, normalizeSelectBoxesComponentValue(value));
scope.normalize[path].normalized = true;
} else if (isTagsComponent(component)) {
set(data, path, normalizeTagsComponentValue(component, value));
scope.normalize[path].normalized = true;
} else if (isTextFieldComponent(component)) {
set(data, path, normalizeTextFieldComponentValue(component, defaultValues, value, path));
scope.normalize[path].normalized = true;
} else if (isTimeComponent(component)) {
set(data, path, normalizeTimeComponentValue(component, value));
scope.normalize[path].normalized = true;
}

// Next perform component-type-agnostic transformations (i.e. super())
if (component.multiple && !Array.isArray(value)) {
set(data, path, value ? [value] : []);
scope.normalize[path].normalized = true;
}
};

Expand Down
Loading