Added more tests

This commit is contained in:
Giuseppe Digilio
2018-06-21 11:40:00 +02:00
parent fe416b294b
commit 40ccbdf653
6 changed files with 276 additions and 78 deletions

View File

@@ -1,10 +1,4 @@
import { import { DYNAMIC_FORM_CONTROL_TYPE_TEXTAREA, DynamicFormControlLayout, serializable } from '@ng-dynamic-forms/core';
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 { DsDynamicInputModel, DsDynamicInputModelConfig } from './ds-dynamic-input.model'; import { DsDynamicInputModel, DsDynamicInputModelConfig } from './ds-dynamic-input.model';
export interface DsDynamicTextAreaModelConfig extends DsDynamicInputModelConfig { export interface DsDynamicTextAreaModelConfig extends DsDynamicInputModelConfig {

View File

@@ -93,10 +93,6 @@ export class RowParser {
fieldModel = new GroupFieldParser(fieldData, this.initFormValues, this.readOnly, this.submissionScope, this.authorityOptions.uuid).parse(); fieldModel = new GroupFieldParser(fieldData, this.initFormValues, this.readOnly, this.submissionScope, this.authorityOptions.uuid).parse();
break; break;
case 'twobox':
// group.push(new TwoboxFieldParser(fieldData).parse());
break;
default: default:
throw new Error(`unknown form control model type defined on JSON object with label "${fieldData.label}"`); throw new Error(`unknown form control model type defined on JSON object with label "${fieldData.label}"`);
} }

View File

@@ -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);
});
});

View File

@@ -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);
});
});

View File

@@ -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;
}
}

View File

@@ -1,9 +1,9 @@
import { formReducer } from './form.reducers'; import { FormEntry, formReducer } from './form.reducers';
import { import {
FormAddError, FormAddError,
FormChangeAction, FormChangeAction, FormClearErrorsAction,
FormInitAction, FormInitAction,
FormRemoveAction, FormRemoveAction, FormRemoveErrorAction,
FormStatusChangeAction FormStatusChangeAction
} from './form.actions'; } from './form.actions';
@@ -13,10 +13,10 @@ describe('formReducer', () => {
const state = { const state = {
testForm: { testForm: {
data: { data: {
'dc.contributor.author': null, author: null,
'dc.title': null, title: null,
'dc.date.issued': null, date: null,
'dc.description': null description: null
}, },
valid: false, valid: false,
errors: [] errors: []
@@ -24,10 +24,10 @@ describe('formReducer', () => {
}; };
const formId = 'testForm'; const formId = 'testForm';
const formData = { const formData = {
'dc.contributor.author': null, author: null,
'dc.title': null, title: null,
'dc.date.issued': null, date: null,
'dc.description': null description: null
}; };
const valid = false; const valid = false;
const action = new FormInitAction(formId, formData, valid); const action = new FormInitAction(formId, formData, valid);
@@ -36,14 +36,54 @@ describe('formReducer', () => {
expect(newState).toEqual(state); 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', () => { it('should change form data on form change', () => {
const initState = { const initState = {
testForm: { testForm: {
data: { data: {
'dc.contributor.author': null, author: null,
'dc.title': null, title: null,
'dc.date.issued': null, date: null,
'dc.description': null description: null
}, },
valid: false, valid: false,
errors: [] errors: []
@@ -52,10 +92,10 @@ describe('formReducer', () => {
const state = { const state = {
testForm: { testForm: {
data: { data: {
'dc.contributor.author': null, author: null,
'dc.title': ['test'], title: ['test'],
'dc.date.issued': null, date: null,
'dc.description': null description: null
}, },
valid: false, valid: false,
errors: [] errors: []
@@ -63,10 +103,10 @@ describe('formReducer', () => {
}; };
const formId = 'testForm'; const formId = 'testForm';
const formData = { const formData = {
'dc.contributor.author': null, author: null,
'dc.title': ['test'], title: ['test'],
'dc.date.issued': null, date: null,
'dc.description': null description: null
}; };
const action = new FormChangeAction(formId, formData); const action = new FormChangeAction(formId, formData);
@@ -79,10 +119,10 @@ describe('formReducer', () => {
const initState = { const initState = {
testForm: { testForm: {
data: { data: {
'dc.contributor.author': null, author: null,
'dc.title': ['test'], title: ['test'],
'dc.date.issued': null, date: null,
'dc.description': null description: null
}, },
valid: false, valid: false,
errors: [] errors: []
@@ -91,10 +131,10 @@ describe('formReducer', () => {
const state = { const state = {
testForm: { testForm: {
data: { data: {
'dc.contributor.author': null, author: null,
'dc.title': ['test'], title: ['test'],
'dc.date.issued': null, date: null,
'dc.description': null description: null
}, },
valid: true, valid: true,
errors: [] errors: []
@@ -112,52 +152,79 @@ describe('formReducer', () => {
const initState = { const initState = {
testForm: { testForm: {
data: { data: {
'dc.contributor.author': null, author: null,
'dc.title': ['test'], title: ['test'],
'dc.date.issued': null, date: null,
'dc.description': null description: null
}, },
valid: true, valid: true,
errors: [] errors: []
} }
}; };
const state = { const expectedErrors = [
testForm: {
data: {
'dc.contributor.author': null,
'dc.title': ['test'],
'dc.date.issued': null,
'dc.description': null
},
valid: true,
errors: [
{ {
fieldId: 'dc.title', fieldId: 'title',
message: 'Not valid' message: 'Not valid'
} }
] ];
}
};
const formId = 'testForm'; const formId = 'testForm';
const fieldId = 'dc.title'; const fieldId = 'title';
const message = 'Not valid'; const message = 'Not valid';
const action = new FormAddError(formId, fieldId, message); const action = new FormAddError(formId, fieldId, message);
const newState = formReducer(initState, action); 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', () => { it('should remove form state', () => {
const initState = { const initState = {
testForm: { testForm: {
data: { data: {
'dc.contributor.author': null, author: null,
'dc.title': ['test'], title: ['test'],
'dc.date.issued': null, date: null,
'dc.description': null description: null
}, },
valid: true, valid: true,
errors: [] errors: []
@@ -171,4 +238,31 @@ describe('formReducer', () => {
expect(newState).toEqual({}); 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([]);
});
}); });