mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-10 03:23:07 +00:00
Added more tests
This commit is contained in:
@@ -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 {
|
||||
|
@@ -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}"`);
|
||||
}
|
||||
|
63
src/app/shared/form/builder/parsers/tag-field-parser.spec.ts
Normal file
63
src/app/shared/form/builder/parsers/tag-field-parser.spec.ts
Normal 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);
|
||||
});
|
||||
|
||||
});
|
@@ -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);
|
||||
});
|
||||
|
||||
});
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -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: [
|
||||
const expectedErrors = [
|
||||
{
|
||||
fieldId: 'dc.title',
|
||||
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([]);
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user