Skip to content

Commit

Permalink
[Core] Update getUrlForApp to support deepLinks (#102172)
Browse files Browse the repository at this point in the history
* [Core] Update getUrlForApp to support deepLinks

* Add unit tests to getUrlForApp with deepLinkId parameter
  • Loading branch information
machadoum committed Jun 15, 2021
1 parent c409985 commit 7dfe203
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
```

Expand All @@ -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>

Expand Down
50 changes: 50 additions & 0 deletions src/core/public/application/application_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 12 additions & 1 deletion src/core/public/application/application_service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down
5 changes: 4 additions & 1 deletion src/core/public/application/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/core/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down

0 comments on commit 7dfe203

Please sign in to comment.