1- import { ComponentFixture , TestBed } from '@angular/core/testing' ;
1+ import { TestBed } from '@angular/core/testing' ;
22
33import { StaticPageComponent } from './static-page.component' ;
44import { HtmlContentService } from '../shared/html-content.service' ;
@@ -11,27 +11,25 @@ import { environment } from '../../environments/environment';
1111import { ClarinSafeHtmlPipe } from '../shared/utils/clarin-safehtml.pipe' ;
1212
1313describe ( 'StaticPageComponent' , ( ) => {
14- let component : StaticPageComponent ;
15- let fixture : ComponentFixture < StaticPageComponent > ;
16-
17- let htmlContentService : HtmlContentService ;
18- let appConfig : any ;
19-
20- const htmlContent = '<div id="idShouldNotBeRemoved">TEST MESSAGE</div>' ;
21-
22- beforeEach ( async ( ) => {
23- htmlContentService = jasmine . createSpyObj ( 'htmlContentService' , {
24- fetchHtmlContent : of ( htmlContent ) ,
25- getHmtlContentByPathAndLocale : Promise . resolve ( htmlContent )
14+ async function setupTest ( html : string , restBase ?: string ) {
15+ const htmlContentService = jasmine . createSpyObj ( 'htmlContentService' , {
16+ fetchHtmlContent : of ( html ) ,
17+ getHmtlContentByPathAndLocale : Promise . resolve ( html )
2618 } ) ;
2719
28- appConfig = Object . assign ( environment , {
20+ const appConfig = {
21+ ...environment ,
2922 ui : {
23+ ...( environment as any ) . ui ,
3024 namespace : 'testNamespace'
25+ } ,
26+ rest : {
27+ ...( environment as any ) . rest ,
28+ baseUrl : restBase
3129 }
32- } ) ;
30+ } ;
3331
34- TestBed . configureTestingModule ( {
32+ await TestBed . configureTestingModule ( {
3533 declarations : [ StaticPageComponent , ClarinSafeHtmlPipe ] ,
3634 imports : [
3735 TranslateModule . forRoot ( )
@@ -41,22 +39,66 @@ describe('StaticPageComponent', () => {
4139 { provide : Router , useValue : new RouterMock ( ) } ,
4240 { provide : APP_CONFIG , useValue : appConfig }
4341 ]
44- } ) ;
45-
46- } ) ;
42+ } ) . compileComponents ( ) ;
4743
48- beforeEach ( ( ) => {
49- fixture = TestBed . createComponent ( StaticPageComponent ) ;
50- component = fixture . componentInstance ;
51- } ) ;
44+ const fixture = TestBed . createComponent ( StaticPageComponent ) ;
45+ const component = fixture . componentInstance ;
46+ return { fixture, component , htmlContentService } ;
47+ }
5248
53- it ( 'should create' , ( ) => {
49+ it ( 'should create' , async ( ) => {
50+ const { component } = await setupTest ( '<div>test</div>' ) ;
5451 expect ( component ) . toBeTruthy ( ) ;
5552 } ) ;
5653
5754 // Load `TEST MESSAGE`
5855 it ( 'should load html file content' , async ( ) => {
56+ const { component } = await setupTest ( '<div id="idShouldNotBeRemoved">TEST MESSAGE</div>' ) ;
5957 await component . ngOnInit ( ) ;
6058 expect ( component . htmlContent . value ) . toBe ( '<div id="idShouldNotBeRemoved">TEST MESSAGE</div>' ) ;
6159 } ) ;
60+
61+ it ( 'should rewrite OAI link with rest.baseUrl' , async ( ) => {
62+ const oaiHtml = '<a href="/server/oai/request?verb=ListSets">OAI</a>' ;
63+ const { fixture, component } = await setupTest ( oaiHtml , 'https://api.example.org/rest' ) ;
64+
65+ await component . ngOnInit ( ) ;
66+ fixture . detectChanges ( ) ;
67+
68+ const rewritten = 'https://api.example.org/server/oai/request?verb=ListSets' ;
69+ expect ( component . htmlContent . value ) . toContain ( rewritten ) ;
70+ const anchor = fixture . nativeElement . querySelector ( 'a' ) ;
71+ expect ( anchor . getAttribute ( 'href' ) ) . toBe ( rewritten ) ;
72+ } ) ;
73+
74+ it ( 'should leave OAI link unchanged when rest.baseUrl is missing' , async ( ) => {
75+ const oaiHtml = '<a href="/server/oai/request?verb=Identify">OAI</a>' ;
76+ const { fixture, component } = await setupTest ( oaiHtml , undefined ) ;
77+
78+ await component . ngOnInit ( ) ;
79+ fixture . detectChanges ( ) ;
80+
81+ expect ( component . htmlContent . value ) . toContain ( '/server/oai/request?verb=Identify' ) ;
82+ } ) ;
83+
84+ it ( 'should avoid double slashes when rest.baseUrl ends with slash' , async ( ) => {
85+ const oaiHtml = '<a href="/server/oai/request?verb=ListRecords">OAI</a>' ;
86+ const { fixture, component } = await setupTest ( oaiHtml , 'https://api.example.org/rest/' ) ;
87+
88+ await component . ngOnInit ( ) ;
89+ fixture . detectChanges ( ) ;
90+
91+ expect ( component . htmlContent . value ) . toContain ( 'https://api.example.org/server/oai/request?verb=ListRecords' ) ;
92+ expect ( component . htmlContent . value ) . not . toContain ( '//server' ) ;
93+ } ) ;
94+
95+ it ( 'should leave content unchanged when no OAI link is present' , async ( ) => {
96+ const otherHtml = '<a href="/server/other">Other</a>' ;
97+ const { fixture, component } = await setupTest ( otherHtml , 'https://api.example.org/rest' ) ;
98+
99+ await component . ngOnInit ( ) ;
100+ fixture . detectChanges ( ) ;
101+
102+ expect ( component . htmlContent . value ) . toBe ( otherHtml ) ;
103+ } ) ;
62104} ) ;
0 commit comments