Skip to content

Commit a84c870

Browse files
Andrea Barbassoatarix83
authored andcommitted
Merged in task/dspace-cris-2023_02_x/DSC-2285 (pull request DSpace#3077)
[DSC-2285] fix authority values not working correctly in dynamic-onebox Approved-by: Giuseppe Digilio
2 parents 1b005a2 + e1014e5 commit a84c870

2 files changed

Lines changed: 65 additions & 21 deletions

File tree

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

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,12 @@ describe('DsDynamicOneboxComponent test suite', () => {
175175
providers: [
176176
ChangeDetectorRef,
177177
DsDynamicOneboxComponent,
178-
{ provide: VocabularyService, useValue: vocabularyServiceStub },
179-
{ provide: DynamicFormLayoutService, useValue: mockDynamicFormLayoutService },
180-
{ provide: DynamicFormValidationService, useValue: mockDynamicFormValidationService },
181-
{ provide: NgbModal, useValue: modal },
182-
{ provide: FormBuilderService },
183-
{ provide: SubmissionService, useClass: SubmissionServiceStub }
178+
{provide: VocabularyService, useValue: vocabularyServiceStub},
179+
{provide: DynamicFormLayoutService, useValue: mockDynamicFormLayoutService},
180+
{provide: DynamicFormValidationService, useValue: mockDynamicFormValidationService},
181+
{provide: NgbModal, useValue: modal},
182+
{provide: FormBuilderService},
183+
{provide: SubmissionService, useClass: SubmissionServiceStub}
184184
],
185185
schemas: [CUSTOM_ELEMENTS_SCHEMA]
186186
}).compileComponents();
@@ -348,7 +348,7 @@ describe('DsDynamicOneboxComponent test suite', () => {
348348
}));
349349
spyOn((oneboxComponent as any).vocabularyService, 'getVocabularyEntryByValue').and.returnValue(entry);
350350
spyOn((oneboxComponent as any).vocabularyService, 'getVocabularyEntryByID').and.returnValue(entry);
351-
(oneboxComponent.model as any).value = new FormFieldMetadataValueObject('test', null, null,null, 'testDisplay');
351+
(oneboxComponent.model as any).value = new FormFieldMetadataValueObject('test', null, null, null, 'testDisplay');
352352
oneboxCompFixture.detectChanges();
353353
});
354354

@@ -359,7 +359,7 @@ describe('DsDynamicOneboxComponent test suite', () => {
359359

360360
it('should init component properly', fakeAsync(() => {
361361
tick();
362-
expect(oneboxComponent.currentValue).toEqual(new FormFieldMetadataValueObject('test', null, null,null, 'testDisplay'));
362+
expect(oneboxComponent.currentValue).toEqual(new FormFieldMetadataValueObject('test', null, null, null, 'testDisplay'));
363363
expect((oneboxComponent as any).vocabularyService.getVocabularyEntryByValue).not.toHaveBeenCalled();
364364
}));
365365

@@ -396,7 +396,7 @@ describe('DsDynamicOneboxComponent test suite', () => {
396396

397397
it('should init component properly', fakeAsync(() => {
398398
tick();
399-
expect(oneboxComponent.currentValue).toEqual(new FormFieldMetadataValueObject('test', null, null,validAuthority, 'test'));
399+
expect(oneboxComponent.currentValue).toEqual(new FormFieldMetadataValueObject('test', null, null, validAuthority, 'test'));
400400
expect((oneboxComponent as any).vocabularyService.getVocabularyEntryByID).not.toHaveBeenCalled();
401401
}));
402402

@@ -457,7 +457,7 @@ describe('DsDynamicOneboxComponent test suite', () => {
457457
}));
458458
spyOn((oneboxComponent as any).vocabularyService, 'getVocabularyEntryByValue').and.returnValue(entry);
459459
spyOn((oneboxComponent as any).vocabularyService, 'getVocabularyEntryByID').and.returnValue(entry);
460-
(oneboxComponent.model as any).value = new FormFieldMetadataValueObject('test', null, null, null, 'testDisplay');
460+
(oneboxComponent.model as any).value = new FormFieldMetadataValueObject('test', null, null, null, 'testDisplay');
461461
oneboxCompFixture.detectChanges();
462462
});
463463

@@ -468,7 +468,7 @@ describe('DsDynamicOneboxComponent test suite', () => {
468468

469469
it('should init component properly', fakeAsync(() => {
470470
tick();
471-
expect(oneboxComponent.currentValue).toEqual(new FormFieldMetadataValueObject('test', null, null,null, 'testDisplay'));
471+
expect(oneboxComponent.currentValue).toEqual(new FormFieldMetadataValueObject('test', null, null, null, 'testDisplay'));
472472
expect((oneboxComponent as any).vocabularyService.getVocabularyEntryByValue).toHaveBeenCalled();
473473
}));
474474

@@ -517,6 +517,38 @@ describe('DsDynamicOneboxComponent test suite', () => {
517517
}));
518518
});
519519

