diff --git a/src/app/static-page/static-page.component.spec.ts b/src/app/static-page/static-page.component.spec.ts index 1ad4e607c3c..0c461042381 100644 --- a/src/app/static-page/static-page.component.spec.ts +++ b/src/app/static-page/static-page.component.spec.ts @@ -1,4 +1,4 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { TestBed } from '@angular/core/testing'; import { StaticPageComponent } from './static-page.component'; import { HtmlContentService } from '../shared/html-content.service'; @@ -11,27 +11,25 @@ import { environment } from '../../environments/environment'; import { ClarinSafeHtmlPipe } from '../shared/utils/clarin-safehtml.pipe'; describe('StaticPageComponent', () => { - let component: StaticPageComponent; - let fixture: ComponentFixture; - - let htmlContentService: HtmlContentService; - let appConfig: any; - - const htmlContent = '
TEST MESSAGE
'; - - beforeEach(async () => { - htmlContentService = jasmine.createSpyObj('htmlContentService', { - fetchHtmlContent: of(htmlContent), - getHmtlContentByPathAndLocale: Promise.resolve(htmlContent) + async function setupTest(html: string, restBase?: string) { + const htmlContentService = jasmine.createSpyObj('htmlContentService', { + fetchHtmlContent: of(html), + getHmtlContentByPathAndLocale: Promise.resolve(html) }); - appConfig = Object.assign(environment, { + const appConfig = { + ...environment, ui: { + ...(environment as any).ui, namespace: 'testNamespace' + }, + rest: { + ...(environment as any).rest, + baseUrl: restBase } - }); + }; - TestBed.configureTestingModule({ + await TestBed.configureTestingModule({ declarations: [ StaticPageComponent, ClarinSafeHtmlPipe ], imports: [ TranslateModule.forRoot() @@ -41,22 +39,66 @@ describe('StaticPageComponent', () => { { provide: Router, useValue: new RouterMock() }, { provide: APP_CONFIG, useValue: appConfig } ] - }); - - }); + }).compileComponents(); - beforeEach(() => { - fixture = TestBed.createComponent(StaticPageComponent); - component = fixture.componentInstance; - }); + const fixture = TestBed.createComponent(StaticPageComponent); + const component = fixture.componentInstance; + return { fixture, component, htmlContentService }; + } - it('should create', () => { + it('should create', async () => { + const { component } = await setupTest('
test
'); expect(component).toBeTruthy(); }); // Load `TEST MESSAGE` it('should load html file content', async () => { + const { component } = await setupTest('
TEST MESSAGE
'); await component.ngOnInit(); expect(component.htmlContent.value).toBe('
TEST MESSAGE
'); }); + + it('should rewrite OAI link with rest.baseUrl', async () => { + const oaiHtml = 'OAI'; + const { fixture, component } = await setupTest(oaiHtml, 'https://api.example.org/rest'); + + await component.ngOnInit(); + fixture.detectChanges(); + + const rewritten = 'https://api.example.org/server/oai/request?verb=ListSets'; + expect(component.htmlContent.value).toContain(rewritten); + const anchor = fixture.nativeElement.querySelector('a'); + expect(anchor.getAttribute('href')).toBe(rewritten); + }); + + it('should leave OAI link unchanged when rest.baseUrl is missing', async () => { + const oaiHtml = 'OAI'; + const { fixture, component } = await setupTest(oaiHtml, undefined); + + await component.ngOnInit(); + fixture.detectChanges(); + + expect(component.htmlContent.value).toContain('/server/oai/request?verb=Identify'); + }); + + it('should avoid double slashes when rest.baseUrl ends with slash', async () => { + const oaiHtml = 'OAI'; + const { fixture, component } = await setupTest(oaiHtml, 'https://api.example.org/rest/'); + + await component.ngOnInit(); + fixture.detectChanges(); + + expect(component.htmlContent.value).toContain('https://api.example.org/server/oai/request?verb=ListRecords'); + expect(component.htmlContent.value).not.toContain('//server'); + }); + + it('should leave content unchanged when no OAI link is present', async () => { + const otherHtml = 'Other'; + const { fixture, component } = await setupTest(otherHtml, 'https://api.example.org/rest'); + + await component.ngOnInit(); + fixture.detectChanges(); + + expect(component.htmlContent.value).toBe(otherHtml); + }); }); diff --git a/src/app/static-page/static-page.component.ts b/src/app/static-page/static-page.component.ts index bb19403a704..aff3eed0aed 100644 --- a/src/app/static-page/static-page.component.ts +++ b/src/app/static-page/static-page.component.ts @@ -28,8 +28,12 @@ export class StaticPageComponent implements OnInit { // Fetch html file name from the url path. `static/some_file.html` this.htmlFileName = this.getHtmlFileName(); - const htmlContent = await this.htmlContentService.getHmtlContentByPathAndLocale(this.htmlFileName); + let htmlContent = await this.htmlContentService.getHmtlContentByPathAndLocale(this.htmlFileName); if (isNotEmpty(htmlContent)) { + const restBase = this.appConfig?.rest?.baseUrl; + const oaiUrl = restBase ? new URL('/server/oai', restBase).href : '/server/oai'; + htmlContent = htmlContent.replace(/href="\/server\/oai/gi, 'href="' + oaiUrl); + this.htmlContent.next(htmlContent); return; }