/
search-select-property.component.spec.ts
326 lines (253 loc) · 10.7 KB
/
search-select-property.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import { waitForAsync, ComponentFixture, TestBed } from '@angular/core/testing';
import { Component, Inject, Input, OnInit, ViewChild } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import {
UntypedFormBuilder,
UntypedFormGroup,
ReactiveFormsModule,
} from '@angular/forms';
import { MatLegacySelectModule as MatSelectModule } from '@angular/material/legacy-select';
import { MatLegacyOptionModule as MatOptionModule } from '@angular/material/legacy-core';
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { HarnessLoader } from '@angular/cdk/testing';
import {
Cardinality,
Constants,
MockOntology,
ResourceClassDefinition,
ResourcePropertyDefinition,
} from '@dasch-swiss/dsp-js';
import { MatLegacySelectHarness as MatSelectHarness } from '@angular/material/legacy-select/testing';
import { MatLegacySlideToggleHarness as MatSlideToggleHarness } from '@angular/material/legacy-slide-toggle/testing';
import { MatCheckboxModule } from '@angular/material/checkbox';
import {
ComparisonOperatorAndValue,
Equals,
ValueLiteral,
} from './specify-property-value/operator';
import { SearchSelectPropertyComponent } from './search-select-property.component';
import { MatLegacySlideToggleModule as MatSlideToggleModule } from '@angular/material/legacy-slide-toggle';
/**
* test host component to simulate parent component.
*/
@Component({
template: ` <app-search-select-property
#selectProp
[formGroup]="form"
[index]="0"
[activeResourceClass]="activeResourceClass"
[properties]="propertyDefs"
[topLevel]="topLevel"
></app-search-select-property>`,
})
class TestHostComponent implements OnInit {
@ViewChild('selectProp') selectProperty: SearchSelectPropertyComponent;
form: UntypedFormGroup;
propertyDefs: ResourcePropertyDefinition[];
activeResourceClass: ResourceClassDefinition;
topLevel: boolean;
constructor(@Inject(UntypedFormBuilder) private _fb: UntypedFormBuilder) {}
ngOnInit() {
this.form = this._fb.group({});
const resProps = MockOntology.mockReadOntology(
'http://0.0.0.0:3333/ontology/0001/anything/v2'
).getPropertyDefinitionsByType(ResourcePropertyDefinition);
this.propertyDefs = resProps;
this.topLevel = true;
}
}
/**
* test component to simulate specify property value component.
*/
@Component({
selector: 'app-specify-property-value',
template: '',
})
class TestSpecifyPropertyValueComponent implements OnInit {
@Input() formGroup: UntypedFormGroup;
@Input() property: ResourcePropertyDefinition;
@Input() topLevel: boolean;
getComparisonOperatorAndValueLiteralForProperty(): ComparisonOperatorAndValue {
return new ComparisonOperatorAndValue(
new Equals(),
new ValueLiteral('1', 'http://www.w3.org/2001/XMLSchema#integer')
);
}
ngOnInit() {}
}
describe('SearchSelectPropertyComponent', () => {
let testHostComponent: TestHostComponent;
let testHostFixture: ComponentFixture<TestHostComponent>;
let loader: HarnessLoader;
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [
BrowserAnimationsModule,
MatCheckboxModule,
MatOptionModule,
MatSelectModule,
MatSlideToggleModule,
ReactiveFormsModule,
],
declarations: [
SearchSelectPropertyComponent,
TestHostComponent,
TestSpecifyPropertyValueComponent,
],
}).compileComponents();
}));
beforeEach(() => {
testHostFixture = TestBed.createComponent(TestHostComponent);
testHostComponent = testHostFixture.componentInstance;
loader = TestbedHarnessEnvironment.loader(testHostFixture);
testHostFixture.detectChanges();
});
it('should create', () => {
expect(testHostComponent).toBeTruthy();
expect(testHostComponent.selectProperty).toBeTruthy();
});
it('should initialise the Inputs correctly', () => {
expect(testHostComponent.selectProperty.formGroup).toBeDefined();
expect(testHostComponent.selectProperty.index).toEqual(0);
expect(
testHostComponent.selectProperty.activeResourceClass
).toBeUndefined();
expect(
Object.keys(testHostComponent.selectProperty.properties).length
).toEqual(24);
expect(testHostComponent.selectProperty.properties.length).toEqual(24);
});
it('should add a new control to the parent form', waitForAsync(() => {
// the control is added to the form as an async operation
// https://angular.io/guide/testing#async-test-with-async
testHostFixture.whenStable().then(() => {
expect(testHostComponent.form.contains('property0')).toBe(true);
});
}));
it('should init the MatSelect and MatOptions correctly', async () => {
const select = await loader.getHarness(MatSelectHarness);
const initVal = await select.getValueText();
// placeholder
expect(initVal).toEqual('Select Properties');
await select.open();
const options = await select.getOptions();
expect(options.length).toEqual(24);
});
it('should set the active property', async () => {
expect(
testHostComponent.selectProperty.propertySelected
).toBeUndefined();
const select = await loader.getHarness(MatSelectHarness);
await select.open();
const options = await select.getOptions();
await options[0].click();
expect(testHostComponent.selectProperty.propertySelected.id).toEqual(
'http://0.0.0.0:3333/ontology/0001/anything/v2#hasBlueThing'
);
expect(
testHostComponent.selectProperty.specifyPropertyValue.property
).toBeDefined();
expect(
testHostComponent.selectProperty.specifyPropertyValue.property.id
).toEqual('http://0.0.0.0:3333/ontology/0001/anything/v2#hasBlueThing');
});
it('should not show the sort slide-toggle when no property is selected', async () => {
const slideToggle = await loader.getAllHarnesses(MatSlideToggleHarness);
expect(slideToggle.length).toEqual(0);
});
it('should show the sort slide-toggle when a property with cardinality 1 is selected for the top level resource', async () => {
const select = await loader.getHarness(MatSelectHarness);
await select.open();
const options = await select.getOptions();
expect(await options[4].getText()).toEqual('Date');
await options[4].click();
const resClass = new ResourceClassDefinition();
resClass.propertiesList = [
{
propertyIndex:
'http://0.0.0.0:3333/ontology/0001/anything/v2#hasDate',
cardinality: Cardinality._1,
isInherited: true,
},
];
testHostComponent.activeResourceClass = resClass;
testHostFixture.detectChanges();
const slideToggle = await loader.getAllHarnesses(MatSlideToggleHarness);
expect(slideToggle.length).toEqual(1);
});
it('should not show the sort slide-toggle when a property with cardinality 1 is selected for a linked resource', async () => {
testHostComponent.topLevel = false;
const select = await loader.getHarness(MatSelectHarness);
await select.open();
const options = await select.getOptions();
expect(await options[4].getText()).toEqual('Date');
await options[4].click();
const resClass = new ResourceClassDefinition();
resClass.propertiesList = [
{
propertyIndex:
'http://0.0.0.0:3333/ontology/0001/anything/v2#hasDate',
cardinality: Cardinality._1,
isInherited: true,
},
];
testHostComponent.activeResourceClass = resClass;
testHostFixture.detectChanges();
const slideToggle = await loader.getAllHarnesses(MatSlideToggleHarness);
expect(slideToggle.length).toEqual(0);
});
it('should get the specified value for the selected property', async () => {
const select = await loader.getHarness(MatSelectHarness);
await select.open();
const options = await select.getOptions();
expect(await options[11].getText()).toEqual('Integer');
await options[11].click();
const propWithVal =
testHostComponent.selectProperty.getPropertySelectedWithValue();
expect(propWithVal.property.id).toEqual(
'http://0.0.0.0:3333/ontology/0001/anything/v2#hasInteger'
);
expect(propWithVal.valueLiteral.value).toEqual(
new ValueLiteral('1', Constants.XsdInteger)
);
expect(propWithVal.valueLiteral.comparisonOperator).toEqual(
new Equals()
);
expect(propWithVal.isSortCriterion).toBe(false);
});
it('should reinitialise the properties', async () => {
expect(
testHostComponent.selectProperty.propertySelected
).toBeUndefined();
const select = await loader.getHarness(MatSelectHarness);
await select.open();
const options = await select.getOptions();
await options[0].click();
expect(testHostComponent.selectProperty.propertySelected.id).toEqual(
'http://0.0.0.0:3333/ontology/0001/anything/v2#hasBlueThing'
);
testHostComponent.propertyDefs = MockOntology.mockReadOntology(
'http://0.0.0.0:3333/ontology/0001/anything/v2'
).getPropertyDefinitionsByType(ResourcePropertyDefinition);
testHostFixture.detectChanges();
expect(
testHostComponent.selectProperty.propertySelected
).toBeUndefined();
});
it('should remove the control from the parent form and unsubscribe from value changes when destroyed', waitForAsync(() => {
expect(
testHostComponent.selectProperty.propertyChangesSubscription.closed
).toBe(false);
// tODO: find out why testHostFixture.destroy() does not trigger the component's ngOnDestroy
testHostComponent.selectProperty.ngOnDestroy();
// the control is added to the form as an async operation
// https://angular.io/guide/testing#async-test-with-async
testHostFixture.whenStable().then(() => {
expect(testHostComponent.form.contains('property0')).toBe(false);
expect(
testHostComponent.selectProperty.propertyChangesSubscription
.closed
).toBe(true);
});
}));
});