Added more tests

This commit is contained in:
Giuseppe Digilio
2018-06-27 17:43:09 +02:00
parent 2ffbdf942c
commit f6b665cb90
3 changed files with 239 additions and 76 deletions

View File

@@ -8,8 +8,8 @@ import 'rxjs/add/observable/of';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { DsDynamicGroupComponent } from './dynamic-group.components'; import { DsDynamicGroupComponent } from './dynamic-group.components';
import { DynamicGroupModel } from './dynamic-group.model'; import { DynamicGroupModel, DynamicGroupModelConfig } from './dynamic-group.model';
import { FormRowModel } from '../../../../../../core/shared/config/config-submission-forms.model'; import { FormRowModel, SubmissionFormsModel } from '../../../../../../core/shared/config/config-submission-forms.model';
import { FormFieldModel } from '../../../models/form-field.model'; import { FormFieldModel } from '../../../models/form-field.model';
import { FormBuilderService } from '../../../form-builder.service'; import { FormBuilderService } from '../../../form-builder.service';
import { FormService } from '../../../../form.service'; import { FormService } from '../../../../form.service';
@@ -20,6 +20,11 @@ import { DynamicFormValidationService } from '@ng-dynamic-forms/core';
import { Store } from '@ngrx/store'; import { Store } from '@ngrx/store';
import { AppState } from '../../../../../../app.reducer'; import { AppState } from '../../../../../../app.reducer';
import { Observable } from 'rxjs/Observable'; import { Observable } from 'rxjs/Observable';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { Chips } from '../../../../../chips/models/chips.model';
import { FormFieldMetadataValueObject } from '../../../models/form-field-metadata-value.model';
import { DsDynamicInputModel } from '../ds-dynamic-input.model';
import { By } from '@angular/platform-browser';
function createTestComponent<T>(html: string, type: { new(...args: any[]): T }): ComponentFixture<T> { function createTestComponent<T>(html: string, type: { new(...args: any[]): T }): ComponentFixture<T> {
TestBed.overrideComponent(type, { TestBed.overrideComponent(type, {
@@ -31,13 +36,70 @@ function createTestComponent<T>(html: string, type: { new(...args: any[]): T }):
return fixture as ComponentFixture<T>; return fixture as ComponentFixture<T>;
} }
describe('Dynamic Group component', () => { export const FORM_GROUP_TEST_MODEL_CONFIG = {
disabled: false,
errorMessages: {required: 'You must specify at least one author.'},
formConfiguration: [{
fields: [{
hints: 'Enter the name of the author.',
input: {type: 'onebox'},
label: 'Author',
languageCodes: [],
mandatory: 'true',
mandatoryMessage: 'Required field!',
repeatable: false,
selectableMetadata: [{
authority: 'RPAuthority',
closed: false,
metadata: 'dc.contributor.author'
}],
} as FormFieldModel]
} as FormRowModel, {
fields: [{
hints: 'Enter the affiliation of the author.',
input: {type: 'onebox'},
label: 'Affiliation',
languageCodes: [],
mandatory: 'false',
repeatable: false,
selectableMetadata: [{
authority: 'OUAuthority',
closed: false,
metadata: 'local.contributor.affiliation'
}]
} as FormFieldModel]
} as FormRowModel],
id: 'dc_contributor_author',
label: 'Authors',
mandatoryField: 'dc.contributor.author',
name: 'dc.contributor.author',
placeholder: 'Authors',
readOnly: false,
relationFields: ['local.contributor.affiliation'],
required: true,
scopeUUID: '43fe1f8c-09a6-4fcf-9c78-5d4fed8f2c8f',
submissionScope: undefined,
validators: {required: null}
} as DynamicGroupModelConfig;
export const FORM_GROUP_TEST_GROUP = new FormGroup({
dc_contributor_author: new FormControl(),
});
describe('DsDynamicGroupComponent test suite', () => {
let testComp: TestComponent; let testComp: TestComponent;
let groupComp: DsDynamicGroupComponent;
let testFixture: ComponentFixture<TestComponent>; let testFixture: ComponentFixture<TestComponent>;
let groupFixture: ComponentFixture<DsDynamicGroupComponent>;
let modelValue: any;
let html; let html;
let control1: FormControl;
let model1: DsDynamicInputModel;
let control2: FormControl;
let model2: DsDynamicInputModel;
const mockStore: Store<AppState> = jasmine.createSpyObj('store', { const store: Store<AppState> = jasmine.createSpyObj('store', {
dispatch: {}, dispatch: {},
select: Observable.of(true) select: Observable.of(true)
}); });
@@ -47,6 +109,7 @@ describe('Dynamic Group component', () => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [
BrowserAnimationsModule,
FormsModule, FormsModule,
ReactiveFormsModule, ReactiveFormsModule,
NgbModule.forRoot(), NgbModule.forRoot(),
@@ -65,34 +128,181 @@ describe('Dynamic Group component', () => {
FormComponent, FormComponent,
FormService, FormService,
{provide: GLOBAL_CONFIG, useValue: {} as GlobalConfig}, {provide: GLOBAL_CONFIG, useValue: {} as GlobalConfig},
{provide: Store, useValue: mockStore}, {provide: Store, useValue: store},
], ],
schemas: [CUSTOM_ELEMENTS_SCHEMA] schemas: [CUSTOM_ELEMENTS_SCHEMA]
}); });
})); }));
// synchronous beforeEach describe('', () => {
beforeEach(() => { // synchronous beforeEach
html = ` beforeEach(() => {
<ds-date-picker html = `
[bindId]='bindId' <ds-dynamic-group [model]="model"
[group]='group' [formId]="formId"
[model]='model' [group]="group"
[showErrorMessages]='showErrorMessages' [showErrorMessages]="showErrorMessages"
(blur)='onBlur($event)' (blur)="onBlur($event)"
(change)='onValueChange($event)' (change)="onValueChange($event)"
(focus)='onFocus($event)'></ds-date-picker>`; (focus)="onFocus($event)"></ds-dynamic-group>`;
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>; testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
testComp = testFixture.componentInstance; testComp = testFixture.componentInstance;
});
it('should create DsDynamicGroupComponent', inject([DsDynamicGroupComponent], (app: DsDynamicGroupComponent) => {
expect(app).toBeDefined();
}));
}); });
it('should create DsDynamicGroupComponent', inject([DsDynamicGroupComponent], (app: DsDynamicGroupComponent) => { describe('when init model value is empty', () => {
beforeEach(inject([FormBuilderService], (service: FormBuilderService) => {
expect(app).toBeDefined(); groupFixture = TestBed.createComponent(DsDynamicGroupComponent);
})); groupComp = groupFixture.componentInstance; // FormComponent test instance
groupComp.formId = 'testForm';
groupComp.group = FORM_GROUP_TEST_GROUP;
groupComp.model = new DynamicGroupModel(FORM_GROUP_TEST_MODEL_CONFIG);
groupComp.showErrorMessages = false;
groupFixture.detectChanges();
control1 = service.getFormControlById('dc_contributor_author', (groupComp as any).formRef.formGroup, groupComp.formModel) as FormControl;
model1 = service.findById('dc_contributor_author', groupComp.formModel) as DsDynamicInputModel;
control2 = service.getFormControlById('local_contributor_affiliation', (groupComp as any).formRef.formGroup, groupComp.formModel) as FormControl;
model2 = service.findById('local_contributor_affiliation', groupComp.formModel) as DsDynamicInputModel;
// spyOn(store, 'dispatch');
}));
afterEach(() => {
groupFixture.destroy();
groupComp = null;
});
it('should init component properly', inject([FormBuilderService], (service: FormBuilderService) => {
const config = {rows: groupComp.model.formConfiguration} as SubmissionFormsModel;
const formModel = service.modelFromConfiguration(config, groupComp.model.scopeUUID, {}, groupComp.model.submissionScope, groupComp.model.readOnly);
const chips = new Chips([], 'value', 'dc.contributor.author');
expect(groupComp.formCollapsed).toEqual(Observable.of(false));
expect(groupComp.formModel.length).toEqual(formModel.length);
expect(groupComp.chips.getChipsItems()).toEqual(chips.getChipsItems());
}));
it('should save a new chips item', () => {
control1.setValue('test author');
(model1 as any).value = new FormFieldMetadataValueObject('test author');
control2.setValue('test affiliation');
(model2 as any).value = new FormFieldMetadataValueObject('test affiliation');
modelValue = [{
'dc.contributor.author': new FormFieldMetadataValueObject('test author'),
'local.contributor.affiliation': new FormFieldMetadataValueObject('test affiliation')
}];
groupFixture.detectChanges();
const de = groupFixture.debugElement.queryAll(By.css('button'));
const buttons = groupFixture.debugElement.nativeElement.querySelectorAll('button');
const btnEl = buttons[0];
btnEl.click();
expect(groupComp.chips.getChipsItems()).toEqual(modelValue);
expect(groupComp.formCollapsed).toEqual(Observable.of(true));
});
it('should clear form inputs', () => {
control1.setValue('test author');
(model1 as any).value = new FormFieldMetadataValueObject('test author');
control2.setValue('test affiliation');
(model2 as any).value = new FormFieldMetadataValueObject('test affiliation');
groupFixture.detectChanges();
const de = groupFixture.debugElement.queryAll(By.css('button'));
const buttons = groupFixture.debugElement.nativeElement.querySelectorAll('button');
const btnEl = buttons[2];
btnEl.click();
expect(control1.value).toBeNull();
expect(control2.value).toBeNull();
expect(groupComp.formCollapsed).toEqual(Observable.of(false));
});
});
describe('when init model value is not empty', () => {
beforeEach(inject([FormBuilderService], (service: FormBuilderService) => {
groupFixture = TestBed.createComponent(DsDynamicGroupComponent);
groupComp = groupFixture.componentInstance; // FormComponent test instance
groupComp.formId = 'testForm';
groupComp.group = FORM_GROUP_TEST_GROUP;
groupComp.model = new DynamicGroupModel(FORM_GROUP_TEST_MODEL_CONFIG);
modelValue = [{
'dc.contributor.author': new FormFieldMetadataValueObject('test author'),
'local.contributor.affiliation': new FormFieldMetadataValueObject('test affiliation')
}];
groupComp.model.value = modelValue;
groupComp.showErrorMessages = false;
groupFixture.detectChanges();
}));
afterEach(() => {
groupFixture.destroy();
groupComp = null;
});
it('should init component properly', inject([FormBuilderService], (service: FormBuilderService) => {
const config = {rows: groupComp.model.formConfiguration} as SubmissionFormsModel;
const formModel = service.modelFromConfiguration(config, groupComp.model.scopeUUID, {}, groupComp.model.submissionScope, groupComp.model.readOnly);
const chips = new Chips(modelValue, 'value', 'dc.contributor.author');
expect(groupComp.formCollapsed).toEqual(Observable.of(true));
expect(groupComp.formModel.length).toEqual(formModel.length);
expect(groupComp.chips.getChipsItems()).toEqual(chips.getChipsItems());
}));
it('should modify existing chips item', inject([FormBuilderService], (service: FormBuilderService) => {
groupComp.onChipSelected(0);
groupFixture.detectChanges();
control1 = service.getFormControlById('dc_contributor_author', (groupComp as any).formRef.formGroup, groupComp.formModel) as FormControl;
model1 = service.findById('dc_contributor_author', groupComp.formModel) as DsDynamicInputModel;
control1.setValue('test author modify');
(model1 as any).value = new FormFieldMetadataValueObject('test author modify');
modelValue = [{
'dc.contributor.author': new FormFieldMetadataValueObject('test author modify'),
'local.contributor.affiliation': new FormFieldMetadataValueObject('test affiliation')
}];
groupFixture.detectChanges();
const de = groupFixture.debugElement.queryAll(By.css('button'));
const buttons = groupFixture.debugElement.nativeElement.querySelectorAll('button');
const btnEl = buttons[0];
btnEl.click();
groupFixture.detectChanges();
expect(groupComp.chips.getChipsItems()).toEqual(modelValue);
expect(groupComp.formCollapsed).toEqual(Observable.of(true));
}));
it('should delete existing chips item', inject([FormBuilderService], (service: FormBuilderService) => {
groupComp.onChipSelected(0);
groupFixture.detectChanges();
const de = groupFixture.debugElement.queryAll(By.css('button'));
const buttons = groupFixture.debugElement.nativeElement.querySelectorAll('button');
const btnEl = buttons[1];
btnEl.click();
expect(groupComp.chips.getChipsItems()).toEqual([]);
expect(groupComp.formCollapsed).toEqual(Observable.of(false));
}));
});
}); });
// declare a test component // declare a test component
@@ -102,55 +312,9 @@ describe('Dynamic Group component', () => {
}) })
class TestComponent { class TestComponent {
group = new FormGroup({ group = FORM_GROUP_TEST_GROUP;
date: new FormControl(),
});
groupModelConfig = { groupModelConfig = FORM_GROUP_TEST_MODEL_CONFIG;
disabled: false,
errorMessages: {required: 'You must specify at least one author.'},
formConfiguration: [{
fields: [{
hints: 'Enter the name of the author.',
input: {type: 'onebox'},
label: 'Authors',
languageCodes: [],
mandatory: 'true',
mandatoryMessage: 'Required field!',
repeatable: false,
selectableMetadata: [{
authority: 'RPAuthority',
closed: false,
metadata: 'dc.contributor.author'
}],
} as FormFieldModel]
} as FormRowModel, {
fields: [{
hints: 'Enter the affiliation of the author.',
input: {type: 'onebox'},
label: 'Affiliation',
languageCodes: [],
mandatory: 'false',
repeatable: false,
selectableMetadata: [{
authority: 'OUAuthority',
closed: false,
metadata: 'local.contributor.affiliation'
}]
} as FormFieldModel]
} as FormRowModel],
id: 'date',
label: 'Date',
mandatoryField: 'dc.contributor.author',
name: 'date',
placeholder: 'Date',
readOnly: false,
relationFields: ['local.contributor.affiliation'],
required: true,
scopeUUID: '43fe1f8c-09a6-4fcf-9c78-5d4fed8f2c8f',
submissionScope: undefined,
validators: {required: null}
};
model = new DynamicGroupModel(this.groupModelConfig); model = new DynamicGroupModel(this.groupModelConfig);

View File

@@ -122,11 +122,12 @@ export class DsDynamicGroupComponent implements OnDestroy, OnInit {
|| this.selectedChipItem.item[model.name].value === PLACEHOLDER_PARENT_METADATA) || this.selectedChipItem.item[model.name].value === PLACEHOLDER_PARENT_METADATA)
? null ? null
: this.selectedChipItem.item[model.name]; : this.selectedChipItem.item[model.name];
if (value instanceof FormFieldMetadataValueObject || value instanceof AuthorityValueModel) { // if (value instanceof FormFieldMetadataValueObject || value instanceof AuthorityValueModel) {
model.valueUpdates.next(value.display); // model.valueUpdates.next(value.display);
} else { // } else {
model.valueUpdates.next(value); // model.valueUpdates.next(value);
} // }
model.valueUpdates.next(value);
}); });
}); });

View File

@@ -13,11 +13,9 @@ export const PLACEHOLDER_PARENT_METADATA = '#PLACEHOLDER_PARENT_METADATA_VALUE#'
export interface DynamicGroupModelConfig extends DsDynamicInputModelConfig { export interface DynamicGroupModelConfig extends DsDynamicInputModelConfig {
formConfiguration: FormRowModel[], formConfiguration: FormRowModel[],
mandatoryField: string, mandatoryField: string,
name: string,
relationFields: string[], relationFields: string[],
scopeUUID: string, scopeUUID: string,
submissionScope: string; submissionScope: string;
value?: any;
} }
/** /**