/
select-properties.component.spec.ts
165 lines (123 loc) · 6.3 KB
/
select-properties.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { Component, CUSTOM_ELEMENTS_SCHEMA, OnInit, ViewChild } from '@angular/core';
import { waitForAsync, ComponentFixture, TestBed } from '@angular/core/testing';
import { FormBuilder, FormGroup } from '@angular/forms';
import { MatTooltipModule } from '@angular/material/tooltip';
import { By } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import {
MockOntology,
ResourceClassAndPropertyDefinitions,
ResourceClassDefinition,
ResourcePropertyDefinition
} from '@dasch-swiss/dsp-js';
import { SelectPropertiesComponent } from './select-properties.component';
import { SwitchPropertiesComponent } from './switch-properties/switch-properties.component';
/**
* test host component to simulate parent component.
*/
@Component({
template: `
<app-select-properties
#selectProps
[ontologyInfo]="ontoInfo"
[selectedResourceClass]="selectedResourceClass"
[properties]="properties"
[parentForm]="propertiesParentForm"
[currentOntoIri]="currentOntoIri">
</app-select-properties>`
})
class TestSelectPropertiesParentComponent implements OnInit {
@ViewChild('selectProps') selectPropertiesComponent: SelectPropertiesComponent;
ontoInfo: ResourceClassAndPropertyDefinitions;
properties: ResourcePropertyDefinition[];
selectedResourceClass: ResourceClassDefinition;
propertiesParentForm: FormGroup;
currentOntoIri: string;
constructor(private _fb: FormBuilder) { }
ngOnInit() {
this.propertiesParentForm = this._fb.group({});
this.ontoInfo = MockOntology.mockIResourceClassAndPropertyDefinitions('http://0.0.0.0:3333/ontology/0001/anything/v2#Thing');
this.selectedResourceClass = this.ontoInfo.classes['http://0.0.0.0:3333/ontology/0001/anything/v2#Thing'];
this.properties = this.ontoInfo.getPropertyDefinitionsByType(ResourcePropertyDefinition).filter(prop => prop.isEditable && !prop.isLinkProperty);
this.currentOntoIri = 'http://0.0.0.0:3333/ontology/0001/anything/v2';
}
}
describe('SelectPropertiesComponent', () => {
let testHostComponent: TestSelectPropertiesParentComponent;
let testHostFixture: ComponentFixture<TestSelectPropertiesParentComponent>;
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [
SelectPropertiesComponent,
SwitchPropertiesComponent,
TestSelectPropertiesParentComponent
],
imports: [
BrowserAnimationsModule,
MatTooltipModule
],
providers: [
FormBuilder
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
.compileComponents();
}));
beforeEach(() => {
testHostFixture = TestBed.createComponent(TestSelectPropertiesParentComponent);
testHostComponent = testHostFixture.componentInstance;
testHostFixture.detectChanges();
expect(testHostComponent).toBeTruthy();
});
it('should create a value component for each property', () => {
expect(testHostComponent.selectPropertiesComponent.switchPropertiesComponent.length).toEqual(18);
});
it('should create a key value pair object', () => {
const propsArray = [];
const keyValuePair = testHostComponent.selectPropertiesComponent.propertyValuesKeyValuePair;
for (const propIri in keyValuePair) {
if (keyValuePair.hasOwnProperty(propIri)) {
propsArray.push(keyValuePair[propIri]);
}
}
// each property has three entries in the keyValuePair object
expect(propsArray.length).toEqual(18 * 3);
});
describe('Add/Delete functionality', () => {
let hostComponentDe;
let selectPropertiesComponentDe;
beforeEach(() => {
testHostFixture = TestBed.createComponent(TestSelectPropertiesParentComponent);
testHostComponent = testHostFixture.componentInstance;
hostComponentDe = testHostFixture.debugElement;
selectPropertiesComponentDe = hostComponentDe.query(By.directive(SelectPropertiesComponent));
testHostFixture.detectChanges();
expect(testHostComponent).toBeTruthy();
});
it('should add a new form to the value when the add button is clicked', () => {
const addButtons = selectPropertiesComponentDe.queryAll(By.css('.create'));
const addButtonNativeElement = addButtons[0].nativeElement;
// keep in mind that this element may not correspond to the first property as it simply grabs the first occurance of a plus button.
// if the first property does not have a plus button due to it's cardinality, addButtonNativeElement will be the button for
// the first property with a cardinality that allows for a plus button.
expect(addButtonNativeElement).toBeDefined();
addButtonNativeElement.click();
// if this fails, that likely means the property has a cardinality of 0-1 and thus could not add another value.
expect(testHostComponent.selectPropertiesComponent.propertyValuesKeyValuePair[testHostComponent.properties[1].id].length).toEqual(2);
});
it('should delete a form from the value when the delete button is clicked', () => {
testHostComponent.selectPropertiesComponent.propertyValuesKeyValuePair[testHostComponent.properties[1].id] = [0, 1];
testHostComponent.selectPropertiesComponent.propertyValuesKeyValuePair[testHostComponent.properties[1].id + '-filtered'] = [0, 1];
testHostFixture.detectChanges();
let deleteButtons = selectPropertiesComponentDe.queryAll(By.css('.delete'));
const deleteButtonNativeElement = deleteButtons[0].nativeElement;
expect(deleteButtonNativeElement).toBeDefined();
deleteButtonNativeElement.click();
testHostFixture.detectChanges();
const expectedArray = [undefined, 1];
expect(testHostComponent.selectPropertiesComponent.propertyValuesKeyValuePair[testHostComponent.properties[1].id]).toEqual(expectedArray);
deleteButtons = selectPropertiesComponentDe.queryAll(By.css('.delete'));
expect(deleteButtons).toEqual([]);
});
});
});