Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanbond committed Apr 25, 2024
1 parent 8524749 commit ef597b5
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 deletions.
10 changes: 9 additions & 1 deletion src/process/validation/rules/__tests__/fixtures/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ export const simpleRadioField: RadioComponent = {
input: true,
};

export const simpleCheckBoxField = {
label: 'Checkbox',
tableView: true,
key: 'component',
type: 'checkbox',
input: true,
};

export const hiddenRequiredField: HiddenComponent = {
type: 'hidden',
key: 'someData',
Expand Down Expand Up @@ -234,4 +242,4 @@ export const requiredNonInputField: any = {
validate: {
required: true
}
};
};
77 changes: 74 additions & 3 deletions src/process/validation/rules/__tests__/validateRequired.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ import { expect } from 'chai';

import { FieldError } from 'error';
import { validateRequired } from '../validateRequired';
import { conditionallyHiddenRequiredHiddenField, hiddenRequiredField, requiredNonInputField, simpleTextField } from './fixtures/components';
import {
conditionallyHiddenRequiredHiddenField,
hiddenRequiredField,
requiredNonInputField,
simpleTextField,
simpleSelectBoxes,
simpleRadioField,
simpleCheckBoxField,
} from './fixtures/components';
import { processOne } from 'processes/processOne';
import { generateProcessorContext } from './fixtures/util';
import { ProcessorsContext, ValidationScope } from 'types';
import { ProcessorsContext, SelectBoxesComponent, ValidationScope } from 'types';
import { validateAllProcess, validateProcessInfo } from 'processes/validation';

it('Validating a simple component that is required and not present in the data will return a field error', async () => {
Expand Down Expand Up @@ -116,7 +124,7 @@ it('Should not validate a non input comonent', async () => {
expect(context.scope.errors.length).to.equal(0);
});

it('Should validate a conditionally hidden compoentn with validateWhenHidden flag set to true', async () => {
it('Should validate a conditionally hidden component with validateWhenHidden flag set to true', async () => {
const component = {...simpleTextField};
component.validate = { required: true };
component.validateWhenHidden = true;
Expand All @@ -132,3 +140,66 @@ it('Should validate a conditionally hidden compoentn with validateWhenHidden fla
expect(context.scope.errors.length).to.equal(1);
expect(context.scope.errors[0] && context.scope.errors[0].errorKeyOrMessage).to.equal('required');
});

it('Validating a simple radio component that is required and present in the data with value set to false will return null', async () => {
const component = { ...simpleRadioField, validate: { required: true }, values: [
{
label: 'Yes',
value: 'true',
},
{
label: 'No',
value: 'false',
}] };
const data = { component: false };
const context = generateProcessorContext(component, data);
const result = await validateRequired(context);
expect(result).to.equal(null);
});


it('Validating a simple selectbox that is required and present in the data with value set to 0 will return null', async () => {
const component = { ...simpleSelectBoxes, validate: { required: true }, values: [
{
label: 'true',
value: 'true',
},
{
label: 'Null',
value: '0',
}] };
const data = { component: 0 };
const context = generateProcessorContext(component, data);
const result = await validateRequired(context);
expect(result).to.equal(null);
});

it('Validating a simple selectbox that is required and present in the data with value set to false will return a FieldError', async () => {
const component: SelectBoxesComponent = { ...simpleSelectBoxes, validate: { required: true }, values: [
{
label: 'true',
value: 'true',
},
{
label: 'false',
value: 'false',
}]
};
const data = {
component: {
true: false,
false: false
}
};
const context = generateProcessorContext(component, data);
const result = await validateRequired(context);
expect(result).to.be.instanceOf(FieldError);
});

it('Validating a simple checkbox that is required and present in the data with value set to false will return a FieldError', async () => {
const component = { ...simpleCheckBoxField, validate: { required: true } };
const data = { component: false };
const context = generateProcessorContext(component, data);
const result = await validateRequired(context);
expect(result).to.be.instanceOf(FieldError);
});
5 changes: 1 addition & 4 deletions src/process/validation/rules/validateRequired.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ export const validateRequiredSync: RuleFnSync = (context: ValidationContext) =>
else if (isComponentThatCannotHaveFalseValue(component)) {
return !valueIsPresent(value, false) ? error : null;
}
else if (!valueIsPresent(value, true)) {
return error;
}
return null;
return !valueIsPresent(value, true) ? error : null;
};

export const validateRequiredInfo: ProcessorInfo<ValidationContext, FieldError | null> = {
Expand Down

0 comments on commit ef597b5

Please sign in to comment.