Skip to content

Commit

Permalink
FIO-8986 fixed validation for Day component with two hidden fields
Browse files Browse the repository at this point in the history
  • Loading branch information
HannaKurban committed Sep 16, 2024
1 parent c2f3780 commit 692a04d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 9 deletions.
87 changes: 81 additions & 6 deletions src/process/validation/rules/__tests__/validateDay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { expect } from 'chai';
import { FieldError } from 'error';
import { simpleDayField, simpleTextField } from './fixtures/components';
import { generateProcessorContext } from './fixtures/util';
import { fastCloneDeep } from 'utils';
import { validateDay } from '../validateDay';

it('Validating a non-day component will return null', async () => {
Expand Down Expand Up @@ -59,7 +60,7 @@ it('Validating a day component with a valid Date object will return a field erro
});

it('Validating a day component with hidden day field with an valid date string value will return null', async () => {
const component = simpleDayField;
const component = fastCloneDeep(simpleDayField);
component.fields.day.hide = true;
const data = {
component: '03/2023',
Expand All @@ -70,7 +71,7 @@ it('Validating a day component with hidden day field with an valid date string v
});

it('Validating a day component with hidden day field with invalid date will return a field error', async () => {
const component = simpleDayField;
const component = fastCloneDeep(simpleDayField);
component.fields.day.hide = true;
const data = {
component: '13/2023',
Expand All @@ -82,7 +83,7 @@ it('Validating a day component with hidden day field with invalid date will retu
});

it('Validating a day component with hidden month field with an valid date string value will return null', async () => {
const component = simpleDayField;
const component = fastCloneDeep(simpleDayField);
component.fields.month.hide = true;
const data = {
component: '23/2023',
Expand All @@ -93,7 +94,7 @@ it('Validating a day component with hidden month field with an valid date string
});

it('Validating a day component with hidden month field with invalid date will return a field error', async () => {
const component = simpleDayField;
const component = fastCloneDeep(simpleDayField);;
component.fields.month.hide = true;
const data = {
component: '130/2023',
Expand All @@ -105,7 +106,7 @@ it('Validating a day component with hidden month field with invalid date will re
});

it('Validating a day component with hidden year field with an valid date string value will return null', async () => {
const component = simpleDayField;
const component = fastCloneDeep(simpleDayField);
component.fields.year.hide = true;
const data = {
component: '01/23',
Expand All @@ -116,7 +117,7 @@ it('Validating a day component with hidden year field with an valid date string
});

it('Validating a day component with hidden year field with invalid date will return a field error', async () => {
const component = simpleDayField;
const component = fastCloneDeep(simpleDayField);
component.fields.year.hide = true;
const data = {
component: '13/23',
Expand All @@ -127,3 +128,77 @@ it('Validating a day component with hidden year field with invalid date will ret
expect(result?.errorKeyOrMessage).to.equal('invalidDay');
});

it('Validating a day component with hidden year and month fields with an valid date string value will return null', async () => {
const component = fastCloneDeep(simpleDayField);
component.fields.year.hide = true;
component.fields.month.hide = true;
const data = {
component: '23',
};
const context = generateProcessorContext(component, data);
const result = await validateDay(context);
expect(result).to.equal(null);
});

it('Validating a day component with hidden year and month fields with invalid date will return a field error', async () => {
const component = fastCloneDeep(simpleDayField);
component.fields.year.hide = true;
component.fields.month.hide = true;
const data = {
component: '123',
};
const context = generateProcessorContext(component, data);
const result = await validateDay(context);
expect(result).to.be.instanceOf(FieldError);
expect(result?.errorKeyOrMessage).to.equal('invalidDay');
});

it('Validating a day component with hidden year and day fields with an valid date string value will return null', async () => {
const component = fastCloneDeep(simpleDayField);
component.fields.year.hide = true;
component.fields.day.hide = true;
const data = {
component: '10',
};
const context = generateProcessorContext(component, data);
const result = await validateDay(context);
expect(result).to.equal(null);
});

it('Validating a day component with hidden year and day fields with invalid date will return a field error', async () => {
const component = fastCloneDeep(simpleDayField);
component.fields.year.hide = true;
component.fields.day.hide = true;
const data = {
component: '22',
};
const context = generateProcessorContext(component, data);
const result = await validateDay(context);
expect(result).to.be.instanceOf(FieldError);
expect(result?.errorKeyOrMessage).to.equal('invalidDay');
});

it('Validating a day component with hidden day and month fields with an valid date string value will return null', async () => {
const component = fastCloneDeep(simpleDayField);
component.fields.month.hide = true;
component.fields.day.hide = true;
const data = {
component: '2024',
};
const context = generateProcessorContext(component, data);
const result = await validateDay(context);
expect(result).to.equal(null);
});

it('Validating a day component with hidden day and month fields with invalid date will return a field error', async () => {
const component = fastCloneDeep(simpleDayField);
component.fields.month.hide = true;
component.fields.day.hide = true;
const data = {
component: '100042',
};
const context = generateProcessorContext(component, data);
const result = await validateDay(context);
expect(result).to.be.instanceOf(FieldError);
expect(result?.errorKeyOrMessage).to.equal('invalidDay');
});
6 changes: 3 additions & 3 deletions src/process/validation/rules/validateDay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ export const validateDaySync: RuleFnSync = (context: ValidationContext) => {
if (component.fields.month.hide) {
DAY = DAY === 0 ? 0 : DAY - 1;
YEAR = YEAR - 1;
day = values[DAY];
day = (component.fields.day.hide && day === 0) ? 0 : values[DAY];
month = 0;
year = values[YEAR];
};
if (component.fields.year.hide) {
day = values[DAY];
month = values[MONTH];
day = (component.fields.day.hide && day === 0) ? 0 : values[DAY];
month = (component.fields.month.hide && month === 0) ? 0 :values[MONTH];
year = 0;
};
}
Expand Down

0 comments on commit 692a04d

Please sign in to comment.