Added tests

This commit is contained in:
Giuseppe Digilio
2019-01-12 15:56:56 +01:00
parent 9898d2a90c
commit 54f80a6e45
10 changed files with 673 additions and 121 deletions

View File

@@ -7,8 +7,10 @@ export function getMockFormBuilderService(): FormBuilderService {
createFormGroup: new FormGroup({}), createFormGroup: new FormGroup({}),
getValueFromModel: {}, getValueFromModel: {},
getFormControlById: new FormControl(), getFormControlById: new FormControl(),
hasMappedGroupValue: false,
findById: {}, findById: {},
getPath: ['test', 'path'], getPath: ['test', 'path'],
getId: 'path',
clearAllModelsValue : {}, clearAllModelsValue : {},
insertFormArrayGroup: {}, insertFormArrayGroup: {},
isQualdrop: false isQualdrop: false

View File

@@ -0,0 +1,17 @@
import { FormOperationsService } from '../../submission/sections/form/form-operations.service';
export function getMockFormOperationsService(): FormOperationsService {
return jasmine.createSpyObj('FormOperationsService', {
dispatchOperationsFromEvent: jasmine.createSpy('dispatchOperationsFromEvent'),
getArrayIndexFromEvent: jasmine.createSpy('getArrayIndexFromEvent'),
isPartOfArrayOfGroup: jasmine.createSpy('isPartOfArrayOfGroup'),
getQualdropValueMap: jasmine.createSpy('getQualdropValueMap'),
getFieldPathFromEvent: jasmine.createSpy('getFieldPathFromEvent'),
getQualdropItemPathFromEvent: jasmine.createSpy('getQualdropItemPathFromEvent'),
getFieldPathSegmentedFromChangeEvent: jasmine.createSpy('getFieldPathSegmentedFromChangeEvent'),
getFieldValueFromChangeEvent: jasmine.createSpy('getFieldValueFromChangeEvent'),
getValueMap: jasmine.createSpy('getValueMap'),
});
}

View File

@@ -1,12 +1,17 @@
import { of as observableOf } from 'rxjs';
import { FormService } from '../form/form.service'; import { FormService } from '../form/form.service';
export function getMockFormService( export function getMockFormService(
id$: string = 'random_id' id$: string = 'random_id'
): FormService { ): FormService {
return jasmine.createSpyObj('FormService', { return jasmine.createSpyObj('FormService', {
getFormData: jasmine.createSpy('getFormData'),
getUniqueId: id$, getUniqueId: id$,
resetForm: {}, resetForm: {},
validateAllFormFields: {} validateAllFormFields: {},
isValid: observableOf(true),
isFormInitialized: observableOf(true)
}); });
} }

View File

@@ -0,0 +1,7 @@
import { ScrollToService } from '@nicky-lenaers/ngx-scroll-to';
export function getMockScrollToService(): ScrollToService {
return jasmine.createSpyObj('scrollToService', {
scrollTo: jasmine.createSpy('scrollTo')
});
}

View File

@@ -93,9 +93,9 @@ describe('SubmissionFormComponent Component', () => {
}); });
afterEach(() => { afterEach(() => {
fixture.destroy();
comp = null; comp = null;
compAsAny = null; compAsAny = null;
fixture = null;
}); });
it('should not has effect when collectionId and submissionId are undefined', () => { it('should not has effect when collectionId and submissionId are undefined', () => {

View File

@@ -17,7 +17,7 @@
aria-hidden="true"></i> aria-hidden="true"></i>
<i *ngIf="(sectionRef.hasErrors())" class="fas fa-exclamation-circle text-danger mr-3" <i *ngIf="(sectionRef.hasErrors())" class="fas fa-exclamation-circle text-danger mr-3"
aria-hidden="true"></i> aria-hidden="true"></i>
<i *ngIf="(sectionRef.isValid() | async)" class="fas fa-check-circle text-success mr-3" <i *ngIf="(sectionRef.isValid() | async) && !(sectionRef.hasErrors())" class="fas fa-check-circle text-success mr-3"
aria-hidden="true"></i> aria-hidden="true"></i>
<a class="close" aria-label="Close"> <a class="close" aria-label="Close">
<span *ngIf="sectionRef.isOpen()" class="fas fa-chevron-up fa-fw" <span *ngIf="sectionRef.isOpen()" class="fas fa-chevron-up fa-fw"
@@ -32,7 +32,7 @@
</div> </div>
</ng-template> </ng-template>
<ng-template ngbPanelContent> <ng-template ngbPanelContent>
<div *ngIf="sectionRef.hasGenericErrors()"> <div id="sectionGenericError_{{sectionData.id}}" *ngIf="sectionRef.hasGenericErrors()">
<ds-alert *ngFor="let error of sectionRef.getErrors(); let i = index" <ds-alert *ngFor="let error of sectionRef.getErrors(); let i = index"
[content]="error" [content]="error"
[dismissible]="true" [dismissible]="true"

View File

@@ -1,81 +1,72 @@
// Load the implementations that should be tested // Load the implementations that should be tested
import { ChangeDetectorRef, Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ActivatedRoute, Router } from '@angular/router';
import { async, ComponentFixture, inject, TestBed, } from '@angular/core/testing'; import { async, ComponentFixture, inject, TestBed, } from '@angular/core/testing';
import { BrowserModule } from '@angular/platform-browser'; import { By } from '@angular/platform-browser';
import { of as observableOf } from 'rxjs'; import { of as observableOf } from 'rxjs';
import { ActionsSubject, Store } from '@ngrx/store';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { TranslateModule } from '@ngx-translate/core'; import { TranslateModule } from '@ngx-translate/core';
import { ScrollToService } from '@nicky-lenaers/ngx-scroll-to';
import { SectionContainerComponent } from './section-container.component'; import { SectionContainerComponent } from './section-container.component';
import { createTestComponent } from '../../../shared/testing/utils'; import { createTestComponent } from '../../../shared/testing/utils';
import { SectionsType } from '../sections-type'; import { SectionsType } from '../sections-type';
import { SectionsDirective } from '../sections.directive'; import { SectionsDirective } from '../sections.directive';
import { SubmissionState } from '../../submission.reducers';
import { SubmissionService } from '../../submission.service'; import { SubmissionService } from '../../submission.service';
import { GLOBAL_CONFIG } from '../../../../config';
import { SubmissionRestService } from '../../submission-rest.service';
import { SubmissionRestServiceStub } from '../../../shared/testing/submission-rest-service-stub';
import { MockRouter } from '../../../shared/mocks/mock-router';
import { RouteService } from '../../../shared/services/route.service';
import { MockActivatedRoute } from '../../../shared/mocks/mock-active-router';
import { SectionsService } from '../sections.service'; import { SectionsService } from '../sections.service';
import { NotificationsService } from '../../../shared/notifications/notifications.service'; import { SubmissionServiceStub } from '../../../shared/testing/submission-service-stub';
import { SectionsServiceStub } from '../../../shared/testing/sections-service-stub';
import { SectionDataObject } from '../models/section-data.model';
import { mockSubmissionCollectionId, mockSubmissionId } from '../../../shared/mocks/mock-submission';
const sectionState = {
header: 'submit.progressbar.describe.stepone',
config: 'https://rest.api/dspace-spring-rest/api/config/submissionforms/traditionalpageone',
mandatory: true,
sectionType: 'submission-form',
collapsed: false,
enabled: true,
data: {},
errors: [],
isLoading: false,
isValid: false
} as any;
const sectionObject: SectionDataObject = {
config: 'https://dspace7.4science.it/or2018/api/config/submissionforms/traditionalpageone',
mandatory: true,
data: {},
errors: [],
header: 'submit.progressbar.describe.stepone',
id: 'traditionalpageone',
sectionType: SectionsType.SubmissionForm
};
describe('SectionContainerComponent test suite', () => { describe('SectionContainerComponent test suite', () => {
let testComp: TestComponent; let comp: SectionContainerComponent;
let testFixture: ComponentFixture<TestComponent>; let compAsAny: any;
let html; let fixture: ComponentFixture<SectionContainerComponent>;
const config = { let submissionServiceStub: SubmissionServiceStub;
autosave: { let sectionsServiceStub: SectionsServiceStub;
metadata: ['dc.title', 'dc.identifier.doi', 'dc.identifier.pmid', 'dc.identifier.arxiv'],
timer: 5 const submissionId = mockSubmissionId;
}, const collectionId = mockSubmissionCollectionId;
metadata: {
icons: [ function init() {
{ sectionsServiceStub = TestBed.get(SectionsService);
name: 'dc.contributor.author', submissionServiceStub = TestBed.get(SubmissionService);
config: {
withAuthority:{ sectionsServiceStub.isSectionValid.and.returnValue(observableOf(true));
style: 'fa-user' sectionsServiceStub.getSectionState.and.returnValue(observableOf(sectionState));
submissionServiceStub.getActiveSectionId.and.returnValue(observableOf('traditionalpageone'));
} }
}
},
{
name: 'local.contributor.affiliation',
config: {
withAuthority:{
style: 'fa-university'
},
withoutAuthority:{
style: 'fa-university text-muted'
}
}
},
{
name: 'default',
config: {}
}
]
}
} as any;
const restService = new SubmissionRestServiceStub();
const router = new MockRouter();
const store = new Store<SubmissionState>(observableOf({}), new ActionsSubject(), undefined);
// async beforeEach // async beforeEach
beforeEach(async(() => { beforeEach(async(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [
BrowserModule,
CommonModule,
NgbModule.forRoot(), NgbModule.forRoot(),
TranslateModule.forRoot() TranslateModule.forRoot()
], ],
@@ -85,25 +76,20 @@ describe('SectionContainerComponent test suite', () => {
TestComponent, TestComponent,
], // declare the test component ], // declare the test component
providers: [ providers: [
ChangeDetectorRef, { provide: SectionsService, useClass: SectionsServiceStub },
NotificationsService, { provide: SubmissionService, useClass: SubmissionServiceStub },
RouteService, SectionContainerComponent
ScrollToService,
SectionContainerComponent,
SectionsService,
SubmissionService,
{ provide: ActivatedRoute, useValue: new MockActivatedRoute() },
{ provide: Router, useValue: router },
{ provide: SubmissionRestService, useValue: restService },
{ provide: GLOBAL_CONFIG, useValue: config },
{ provide: Store, useValue: store }
], ],
schemas: [CUSTOM_ELEMENTS_SCHEMA] schemas: [CUSTOM_ELEMENTS_SCHEMA]
}); }).compileComponents();
})); }));
describe('', () => { describe('', () => {
let testComp: TestComponent;
let testFixture: ComponentFixture<TestComponent>;
let html;
// synchronous beforeEach // synchronous beforeEach
beforeEach(() => { beforeEach(() => {
html = ` html = `
@@ -113,13 +99,126 @@ describe('SectionContainerComponent test suite', () => {
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>; testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
testComp = testFixture.componentInstance; testComp = testFixture.componentInstance;
init();
}); });
it('should create Chips Component', inject([SectionContainerComponent], (app: SectionContainerComponent) => { it('should create SectionContainerComponent', inject([SectionContainerComponent], (app: SectionContainerComponent) => {
expect(app).toBeDefined(); expect(app).toBeDefined();
})); }));
}); });
describe('', () => {
beforeEach(() => {
init();
fixture = TestBed.createComponent(SectionContainerComponent);
comp = fixture.componentInstance;
compAsAny = comp;
comp.submissionId = submissionId;
comp.collectionId = collectionId;
comp.sectionData = sectionObject;
spyOn(comp, 'getSectionContent');
fixture.detectChanges();
});
afterEach(() => {
fixture.destroy();
comp = null;
compAsAny = null;
});
it('should inject section properly', () => {
spyOn(comp.sectionRef, 'isEnabled').and.returnValue(observableOf(true));
spyOn(comp.sectionRef, 'hasGenericErrors').and.returnValue(false);
comp.ngOnInit();
fixture.detectChanges();
const section = fixture.debugElement.query(By.css('[id^=\'sectionContent_\']'));
expect(comp.getSectionContent).toHaveBeenCalled();
expect(section).toBeDefined();
});
it('should call removeSection properly', () => {
const mockEvent = jasmine.createSpyObj('event', {
preventDefault: jasmine.createSpy('preventDefault'),
stopPropagation: jasmine.createSpy('stopPropagation'),
});
spyOn(comp.sectionRef, 'removeSection');
comp.removeSection(mockEvent);
expect(mockEvent.preventDefault).toHaveBeenCalled();
expect(mockEvent.stopPropagation).toHaveBeenCalled();
expect(comp.sectionRef.removeSection).toHaveBeenCalledWith(submissionId, 'traditionalpageone');
});
it('should display generic section errors div', () => {
let sectionErrorsDiv = fixture.debugElement.query(By.css('[id^=\'sectionGenericError_\']'));
expect(sectionErrorsDiv).toBeNull();
spyOn(comp.sectionRef, 'isEnabled').and.returnValue(observableOf(true));
spyOn(comp.sectionRef, 'hasGenericErrors').and.returnValue(true);
comp.ngOnInit();
fixture.detectChanges();
sectionErrorsDiv = fixture.debugElement.query(By.css('[id^=\'sectionGenericError_\']'));
expect(sectionErrorsDiv).toBeDefined();
});
it('should display warning icon', () => {
spyOn(comp.sectionRef, 'isEnabled').and.returnValue(observableOf(true));
spyOn(comp.sectionRef, 'isValid').and.returnValue(observableOf(false));
spyOn(comp.sectionRef, 'hasErrors').and.returnValue(false);
comp.ngOnInit();
fixture.detectChanges();
const iconWarn = fixture.debugElement.query(By.css('i.text-warning'));
const iconErr = fixture.debugElement.query(By.css('i.text-danger'));
const iconSuccess = fixture.debugElement.query(By.css('i.text-success'));
expect(iconWarn).toBeDefined();
expect(iconErr).toBeNull();
expect(iconSuccess).toBeNull();
});
it('should display error icon', () => {
spyOn(comp.sectionRef, 'isEnabled').and.returnValue(observableOf(true));
spyOn(comp.sectionRef, 'isValid').and.returnValue(observableOf(false));
spyOn(comp.sectionRef, 'hasErrors').and.returnValue(true);
comp.ngOnInit();
fixture.detectChanges();
const iconWarn = fixture.debugElement.query(By.css('i.text-warning'));
const iconErr = fixture.debugElement.query(By.css('i.text-danger'));
const iconSuccess = fixture.debugElement.query(By.css('i.text-success'));
expect(iconWarn).toBeNull();
expect(iconErr).toBeDefined();
expect(iconSuccess).toBeNull();
});
it('should display success icon', () => {
spyOn(comp.sectionRef, 'isEnabled').and.returnValue(observableOf(true));
spyOn(comp.sectionRef, 'isValid').and.returnValue(observableOf(true));
spyOn(comp.sectionRef, 'hasErrors').and.returnValue(false);
comp.ngOnInit();
fixture.detectChanges();
const iconWarn = fixture.debugElement.query(By.css('i.text-warning'));
const iconErr = fixture.debugElement.query(By.css('i.text-danger'));
const iconSuccess = fixture.debugElement.query(By.css('i.text-success'));
expect(iconWarn).toBeNull();
expect(iconErr).toBeNull();
expect(iconSuccess).toBeDefined();
});
});
}); });
// declare a test component // declare a test component
@@ -130,14 +229,6 @@ describe('SectionContainerComponent test suite', () => {
class TestComponent { class TestComponent {
public collectionId = '1c11f3f1-ba1f-4f36-908a-3f1ea9a557eb'; public collectionId = '1c11f3f1-ba1f-4f36-908a-3f1ea9a557eb';
public submissionId = 1; public submissionId = mockSubmissionId;
public object = { public object = sectionObject;
config: 'https://dspace7.4science.it/or2018/api/config/submissionforms/traditionalpageone',
mandatory: true,
data: {},
errors: [],
header: 'submit.progressbar.describe.stepone',
id: 'traditionalpageone',
sectionType: SectionsType.SubmissionForm
}
} }

View File

@@ -0,0 +1,454 @@
import { ChangeDetectorRef, Component, NO_ERRORS_SCHEMA } from '@angular/core';
import { async, ComponentFixture, inject, TestBed } from '@angular/core/testing';
import { of as observableOf } from 'rxjs';
import { TranslateModule, TranslateService } from '@ngx-translate/core';
import { createTestComponent } from '../../../shared/testing/utils';
import { NotificationsService } from '../../../shared/notifications/notifications.service';
import { NotificationsServiceStub } from '../../../shared/testing/notifications-service-stub';
import { SubmissionService } from '../../submission.service';
import { SubmissionServiceStub } from '../../../shared/testing/submission-service-stub';
import { getMockTranslateService } from '../../../shared/mocks/mock-translate.service';
import { SectionsService } from '../sections.service';
import { SectionsServiceStub } from '../../../shared/testing/sections-service-stub';
import { FormSectionComponent } from './section-form.component';
import { FormBuilderService } from '../../../shared/form/builder/form-builder.service';
import { getMockFormBuilderService } from '../../../shared/mocks/mock-form-builder-service';
import { getMockFormOperationsService } from '../../../shared/mocks/mock-form-operations-service';
import { FormOperationsService } from './form-operations.service';
import { getMockFormService } from '../../../shared/mocks/mock-form-service';
import { FormService } from '../../../shared/form/form.service';
import { SubmissionFormsConfigService } from '../../../core/config/submission-forms-config.service';
import { GLOBAL_CONFIG, GlobalConfig } from '../../../../config';
import { MOCK_SUBMISSION_CONFIG } from '../../../shared/testing/mock-submission-config';
import { SectionDataObject } from '../models/section-data.model';
import { SectionsType } from '../sections-type';
import {
mockSubmissionCollectionId,
mockSubmissionId,
mockUploadResponse1ParsedErrors
} from '../../../shared/mocks/mock-submission';
import { BrowserModule } from '@angular/platform-browser';
import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { FormComponent } from '../../../shared/form/form.component';
import { FormFieldModel } from '../../../shared/form/builder/models/form-field.model';
import { FormRowModel } from '../../../core/config/models/config-submission-forms.model';
import { ConfigData } from '../../../core/config/config-data';
import { PageInfo } from '../../../core/shared/page-info.model';
import { FormFieldMetadataValueObject } from '../../../shared/form/builder/models/form-field-metadata-value.model';
import { DynamicRowGroupModel } from '../../../shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-row-group-model';
import { DsDynamicInputModel } from '../../../shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-input.model';
import { SubmissionSectionError } from '../../objects/submission-objects.reducer';
import { DynamicFormControlEvent, DynamicFormControlEventType } from '@ng-dynamic-forms/core';
import { JsonPatchOperationPathCombiner } from '../../../core/json-patch/builder/json-patch-operation-path-combiner';
function getMockSubmissionFormsConfigService(): SubmissionFormsConfigService {
return jasmine.createSpyObj('FormOperationsService', {
getConfigAll: jasmine.createSpy('getConfigAll'),
getConfigByHref: jasmine.createSpy('getConfigByHref'),
getConfigByName: jasmine.createSpy('getConfigByName'),
getConfigBySearch: jasmine.createSpy('getConfigBySearch')
});
}
const sectionObject: SectionDataObject = {
config: 'https://dspace7.4science.it/or2018/api/config/submissionforms/traditionalpageone',
mandatory: true,
data: {},
errors: [],
header: 'submit.progressbar.describe.stepone',
id: 'traditionalpageone',
sectionType: SectionsType.SubmissionForm
};
const testFormConfiguration = {
name: 'testFormConfiguration',
rows: [
{
fields: [
{
input: {
type: 'onebox'
},
label: 'Title',
mandatory: 'true',
repeatable: false,
hints: ' Enter Title.',
selectableMetadata: [
{
metadata: 'dc.title'
}
],
languageCodes: []
} as FormFieldModel
]
} as FormRowModel,
{
fields: [
{
input: {
type: 'onebox'
},
label: 'Author',
mandatory: 'false',
repeatable: false,
hints: ' Enter Author.',
selectableMetadata: [
{
metadata: 'dc.contributor'
}
],
languageCodes: []
} as FormFieldModel
]
} as FormRowModel,
],
self: 'testFormConfiguration.url',
type: 'submissionform',
_links: {
self: 'testFormConfiguration.url'
}
} as any;
const testFormModel = [
new DynamicRowGroupModel({
id: 'df-row-group-config-1',
group: [new DsDynamicInputModel({ id: 'dc.title' })],
}),
new DynamicRowGroupModel({
id: 'df-row-group-config-2',
group: [new DsDynamicInputModel({ id: 'dc.contributor' })],
})
];
const dynamicFormControlEvent: DynamicFormControlEvent = {
$event: new Event('change'),
context: null,
control: null,
group: testFormModel[0] as any,
model: testFormModel[0].group[0],
type: DynamicFormControlEventType.Change
};
describe('FormSectionComponent test suite', () => {
let comp: FormSectionComponent;
let compAsAny: any;
let fixture: ComponentFixture<FormSectionComponent>;
let submissionServiceStub: SubmissionServiceStub;
let sectionsServiceStub: SectionsServiceStub;
let notificationsServiceStub: NotificationsServiceStub;
let formService: any;
let formConfigService: any;
let formOperationsService: any;
let formBuilderService: any;
let translateService: any;
const envConfig: GlobalConfig = MOCK_SUBMISSION_CONFIG;
const submissionId = mockSubmissionId;
const collectionId = mockSubmissionCollectionId;
const parsedSectionErrors: any = mockUploadResponse1ParsedErrors.traditionalpageone;
const formConfigData = new ConfigData(new PageInfo(), testFormConfiguration);
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
BrowserModule,
CommonModule,
FormsModule,
ReactiveFormsModule,
TranslateModule.forRoot()
],
declarations: [
FormComponent,
FormSectionComponent,
TestComponent
],
providers: [
{ provide: FormBuilderService, useValue: getMockFormBuilderService() },
{ provide: FormOperationsService, useValue: getMockFormOperationsService() },
{ provide: FormService, useValue: getMockFormService() },
{ provide: SubmissionFormsConfigService, useValue: getMockSubmissionFormsConfigService() },
{ provide: NotificationsService, useClass: NotificationsServiceStub },
{ provide: SectionsService, useClass: SectionsServiceStub },
{ provide: SubmissionService, useClass: SubmissionServiceStub },
{ provide: TranslateService, useValue: getMockTranslateService() },
{ provide: GLOBAL_CONFIG, useValue: envConfig },
{ provide: 'collectionIdProvider', useValue: collectionId },
{ provide: 'sectionDataProvider', useValue: sectionObject },
{ provide: 'submissionIdProvider', useValue: submissionId },
ChangeDetectorRef,
FormSectionComponent
],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents().then();
}));
describe('', () => {
let testComp: TestComponent;
let testFixture: ComponentFixture<TestComponent>;
// synchronous beforeEach
beforeEach(() => {
const html = `
<ds-submission-section-form></ds-submission-section-form>`;
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
testComp = testFixture.componentInstance;
});
afterEach(() => {
testFixture.destroy();
});
it('should create FormSectionComponent', inject([FormSectionComponent], (app: FormSectionComponent) => {
expect(app).toBeDefined();
}));
});
describe('', () => {
beforeEach(() => {
fixture = TestBed.createComponent(FormSectionComponent);
comp = fixture.componentInstance;
compAsAny = comp;
submissionServiceStub = TestBed.get(SubmissionService);
sectionsServiceStub = TestBed.get(SectionsService);
formService = TestBed.get(FormService);
formConfigService = TestBed.get(SubmissionFormsConfigService);
formBuilderService = TestBed.get(FormBuilderService);
formOperationsService = TestBed.get(FormOperationsService);
translateService = TestBed.get(TranslateService);
notificationsServiceStub = TestBed.get(NotificationsService);
translateService.get.and.returnValue(observableOf('test'));
compAsAny.pathCombiner = new JsonPatchOperationPathCombiner('sections', sectionObject.id);
});
afterEach(() => {
fixture.destroy();
comp = null;
compAsAny = null;
});
it('should init section properly', () => {
const sectionData = {};
formConfigService.getConfigByHref.and.returnValue(observableOf(formConfigData));
sectionsServiceStub.getSectionData.and.returnValue(observableOf(sectionData));
spyOn(comp, 'initForm');
spyOn(comp, 'subscriptions');
comp.onSectionInit();
fixture.detectChanges();
expect(compAsAny.formConfig).toEqual(formConfigData.payload);
expect(comp.sectionData.errors).toEqual([]);
expect(comp.sectionData.data).toEqual(sectionData);
expect(comp.isLoading).toBeFalsy();
expect(comp.initForm).toHaveBeenCalledWith(sectionData);
expect(comp.subscriptions).toHaveBeenCalled();
});
it('should init form model properly', () => {
formBuilderService.modelFromConfiguration.and.returnValue(testFormModel);
const sectionData = {};
comp.initForm(sectionData);
expect(comp.formModel).toEqual(testFormModel);
});
it('should set a section Error when init form model fails', () => {
formBuilderService.modelFromConfiguration.and.throwError('test');
const sectionData = {};
const sectionError: SubmissionSectionError = {
message: 'test' + 'Error: test',
path: '/sections/' + sectionObject.id
};
comp.initForm(sectionData);
expect(comp.formModel).toBeUndefined();
expect(sectionsServiceStub.setSectionError).toHaveBeenCalledWith(submissionId, sectionObject.id, sectionError);
});
it('should return true when has Metadata Enrichment', () => {
const newSectionData = {
'dc.title': [new FormFieldMetadataValueObject('test')]
};
compAsAny.formData = {};
expect(comp.hasMetadataEnrichment(newSectionData)).toBeTruthy();
});
it('should return false when has not Metadata Enrichment', () => {
const newSectionData = {
'dc.title': [new FormFieldMetadataValueObject('test')]
};
compAsAny.formData = newSectionData;
expect(comp.hasMetadataEnrichment(newSectionData)).toBeFalsy();
});
it('should update form properly', () => {
spyOn(comp, 'initForm');
spyOn(comp, 'checksForErrors');
const sectionData: any = {
'dc.title': [new FormFieldMetadataValueObject('test')]
};
const sectionError = [];
comp.sectionData.data = {};
comp.sectionData.errors = [];
compAsAny.formData = {};
comp.updateForm(sectionData, sectionError);
expect(comp.isUpdating).toBeFalsy();
expect(comp.initForm).toHaveBeenCalled();
expect(comp.checksForErrors).toHaveBeenCalled();
expect(notificationsServiceStub.info).toHaveBeenCalled();
expect(comp.sectionData.data).toEqual(sectionData);
});
it('should update form error properly', () => {
spyOn(comp, 'initForm');
spyOn(comp, 'checksForErrors');
const sectionData: any = {};
comp.updateForm(sectionData, parsedSectionErrors);
expect(comp.initForm).not.toHaveBeenCalled();
expect(comp.checksForErrors).toHaveBeenCalled();
});
it('should check for error', () => {
comp.isUpdating = false;
comp.formId = 'test';
comp.sectionData.errors = [];
comp.checksForErrors(parsedSectionErrors);
expect(sectionsServiceStub.checkSectionErrors).toHaveBeenCalledWith(
submissionId,
sectionObject.id,
'test',
parsedSectionErrors,
[]
);
expect(comp.sectionData.errors).toEqual(parsedSectionErrors);
});
it('should subscribe to state properly', () => {
spyOn(comp, 'updateForm');
const formData = {
'dc.title': [new FormFieldMetadataValueObject('test')]
};
const sectionData: any = {
'dc.title': [new FormFieldMetadataValueObject('test')]
};
const sectionState = {
data: sectionData,
errors: parsedSectionErrors
};
formService.getFormData.and.returnValue(observableOf(formData));
sectionsServiceStub.getSectionState.and.returnValue(observableOf(sectionState));
comp.subscriptions();
expect(compAsAny.subs.length).toBe(2);
expect(compAsAny.formData).toEqual(formData);
expect(comp.updateForm).toHaveBeenCalledWith(sectionState.data, sectionState.errors);
});
it('should call dispatchOperationsFromEvent on form change', () => {
spyOn(comp, 'hasStoredValue').and.returnValue(false);
formOperationsService.getFieldPathSegmentedFromChangeEvent.and.returnValue('path');
formOperationsService.getFieldValueFromChangeEvent.and.returnValue('test');
comp.onChange(dynamicFormControlEvent);
expect(formOperationsService.dispatchOperationsFromEvent).toHaveBeenCalled();
expect(formOperationsService.getFieldPathSegmentedFromChangeEvent).toHaveBeenCalledWith(dynamicFormControlEvent);
expect(formOperationsService.getFieldValueFromChangeEvent).toHaveBeenCalledWith(dynamicFormControlEvent);
expect(submissionServiceStub.dispatchSave).not.toHaveBeenCalledWith(submissionId);
});
it('should call dispatchSave on form change when metadata is in submission autosave configuration', () => {
spyOn(comp, 'hasStoredValue').and.returnValue(false);
formOperationsService.getFieldPathSegmentedFromChangeEvent.and.returnValue('dc.title');
formOperationsService.getFieldValueFromChangeEvent.and.returnValue('test');
comp.onChange(dynamicFormControlEvent);
expect(formOperationsService.dispatchOperationsFromEvent).toHaveBeenCalled();
expect(formOperationsService.getFieldPathSegmentedFromChangeEvent).toHaveBeenCalledWith(dynamicFormControlEvent);
expect(formOperationsService.getFieldValueFromChangeEvent).toHaveBeenCalledWith(dynamicFormControlEvent);
expect(submissionServiceStub.dispatchSave).toHaveBeenCalledWith(submissionId);
});
it('should set previousValue on form focus event', () => {
formBuilderService.hasMappedGroupValue.and.returnValue(false);
formOperationsService.getFieldValueFromChangeEvent.and.returnValue('test');
comp.onFocus(dynamicFormControlEvent);
expect(compAsAny.previousValue.path).toEqual(['test', 'path']);
expect(compAsAny.previousValue.value).toBe('test');
formBuilderService.hasMappedGroupValue.and.returnValue(true);
formOperationsService.getQualdropValueMap.and.returnValue('qualdrop');
comp.onFocus(dynamicFormControlEvent);
expect(compAsAny.previousValue.path).toEqual(['test', 'path']);
expect(compAsAny.previousValue.value).toBe('qualdrop');
formBuilderService.hasMappedGroupValue.and.returnValue(false);
formOperationsService.getFieldValueFromChangeEvent.and.returnValue(new FormFieldMetadataValueObject('form value test'));
comp.onFocus(dynamicFormControlEvent);
expect(compAsAny.previousValue.path).toEqual(['test', 'path']);
expect(compAsAny.previousValue.value).toEqual(new FormFieldMetadataValueObject('form value test'));
});
it('should call dispatchOperationsFromEvent on form remove event', () => {
spyOn(comp, 'hasStoredValue').and.returnValue(false);
comp.onRemove(dynamicFormControlEvent);
expect(formOperationsService.dispatchOperationsFromEvent).toHaveBeenCalled();
});
it('should check if has stored value in the section state', () => {
comp.sectionData.data = {
'dc.title': [new FormFieldMetadataValueObject('test')]
} as any;
expect(comp.hasStoredValue('dc.title', 0)).toBeTruthy();
expect(comp.hasStoredValue('dc.title', 1)).toBeFalsy();
expect(comp.hasStoredValue('title', 0)).toBeFalsy();
});
});
});
// declare a test component
@Component({
selector: 'ds-test-cmp',
template: ``
})
class TestComponent {
}

