/
search-resource.component.spec.ts
122 lines (90 loc) · 4.05 KB
/
search-resource.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
import { Component, Inject, Input, OnInit, ViewChild } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { FormBuilder, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MockOntology, ResourcePropertyDefinition } from '@dasch-swiss/dsp-js';
import { ComparisonOperatorAndValue, GreaterThan, LinkedResource, PropertyWithValue, ValueLiteral } from '../operator';
import { SearchResourceComponent } from './search-resource.component';
/**
* test host component to simulate parent component.
*/
@Component({
template: `
<app-search-resource #searchRes [formGroup]="form" [restrictResourceClass]="resClass"></app-search-resource>`
})
class TestHostComponent implements OnInit {
@ViewChild('searchRes', { static: false }) searchResource: SearchResourceComponent;
form;
resClass: string;
constructor(@Inject(FormBuilder) private _fb: FormBuilder) {
}
ngOnInit() {
this.form = this._fb.group({});
this.resClass = 'http://0.0.0.0:3333/ontology/0001/anything/v2#Thing';
}
}
/**
* test component to simulate ResourceAndPropertySelectionComponent.
*/
@Component({
selector: 'app-resource-and-property-selection',
template: ''
})
class TestResourceAndPropertySelectionComponent implements OnInit {
@Input() formGroup: FormGroup;
@Input() activeOntology: string;
@Input() resourceClassRestriction?: string;
@Input() topLevel;
// mock ref to child comp.
resourceClassComponent = {
selectedResourceClassIri: 'http://0.0.0.0:3333/ontology/0001/anything/v2#Thing'
};
// mock ref to child comp.
propertyComponents = [];
ngOnInit() {
const anythingOnto = MockOntology.mockReadOntology('http://0.0.0.0:3333/ontology/0001/anything/v2');
const linkedResValue = new ComparisonOperatorAndValue(new GreaterThan(), new ValueLiteral('0.5', 'http://www.w3.org/2001/XMLSchema#decimal'));
const hasDecimal = anythingOnto.properties['http://0.0.0.0:3333/ontology/0001/anything/v2#hasDecimal'];
const linkedResourceWithVal = new PropertyWithValue(hasDecimal as ResourcePropertyDefinition, linkedResValue, false);
this.propertyComponents = [{
getPropertySelectedWithValue: () => linkedResourceWithVal
}];
}
}
describe('SearchResourceComponent', () => {
let testHostComponent: TestHostComponent;
let testHostFixture: ComponentFixture<TestHostComponent>;
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [
BrowserAnimationsModule,
ReactiveFormsModule
],
declarations: [
SearchResourceComponent,
TestResourceAndPropertySelectionComponent,
TestHostComponent
]
})
.compileComponents();
}));
beforeEach(() => {
testHostFixture = TestBed.createComponent(TestHostComponent);
testHostComponent = testHostFixture.componentInstance;
testHostFixture.detectChanges();
});
it('should create', () => {
expect(testHostComponent).toBeTruthy();
expect(testHostComponent.searchResource).toBeTruthy();
});
it('should correctly determine the ontology from the resource class constraint', () => {
expect(testHostComponent.searchResource.restrictResourceClass).toEqual('http://0.0.0.0:3333/ontology/0001/anything/v2#Thing');
});
it('should return a specified resource', () => {
const linkedRes = testHostComponent.searchResource.getValue();
expect(linkedRes instanceof LinkedResource).toBeTrue();
expect((linkedRes as LinkedResource).resourceClass).toEqual('http://0.0.0.0:3333/ontology/0001/anything/v2#Thing');
expect((linkedRes as LinkedResource).properties.length).toEqual(1);
expect((linkedRes as LinkedResource).properties[0].property.id).toEqual('http://0.0.0.0:3333/ontology/0001/anything/v2#hasDecimal');
});
});