[DURACOM-191] fix: section-license unit-tests

This commit is contained in:
Vlad Nouski
2023-12-04 18:31:23 +01:00
parent 79389009dc
commit 9be8254fbd
2 changed files with 34 additions and 39 deletions

View File

@@ -41,8 +41,6 @@ import { provideMockStore } from '@ngrx/store/testing';
import { DsDynamicTypeBindRelationService } from 'src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-type-bind-relation.service';
import { APP_CONFIG } from 'src/config/app-config.interface';
import { environment } from 'src/environments/environment.test';
import { getMockFormBuilderService } from 'src/app/shared/mocks/form-builder-service.mock';
import { SECTION_LICENSE_FORM_MODEL } from './section-license.model';
function getMockDsDynamicTypeBindRelationService(): DsDynamicTypeBindRelationService {
return jasmine.createSpyObj('DsDynamicTypeBindRelationService', {
@@ -75,17 +73,6 @@ function getMockSubmissionFormsConfigService(): SubmissionFormsConfigDataService
});
}
const formBuilderServiceStub = {
findById: jasmine.createSpy('findById'),
fromJSON: jasmine.createSpy('fromJSON'),
createFormGroup: () => {
return {
patchValue: () => { },
reset(_value?: any, _options?: { onlySelf?: boolean; emitEvent?: boolean; }): void { },
};
}
};
const sectionObject: SectionDataObject = {
config: 'https://dspace7.4science.it/or2018/api/config/submissionforms/license',
mandatory: true,
@@ -110,7 +97,7 @@ const dynamicFormControlEvent: DynamicFormControlEvent = {
type: DynamicFormControlEventType.Change
};
describe('SubmissionSectionLicenseComponent test suite', () => {
fdescribe('SubmissionSectionLicenseComponent test suite', () => {
let comp: SubmissionSectionLicenseComponent;
let compAsAny: any;
@@ -134,9 +121,17 @@ describe('SubmissionSectionLicenseComponent test suite', () => {
findById: jasmine.createSpy('findById'),
findByHref: jasmine.createSpy('findByHref')
});
const initialState: any = {
core: {
'cache/object': {},
'cache/syncbuffer': {},
'cache/object-updates': {},
'data/request': {},
'index': {},
}
};
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
void TestBed.configureTestingModule({
imports: [
CommonModule,
FormsModule,
@@ -159,9 +154,9 @@ describe('SubmissionSectionLicenseComponent test suite', () => {
{ provide: 'sectionDataProvider', useValue: Object.assign({}, sectionObject) },
{ provide: 'submissionIdProvider', useValue: submissionId },
ChangeDetectorRef,
provideMockStore({}),
{ provide: FormBuilderService, useValue: getMockFormBuilderService() },
{ provide:DsDynamicTypeBindRelationService, useValue: getMockDsDynamicTypeBindRelationService() },
provideMockStore({initialState}),
FormBuilderService,
{ provide: DsDynamicTypeBindRelationService, useValue: getMockDsDynamicTypeBindRelationService() },
{ provide: APP_CONFIG, useValue: environment },
SubmissionSectionLicenseComponent
],
@@ -222,16 +217,11 @@ describe('SubmissionSectionLicenseComponent test suite', () => {
mockCollectionDataService.findById.and.returnValue(createSuccessfulRemoteDataObject$(mockCollection));
sectionsServiceStub.getSectionErrors.and.returnValue(observableOf([]));
sectionsServiceStub.isSectionReadOnly.and.returnValue(observableOf(false));
(formBuilderService.findById as jasmine.Spy).and.returnValue(new DynamicCheckboxModel({ id: 'granted'}));
(formBuilderService.fromJSON as jasmine.Spy).and.returnValue(SECTION_LICENSE_FORM_MODEL);
// formBuilderServiceStub.findById.and.returnValue(new DynamicCheckboxModel({ id: 'granted'}));
// formBuilderServiceStub.fromJSON.and.returnValue(SECTION_LICENSE_FORM_MODEL);
comp.onSectionInit();
fixture.detectChanges();
spyOn(formBuilderService, 'findById').and.returnValue(new DynamicCheckboxModel({ id: 'granted' }));
});
it('should init section properly', () => {
fixture.detectChanges();
spyOn(compAsAny, 'getSectionStatus');
const model = formBuilderService.findById('granted', comp.formModel);
@@ -251,11 +241,8 @@ describe('SubmissionSectionLicenseComponent test suite', () => {
acceptanceDate: Date.now(),
granted: true
} as any;
spyOn(compAsAny, 'getSectionStatus');
fixture.detectChanges();
const model = formBuilderService.findById('granted', comp.formModel);
expect(compAsAny.subs.length).toBe(2);
expect(comp.formModel).toBeDefined();
expect(model.value).toBeTruthy();
@@ -265,19 +252,21 @@ describe('SubmissionSectionLicenseComponent test suite', () => {
}));
});
fit('should have status true when checkbox is selected', () => {
const model = formBuilderService.findById('granted', comp.formModel);
it('should have status true when checkbox is selected', (done) => {
fixture.detectChanges();
const model = formBuilderService.findById('granted', comp.formModel);
(model as DynamicCheckboxModel).value = true;
compAsAny.getSectionStatus().subscribe((status) => {
expect(status).toBeTruthy();
done();
});
});
it('should have status false when checkbox is not selected', () => {
fixture.detectChanges();
const model = formBuilderService.findById('granted', comp.formModel);
compAsAny.getSectionStatus().subscribe((status) => {
expect(status).toBeFalsy();
});
@@ -295,7 +284,7 @@ describe('SubmissionSectionLicenseComponent test suite', () => {
});
it('should set section errors properly', () => {
comp.onSectionInit();
fixture.detectChanges();
const expectedErrors = mockLicenseParsedErrors.license;
expect(sectionsServiceStub.checkSectionErrors).toHaveBeenCalled();
@@ -310,7 +299,7 @@ describe('SubmissionSectionLicenseComponent test suite', () => {
granted: true
} as any;
comp.onSectionInit();
fixture.detectChanges();
expect(sectionsServiceStub.dispatchRemoveSectionErrors).toHaveBeenCalled();
@@ -352,8 +341,10 @@ describe('SubmissionSectionLicenseComponent test suite', () => {
template: ``,
standalone: true,
imports: [
SubmissionSectionLicenseComponent,
CommonModule,
FormsModule,
FormComponent,
ReactiveFormsModule
]
})

View File

@@ -1,4 +1,4 @@
import { ChangeDetectorRef, Component, Inject, ViewChild } from '@angular/core';
import { AfterViewChecked, ChangeDetectorRef, Component, Inject, ViewChild } from '@angular/core';
import {
DynamicCheckboxModel,
DynamicFormControlEvent,
@@ -39,15 +39,16 @@ import { AsyncPipe, NgIf } from '@angular/common';
selector: 'ds-submission-section-license',
styleUrls: ['./section-license.component.scss'],
templateUrl: './section-license.component.html',
providers: [],
imports: [
FormComponent,
NgIf,
AsyncPipe
AsyncPipe,
],
standalone: true
})
@renderSectionFor(SectionsType.License)
export class SubmissionSectionLicenseComponent extends SectionModelComponent {
export class SubmissionSectionLicenseComponent extends SectionModelComponent implements AfterViewChecked {
/**
* The form id
@@ -190,11 +191,14 @@ export class SubmissionSectionLicenseComponent extends SectionModelComponent {
// Remove any section's errors
this.sectionService.dispatchRemoveSectionErrors(this.submissionId, this.sectionData.id);
}
this.changeDetectorRef.detectChanges();
})
);
}
ngAfterViewChecked(): void {
this.changeDetectorRef.detectChanges();
}
/**
* Get section status
*