Skip to content

Commit b7ae91e

Browse files
[DSC-2544] fix tests, adapt logic to pre-existing check for link
1 parent 926d0df commit b7ae91e

File tree

5 files changed

+47
-32
lines changed

5 files changed

+47
-32
lines changed

src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/link/link.component.spec.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ describe('LinkComponent', () => {
109109
describe('with sub-type label', () => {
110110
beforeEach(() => {
111111
component.renderingSubType = 'LABEL';
112+
component.metadataValueProvider = Object.assign(new MetadataValue(), metadataValue, {
113+
value: '[Default Label](http://rest.api/item/link/id)',
114+
});
115+
component.metadataValue = component.metadataValueProvider;
112116
spyOn(translateService, 'instant').and.returnValue(i18nLabel);
113117
fixture.detectChanges();
114118
});
@@ -252,7 +256,7 @@ describe('LinkComponent', () => {
252256

253257
expect(result).toEqual({
254258
label: 'My Label',
255-
value: 'https://example.com/path'
259+
value: 'https://example.com/path',
256260
});
257261
});
258262

@@ -262,7 +266,7 @@ describe('LinkComponent', () => {
262266

263267
expect(result).toEqual({
264268
label: input,
265-
value: input
269+
value: input,
266270
});
267271
});
268272

@@ -272,7 +276,7 @@ describe('LinkComponent', () => {
272276

273277
expect(result).toEqual({
274278
label: '',
275-
value: ''
279+
value: '',
276280
});
277281
});
278282

src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/link/link.component.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ export class LinkComponent extends RenderingTypeValueModelComponent implements O
6565
* Check if the metadata value is a valid URL
6666
*/
6767
isValidUrl(): boolean {
68-
const value = this.metadataValue.value.trim();
68+
let value = this.metadataValue.value.trim();
69+
70+
// If the subtype is LABEL, the value is in [Label](URL) format — extract the URL part
71+
if (hasValue(this.renderingSubType) && this.renderingSubType.toUpperCase() === TYPES.LABEL.toString()) {
72+
const parsed = this.parseLabelValue(value);
73+
value = parsed.value;
74+
}
6975

7076
// Comprehensive URL regex that matches:
7177
// - URLs with protocols (http, https, ftp, mailto, etc.)
@@ -84,21 +90,21 @@ export class LinkComponent extends RenderingTypeValueModelComponent implements O
8490
let linkText: string;
8591
let metadataValue: string;
8692

87-
if (hasValue(this.renderingSubType) && this.renderingSubType.toUpperCase() === TYPES.EMAIL) {
88-
this.isEmail = true;
89-
metadataValue = 'mailto:' + this.metadataValue.value;
90-
linkText = (hasValue(this.renderingSubType) &&
91-
this.renderingSubType.toUpperCase() === TYPES.EMAIL) ? this.metadataValue.value : this.translateService.instant(this.field.label);
92-
} else if ((hasValue(this.renderingSubType) && this.renderingSubType.toUpperCase() === TYPES.LABEL)) {
93-
// Parse value in format [Label](URL)
94-
const parsedValue = this.parseLabelValue(this.metadataValue.value);
95-
96-
metadataValue = this.getLinkWithProtocol(parsedValue.value);
97-
linkText = parsedValue.label;
93+
if (hasValue(this.renderingSubType) && this.renderingSubType.toUpperCase() === TYPES.EMAIL.toString()) {
94+
this.isEmail = true;
95+
metadataValue = 'mailto:' + this.metadataValue.value;
96+
linkText = (hasValue(this.renderingSubType) &&
97+
this.renderingSubType.toUpperCase() === TYPES.EMAIL.toString()) ? this.metadataValue.value : this.translateService.instant(this.field.label);
98+
} else if ((hasValue(this.renderingSubType) && this.renderingSubType.toUpperCase() === TYPES.LABEL.toString())) {
99+
// Parse value in format [Label](URL)
100+
const parsedValue = this.parseLabelValue(this.metadataValue.value);
101+
102+
metadataValue = this.getLinkWithProtocol(parsedValue.value);
103+
linkText = parsedValue.label;
98104
} else {
99-
// Use same value for link and label, correcting the protocol for link if needed
100-
metadataValue = this.getLinkWithProtocol(this.metadataValue.value);
101-
linkText = this.metadataValue.value;
105+
// Use same value for link and label, correcting the protocol for link if needed
106+
metadataValue = this.getLinkWithProtocol(this.metadataValue.value);
107+
linkText = this.metadataValue.value;
102108
}
103109

104110
return {
@@ -117,7 +123,7 @@ export class LinkComponent extends RenderingTypeValueModelComponent implements O
117123
if (!match) {
118124
return {
119125
label: input,
120-
value: input
126+
value: input,
121127
};
122128
}
123129

src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/valuepair/valuepair.component.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
{{ value$ | async }}
55
</span>
66
</ng-container>
7-
</div>
87

9-
<ng-template #linkTemplate>
8+
<ng-template #linkTemplate>
109
<a [href]="metadataValue.value" class="text-value">
1110
{{ value$ | async }}
1211
</a>
13-
</ng-template>
12+
</ng-template>
13+
</div>

src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/valuepair/valuepair.component.spec.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import { AuthService } from '../../../../../../../core/auth/auth.service';
1616
import { LayoutField } from '../../../../../../../core/layout/models/box.model';
1717
import { Item } from '../../../../../../../core/shared/item.model';
18+
import { MetadataValue } from '../../../../../../../core/shared/metadata.models';
1819
import { VocabularyService } from '../../../../../../../core/submission/vocabularies/vocabulary.service';
1920
import { TranslateLoaderMock } from '../../../../../../../shared/mocks/translate-loader.mock';
2021
import { AuthServiceStub } from '../../../../../../../shared/testing/auth-service.stub';
@@ -195,7 +196,6 @@ describe('ValuepairComponent', () => {
195196
});
196197

197198
describe('when the metadata value is a link', () => {
198-
199199
const linkValue = 'https://example.com';
200200
const testFieldLink: LayoutField = {
201201
metadata: 'dc.identifier',
@@ -204,7 +204,7 @@ describe('ValuepairComponent', () => {
204204
fieldType: 'METADATA',
205205
metadataGroup: null,
206206
labelAsHeading: false,
207-
valuesInline: false
207+
valuesInline: false,
208208
};
209209

210210
const testItemLink = Object.assign(new Item(), {
@@ -217,11 +217,12 @@ describe('ValuepairComponent', () => {
217217
TranslateModule.forRoot({
218218
loader: {
219219
provide: TranslateLoader,
220-
useClass: TranslateLoaderMock
221-
}
220+
useClass: TranslateLoaderMock,
221+
},
222222
}),
223+
ValuepairComponent,
224+
DsDatePipe,
223225
],
224-
declarations: [ValuepairComponent, DsDatePipe],
225226
providers: [
226227
{ provide: VocabularyService, useValue: vocabularyServiceSpy },
227228
{ provide: AuthService, useValue: authService },
@@ -237,8 +238,9 @@ describe('ValuepairComponent', () => {
237238
beforeEach(() => {
238239
fixture = TestBed.createComponent(ValuepairComponent);
239240
component = fixture.componentInstance;
240-
241-
// Manually emit the value since vocabulary service won't emit
241+
component.metadataValue = Object.assign(new MetadataValue(), {
242+
value: linkValue,
243+
});
242244
component.value$.next(linkValue);
243245

244246
fixture.detectChanges();
@@ -253,7 +255,7 @@ describe('ValuepairComponent', () => {
253255
const linkEl = compiled.querySelector('a.text-value') as HTMLAnchorElement;
254256

255257
expect(linkEl).toBeTruthy();
256-
expect(linkEl.href).toBe(linkValue + '/'); // Browser adds trailing slash
258+
expect(linkEl.href).toBe(linkValue + '/');
257259
expect(linkEl.textContent.trim()).toBe(linkValue);
258260
});
259261
});

src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/valuepair/valuepair.component.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { AsyncPipe } from '@angular/common';
1+
import {
2+
AsyncPipe,
3+
NgIf,
4+
} from '@angular/common';
25
import {
36
Component,
47
Inject,
@@ -32,7 +35,7 @@ import { RenderingTypeValueModelComponent } from '../rendering-type-value.model'
3235
templateUrl: './valuepair.component.html',
3336
styleUrls: ['./valuepair.component.scss'],
3437
standalone: true,
35-
imports: [AsyncPipe],
38+
imports: [AsyncPipe, NgIf],
3639
})
3740
export class ValuepairComponent extends RenderingTypeValueModelComponent implements OnInit {
3841

0 commit comments

Comments
 (0)