From 2fb448e5ae7d0a96d6e1edbd42717ea837371c20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Kilchenmann?= Date: Tue, 23 Feb 2021 06:47:40 +0100 Subject: [PATCH] fix(ontology): bug fix in list property (DSP-1368) (#390) * fix(ontology): bug fix in list property * test(ontology): new test for list property * refactor(ontology): delete console.log * test(ontology): reactivate all tests --- .../property-info.component.spec.ts | 137 ++++++++++++++++-- .../property-info/property-info.component.ts | 6 +- 2 files changed, 132 insertions(+), 11 deletions(-) diff --git a/src/app/project/ontology/property-info/property-info.component.spec.ts b/src/app/project/ontology/property-info/property-info.component.spec.ts index 950cc37619..203aabc96b 100644 --- a/src/app/project/ontology/property-info/property-info.component.spec.ts +++ b/src/app/project/ontology/property-info/property-info.component.spec.ts @@ -7,7 +7,7 @@ import { MatSnackBarModule } from '@angular/material/snack-bar'; import { MatTooltipModule } from '@angular/material/tooltip'; import { By } from '@angular/platform-browser'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; -import { Constants, IHasProperty, MockOntology, ReadOntology, ResourcePropertyDefinitionWithAllLanguages } from '@dasch-swiss/dsp-js'; +import { Constants, IHasProperty, ListNodeInfo, MockOntology, ReadOntology, ResourcePropertyDefinitionWithAllLanguages } from '@dasch-swiss/dsp-js'; import { of } from 'rxjs'; import { CacheService } from 'src/app/main/cache/cache.service'; import { PropertyInfoComponent } from './property-info.component'; @@ -98,6 +98,52 @@ class LinkHostComponent { } +/** + * Test host component to simulate parent component + * Property is of type list dropdown + */ +@Component({ + template: '' +}) +class ListHostComponent { + + @ViewChild('propertyInfo') propertyInfoComponent: PropertyInfoComponent; + + propertyCardinality: IHasProperty = { + "propertyIndex": "http://0.0.0.0:3333/ontology/0001/anything/v2#hasListItem", + "cardinality": 2, + "guiOrder": 0, + "isInherited": true + }; + propertyDefinition: ResourcePropertyDefinitionWithAllLanguages = { + "id": "http://0.0.0.0:3333/ontology/0001/anything/v2#hasListItem", + "subPropertyOf": ["http://api.knora.org/ontology/knora-api/v2#hasValue"], + "label": "Listenelement", + "guiElement": "http://api.knora.org/ontology/salsah-gui/v2#List", + "subjectType": "http://0.0.0.0:3333/ontology/0001/anything/v2#Thing", + "objectType": "http://api.knora.org/ontology/knora-api/v2#ListValue", + "isLinkProperty": false, + "isLinkValueProperty": false, + "isEditable": true, + "guiAttributes": ["hlist="], + "comments": [], + "labels": [{ + "language": "de", + "value": "Listenelement" + }, { + "language": "en", + "value": "List element" + }, { + "language": "fr", + "value": "Elément de liste" + }, { + "language": "it", + "value": "Elemento di lista" + }] + }; + +} + describe('PropertyInfoComponent', () => { let simpleTextHostComponent: SimpleTextHostComponent; let simpleTextHostFixture: ComponentFixture; @@ -105,6 +151,9 @@ describe('PropertyInfoComponent', () => { let linkHostComponent: LinkHostComponent; let linkHostFixture: ComponentFixture; + let listHostComponent: ListHostComponent; + let listHostFixture: ComponentFixture; + beforeEach(async(() => { const dspConnSpy = { v2: { @@ -117,6 +166,7 @@ describe('PropertyInfoComponent', () => { TestBed.configureTestingModule({ declarations: [ LinkHostComponent, + ListHostComponent, SimpleTextHostComponent, PropertyInfoComponent ], @@ -138,6 +188,14 @@ describe('PropertyInfoComponent', () => { .compileComponents(); })); + beforeEach(() => { + simpleTextHostFixture = TestBed.createComponent(SimpleTextHostComponent); + simpleTextHostComponent = simpleTextHostFixture.componentInstance; + simpleTextHostFixture.detectChanges(); + + expect(simpleTextHostComponent).toBeTruthy(); + }); + beforeEach(() => { // mock cache service for currentOntology const cacheSpy = TestBed.inject(CacheService); @@ -149,14 +207,6 @@ describe('PropertyInfoComponent', () => { } ); - simpleTextHostFixture = TestBed.createComponent(SimpleTextHostComponent); - simpleTextHostComponent = simpleTextHostFixture.componentInstance; - simpleTextHostFixture.detectChanges(); - - expect(simpleTextHostComponent).toBeTruthy(); - }); - - beforeEach(() => { linkHostFixture = TestBed.createComponent(LinkHostComponent); linkHostComponent = linkHostFixture.componentInstance; linkHostFixture.detectChanges(); @@ -164,6 +214,64 @@ describe('PropertyInfoComponent', () => { expect(linkHostComponent).toBeTruthy(); }); + beforeEach(() => { + // mock cache service for currentOntology + const cacheSpy = TestBed.inject(CacheService); + + (cacheSpy as jasmine.SpyObj).get.and.callFake( + (key = 'currentOntologyLists') => { + let response: ListNodeInfo[] = [{ + "comments": [], + "id": "http://rdfh.ch/lists/0001/otherTreeList", + "isRootNode": true, + "labels": [{ + "language": "en", + "value": "Tree list root" + }], + "projectIri": "http://rdfh.ch/projects/0001" + }, { + "comments": [{ + "language": "en", + "value": "a list that is not in used in ontology or data" + }], + "id": "http://rdfh.ch/lists/0001/notUsedList", + "isRootNode": true, + "labels": [{ + "language": "de", + "value": "unbenutzte Liste" + }, { + "language": "en", + "value": "a list that is not used" + }], + "name": "notUsedList", + "projectIri": "http://rdfh.ch/projects/0001" + }, { + "comments": [{ + "language": "en", + "value": "Anything Tree List" + }], + "id": "http://rdfh.ch/lists/0001/treeList", + "isRootNode": true, + "labels": [{ + "language": "de", + "value": "Listenwurzel" + }, { + "language": "en", + "value": "Tree list root" + }], + "name": "treelistroot", + "projectIri": "http://rdfh.ch/projects/0001" + }]; + return of(response); + } + ); + listHostFixture = TestBed.createComponent(ListHostComponent); + listHostComponent = listHostFixture.componentInstance; + listHostFixture.detectChanges(); + + expect(listHostComponent).toBeTruthy(); + }); + it('should create an instance', () => { expect(simpleTextHostComponent.propertyInfoComponent).toBeTruthy(); }); @@ -237,4 +345,15 @@ describe('PropertyInfoComponent', () => { // and cardinality 2 means also "not required value" expect(requiredIcon.nativeElement.innerText).toEqual('check_box_outline_blank'); }); + + it('expect list property with connection to list "Listenwurzel"', () => { + expect(listHostComponent.propertyInfoComponent).toBeTruthy(); + expect(listHostComponent.propertyInfoComponent.propDef).toBeDefined(); + + const hostCompDe = listHostFixture.debugElement; + + const attribute: DebugElement = hostCompDe.query(By.css('.attribute')); + // expect list called "Listenwurzel" + expect(attribute.nativeElement.innerText).toContain('Listenwurzel'); + }); }); diff --git a/src/app/project/ontology/property-info/property-info.component.ts b/src/app/project/ontology/property-info/property-info.component.ts index 0fc815961e..b5bd26191c 100644 --- a/src/app/project/ontology/property-info/property-info.component.ts +++ b/src/app/project/ontology/property-info/property-info.component.ts @@ -5,6 +5,7 @@ import { ApiResponseData, Constants, IHasProperty, + ListNodeInfo, ListsResponse, ReadOntology, ResourcePropertyDefinitionWithAllLanguages @@ -119,11 +120,12 @@ export class PropertyInfoComponent implements OnInit, AfterContentInit { // this property is a list property // get current ontology lists to get linked list information this._cache.get('currentOntologyLists').subscribe( - (response: ApiResponseData) => { + (response: ListNodeInfo[]) => { const re: RegExp = /\<([^)]+)\>/; const listIri = this.propDef.guiAttributes[0].match(re)[1]; const listUrl = `/project/${this.projectcode}/lists/${encodeURIComponent(listIri)}`; - this.propAttribute = `${response[0].labels[0].value}`; + const list = response.find(i => i.id === listIri); + this.propAttribute = `${list.labels[0].value}`; } ); }