View File

@@ -73,7 +73,7 @@ export class FormSectionComponent extends SectionModelComponent {
this.formConfigService.getConfigByHref(this.sectionData.config).pipe( this.formConfigService.getConfigByHref(this.sectionData.config).pipe(
map((configData: ConfigData) => configData.payload), map((configData: ConfigData) => configData.payload),
tap((config: SubmissionFormsModel) => this.formConfig = config), tap((config: SubmissionFormsModel) => this.formConfig = config),
flatMap((config: ConfigData) => this.sectionService.getSectionData(this.submissionId, this.sectionData.id)), flatMap(() => this.sectionService.getSectionData(this.submissionId, this.sectionData.id)),
take(1)) take(1))
.subscribe((sectionData: WorkspaceitemSectionDataType) => { .subscribe((sectionData: WorkspaceitemSectionDataType) => {
if (isUndefined(this.formModel)) { if (isUndefined(this.formModel)) {
@@ -86,25 +86,6 @@ export class FormSectionComponent extends SectionModelComponent {
this.cdr.detectChanges(); this.cdr.detectChanges();
} }
}) })
/* this.formConfigService.getConfigByHref(this.sectionData.config).pipe(
map((config: ConfigData) => config.payload))
.subscribe((config: SubmissionFormsModel) => {
this.formConfig = config;
this.sectionService.getSectionData(this.submissionId, this.sectionData.id).pipe(
take(1))
.subscribe((sectionData: WorkspaceitemSectionDataType) => {
if (isUndefined(this.formModel)) {
this.sectionData.errors = [];
// Is the first loading so init form
this.initForm(sectionData);
this.sectionData.data = sectionData;
this.subscriptions();
this.isLoading = false;
this.cdr.detectChanges();
}
})
});*/
} }
onSectionDestroy() { onSectionDestroy() {
@@ -248,8 +229,8 @@ export class FormSectionComponent extends SectionModelComponent {
} }
hasStoredValue(fieldId, index) { hasStoredValue(fieldId, index) {
if (isNotEmpty(this.sectionData.data) && isNotEmpty(this.sectionData.data[index])) { if (isNotEmpty(this.sectionData.data)) {
return this.sectionData.data.hasOwnProperty(fieldId); return this.sectionData.data.hasOwnProperty(fieldId) && isNotEmpty(this.sectionData.data[fieldId][index]);
} else { } else {
return false; return false;
} }

View File

@@ -27,6 +27,7 @@ import { FormAddError, FormClearErrorsAction, FormRemoveErrorAction } from '../.
import parseSectionErrors from '../utils/parseSectionErrors'; import parseSectionErrors from '../utils/parseSectionErrors';
import { SubmissionScopeType } from '../../core/submission/submission-scope-type'; import { SubmissionScopeType } from '../../core/submission/submission-scope-type';
import { SubmissionSectionError } from '../objects/submission-objects.reducer'; import { SubmissionSectionError } from '../objects/submission-objects.reducer';
import { getMockScrollToService } from '../../shared/mocks/mock-scroll-to-service';
describe('SectionsService test suite', () => { describe('SectionsService test suite', () => {
let notificationsServiceStub: NotificationsServiceStub; let notificationsServiceStub: NotificationsServiceStub;
@@ -47,12 +48,6 @@ describe('SectionsService test suite', () => {
select: jasmine.createSpy('select') select: jasmine.createSpy('select')
}); });
function getMockScrollToService(): ScrollToService {
return jasmine.createSpyObj('scrollToService', {
scrollTo: jasmine.createSpy('scrollTo')
});
}
beforeEach(async(() => { beforeEach(async(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [