[Core] Update getUrlForApp to support deepLinks (#102172) (#102300)

* [Core] Update getUrlForApp to support deepLinks

* Add unit tests to getUrlForApp with deepLinkId parameter
This commit is contained in:
Pablo Machado 2021-06-16 12:07:09 +02:00 committed by GitHub
parent bd1e0ae11d
commit 22f73f6ac4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 3 deletions

View file

@ -16,6 +16,7 @@ Note that when generating absolute urls, the origin (protocol, host and port) ar
getUrlForApp(appId: string, options?: {
path?: string;
absolute?: boolean;
deepLinkId?: string;
}): string;
```
@ -24,7 +25,7 @@ getUrlForApp(appId: string, options?: {
| Parameter | Type | Description |
| --- | --- | --- |
| appId | <code>string</code> | |
| options | <code>{</code><br/><code> path?: string;</code><br/><code> absolute?: boolean;</code><br/><code> }</code> | |
| options | <code>{</code><br/><code> path?: string;</code><br/><code> absolute?: boolean;</code><br/><code> deepLinkId?: string;</code><br/><code> }</code> | |
<b>Returns:</b>

View file

@ -497,6 +497,56 @@ describe('#start()', () => {
expect(getUrlForApp('app1', { path: 'deep/link///' })).toBe('/base-path/app/app1/deep/link');
});
describe('deepLinkId option', () => {
it('ignores the deepLinkId parameter if it is unknown', async () => {
service.setup(setupDeps);
service.setup(setupDeps);
const { getUrlForApp } = await service.start(startDeps);
expect(getUrlForApp('app1', { deepLinkId: 'unkown-deep-link' })).toBe(
'/base-path/app/app1'
);
});
it('creates URLs with deepLinkId parameter', async () => {
const { register } = service.setup(setupDeps);
register(
Symbol(),
createApp({
id: 'app1',
appRoute: '/custom/app-path',
deepLinks: [{ id: 'dl1', title: 'deep link 1', path: '/deep-link' }],
})
);
const { getUrlForApp } = await service.start(startDeps);
expect(getUrlForApp('app1', { deepLinkId: 'dl1' })).toBe(
'/base-path/custom/app-path/deep-link'
);
});
it('creates URLs with deepLinkId and path parameters', async () => {
const { register } = service.setup(setupDeps);
register(
Symbol(),
createApp({
id: 'app1',
appRoute: '/custom/app-path',
deepLinks: [{ id: 'dl1', title: 'deep link 1', path: '/deep-link' }],
})
);
const { getUrlForApp } = await service.start(startDeps);
expect(getUrlForApp('app1', { deepLinkId: 'dl1', path: 'foo/bar' })).toBe(
'/base-path/custom/app-path/deep-link/foo/bar'
);
});
});
it('does not append trailing slash if hash is provided in path parameter', async () => {
service.setup(setupDeps);
const { getUrlForApp } = await service.start(startDeps);

View file

@ -282,8 +282,19 @@ export class ApplicationService {
history: this.history!,
getUrlForApp: (
appId,
{ path, absolute = false }: { path?: string; absolute?: boolean } = {}
{
path,
absolute = false,
deepLinkId,
}: { path?: string; absolute?: boolean; deepLinkId?: string } = {}
) => {
if (deepLinkId) {
const deepLinkPath = getAppDeepLinkPath(availableMounters, appId, deepLinkId);
if (deepLinkPath) {
path = appendAppPath(deepLinkPath, path);
}
}
const relUrl = http.basePath.prepend(getAppUrl(availableMounters, appId, path));
return absolute ? relativeToAbsolute(relUrl) : relUrl;
},

View file

@ -780,7 +780,10 @@ export interface ApplicationStart {
* @param options.path - optional path inside application to deep link to
* @param options.absolute - if true, will returns an absolute url instead of a relative one
*/
getUrlForApp(appId: string, options?: { path?: string; absolute?: boolean }): string;
getUrlForApp(
appId: string,
options?: { path?: string; absolute?: boolean; deepLinkId?: string }
): string;
/**
* An observable that emits the current application id and each subsequent id update.

View file

@ -150,6 +150,7 @@ export interface ApplicationStart {
getUrlForApp(appId: string, options?: {
path?: string;
absolute?: boolean;
deepLinkId?: string;
}): string;
navigateToApp(appId: string, options?: NavigateToAppOptions): Promise<void>;
navigateToUrl(url: string): Promise<void>;