From 40ccbdf65363db1577c81cdd71d52ced3a0cdcdd Mon Sep 17 00:00:00 2001 From: Giuseppe Digilio Date: Thu, 21 Jun 2018 11:40:00 +0200 Subject: [PATCH] Added more tests --- .../models/ds-dynamic-textarea.model.ts | 8 +- .../shared/form/builder/parsers/row-parser.ts | 4 - .../builder/parsers/tag-field-parser.spec.ts | 63 ++++++ .../parsers/textarea-field-parser.spec.ts | 61 +++++ .../builder/parsers/twobox-field-parser.ts | 10 - src/app/shared/form/form.reducer.spec.ts | 208 +++++++++++++----- 6 files changed, 276 insertions(+), 78 deletions(-) create mode 100644 src/app/shared/form/builder/parsers/tag-field-parser.spec.ts create mode 100644 src/app/shared/form/builder/parsers/textarea-field-parser.spec.ts delete mode 100644 src/app/shared/form/builder/parsers/twobox-field-parser.ts diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-textarea.model.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-textarea.model.ts index 9aead0447f..00d385edef 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-textarea.model.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-textarea.model.ts @@ -1,10 +1,4 @@ -import { - DYNAMIC_FORM_CONTROL_TYPE_TEXTAREA, - DynamicFormControlLayout, DynamicTextAreaModel, DynamicTextAreaModelConfig, - serializable -} from '@ng-dynamic-forms/core'; -import { Subject } from 'rxjs/Subject'; -import { LanguageCode } from '../../models/form-field-language-value.model'; +import { DYNAMIC_FORM_CONTROL_TYPE_TEXTAREA, DynamicFormControlLayout, serializable } from '@ng-dynamic-forms/core'; import { DsDynamicInputModel, DsDynamicInputModelConfig } from './ds-dynamic-input.model'; export interface DsDynamicTextAreaModelConfig extends DsDynamicInputModelConfig { diff --git a/src/app/shared/form/builder/parsers/row-parser.ts b/src/app/shared/form/builder/parsers/row-parser.ts index 3f76c1c5c1..d21799c584 100644 --- a/src/app/shared/form/builder/parsers/row-parser.ts +++ b/src/app/shared/form/builder/parsers/row-parser.ts @@ -93,10 +93,6 @@ export class RowParser { fieldModel = new GroupFieldParser(fieldData, this.initFormValues, this.readOnly, this.submissionScope, this.authorityOptions.uuid).parse(); break; - case 'twobox': - // group.push(new TwoboxFieldParser(fieldData).parse()); - break; - default: throw new Error(`unknown form control model type defined on JSON object with label "${fieldData.label}"`); } diff --git a/src/app/shared/form/builder/parsers/tag-field-parser.spec.ts b/src/app/shared/form/builder/parsers/tag-field-parser.spec.ts new file mode 100644 index 0000000000..d71681b32d --- /dev/null +++ b/src/app/shared/form/builder/parsers/tag-field-parser.spec.ts @@ -0,0 +1,63 @@ +import { FormFieldModel } from '../models/form-field.model'; +import { FormFieldMetadataValueObject } from '../models/form-field-metadata-value.model'; +import { TagFieldParser } from './tag-field-parser'; +import { DynamicTagModel } from '../ds-dynamic-form-ui/models/tag/dynamic-tag.model'; + +describe('TagFieldParser test suite', () => { + let field: FormFieldModel; + let initFormValues: any = {}; + + const authorityUuid = 'testScopeUUID'; + const readOnly = false; + + beforeEach(() => { + field = { + input: { + type: 'tag' + }, + label: 'Keywords', + mandatory: 'false', + repeatable: false, + hints: 'Local controlled vocabulary.', + selectableMetadata: [ + { + metadata: 'subject', + authority: 'JOURNALAuthority', + closed: false + } + ], + languageCodes: [] + } as FormFieldModel; + + }); + + it('should init parser properly', () => { + const parser = new TagFieldParser(field, initFormValues, readOnly, authorityUuid); + + expect(parser instanceof TagFieldParser).toBe(true); + }); + + it('should return a DynamicTagModel object when repeatable option is false', () => { + const parser = new TagFieldParser(field, initFormValues, readOnly, authorityUuid); + + const fieldModel = parser.parse(); + + expect(fieldModel instanceof DynamicTagModel).toBe(true); + }); + + it('should set init value properly', () => { + initFormValues = { + subject: [ + new FormFieldMetadataValueObject('test subject'), + new FormFieldMetadataValueObject('another test subject'), + ], + }; + + const parser = new TagFieldParser(field, initFormValues, readOnly, authorityUuid); + + const fieldModel = parser.parse(); + + expect(fieldModel.value).toEqual(initFormValues.subject); + }); + +}); diff --git a/src/app/shared/form/builder/parsers/textarea-field-parser.spec.ts b/src/app/shared/form/builder/parsers/textarea-field-parser.spec.ts new file mode 100644 index 0000000000..3dbc9c0814 --- /dev/null +++ b/src/app/shared/form/builder/parsers/textarea-field-parser.spec.ts @@ -0,0 +1,61 @@ +import { FormFieldModel } from '../models/form-field.model'; +import { FormFieldMetadataValueObject } from '../models/form-field-metadata-value.model'; +import { TextareaFieldParser } from './textarea-field-parser'; +import { DsDynamicTextAreaModel } from '../ds-dynamic-form-ui/models/ds-dynamic-textarea.model'; + +describe('TextareaFieldParser test suite', () => { + let field: FormFieldModel; + let initFormValues: any = {}; + + const authorityUuid = 'testScopeUUID'; + const readOnly = false; + + beforeEach(() => { + field = { + input: { + type: 'textarea' + }, + label: 'Description', + mandatory: 'false', + repeatable: false, + hints: 'Enter a description.', + selectableMetadata: [ + { + metadata: 'description' + } + ], + languageCodes: [] + } as FormFieldModel; + + }); + + it('should init parser properly', () => { + const parser = new TextareaFieldParser(field, initFormValues, readOnly); + + expect(parser instanceof TextareaFieldParser).toBe(true); + }); + + it('should return a DsDynamicTextAreaModel object when repeatable option is false', () => { + const parser = new TextareaFieldParser(field, initFormValues, readOnly); + + const fieldModel = parser.parse(); + + expect(fieldModel instanceof DsDynamicTextAreaModel).toBe(true); + }); + + it('should set init value properly', () => { + initFormValues = { + description: [ + new FormFieldMetadataValueObject('test description'), + ], + }; + const expectedValue ='test description'; + + const parser = new TextareaFieldParser(field, initFormValues, readOnly); + + const fieldModel = parser.parse(); + + expect(fieldModel.value).toEqual(expectedValue); + }); + +}); diff --git a/src/app/shared/form/builder/parsers/twobox-field-parser.ts b/src/app/shared/form/builder/parsers/twobox-field-parser.ts deleted file mode 100644 index 1614ccf331..0000000000 --- a/src/app/shared/form/builder/parsers/twobox-field-parser.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { FieldParser } from './field-parser'; -import { FormFieldModel } from '../models/form-field.model'; - -// @TODO to be implemented -export class TwoboxFieldParser extends FieldParser { - - public modelFactory(): any { - return null; - } -} diff --git a/src/app/shared/form/form.reducer.spec.ts b/src/app/shared/form/form.reducer.spec.ts index 64141a15b6..a76bfa5c03 100644 --- a/src/app/shared/form/form.reducer.spec.ts +++ b/src/app/shared/form/form.reducer.spec.ts @@ -1,9 +1,9 @@ -import { formReducer } from './form.reducers'; +import { FormEntry, formReducer } from './form.reducers'; import { FormAddError, - FormChangeAction, + FormChangeAction, FormClearErrorsAction, FormInitAction, - FormRemoveAction, + FormRemoveAction, FormRemoveErrorAction, FormStatusChangeAction } from './form.actions'; @@ -13,10 +13,10 @@ describe('formReducer', () => { const state = { testForm: { data: { - 'dc.contributor.author': null, - 'dc.title': null, - 'dc.date.issued': null, - 'dc.description': null + author: null, + title: null, + date: null, + description: null }, valid: false, errors: [] @@ -24,10 +24,10 @@ describe('formReducer', () => { }; const formId = 'testForm'; const formData = { - 'dc.contributor.author': null, - 'dc.title': null, - 'dc.date.issued': null, - 'dc.description': null + author: null, + title: null, + date: null, + description: null }; const valid = false; const action = new FormInitAction(formId, formData, valid); @@ -36,14 +36,54 @@ describe('formReducer', () => { expect(newState).toEqual(state); }); + it('should update state of the form when it\'s already present', () => { + const initState = { + testForm: { + data: { + author: null, + title: null, + date: null, + description: null + }, + valid: false, + errors: [] + } + }; + const formId = 'testForm'; + const formData = { + author: null, + title: 'title', + date: null, + description: null + }; + const state = { + testForm: { + data: { + author: null, + title: 'title', + date: null, + description: null + }, + valid: false, + errors: [] + } + }; + + const valid = false; + const action = new FormInitAction(formId, formData, valid); + const newState = formReducer(initState, action); + + expect(newState).toEqual(state); + }); + it('should change form data on form change', () => { const initState = { testForm: { data: { - 'dc.contributor.author': null, - 'dc.title': null, - 'dc.date.issued': null, - 'dc.description': null + author: null, + title: null, + date: null, + description: null }, valid: false, errors: [] @@ -52,10 +92,10 @@ describe('formReducer', () => { const state = { testForm: { data: { - 'dc.contributor.author': null, - 'dc.title': ['test'], - 'dc.date.issued': null, - 'dc.description': null + author: null, + title: ['test'], + date: null, + description: null }, valid: false, errors: [] @@ -63,10 +103,10 @@ describe('formReducer', () => { }; const formId = 'testForm'; const formData = { - 'dc.contributor.author': null, - 'dc.title': ['test'], - 'dc.date.issued': null, - 'dc.description': null + author: null, + title: ['test'], + date: null, + description: null }; const action = new FormChangeAction(formId, formData); @@ -79,10 +119,10 @@ describe('formReducer', () => { const initState = { testForm: { data: { - 'dc.contributor.author': null, - 'dc.title': ['test'], - 'dc.date.issued': null, - 'dc.description': null + author: null, + title: ['test'], + date: null, + description: null }, valid: false, errors: [] @@ -91,10 +131,10 @@ describe('formReducer', () => { const state = { testForm: { data: { - 'dc.contributor.author': null, - 'dc.title': ['test'], - 'dc.date.issued': null, - 'dc.description': null + author: null, + title: ['test'], + date: null, + description: null }, valid: true, errors: [] @@ -112,52 +152,79 @@ describe('formReducer', () => { const initState = { testForm: { data: { - 'dc.contributor.author': null, - 'dc.title': ['test'], - 'dc.date.issued': null, - 'dc.description': null + author: null, + title: ['test'], + date: null, + description: null }, valid: true, errors: [] } }; - const state = { - testForm: { - data: { - 'dc.contributor.author': null, - 'dc.title': ['test'], - 'dc.date.issued': null, - 'dc.description': null - }, - valid: true, - errors: [ - { - fieldId: 'dc.title', - message: 'Not valid' - } - ] + const expectedErrors = [ + { + fieldId: 'title', + message: 'Not valid' } - }; + ]; const formId = 'testForm'; - const fieldId = 'dc.title'; + const fieldId = 'title'; const message = 'Not valid'; const action = new FormAddError(formId, fieldId, message); const newState = formReducer(initState, action); - expect(newState).toEqual(state); + expect(newState.testForm.errors).toEqual(expectedErrors); + }); + + it('should remove errors from field', () => { + const initState = { + testForm: { + data: { + author: null, + title: ['test'], + date: null, + description: null + }, + valid: true, + errors: [ + { + fieldId: 'author', + message: 'error.validation.required' + }, + { + fieldId: 'title', + message: 'error.validation.required' + } + ] + } + }; + + const expectedErrors = [ + { + fieldId: 'title', + message: 'error.validation.required' + } + ]; + + const formId = 'testForm'; + + const action = new FormRemoveErrorAction(formId, 'author'); + const newState = formReducer(initState, action); + + expect(newState.testForm.errors).toEqual(expectedErrors); }); it('should remove form state', () => { const initState = { testForm: { data: { - 'dc.contributor.author': null, - 'dc.title': ['test'], - 'dc.date.issued': null, - 'dc.description': null + author: null, + title: ['test'], + date: null, + description: null }, valid: true, errors: [] @@ -171,4 +238,31 @@ describe('formReducer', () => { expect(newState).toEqual({}); }); + + it('should clear form errors', () => { + const initState = { + testForm: { + data: { + author: null, + title: ['test'], + date: null, + description: null + }, + valid: true, + errors: [ + { + fieldId: 'author', + message: 'error.validation.required' + } + ] + } + }; + + const formId = 'testForm'; + + const action = new FormClearErrorsAction(formId); + const newState = formReducer(initState, action); + + expect(newState.testForm.errors).toEqual([]); + }); });