/
properties.component.spec.ts
464 lines (349 loc) · 16.5 KB
/
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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import { ClipboardModule } from '@angular/cdk/clipboard';
import { Component, EventEmitter, Input, OnDestroy, OnInit, Output, ViewChild } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
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 { RouterTestingModule } from '@angular/router/testing';
import {
ApiResponseError,
Constants,
IHasPropertyWithPropertyDefinition,
MockProjects,
MockResource,
MockUsers,
ProjectsEndpointAdmin,
ReadLinkValue,
ReadResource,
ReadResourceSequence,
ReadValue,
ResourcePropertyDefinition,
SystemPropertyDefinition
} from '@dasch-swiss/dsp-js';
import {
AppInitService,
DspActionModule,
EmitEvent,
Events,
PropertyInfoValues,
ValueOperationEventService
} from '@dasch-swiss/dsp-ui';
import { of, Subscription } from 'rxjs';
import { DspResource } from '../dsp-resource';
import { PropertiesComponent } from './properties.component';
import { IncomingService } from '../incoming.service';
import { DspApiConnectionToken } from 'src/app/main/declarations/dsp-api-tokens';
import { UserService } from '../services/user.service';
/**
* test host component to simulate parent component.
*/
@Component({
template: `
<app-properties #propView
[resource]="parentResource">
</app-properties>`
})
class TestPropertyParentComponent implements OnInit, OnDestroy {
@ViewChild('propView') propertiesComponent: PropertiesComponent;
parentResource: DspResource;
voeSubscriptions: Subscription[] = [];
myNum = 0;
constructor(public _valueOperationEventService: ValueOperationEventService) { }
ngOnInit() {
this.voeSubscriptions.push(this._valueOperationEventService.on(Events.ValueAdded, () => this.myNum = 1));
this.voeSubscriptions.push(this._valueOperationEventService.on(Events.ValueUpdated, () => this.myNum = 2));
this.voeSubscriptions.push(this._valueOperationEventService.on(Events.ValueDeleted, () => this.myNum = 3));
MockResource.getTestThing().subscribe(
(response: ReadResource) => {
this.parentResource = new DspResource(response);
// gather resource property information
this.parentResource.resProps = this.parentResource.res.entityInfo.classes[this.parentResource.res.type].getResourcePropertiesList().map(
(prop: IHasPropertyWithPropertyDefinition) => {
const propInfoAndValues: PropertyInfoValues = {
propDef: prop.propertyDefinition,
guiDef: prop,
values: this.parentResource.res.getValues(prop.propertyIndex)
};
return propInfoAndValues;
}
);
// sort properties by guiOrder
this.parentResource.resProps.sort((a, b) => (a.guiDef.guiOrder > b.guiDef.guiOrder) ? 1 : -1);
// get system property information
this.parentResource.systemProps = this.parentResource.res.entityInfo.getPropertyDefinitionsByType(SystemPropertyDefinition);
},
(error: ApiResponseError) => {
console.error('Error to get the mock resource', error);
}
);
}
ngOnDestroy() {
if (this.voeSubscriptions) {
this.voeSubscriptions.forEach(sub => sub.unsubscribe());
}
}
}
/**
* test host component to simulate child component, here display-edit.
*/
@Component({
selector: 'dsp-display-edit',
template: ''
})
class TestDisplayValueComponent {
@Input() parentResource: DspResource;
@Input() displayValue: ReadValue;
@Input() configuration?: object;
@Output() referredResourceClicked: EventEmitter<ReadLinkValue> = new EventEmitter<ReadLinkValue>();
@Output() referredResourceHovered: EventEmitter<ReadLinkValue> = new EventEmitter<ReadLinkValue>();
}
/**
* test host component to simulate child component, here add-value.
*/
@Component({
selector: 'dsp-add-value',
template: ''
})
class TestAddValueComponent {
@Input() parentResource: DspResource;
@Input() resourcePropertyDefinition: ResourcePropertyDefinition;
}
describe('PropertiesComponent', () => {
let testHostComponent: TestPropertyParentComponent;
let testHostFixture: ComponentFixture<TestPropertyParentComponent>;
let voeService: ValueOperationEventService;
beforeEach(waitForAsync(() => {
const adminSpyObj = {
admin: {
projectsEndpoint: jasmine.createSpyObj('projectsEndpoint', ['getProjectByIri'])
}
};
const userServiceSpy = jasmine.createSpyObj('UserService', ['getUser']);
const incomingServiceSpy = jasmine.createSpyObj('IncomingService', ['getIncomingLinks']);
TestBed.configureTestingModule({
imports: [
ClipboardModule,
DspActionModule,
MatIconModule,
MatMenuModule,
MatSnackBarModule,
MatTooltipModule,
RouterTestingModule
],
declarations: [
TestPropertyParentComponent,
TestDisplayValueComponent,
TestAddValueComponent,
PropertiesComponent
],
providers: [
ValueOperationEventService,
AppInitService,
{
provide: DspApiConnectionToken,
useValue: adminSpyObj
},
{
provide: UserService,
useValue: userServiceSpy
},
{
provide: IncomingService,
useValue: incomingServiceSpy
},
]
})
.compileComponents();
voeService = TestBed.inject(ValueOperationEventService);
}));
// mock localStorage
beforeEach(() => {
let store = {};
spyOn(localStorage, 'getItem').and.callFake(
(key: string): string => store[key] || null
);
spyOn(localStorage, 'removeItem').and.callFake(
(key: string): void => {
delete store[key];
}
);
spyOn(localStorage, 'setItem').and.callFake(
(key: string, value: string): string => (store[key] = <any>value)
);
spyOn(localStorage, 'clear').and.callFake(() => {
store = {};
});
});
beforeEach(() => {
const adminSpy = TestBed.inject(DspApiConnectionToken);
// mock getProjectByIri response
(adminSpy.admin.projectsEndpoint as jasmine.SpyObj<ProjectsEndpointAdmin>).getProjectByIri.and.callFake(
() => {
const project = MockProjects.mockProject();
return of(project);
}
);
// mock getUserByIri response
const userSpy = TestBed.inject(UserService);
// mock getUserByIri response
(userSpy as jasmine.SpyObj<UserService>).getUser.and.callFake(
() => {
const user = MockUsers.mockUser();
return of(user.body);
}
);
const incomingLinksSpy = TestBed.inject(IncomingService);
(incomingLinksSpy as jasmine.SpyObj<IncomingService>).getIncomingLinks.and.callFake(
() => {
const resources = new ReadResource();
const incomingLinks = new ReadResourceSequence([resources], true);
return of(incomingLinks);
}
);
testHostFixture = TestBed.createComponent(TestPropertyParentComponent);
testHostComponent = testHostFixture.componentInstance;
testHostFixture.detectChanges();
expect(testHostComponent).toBeTruthy();
});
it('should get one incoming link', () => {
expect(testHostComponent.propertiesComponent.incomingLinkResources.length).toEqual(1);
});
it('should get the resource testding', () => {
expect(testHostComponent.parentResource).toBeTruthy();
expect(testHostComponent.parentResource.res.id).toEqual('http://rdfh.ch/0001/H6gBWUuJSuuO-CilHV8kQw');
expect(testHostComponent.parentResource.res.label).toEqual('testding');
});
describe('Toolbar', () => {
let hostCompDe;
let propertyToolbarComponentDe;
beforeEach(() => {
expect(testHostComponent.propertiesComponent).toBeTruthy();
hostCompDe = testHostFixture.debugElement;
propertyToolbarComponentDe = hostCompDe.query(By.directive(PropertiesComponent));
expect(testHostComponent).toBeTruthy();
testHostFixture.detectChanges();
});
it('should have the label "testding"', () => {
const resLabelDebugElement = propertyToolbarComponentDe.query(By.css('h3.label'));
const resLabelNativeElement = resLabelDebugElement.nativeElement;
expect(resLabelNativeElement.textContent.trim()).toBe('testding');
});
it('should toggle list of properties', () => {
const resLabelDebugElement = propertyToolbarComponentDe.query(By.css('button.toggle-props'));
const resLabelNativeElement = resLabelDebugElement.nativeElement;
// the button contains an icon "unfold_more" and the text "Increase properties"
expect(resLabelNativeElement.textContent.trim()).toBe('unfold_more');
resLabelNativeElement.click();
testHostFixture.detectChanges();
// the button contains an icon "unfold_less" and the text "Decrease properties"
expect(resLabelNativeElement.textContent.trim()).toBe('unfold_less');
});
});
// --> TODO: currently not possible to test copy to clipboard from Material Angular
// https://stackoverflow.com/questions/60337742/test-copy-to-clipboard-function
describe('List of properties', () => {
it('should get 25 properties', () => {
expect(testHostComponent.parentResource.resProps).toBeTruthy();
expect(testHostComponent.parentResource.resProps.length).toBe(25);
});
it('should get the resource testding', () => {
expect(testHostComponent.parentResource.res).toBeTruthy();
expect(testHostComponent.parentResource.res.id).toEqual('http://rdfh.ch/0001/H6gBWUuJSuuO-CilHV8kQw');
expect(testHostComponent.parentResource.res.label).toEqual('testding');
});
it('should display a text value among the property list', () => {
expect(testHostComponent.parentResource.resProps[4].propDef.label).toEqual('Text');
expect(testHostComponent.parentResource.resProps[4].propDef.comment).toBe(undefined);
expect(testHostComponent.parentResource.resProps[4].guiDef.cardinality).toEqual(2);
expect(testHostComponent.parentResource.resProps[4].guiDef.guiOrder).toEqual(2);
expect(testHostComponent.parentResource.resProps[4].values[0].type).toEqual('http://api.knora.org/ontology/knora-api/v2#TextValue');
});
it('should get some system properties', () => {
expect(testHostComponent.parentResource.systemProps).toBeTruthy();
expect(testHostComponent.parentResource.systemProps.length).toEqual(13);
// check if the first system property is an ARK url
expect(testHostComponent.parentResource.systemProps[0].label).toEqual('ARK URL');
});
it('should trigger the callback when an event is emitted', () => {
expect(testHostComponent.myNum).toEqual(0);
voeService.emit(new EmitEvent(Events.ValueAdded));
expect(testHostComponent.myNum).toEqual(1);
});
it('should unsubscribe from changes when destroyed', () => {
testHostComponent.voeSubscriptions.forEach(sub => {
expect(sub.closed).toBe(false);
});
testHostFixture.destroy();
testHostComponent.voeSubscriptions.forEach(sub => {
expect(sub.closed).toBe(true);
});
});
});
describe('Add value', () => {
let hostCompDe;
let propertyViewComponentDe;
beforeEach(() => {
expect(testHostComponent.propertiesComponent).toBeTruthy();
hostCompDe = testHostFixture.debugElement;
propertyViewComponentDe = hostCompDe.query(By.directive(PropertiesComponent));
expect(testHostComponent).toBeTruthy();
testHostComponent.propertiesComponent.addButtonIsVisible = true;
testHostComponent.propertiesComponent.addValueFormIsVisible = false;
testHostFixture.detectChanges();
});
it('should show an add button under each property that has a value component and value and appropriate cardinality', () => {
const addButtons = propertyViewComponentDe.queryAll(By.css('button.create'));
expect(addButtons.length).toEqual(14);
});
it('should show an add button under a property with a cardinality of 1 and does not have a value', () => {
// show all properties so that we can access properties with no values
const toggleAllPropsElement = testHostFixture.debugElement.query(By.css('button.toggle-props'));
toggleAllPropsElement.nativeElement.click();
testHostFixture.detectChanges();
let addButtons = propertyViewComponentDe.queryAll(By.css('button.create'));
// current amount of buttons should equal 17
// because the boolean property shouldn't have an add button if it has a value
// standoff links value and has incoming link value are system props and cannot be added: -2
expect(addButtons.length).toEqual(17);
// remove value from the boolean property
testHostComponent.parentResource.resProps[9].values = [];
testHostFixture.detectChanges();
// now the boolean property should have an add button
// so the amount of add buttons on the page should increase by 1
// standoff links value and has incoming link value are system props and cannot be added: -2
addButtons = propertyViewComponentDe.queryAll(By.css('button.create'));
expect(addButtons.length).toEqual(18);
});
it('should show an add value component when the add button is clicked', () => {
const addButtonDebugElement = propertyViewComponentDe.query(By.css('button.create'));
const addButtonNativeElement = addButtonDebugElement.nativeElement;
expect(propertyViewComponentDe.query(By.css('.add-value'))).toBeNull();
addButtonNativeElement.click();
testHostFixture.detectChanges();
const addButtons = propertyViewComponentDe.queryAll(By.css('button.create'));
// the add button for the property with the open add value form is hidden
expect(addButtons.length).toEqual(13);
expect(propertyViewComponentDe.query(By.css('.add-value'))).toBeDefined();
});
it('should determine that adding a standoff link value is not allowed', () => {
const standoffLinkVal = testHostComponent.parentResource.resProps.filter(
propVal => propVal.propDef.id === Constants.HasStandoffLinkToValue
);
expect(testHostComponent.propertiesComponent.addValueIsAllowed(standoffLinkVal[0])).toBeFalsy();
});
it('should determine that adding a incoming link value is not allowed', () => {
const standoffLinkVal = testHostComponent.parentResource.resProps.filter(
propVal => propVal.propDef.id === 'http://api.knora.org/ontology/knora-api/v2#hasIncomingLinkValue'
);
expect(testHostComponent.propertiesComponent.addValueIsAllowed(standoffLinkVal[0])).toBeFalsy();
});
it('should determine that adding an int value is allowed', () => {
const standoffLinkVal = testHostComponent.parentResource.resProps.filter(
propVal => propVal.propDef.id === 'http://0.0.0.0:3333/ontology/0001/anything/v2#hasInteger'
);
expect(testHostComponent.propertiesComponent.addValueIsAllowed(standoffLinkVal[0])).toBeTruthy();
});
});
});