Skip to content

Commit

Permalink
fix: P002271 7753/refactoring get url (#354) (#356)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
KimFFVII and jufiedle authored Aug 1, 2024
1 parent c609381 commit 209841d
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import { AppStateServiceMock, provideAppStateServiceMock } from '@onecx/angular-
describe('WorkspaceService', () => {
let service: WorkspaceService
let mockAppStateService: AppStateServiceMock
const params: Record<string, unknown> = {
const endpointParameters: Record<string, unknown> = {
id: 5,
key: 'xy',
}

const paramsWrong: Record<string, unknown> = {
const endpointParametersWrong: Record<string, unknown> = {
idx: 5,
}

const appId = 'onecx-workspace-ui'
const productName = 'onecx-workspace'

beforeEach(() => {
TestBed.configureTestingModule({
providers: [provideAppStateServiceMock()],
Expand Down Expand Up @@ -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()
})
Expand All @@ -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()
})
Expand All @@ -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()
})
Expand All @@ -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()
})
Expand All @@ -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()
})
Expand All @@ -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()
})
Expand All @@ -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()
})
Expand All @@ -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',
Expand All @@ -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()
})
Expand All @@ -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()
})
Expand All @@ -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()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ export class WorkspaceService {
constructor(protected appStateService: AppStateService) {}

getUrl(
appId: string,
productName: string,
endpointName: string,
params: Record<string, unknown> = {}
appId: string,
endpointName?: string,
endpointParameters?: Record<string, unknown>
): Observable<string> {
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
})
)
Expand All @@ -42,8 +42,8 @@ export class WorkspaceService {
workspace: any,
appId: string,
productName: string,
endpointName: string,
params: Record<string, unknown>
endpointName?: string,
endpointParameters?: Record<string, unknown>
): string {
const route = this.filterRouteFromList(workspace.routes, appId, productName)
let url = this.constructBaseUrlFromWorkspace(workspace)
Expand All @@ -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, unknown>): string {
private constructEndpointUrl(
route: any,
endpointName: string,
endpointParameters: Record<string, unknown> = {}
): string {
if (!route.endpoints) {
return ''
}
Expand All @@ -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 ''
Expand Down Expand Up @@ -125,13 +132,13 @@ export class WorkspaceService {
return endpoint
}

private fillParamsForPath(path: string, params: Record<string, unknown>): string {
private fillParamsForPath(path: string, endpointParameters: Record<string, unknown>): 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 {
Expand Down

0 comments on commit 209841d

Please sign in to comment.