[CST-3088] added test for hierarchical vocabulary

This commit is contained in:
Giuseppe Digilio
2020-07-02 11:42:30 +02:00
parent 44381d7653
commit e8237f196f
3 changed files with 106 additions and 12 deletions

View File

@@ -64,7 +64,7 @@ export abstract class DsDynamicVocabularyComponent extends DynamicFormControlCom
value: initEntry.value, value: initEntry.value,
authority: initEntry.authority, authority: initEntry.authority,
display: initEntry.display, display: initEntry.display,
otherInformation: initEntry.otherInformation otherInformation: initEntry.otherInformation || null
}); });
} else { } else {
return this.model.value as any; return this.model.value as any;

View File

@@ -5,7 +5,7 @@ import { async, ComponentFixture, fakeAsync, inject, TestBed, tick, } from '@ang
import { By } from '@angular/platform-browser'; import { By } from '@angular/platform-browser';
import { of as observableOf } from 'rxjs'; import { of as observableOf } from 'rxjs';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; import { NgbModal, NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { DynamicFormLayoutService, DynamicFormsCoreModule, DynamicFormValidationService } from '@ng-dynamic-forms/core'; import { DynamicFormLayoutService, DynamicFormsCoreModule, DynamicFormValidationService } from '@ng-dynamic-forms/core';
import { DynamicFormsNGBootstrapUIModule } from '@ng-dynamic-forms/ui-ng-bootstrap'; import { DynamicFormsNGBootstrapUIModule } from '@ng-dynamic-forms/ui-ng-bootstrap';
import { TranslateModule } from '@ngx-translate/core'; import { TranslateModule } from '@ngx-translate/core';
@@ -21,6 +21,10 @@ import { AuthorityConfidenceStateDirective } from '../../../../../authority-conf
import { ObjNgFor } from '../../../../../utils/object-ngfor.pipe'; import { ObjNgFor } from '../../../../../utils/object-ngfor.pipe';
import { VocabularyEntry } from '../../../../../../core/submission/vocabularies/models/vocabulary-entry.model'; import { VocabularyEntry } from '../../../../../../core/submission/vocabularies/models/vocabulary-entry.model';
import { createSuccessfulRemoteDataObject$ } from '../../../../../remote-data.utils'; import { createSuccessfulRemoteDataObject$ } from '../../../../../remote-data.utils';
import { VocabularyTreeviewComponent } from '../../../../../vocabulary-treeview/vocabulary-treeview.component';
import { CdkTreeModule } from '@angular/cdk/tree';
import { TestScheduler } from 'rxjs/testing';
import { getTestScheduler } from 'jasmine-marbles';
export let TYPEAHEAD_TEST_GROUP; export let TYPEAHEAD_TEST_GROUP;
@@ -51,14 +55,16 @@ function init() {
}; };
} }
fdescribe('DsDynamicTypeaheadComponent test suite', () => { describe('DsDynamicTypeaheadComponent test suite', () => {
let scheduler: TestScheduler;
let testComp: TestComponent; let testComp: TestComponent;
let typeaheadComp: DsDynamicTypeaheadComponent; let typeaheadComp: DsDynamicTypeaheadComponent;
let testFixture: ComponentFixture<TestComponent>; let testFixture: ComponentFixture<TestComponent>;
let typeaheadFixture: ComponentFixture<DsDynamicTypeaheadComponent>; let typeaheadFixture: ComponentFixture<DsDynamicTypeaheadComponent>;
let service: any; let vocabularyServiceStub: any;
let html; let html;
let modal;
let vocabulary = { let vocabulary = {
id: 'vocabulary', id: 'vocabulary',
name: 'vocabulary', name: 'vocabulary',
@@ -95,7 +101,15 @@ fdescribe('DsDynamicTypeaheadComponent test suite', () => {
// async beforeEach // async beforeEach
beforeEach(async(() => { beforeEach(async(() => {
const vocabularyServiceStub = new VocabularyServiceStub(); vocabularyServiceStub = new VocabularyServiceStub();
modal = jasmine.createSpyObj('modal', ['open', 'close', 'dismiss']);
/* jasmine.createSpyObj('modal',
{
open: jasmine.createSpy('open'),
close: jasmine.createSpy('close'),
dismiss: jasmine.createSpy('dismiss'),
}
);*/
init(); init();
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [
@@ -104,20 +118,23 @@ fdescribe('DsDynamicTypeaheadComponent test suite', () => {
FormsModule, FormsModule,
NgbModule, NgbModule,
ReactiveFormsModule, ReactiveFormsModule,
TranslateModule.forRoot() TranslateModule.forRoot(),
CdkTreeModule
], ],
declarations: [ declarations: [
DsDynamicTypeaheadComponent, DsDynamicTypeaheadComponent,
TestComponent, TestComponent,
AuthorityConfidenceStateDirective, AuthorityConfidenceStateDirective,
ObjNgFor ObjNgFor,
VocabularyTreeviewComponent
], // declare the test component ], // declare the test component
providers: [ providers: [
ChangeDetectorRef, ChangeDetectorRef,
DsDynamicTypeaheadComponent, DsDynamicTypeaheadComponent,
{ provide: VocabularyService, useValue: vocabularyServiceStub }, { provide: VocabularyService, useValue: vocabularyServiceStub },
{ provide: DynamicFormLayoutService, useValue: {} }, { provide: DynamicFormLayoutService, useValue: {} },
{ provide: DynamicFormValidationService, useValue: {} } { provide: DynamicFormValidationService, useValue: {} },
{ provide: NgbModal, useValue: modal }
], ],
schemas: [CUSTOM_ELEMENTS_SCHEMA] schemas: [CUSTOM_ELEMENTS_SCHEMA]
}); });
@@ -135,6 +152,7 @@ fdescribe('DsDynamicTypeaheadComponent test suite', () => {
(change)="onValueChange($event)" (change)="onValueChange($event)"
(focus)="onFocus($event)"></ds-dynamic-typeahead>`; (focus)="onFocus($event)"></ds-dynamic-typeahead>`;
spyOn(vocabularyServiceStub, 'findVocabularyById').and.returnValue(createSuccessfulRemoteDataObject$(vocabulary));
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>; testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
testComp = testFixture.componentInstance; testComp = testFixture.componentInstance;
}); });
@@ -143,12 +161,15 @@ fdescribe('DsDynamicTypeaheadComponent test suite', () => {
testFixture.destroy(); testFixture.destroy();
}); });
it('should create DsDynamicTypeaheadComponent', inject([DsDynamicTypeaheadComponent], (app: DsDynamicTypeaheadComponent) => { it('should create DsDynamicTypeaheadComponent', inject([DsDynamicTypeaheadComponent], (app: DsDynamicTypeaheadComponent) => {
expect(app).toBeDefined(); expect(app).toBeDefined();
})); }));
}); });
describe('not hiearchical', () => { describe('Has not hierarchical vocabulary', () => {
beforeEach(() => {
spyOn(vocabularyServiceStub, 'findVocabularyById').and.returnValue(createSuccessfulRemoteDataObject$(vocabulary));
});
describe('when init model value is empty', () => { describe('when init model value is empty', () => {
beforeEach(() => { beforeEach(() => {
@@ -156,8 +177,6 @@ fdescribe('DsDynamicTypeaheadComponent test suite', () => {
typeaheadComp = typeaheadFixture.componentInstance; // FormComponent test instance typeaheadComp = typeaheadFixture.componentInstance; // FormComponent test instance
typeaheadComp.group = TYPEAHEAD_TEST_GROUP; typeaheadComp.group = TYPEAHEAD_TEST_GROUP;
typeaheadComp.model = new DynamicTypeaheadModel(TYPEAHEAD_TEST_MODEL_CONFIG); typeaheadComp.model = new DynamicTypeaheadModel(TYPEAHEAD_TEST_MODEL_CONFIG);
service = (typeaheadComp as any).vocabularyService;
spyOn(service, 'findVocabularyById').and.returnValue(createSuccessfulRemoteDataObject$(vocabulary));
typeaheadFixture.detectChanges(); typeaheadFixture.detectChanges();
}); });
@@ -341,6 +360,77 @@ fdescribe('DsDynamicTypeaheadComponent test suite', () => {
}); });
}); });
}); });
describe('Has hierarchical vocabulary', () => {
beforeEach(() => {
scheduler = getTestScheduler();
spyOn(vocabularyServiceStub, 'findVocabularyById').and.returnValue(createSuccessfulRemoteDataObject$(hierarchicalVocabulary));
});
describe('when init model value is empty', () => {
beforeEach(() => {
typeaheadFixture = TestBed.createComponent(DsDynamicTypeaheadComponent);
typeaheadComp = typeaheadFixture.componentInstance; // FormComponent test instance
typeaheadComp.group = TYPEAHEAD_TEST_GROUP;
typeaheadComp.model = new DynamicTypeaheadModel(TYPEAHEAD_TEST_MODEL_CONFIG);
typeaheadFixture.detectChanges();
});
afterEach(() => {
typeaheadFixture.destroy();
typeaheadComp = null;
});
it('should init component properly', () => {
expect(typeaheadComp.currentValue).not.toBeDefined();
});
it('should open tree properly', () => {
scheduler.schedule(() => typeaheadComp.openTree(new Event('click')));
scheduler.flush();
expect((typeaheadComp as any).modalService.open).toHaveBeenCalled();
});
});
describe('when init model value is not empty', () => {
beforeEach(() => {
typeaheadFixture = TestBed.createComponent(DsDynamicTypeaheadComponent);
typeaheadComp = typeaheadFixture.componentInstance; // FormComponent test instance
typeaheadComp.group = TYPEAHEAD_TEST_GROUP;
typeaheadComp.model = new DynamicTypeaheadModel(TYPEAHEAD_TEST_MODEL_CONFIG);
const entry = observableOf(Object.assign(new VocabularyEntry(), {
authority: null,
value: 'test',
display: 'testDisplay'
}));
spyOn((typeaheadComp as any).vocabularyService, 'getVocabularyEntryByValue').and.returnValue(entry);
spyOn((typeaheadComp as any).vocabularyService, 'getVocabularyEntryByID').and.returnValue(entry);
(typeaheadComp.model as any).value = new FormFieldMetadataValueObject('test', null, null, 'testDisplay');
typeaheadFixture.detectChanges();
});
afterEach(() => {
typeaheadFixture.destroy();
typeaheadComp = null;
});
it('should init component properly', fakeAsync(() => {
tick();
expect(typeaheadComp.currentValue).toEqual(new FormFieldMetadataValueObject('test', null, null, 'testDisplay'));
expect((typeaheadComp as any).vocabularyService.getVocabularyEntryByValue).toHaveBeenCalled();
}));
it('should open tree properly', () => {
scheduler.schedule(() => typeaheadComp.openTree(new Event('click')));
scheduler.flush();
expect((typeaheadComp as any).modalService.open).toHaveBeenCalled();
});
});
});
}); });
// declare a test component // declare a test component

View File

@@ -201,6 +201,10 @@ export class DsDynamicTypeaheadComponent extends DsDynamicVocabularyComponent im
this.dispatchUpdate(event.item); this.dispatchUpdate(event.item);
} }
/**
* Open modal to show tree for hierarchical vocabulary
* @param event The click event fired
*/
openTree(event) { openTree(event) {
event.preventDefault(); event.preventDefault();
event.stopImmediatePropagation(); event.stopImmediatePropagation();