From 209841dbedb7934a6f49753295297e4284af6240 Mon Sep 17 00:00:00 2001 From: Kim Tran Date: Thu, 1 Aug 2024 12:10:33 +0200 Subject: [PATCH] fix: P002271 7753/refactoring get url (#354) (#356) * P002271-7641: fixing accessibility issues for pageHeaderComponent * P002271-7641: fix failing test * P002271-7641: fix test * P002271-7641: renamed function for better understanding * P002271-7753: reordering and renaming of params, making endpoint optional * P002271-7753: fixed test * P002271-7753: Clean after wrong commit * P002271-7753: Clean after wrong commit * P002271-7753: Clean after wrong commit * P002271-7753: added tests not giving endpointsParameters Co-authored-by: jufiedle --- .../lib/services/workspace.service.spec.ts | 106 ++++++++++++++---- .../src/lib/services/workspace.service.ts | 29 +++-- 2 files changed, 101 insertions(+), 34 deletions(-) diff --git a/libs/angular-integration-interface/src/lib/services/workspace.service.spec.ts b/libs/angular-integration-interface/src/lib/services/workspace.service.spec.ts index f5e7d44a..44413d4a 100644 --- a/libs/angular-integration-interface/src/lib/services/workspace.service.spec.ts +++ b/libs/angular-integration-interface/src/lib/services/workspace.service.spec.ts @@ -5,15 +5,18 @@ import { AppStateServiceMock, provideAppStateServiceMock } from '@onecx/angular- describe('WorkspaceService', () => { let service: WorkspaceService let mockAppStateService: AppStateServiceMock - const params: Record = { + const endpointParameters: Record = { id: 5, key: 'xy', } - const paramsWrong: Record = { + const endpointParametersWrong: Record = { idx: 5, } + const appId = 'onecx-workspace-ui' + const productName = 'onecx-workspace' + beforeEach(() => { TestBed.configureTestingModule({ providers: [provideAppStateServiceMock()], @@ -48,7 +51,7 @@ describe('WorkspaceService', () => { describe('getUrl', () => { it('should find endpoint and return correct url from route and endpoint ', (done) => { - service.getUrl('onecx-workspace-ui', 'onecx-workspace', 'details', params).subscribe((url) => { + service.getUrl(productName, appId, 'details', endpointParameters).subscribe((url) => { expect(url).toBe('http://example.com/workspace/baseurl/details/5') done() }) @@ -63,7 +66,7 @@ describe('WorkspaceService', () => { routes: [], }) - service.getUrl('onecx-workspace-ui', 'onecx-workspace', 'detailswrong', params).subscribe((url) => { + service.getUrl(productName, appId, 'detailswrong', endpointParameters).subscribe((url) => { expect(url).toBe('') done() }) @@ -77,7 +80,7 @@ describe('WorkspaceService', () => { baseUrl: 'http://example.com', }) - service.getUrl('onecx-workspace-ui', 'onecx-workspace', 'detailswrong', params).subscribe((url) => { + service.getUrl(productName, appId, 'detailswrong', endpointParameters).subscribe((url) => { expect(url).toBe('http://example.com') done() }) @@ -92,14 +95,14 @@ describe('WorkspaceService', () => { routes: [], }) - service.getUrl('onecx-workspace-ui', 'onecx-workspace', 'detailswrong', params).subscribe((url) => { + service.getUrl(productName, appId, 'detailswrong', endpointParameters).subscribe((url) => { expect(url).toBe('http://example.com') done() }) }) - it('should return workspace baseUrl when route for appId and productName was not found"', (done) => { - service.getUrl('onecx-workspace-uix', 'onecx-workspace', 'details', {}).subscribe((url) => { + it('should return workspace baseUrl when route for productName and appId was not found"', (done) => { + service.getUrl('wrong-productname', appId, 'details', {}).subscribe((url) => { expect(url).toBe('http://example.com') done() }) @@ -124,7 +127,7 @@ describe('WorkspaceService', () => { ], }) - service.getUrl('onecx-workspace-ui', 'onecx-workspace', 'details', params).subscribe((url) => { + service.getUrl(productName, appId, 'details', endpointParameters).subscribe((url) => { expect(url).toBe('http://example.com/details/5') done() }) @@ -150,7 +153,7 @@ describe('WorkspaceService', () => { ], }) - service.getUrl('onecx-workspace-ui', 'onecx-workspace', 'details', params).subscribe((url) => { + service.getUrl(productName, appId, 'details', endpointParameters).subscribe((url) => { expect(url).toBe('http://example.com/details/5') done() }) @@ -172,35 +175,35 @@ describe('WorkspaceService', () => { ], }) - service.getUrl('onecx-workspace-ui', 'onecx-workspace', 'detailswrong', params).subscribe((url) => { + service.getUrl(productName, appId, 'detailswrong', endpointParameters).subscribe((url) => { expect(url).toBe('http://example.com/workspace/baseurl') done() }) }) it('should return route.baseUrl when endpoint was not found', (done) => { - service.getUrl('onecx-workspace-ui', 'onecx-workspace', 'detailswrong', params).subscribe((url) => { + service.getUrl(productName, appId, 'detailswrong', endpointParameters).subscribe((url) => { expect(url).toBe('http://example.com/workspace/baseurl') done() }) }) it('should return well formed url for endpoint with 1 alias', (done) => { - service.getUrl('onecx-workspace-ui', 'onecx-workspace', 'edit', params).subscribe((url) => { + service.getUrl(productName, appId, 'edit', endpointParameters).subscribe((url) => { expect(url).toBe('http://example.com/workspace/baseurl/details/5') done() }) }) it('should return well formed url for endpoint with 2 alias ', (done) => { - service.getUrl('onecx-workspace-ui', 'onecx-workspace', 'change', params).subscribe((url) => { + service.getUrl(productName, appId, 'change', endpointParameters).subscribe((url) => { expect(url).toBe('http://example.com/workspace/baseurl/details/5') done() }) }) it('should return baseurl when endpoint was not found', (done) => { - service.getUrl('onecx-workspace-ui', 'onecx-workspace', 'changexy', params).subscribe((url) => { + service.getUrl(productName, appId, 'changexy', endpointParameters).subscribe((url) => { expect(url).toBe('http://example.com/workspace/baseurl') done() }) @@ -225,27 +228,84 @@ describe('WorkspaceService', () => { ], }) - service.getUrl('onecx-workspace-ui', 'onecx-workspace', 'change', params).subscribe((url) => { + service.getUrl(productName, appId, 'change', endpointParameters).subscribe((url) => { expect(url).toBe('http://example.com/workspace/baseurl') done() }) }) + it('should return baseurl + endpoint with no endpointparameters', (done) => { + mockAppStateService.currentWorkspace$.publish({ + portalName: 'test-portal', + workspaceName: 'test-workspace', + microfrontendRegistrations: [], + baseUrl: 'http://example.com', + routes: [ + { + appId: 'onecx-workspace-ui', + productName: 'onecx-workspace', + baseUrl: 'http://example.com/workspace/baseurl', + endpoints: [ + { name: 'details', path: '/details' }, + { name: 'change', path: '[[edit]]' }, + ], + }, + ], + }) + + service.getUrl(productName, appId, 'details').subscribe((url) => { + expect(url).toBe('http://example.com/workspace/baseurl/details') + done() + }) + }) + + it('should return baseurl + endpoint with no endpointparameters', (done) => { + mockAppStateService.currentWorkspace$.publish({ + portalName: 'test-portal', + workspaceName: 'test-workspace', + microfrontendRegistrations: [], + baseUrl: 'http://example.com', + routes: [ + { + appId: 'onecx-workspace-ui', + productName: 'onecx-workspace', + baseUrl: 'http://example.com/workspace/baseurl', + endpoints: [ + { name: 'details', path: '/details' }, + { name: 'change', path: '[[edit]]' }, + ], + }, + ], + }) + + service.getUrl(productName, appId, 'details', undefined).subscribe((url) => { + expect(url).toBe('http://example.com/workspace/baseurl/details') + done() + }) + }) + it('should return baseurl when param was not found"', (done) => { - service.getUrl('onecx-workspace-ui', 'onecx-workspace', 'details', paramsWrong).subscribe((url) => { + service.getUrl(productName, appId, 'details', endpointParametersWrong).subscribe((url) => { + expect(url).toBe('http://example.com/workspace/baseurl') + done() + }) + }) + + it('should return baseurl when no endpointName and endpointParameters are given"', (done) => { + service.getUrl(productName, appId).subscribe((url) => { expect(url).toBe('http://example.com/workspace/baseurl') done() }) }) - it('should baseurl without endpoint when params are empty"', (done) => { - service.getUrl('onecx-workspace-ui', 'onecx-workspace', 'details', {}).subscribe((url) => { + it('should baseurl without endpoint when endpointParameters are empty"', (done) => { + service.getUrl(productName, appId, 'details', {}).subscribe((url) => { expect(url).toBe('http://example.com/workspace/baseurl') done() }) }) - it('should return well formed url with 2 params in endpoint', (done) => { + it('should return well formed url with 2 endpointParameters in endpoint', (done) => { mockAppStateService.currentWorkspace$.publish({ portalName: 'test-portal', workspaceName: 'test-workspace', @@ -265,7 +325,7 @@ describe('WorkspaceService', () => { ], }) - service.getUrl('onecx-workspace-ui', 'onecx-workspace', 'details', params).subscribe((url) => { + service.getUrl(productName, appId, 'details', endpointParameters).subscribe((url) => { expect(url).toBe('http://example.com/workspace/baseurl/details/5/xy') done() }) @@ -286,7 +346,7 @@ describe('WorkspaceService', () => { ], }) - service.getUrl('onecx-workspace-ui', 'onecx-workspace', 'detailswrong', params).subscribe((url) => { + service.getUrl(productName, appId, 'detailswrong', endpointParameters).subscribe((url) => { expect(url).toBe('http://example.com/workspace/baseurl') done() }) @@ -312,7 +372,7 @@ describe('WorkspaceService', () => { ], }) - service.getUrl('onecx-workspace-ui', 'onecx-workspace', 'details', params).subscribe((url) => { + service.getUrl(productName, appId, 'details', endpointParameters).subscribe((url) => { expect(url).toBe('http://example.com/workspace/baseurl/details/5') done() }) diff --git a/libs/angular-integration-interface/src/lib/services/workspace.service.ts b/libs/angular-integration-interface/src/lib/services/workspace.service.ts index a08da7a6..47be37bb 100644 --- a/libs/angular-integration-interface/src/lib/services/workspace.service.ts +++ b/libs/angular-integration-interface/src/lib/services/workspace.service.ts @@ -17,14 +17,14 @@ export class WorkspaceService { constructor(protected appStateService: AppStateService) {} getUrl( - appId: string, productName: string, - endpointName: string, - params: Record = {} + appId: string, + endpointName?: string, + endpointParameters?: Record ): Observable { return this.appStateService.currentWorkspace$.pipe( map((workspace) => { - const finalUrl = this.constructRouteUrl(workspace, appId, productName, endpointName, params) + const finalUrl = this.constructRouteUrl(workspace, appId, productName, endpointName, endpointParameters) return finalUrl }) ) @@ -42,8 +42,8 @@ export class WorkspaceService { workspace: any, appId: string, productName: string, - endpointName: string, - params: Record + endpointName?: string, + endpointParameters?: Record ): string { const route = this.filterRouteFromList(workspace.routes, appId, productName) let url = this.constructBaseUrlFromWorkspace(workspace) @@ -58,12 +58,19 @@ export class WorkspaceService { if (route.baseUrl !== undefined && route.baseUrl.length > 0) { url = route.baseUrl } + if (endpointName == undefined) { + return url + } - url = Location.joinWithSlash(url, this.constructEndpointUrl(route, endpointName, params)) + url = Location.joinWithSlash(url, this.constructEndpointUrl(route, endpointName, endpointParameters)) return url } - private constructEndpointUrl(route: any, endpointName: string, params: Record): string { + private constructEndpointUrl( + route: any, + endpointName: string, + endpointParameters: Record = {} + ): string { if (!route.endpoints) { return '' } @@ -73,7 +80,7 @@ export class WorkspaceService { return '' } - const paramsFilled = this.fillParamsForPath(finalEndpoint.path, params) + const paramsFilled = this.fillParamsForPath(finalEndpoint.path, endpointParameters) if (paramsFilled === undefined) { console.log('WARNING: Params could not be filled correctly') return '' @@ -125,13 +132,13 @@ export class WorkspaceService { return endpoint } - private fillParamsForPath(path: string, params: Record): string { + private fillParamsForPath(path: string, endpointParameters: Record): string { while (path.includes(this.paramStart)) { const paramName = path.substring( path.indexOf(this.paramStart) + this.paramStart.length, path.indexOf(this.paramEnd) ) - const paramValue = this.getStringFromUnknown(params[paramName]) + const paramValue = this.getStringFromUnknown(endpointParameters[paramName]) if (paramValue != undefined && paramValue.length > 0) { path = path.replace(this.paramStart.concat(paramName).concat(this.paramEnd), paramValue) } else {