520+
describe('selectAlternativeInformation', () => {
521+
beforeEach(() => {
522+
oneboxCompFixture = TestBed.createComponent(DsDynamicOneboxComponent);
523+
debugElement = oneboxCompFixture.debugElement;
524+
oneboxComponent = oneboxCompFixture.componentInstance;
525+
oneboxComponent.currentValue = new FormFieldMetadataValueObject('test', null, null, null, 'testDisplay');
526+
oneboxComponent.model = new DynamicOneboxModel(ONEBOX_TEST_MODEL_CONFIG);
527+
528+
spyOn(oneboxComponent, 'onSelectItem').and.returnValue(undefined);
529+
spyOn(oneboxComponent, 'toggleOtherInfoSelection').and.returnValue(undefined);
530+
});
531+
532+
it('sets authority when unformattedOtherInfoValue contains "::"', () => {
533+
const info = 'testInfo';
534+
const unformattedItem = 'testInfo::authorityValue';
535+
oneboxComponent.otherInfoValuesUnformatted = [unformattedItem];
536+
537+
oneboxComponent.selectAlternativeInfo(info);
538+
539+
expect(oneboxComponent.currentValue.authority).toBe('authorityValue');
540+
});
541+
542+
it('sets authority to undefined when unformattedOtherInfoValue does not contain "::"', () => {
543+
const info = 'testInfo';
544+
const unformattedItem = 'testInfo';
545+
oneboxComponent.otherInfoValuesUnformatted = [unformattedItem];
546+
547+
oneboxComponent.selectAlternativeInfo(info);
548+
549+
expect(oneboxComponent.currentValue.authority).toBeUndefined();
550+
});
551+
});
520552
});
521553
});
522554

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
import {
2-
ChangeDetectorRef,
3-
Component,
4-
EventEmitter,
5-
Input,
6-
OnInit,
7-
Output,
8-
ViewChild
9-
} from '@angular/core';
1+
import { ChangeDetectorRef, Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core';
102
import { UntypedFormGroup } from '@angular/forms';
113

124
import {
@@ -373,6 +365,19 @@ export class DsDynamicOneboxComponent extends DsDynamicVocabularyComponent imple
373365
this.currentValue = null;
374366
this.currentValue = temp;
375367

368+
const unformattedOtherInfoValue = this.otherInfoValuesUnformatted.find((unformattedItem) => {
369+
return unformattedItem.startsWith(info);
370+
});
371+
372+
if (hasValue(unformattedOtherInfoValue)) {
373+
const lastIndexOfSeparator = unformattedOtherInfoValue.lastIndexOf('::');
374+
if (lastIndexOfSeparator !== -1) {
375+
this.currentValue.authority = unformattedOtherInfoValue.substring(lastIndexOfSeparator + 2);
376+
} else {
377+
this.currentValue.authority = undefined;
378+
}
379+
}
380+
376381
const event = {
377382
item: this.currentValue
378383
} as any;
@@ -388,7 +393,14 @@ export class DsDynamicOneboxComponent extends DsDynamicVocabularyComponent imple
388393

389394
this.otherInfoKey = hasAlternativeNames ? this.alternativeNamesKey : keys.find(key => hasValue(item.otherInformation[key]) && item.otherInformation[key].includes('|||'));
390395
this.otherInfoValuesUnformatted = item.otherInformation[this.otherInfoKey] ? item.otherInformation[this.otherInfoKey].split('|||') : [];
391-
this.otherInfoValues = this.otherInfoValuesUnformatted.map(unformattedItem => unformattedItem.substring(0, unformattedItem.lastIndexOf('::')));
396+
397+
this.otherInfoValues = this.otherInfoValuesUnformatted.map(unformattedItem => {
398+
let lastIndexOfSeparator = unformattedItem.lastIndexOf('::');
399+
if (lastIndexOfSeparator === -1) {
400+
lastIndexOfSeparator = undefined;
401+
}
402+
return unformattedItem.substring(0, lastIndexOfSeparator);
403+
});
392404

393405
if (hasAlternativeNames) {
394406
this.otherName = hasValue(this.otherName) ? this.otherName : this.otherInfoValues[0];

0 commit comments

Comments
 (0)