Skip to content
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
69 changes: 69 additions & 0 deletions src/app/shared/form/builder/parsers/complex-field-parser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { TestBed } from '@angular/core/testing';
import { DynamicFormService, DynamicFormValidationService } from '@ng-dynamic-forms/core';
import { FormFieldModel } from '../models/form-field.model';
import { ParserOptions } from './parser-options';
import { DynamicComplexModel } from '../ds-dynamic-form-ui/models/ds-dynamic-complex.model';
import { DsDynamicInputModel } from '../ds-dynamic-form-ui/models/ds-dynamic-input.model';
import { DynamicRowArrayModel } from '../ds-dynamic-form-ui/models/ds-dynamic-row-array-model';
import {ComplexFieldParser} from './complex-field-parser';
import { getMockTranslateService } from '../../../mocks/translate.service.mock';
Expand All @@ -22,6 +26,9 @@ describe('ComplexFieldParser test suite', () => {
};
const separator = ';';

const requiredInputs = (complexModel: DynamicComplexModel) =>
(complexModel.group as DsDynamicInputModel[]).filter((input) => input.required);

beforeEach(() => {
field = {
input: {
Expand Down Expand Up @@ -63,4 +70,66 @@ describe('ComplexFieldParser test suite', () => {
expect(fieldModel instanceof DynamicRowArrayModel).toBe(true);
expect(fieldModel.label).toBe(expectedValue);
});

it('should keep the mandatory message of a mandatory field on its required inputs', () => {
field.mandatory = 'true';
field.mandatoryMessage = 'Please fill all the fields for the contact person.';
const parser = new ComplexFieldParser(submissionId, field, initFormValues, parserOptions, separator, [],
translateService);

const inputs = requiredInputs(parser.modelFactory());

expect(inputs.length).toBe(3);
inputs.forEach((input) =>
expect(input.errorMessages.required).toBe('Please fill all the fields for the contact person.'));
});

describe('an optional field with required inputs', () => {
let complexModel: DynamicComplexModel;

beforeEach(() => {
const fundingField = {
input: {
type: 'complex',
},
mandatory: false,
label: 'Funding',
repeatable: true,
selectableMetadata: [
{
metadata: 'local.sponsor',
},
],
languageCodes: [],
complexDefinition: '[{"type":{"name":"type","value-pairs-name":"metashare_funding","input-type":"dropdown",' +
'"label":"Funding type","required":"true"}},{"code":{"name":"code","input-type":"autocomplete",' +
'"label":"Grant no. or funding project code","required":"true"}},{"orgname":{"name":"orgname",' +
'"input-type":"text","label":"Funding organization","required":"true"}},{"projname":{"name":"projname",' +
'"input-type":"autocomplete","label":"Funding project name","required":"true"}},{"openaire_id":' +
'{"name":"openaire_id","input-type":"text","label":"EU Project Identifier (OpenAIRE)","readonly":"true"}}]',
} as unknown as FormFieldModel;
const parser = new ComplexFieldParser(submissionId, fundingField, initFormValues, parserOptions, separator, [],
translateService);
complexModel = parser.modelFactory();
});

it('should give the required inputs the default required message', () => {
const inputs = requiredInputs(complexModel);

expect(inputs.length).toBe(4);
inputs.forEach((input) => expect(input.errorMessages.required).toBe('error.validation.required'));
});

it('should build the error messages of the empty required inputs', () => {
const formGroup = TestBed.inject(DynamicFormService).createFormGroup([complexModel]);
const validationService = TestBed.inject(DynamicFormValidationService);

requiredInputs(complexModel).forEach((input) => {
const control = formGroup.get([complexModel.id, input.id]);

expect(control.hasError('required')).toBe(true);
expect(validationService.createErrorMessages(control, input)).toEqual(['error.validation.required']);
});
});
});
});
6 changes: 5 additions & 1 deletion src/app/shared/form/builder/parsers/complex-field-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
SPONSOR_METADATA_NAME,

} from '../ds-dynamic-form-ui/models/ds-dynamic-complex.model';
import { hasValue, isNotEmpty, isUndefined } from '../../../empty.util';
import { hasValue, isEmpty, isNotEmpty, isUndefined } from '../../../empty.util';
import { ParserOptions } from './parser-options';
import {
CONFIG_DATA,
Expand Down Expand Up @@ -160,6 +160,10 @@ export class ComplexFieldParser extends FieldParser {
// required. It should be marked as required in the complex group for every input field.
if (inputConfig.required) {
this.markAsRequired(inputModel);
// an optional complex field has no mandatory message of its own
if (isEmpty(inputModel.errorMessages.required)) {
inputModel.errorMessages.required = 'error.validation.required';
}
}
concatGroup.group.push(inputModel);
});
Expand Down
Loading