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-8986 fixed validation for Day component with two hidden fields #149

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
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
Loading