Skip to content

Commit e9f3df5

Browse files
[DURACOM-426] improve tests
1 parent 36c77fe commit e9f3df5

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed

src/app/core/shared/metadata.utils.spec.ts

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,20 @@ const multiViewModelList = [
5050
{ key: 'foo', ...bar, order: 0 },
5151
];
5252

53-
const testMethod = (fn, resultKind, mapOrMaps, keyOrKeys, hitHighlights, expected, filter?) => {
53+
const testMethod = (fn, resultKind, mapOrMaps, keyOrKeys, hitHighlights, expected, filter?, limit?: number) => {
5454
const keys = keyOrKeys instanceof Array ? keyOrKeys : [keyOrKeys];
5555
describe('and key' + (keys.length === 1 ? (' ' + keys[0]) : ('s ' + JSON.stringify(keys)))
5656
+ ' with ' + (isUndefined(filter) ? 'no filter' : 'filter ' + JSON.stringify(filter)), () => {
57-
const result = fn(mapOrMaps, keys, hitHighlights, filter);
57+
const result = fn(mapOrMaps, keys, hitHighlights, filter, true, limit);
5858
let shouldReturn;
5959
if (resultKind === 'boolean') {
6060
shouldReturn = expected;
6161
} else if (isUndefined(expected)) {
6262
shouldReturn = 'undefined';
6363
} else if (expected instanceof Array) {
6464
shouldReturn = 'an array with ' + expected.length + ' ' + (expected.length > 1 ? 'ordered ' : '')
65-
+ resultKind + (expected.length !== 1 ? 's' : '');
65+
+ resultKind + (expected.length !== 1 ? 's' : '')
66+
+ (isUndefined(limit) ? '' : ' (limited to ' + limit + ')');
6667
} else {
6768
shouldReturn = 'a ' + resultKind;
6869
}
@@ -255,4 +256,60 @@ describe('Metadata', () => {
255256

256257
});
257258

259+
describe('all method with limit', () => {
260+
const testAllWithLimit = (mapOrMaps, keyOrKeys, expected, limit) =>
261+
testMethod(Metadata.all, 'value', mapOrMaps, keyOrKeys, undefined, expected, undefined, limit);
262+
263+
describe('with multiMap and limit', () => {
264+
testAllWithLimit(multiMap, 'dc.title', [dcTitle1], 1);
265+
});
266+
});
267+
268+
describe('hasValue method', () => {
269+
const testHasValue = (value, expected) =>
270+
testMethod(Metadata.hasValue, 'boolean', value, undefined, undefined, expected);
271+
272+
describe('with undefined value', () => {
273+
testHasValue(undefined, false);
274+
});
275+
describe('with null value', () => {
276+
testHasValue(null, false);
277+
});
278+
describe('with string value', () => {
279+
testHasValue('test', true);
280+
});
281+
describe('with empty string value', () => {
282+
testHasValue('', false);
283+
});
284+
describe('with undefined value for a MetadataValue object', () => {
285+
const value: Partial<MetadataValue> = {
286+
value: undefined,
287+
};
288+
testHasValue(value, false);
289+
});
290+
describe('with null value for a MetadataValue object', () => {
291+
const value: Partial<MetadataValue> = {
292+
value: null,
293+
};
294+
testHasValue(value, false);
295+
});
296+
describe('with empty string for a MetadataValue object', () => {
297+
const value: Partial<MetadataValue> = {
298+
value: '',
299+
};
300+
testHasValue(value, false);
301+
});
302+
describe('with value for a MetadataValue object', () => {
303+
const value: Partial<MetadataValue> = {
304+
value: 'test',
305+
};
306+
testHasValue(value, true);
307+
});
308+
describe('with a generic object', () => {
309+
const value: any = {
310+
test: 'test',
311+
};
312+
testHasValue(value, true);
313+
});
314+
});
258315
});

src/app/core/shared/metadata.utils.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,8 @@ export class Metadata {
5454
for (const mdKey of Metadata.resolveKeys(hitHighlights, keyOrKeys)) {
5555
if (hitHighlights[mdKey]) {
5656
for (const candidate of hitHighlights[mdKey]) {
57-
if (Metadata.valueMatches(candidate as MetadataValue, filter)) {
58-
if (isEmpty(limit) || (hasValue(limit) && matches.length < limit)) {
59-
matches.push(candidate as MetadataValue);
60-
}
57+
if (Metadata.valueMatches(candidate as MetadataValue, filter) && (isEmpty(limit) || (hasValue(limit) && matches.length < limit))) {
58+
matches.push(candidate as MetadataValue);
6159
}
6260
}
6361
}
@@ -69,7 +67,7 @@ export class Metadata {
6967
for (const mdKey of Metadata.resolveKeys(metadata, keyOrKeys)) {
7068
if (metadata[mdKey]) {
7169
for (const candidate of metadata[mdKey]) {
72-
if (Metadata.valueMatches(candidate as MetadataValue, filter)) {
70+
if (Metadata.valueMatches(candidate as MetadataValue, filter) && (isEmpty(limit) || (hasValue(limit) && matches.length < limit))) {
7371
if (escapeHTML) {
7472
matches.push(Object.assign(new MetadataValue(), candidate, {
7573
value: escape(candidate.value),

src/app/shared/form/builder/ds-dynamic-form-ui/models/onebox/dynamic-onebox.component.spec.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,22 @@ import { TranslateModule } from '@ngx-translate/core';
4545
import { getTestScheduler } from 'jasmine-marbles';
4646
import { of } from 'rxjs';
4747
import { TestScheduler } from 'rxjs/testing';
48+
import { v4 as uuidv4 } from 'uuid';
4849

4950
import { ObjNgFor } from '../../../../../utils/object-ngfor.pipe';
5051
import { AuthorityConfidenceStateDirective } from '../../../../directives/authority-confidence-state.directive';
5152
import { VocabularyTreeviewComponent } from '../../../../vocabulary-treeview/vocabulary-treeview.component';
5253
import { DsDynamicOneboxComponent } from './dynamic-onebox.component';
5354
import { DynamicOneboxModel } from './dynamic-onebox.model';
5455

56+
5557
export let ONEBOX_TEST_GROUP;
5658

5759
export let ONEBOX_TEST_MODEL_CONFIG;
5860

5961

62+
const validAuthority = uuidv4();
63+
6064
// Mock class for NgbModalRef
6165
export class MockNgbModalRef {
6266
componentInstance = {
@@ -364,13 +368,13 @@ describe('DsDynamicOneboxComponent test suite', () => {
364368
oneboxComponent.group = ONEBOX_TEST_GROUP;
365369
oneboxComponent.model = new DynamicOneboxModel(ONEBOX_TEST_MODEL_CONFIG);
366370
const entry = of(Object.assign(new VocabularyEntry(), {
367-
authority: 'test001',
371+
authority: validAuthority,
368372
value: 'test001',
369373
display: 'test',
370374
}));
371375
spyOn((oneboxComponent as any).vocabularyService, 'getVocabularyEntryByValue').and.returnValue(entry);
372376
spyOn((oneboxComponent as any).vocabularyService, 'getVocabularyEntryByID').and.returnValue(entry);
373-
(oneboxComponent.model as any).value = new FormFieldMetadataValueObject('test', null, 'test001');
377+
(oneboxComponent.model as any).value = new FormFieldMetadataValueObject('test', null, validAuthority, 'test001');
374378
oneboxCompFixture.detectChanges();
375379
});
376380

@@ -381,7 +385,7 @@ describe('DsDynamicOneboxComponent test suite', () => {
381385

382386
it('should init component properly', fakeAsync(() => {
383387
tick();
384-
expect(oneboxComponent.currentValue).toEqual(new FormFieldMetadataValueObject('test001', null, 'test001', 'test'));
388+
expect(oneboxComponent.currentValue).toEqual(new FormFieldMetadataValueObject('test001', null, validAuthority, 'test'));
385389
expect((oneboxComponent as any).vocabularyService.getVocabularyEntryByID).toHaveBeenCalled();
386390
}));
387391

0 commit comments

Comments
 (0)