-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing PagesRouter to navigate between packages (#11883)
* Implementing PagesRouter to navigate between packages * fixing feedback from PR * fixing feedback from PR
- Loading branch information
WilliamThorenfeldt
authored
Dec 19, 2023
1 parent
d77979f
commit 705fb4c
Showing
8 changed files
with
140 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
frontend/packages/shared/src/navigation/PackagesRouter/PackagesRouter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { PackagesRouter } from './PackagesRouter'; | ||
|
||
const mockOrg: string = 'org'; | ||
const mockApp: string = 'app'; | ||
|
||
describe('PackagesRouter', () => { | ||
describe('constructor', () => { | ||
it('should default to empty strings if app and org are not provided', () => { | ||
const routerWithoutParams = new PackagesRouter(); | ||
expect(routerWithoutParams['app']).toEqual(''); | ||
expect(routerWithoutParams['org']).toEqual(''); | ||
}); | ||
}); | ||
|
||
describe('navigateToPackage', () => { | ||
it('should navigate to the correct "editor/overview page when the location parameter is set to "editorOverview"', () => { | ||
const packagesRouter = new PackagesRouter({ org: mockOrg, app: mockApp }); | ||
const expectedUrl = `/editor/${mockOrg}/${mockApp}/overview`; | ||
|
||
// Mock the window.location.assign method | ||
const assignMock = jest.fn(); | ||
Object.defineProperty(window, 'location', { | ||
value: { assign: assignMock }, | ||
writable: true, | ||
}); | ||
|
||
packagesRouter.navigateToPackage('editorOverview'); | ||
|
||
expect(assignMock).toHaveBeenCalledWith(expectedUrl); | ||
}); | ||
|
||
it('should navigate to the correct URL and include queryParams', () => { | ||
const packagesRouter = new PackagesRouter({ org: mockOrg, app: mockApp }); | ||
|
||
const mockQueryParams = '?layout=123'; | ||
const expectedUrl = `/editor/${mockOrg}/${mockApp}/ui-editor${mockQueryParams}`; | ||
|
||
const assignMock = jest.fn(); | ||
Object.defineProperty(window, 'location', { | ||
value: { assign: assignMock }, | ||
writable: true, | ||
}); | ||
|
||
packagesRouter.navigateToPackage('editorUiEditor', mockQueryParams); | ||
|
||
expect(assignMock).toHaveBeenCalledWith(expectedUrl); | ||
}); | ||
}); | ||
|
||
describe('getPackageNavigationUrl', () => { | ||
it('should return the correct URL for a package route with placeholders', () => { | ||
const packagesRouter = new PackagesRouter({ org: mockOrg, app: mockApp }); | ||
const expectedUrl = `/editor/${mockOrg}/${mockApp}/deploy`; | ||
|
||
const result = packagesRouter.getPackageNavigationUrl('editorPublish'); | ||
|
||
expect(result).toEqual(expectedUrl); | ||
}); | ||
|
||
it('should return the correct URL for a package route without placeholders', () => { | ||
const packagesRouter = new PackagesRouter({ org: mockOrg, app: mockApp }); | ||
const expectedUrl = '/dashboard'; | ||
|
||
const result = packagesRouter.getPackageNavigationUrl('dashboard'); | ||
|
||
expect(result).toEqual(expectedUrl); | ||
}); | ||
}); | ||
|
||
describe('replaceOrgAndApp', () => { | ||
it('should replace {{org}} and {{app}} placeholders in the given URL', () => { | ||
const packagesRouter = new PackagesRouter({ org: mockOrg, app: mockApp }); | ||
|
||
const mockUrl = '/editor/{{org}}/{{app}}/overview'; | ||
const expectedUrl = `/editor/${mockOrg}/${mockApp}/overview`; | ||
|
||
const result = packagesRouter['replaceOrgAndApp'](mockUrl); | ||
|
||
expect(result).toEqual(expectedUrl); | ||
}); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
frontend/packages/shared/src/navigation/PackagesRouter/PackagesRouter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
type ParamsOptions = { | ||
org?: string; | ||
app?: string; | ||
}; | ||
|
||
type PackagesRoute = | ||
| 'dashboard' | ||
| 'editorOverview' | ||
| 'editorUiEditor' | ||
| 'preview' | ||
| 'editorPublish'; | ||
|
||
const packagesRoutes: Record<PackagesRoute, string> = { | ||
dashboard: '/dashboard', | ||
editorOverview: '/editor/{{org}}/{{app}}/overview', | ||
editorUiEditor: '/editor/{{org}}/{{app}}/ui-editor', | ||
editorPublish: '/editor/{{org}}/{{app}}/deploy', | ||
preview: '/preview/{{org}}/{{app}}', | ||
}; | ||
|
||
export class PackagesRouter { | ||
private app: string; | ||
private org: string; | ||
|
||
constructor(private paramsOptions?: ParamsOptions) { | ||
this.app = this.paramsOptions?.app ?? ''; | ||
this.org = this.paramsOptions?.org ?? ''; | ||
} | ||
|
||
public navigateToPackage(packageRoute: PackagesRoute, queryParams?: string): void { | ||
window.location.assign(`${this.getPackageNavigationUrl(packageRoute)}${queryParams ?? ''}`); | ||
} | ||
|
||
public getPackageNavigationUrl(packageRoute: PackagesRoute): string { | ||
const selectedPackageRoute = packagesRoutes[packageRoute]; | ||
|
||
if (selectedPackageRoute.includes('{{org}}') || selectedPackageRoute.includes('{{app}}')) { | ||
return this.replaceOrgAndApp(selectedPackageRoute); | ||
} | ||
|
||
return selectedPackageRoute; | ||
} | ||
|
||
private replaceOrgAndApp(url: string): string { | ||
return url.replace('{{org}}', this.org).replace('{{app}}', this.app); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
frontend/packages/shared/src/navigation/PackagesRouter/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { PackagesRouter } from './PackagesRouter'; |