From e8237f196f796e365eacb9a6b3945ca0cbcc3a14 Mon Sep 17 00:00:00 2001 From: Giuseppe Digilio Date: Thu, 2 Jul 2020 11:42:30 +0200 Subject: [PATCH] [CST-3088] added test for hierarchical vocabulary --- .../models/dynamic-vocabulary.component.ts | 2 +- .../dynamic-typeahead.component.spec.ts | 112 ++++++++++++++++-- .../typeahead/dynamic-typeahead.component.ts | 4 + 3 files changed, 106 insertions(+), 12 deletions(-) diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/dynamic-vocabulary.component.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/dynamic-vocabulary.component.ts index 14ae2128b5..5e6acf8581 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/models/dynamic-vocabulary.component.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/dynamic-vocabulary.component.ts @@ -64,7 +64,7 @@ export abstract class DsDynamicVocabularyComponent extends DynamicFormControlCom value: initEntry.value, authority: initEntry.authority, display: initEntry.display, - otherInformation: initEntry.otherInformation + otherInformation: initEntry.otherInformation || null }); } else { return this.model.value as any; diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/typeahead/dynamic-typeahead.component.spec.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/typeahead/dynamic-typeahead.component.spec.ts index 72fc7fcba0..d837af5402 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/models/typeahead/dynamic-typeahead.component.spec.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/typeahead/dynamic-typeahead.component.spec.ts @@ -5,7 +5,7 @@ import { async, ComponentFixture, fakeAsync, inject, TestBed, tick, } from '@ang import { By } from '@angular/platform-browser'; 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 { DynamicFormsNGBootstrapUIModule } from '@ng-dynamic-forms/ui-ng-bootstrap'; import { TranslateModule } from '@ngx-translate/core'; @@ -21,6 +21,10 @@ import { AuthorityConfidenceStateDirective } from '../../../../../authority-conf import { ObjNgFor } from '../../../../../utils/object-ngfor.pipe'; import { VocabularyEntry } from '../../../../../../core/submission/vocabularies/models/vocabulary-entry.model'; 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; @@ -51,14 +55,16 @@ function init() { }; } -fdescribe('DsDynamicTypeaheadComponent test suite', () => { +describe('DsDynamicTypeaheadComponent test suite', () => { + let scheduler: TestScheduler; let testComp: TestComponent; let typeaheadComp: DsDynamicTypeaheadComponent; let testFixture: ComponentFixture; let typeaheadFixture: ComponentFixture; - let service: any; + let vocabularyServiceStub: any; let html; + let modal; let vocabulary = { id: 'vocabulary', name: 'vocabulary', @@ -95,7 +101,15 @@ fdescribe('DsDynamicTypeaheadComponent test suite', () => { // async beforeEach 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(); TestBed.configureTestingModule({ imports: [ @@ -104,20 +118,23 @@ fdescribe('DsDynamicTypeaheadComponent test suite', () => { FormsModule, NgbModule, ReactiveFormsModule, - TranslateModule.forRoot() + TranslateModule.forRoot(), + CdkTreeModule ], declarations: [ DsDynamicTypeaheadComponent, TestComponent, AuthorityConfidenceStateDirective, - ObjNgFor + ObjNgFor, + VocabularyTreeviewComponent ], // declare the test component providers: [ ChangeDetectorRef, DsDynamicTypeaheadComponent, { provide: VocabularyService, useValue: vocabularyServiceStub }, { provide: DynamicFormLayoutService, useValue: {} }, - { provide: DynamicFormValidationService, useValue: {} } + { provide: DynamicFormValidationService, useValue: {} }, + { provide: NgbModal, useValue: modal } ], schemas: [CUSTOM_ELEMENTS_SCHEMA] }); @@ -135,6 +152,7 @@ fdescribe('DsDynamicTypeaheadComponent test suite', () => { (change)="onValueChange($event)" (focus)="onFocus($event)">`; + spyOn(vocabularyServiceStub, 'findVocabularyById').and.returnValue(createSuccessfulRemoteDataObject$(vocabulary)); testFixture = createTestComponent(html, TestComponent) as ComponentFixture; testComp = testFixture.componentInstance; }); @@ -143,12 +161,15 @@ fdescribe('DsDynamicTypeaheadComponent test suite', () => { testFixture.destroy(); }); it('should create DsDynamicTypeaheadComponent', inject([DsDynamicTypeaheadComponent], (app: DsDynamicTypeaheadComponent) => { - 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', () => { beforeEach(() => { @@ -156,8 +177,6 @@ fdescribe('DsDynamicTypeaheadComponent test suite', () => { typeaheadComp = typeaheadFixture.componentInstance; // FormComponent test instance typeaheadComp.group = TYPEAHEAD_TEST_GROUP; typeaheadComp.model = new DynamicTypeaheadModel(TYPEAHEAD_TEST_MODEL_CONFIG); - service = (typeaheadComp as any).vocabularyService; - spyOn(service, 'findVocabularyById').and.returnValue(createSuccessfulRemoteDataObject$(vocabulary)); 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 diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/typeahead/dynamic-typeahead.component.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/typeahead/dynamic-typeahead.component.ts index 93e5b228e7..5d7413e4dd 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/models/typeahead/dynamic-typeahead.component.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/typeahead/dynamic-typeahead.component.ts @@ -201,6 +201,10 @@ export class DsDynamicTypeaheadComponent extends DsDynamicVocabularyComponent im this.dispatchUpdate(event.item); } + /** + * Open modal to show tree for hierarchical vocabulary + * @param event The click event fired + */ openTree(event) { event.preventDefault(); event.stopImmediatePropagation();