/
resource-class-info.component.spec.ts
140 lines (113 loc) · 4.9 KB
/
resource-class-info.component.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { DragDropModule } from '@angular/cdk/drag-drop';
import { Component, DebugElement, OnInit, ViewChild } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { MatCardModule } from '@angular/material/card';
import { MatIconModule } from '@angular/material/icon';
import { MatMenuModule } from '@angular/material/menu';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import { MatTooltipModule } from '@angular/material/tooltip';
import { By } from '@angular/platform-browser';
import { ClassDefinition, Constants, MockOntology, ReadOntology } from '@dasch-swiss/dsp-js';
import { DspActionModule, DspApiConnectionToken, SortingService } from '@dasch-swiss/dsp-ui';
import { of } from 'rxjs';
import { CacheService } from 'src/app/main/cache/cache.service';
import { PropertyInfoComponent } from '../property-info/property-info.component';
import { ResourceClassInfoComponent } from './resource-class-info.component';
/**
* test host component to simulate parent component
* Property is of type simple text
*/
@Component({
template: '<app-resource-class-info #resClassInfo [resourceClass]="resourceClass"></app-resource-class-info>'
})
class HostComponent implements OnInit {
@ViewChild('resClassInfo') resourceClassInfoComponent: ResourceClassInfoComponent;
// get ontology from DSP-JS-Lib test data
ontology: ReadOntology;
resourceClass: ClassDefinition;
constructor(
private _cache: CacheService,
private _sortingService: SortingService
) {
}
ngOnInit() {
this._cache.get('currentOntology').subscribe(
(response: ReadOntology) => {
this.ontology = response;
const allOntoClasses = response.getAllClassDefinitions();
// reset the ontology classes
let classesToDisplay = [];
// display only the classes which are not a subClass of Standoff
allOntoClasses.forEach(resClass => {
if (resClass.subClassOf.length) {
const splittedSubClass = resClass.subClassOf[0].split('#');
if (!splittedSubClass[0].includes(Constants.StandoffOntology) && !splittedSubClass[1].includes('Standoff')) {
classesToDisplay.push(resClass);
}
}
});
classesToDisplay = this._sortingService.keySortByAlphabetical(classesToDisplay, 'label');
this.resourceClass = classesToDisplay[0];
}
);
}
}
describe('ResourceClassInfoComponent', () => {
let hostComponent: HostComponent;
let hostFixture: ComponentFixture<HostComponent>;
beforeEach(waitForAsync(() => {
const ontologyEndpointSpyObj = {
v2: {
onto: jasmine.createSpyObj('onto', ['getOntology', 'replaceGuiOrderOfCardinalities'])
}
};
const cacheServiceSpy = jasmine.createSpyObj('CacheService', ['get']);
TestBed.configureTestingModule({
declarations: [
HostComponent,
ResourceClassInfoComponent
],
imports: [
DspActionModule,
MatCardModule,
MatIconModule,
MatMenuModule,
MatTooltipModule
],
providers: [
{
provide: DspApiConnectionToken,
useValue: ontologyEndpointSpyObj
},
{
provide: CacheService,
useValue: cacheServiceSpy
}
]
})
.compileComponents();
}));
beforeEach(() => {
// mock cache service for currentOntology
const cacheSpy = TestBed.inject(CacheService);
(cacheSpy as jasmine.SpyObj<CacheService>).get.and.callFake(
() => {
const response: ReadOntology = MockOntology.mockReadOntology('http://0.0.0.0:3333/ontology/0001/anything/v2');
return of(response);
}
);
hostFixture = TestBed.createComponent(HostComponent);
hostComponent = hostFixture.componentInstance;
hostFixture.detectChanges();
expect(hostComponent).toBeTruthy();
});
it('expect title to be "Blue thing" and subclass of "Thing"', () => {
expect(hostComponent.resourceClassInfoComponent).toBeTruthy();
expect(hostComponent.resourceClassInfoComponent.resourceClass).toBeDefined();
const hostCompDe = hostFixture.debugElement;
const title: DebugElement = hostCompDe.query(By.css('mat-card-title'));
expect(title.nativeElement.innerText).toEqual('Blue thing');
const subtitle: DebugElement = hostCompDe.query(By.css('mat-card-subtitle'));
expect(subtitle.nativeElement.innerText).toEqual('Thing');
});
});