Skip to content

Commit

Permalink
FIO-8798: update normalization for day component (#150)
Browse files Browse the repository at this point in the history
Co-authored-by: Maria.Golomb <[email protected]>
  • Loading branch information
Maria-Golomb and Maria.Golomb authored Sep 16, 2024
1 parent a7dca4b commit a25a9a3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
21 changes: 20 additions & 1 deletion src/process/normalize/__tests__/normalize.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';

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

Expand Down Expand Up @@ -286,3 +286,22 @@ it('Should normalize a number component value with a multiple values allowed', (
normalizeProcessSync(context);
expect(context.data).to.deep.equal({ number: [0.0123, 123] });
});

it('Should normalize a day component with disabled components ', async () => {
const dayComp: DayComponent = {
type: 'day',
key: 'day',
label: 'Day',
input: true,
defaultValue:'',
fields: {
day: {hide: true},
month: {hide: false},
year: {hide: false}
}
};
const data = { day: '01/2025', };
const context: ProcessorContext<ProcessorScope> = generateProcessorContext(dayComp, data);
normalizeProcessSync(context);
expect(context.data).to.deep.equal({ day: '01/2025' });
});
17 changes: 11 additions & 6 deletions src/process/normalize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,25 @@ const normalizeDayComponentValue = (component: DayComponent, form: any, value: a
const [DAY, MONTH, YEAR] = component.dayFirst ? [0, 1, 2] : [1, 0, 2];
const defaultValue = component.defaultValue ? component.defaultValue.split('/') : '';

const getNextPart = (shouldTake: boolean, defaultValue: string) =>
dateParts.push(shouldTake ? valueParts.shift() : defaultValue);
const getNextPart = (shouldTake: boolean, defaultValue: string) => {
// Only push the part if it's not an empty string
const part: string = shouldTake ? valueParts.shift() : defaultValue;
if (part !== '') {
dateParts.push(part);
}
}

if (isDayFirst) {
getNextPart(showDay, defaultValue ? defaultValue[DAY] : '00');
getNextPart(showDay, defaultValue ? defaultValue[DAY] : '');
}

getNextPart(showMonth, defaultValue ? defaultValue[MONTH] : '00');
getNextPart(showMonth, defaultValue ? defaultValue[MONTH] : '');

if (!isDayFirst) {
getNextPart(showDay, defaultValue ? defaultValue[DAY] : '00');
getNextPart(showDay, defaultValue ? defaultValue[DAY] : '');
}

getNextPart(showYear, defaultValue ? defaultValue[YEAR] : '0000');
getNextPart(showYear, defaultValue ? defaultValue[YEAR] : '');

return dateParts.join('/');
};
Expand Down

0 comments on commit a25a9a3

Please sign in to comment.